# Testing
source: https://developer.mastercard.com/bs-creditor-api/documentation/testing/index.md

The sandbox environment is designed as a preliminary integration and testing platform for the BS Creditor API. This allows integrators to develop and test their applications before moving to the production environment.

Similar to obtaining access to the production environment, an integrator can request access to the sandbox environment by signing up for Mastercard Developers and acquiring a consumer key. After signing up and obtaining a consumer key, integrators can simulate interactions with the API, ensuring their systems are compatible and optimized for real-world operations.

When testing in the sandbox environment, our API simulates the production workflow with a few key differences to ensure safety and control.

* In the production environment, we publish data and await a response from the upstream services.
* In the sandbox environment, we publish the data, but instead of working with the actual services, a mock publisher processes the data. This mock publisher registers a predefined response, simulating the behavior of the real service. A scheduler then processes this response, introducing a delay of 30-90 seconds to mimic upstream processing.

## Test Data {#test-data}

The following data set represents the test data and its structure in the sandbox environment. It encompasses a wide range of test cases to ensure comprehensive coverage of varied scenarios.

#### Creditors {#creditors}

| CREDITOR_ID | CVR_NUMBER | MOCK_RESPONSE_STATUS (30-90s delay) |                   ERROR                   |
|-------------|------------|-------------------------------------|-------------------------------------------|
| 40000       | 42222222   | VALIDATED                           | N/A                                       |
| 50000       | 52222222   | FAILED                              | DEBTOR_IDENTITY_IS_NOT_CORRECT            |
| 60000       | 62222222   | FAILED                              | DEBTOR_ACCOUNT_INFORMATION_IS_NOT_CORRECT |
| 70000       | 72222222   | FAILED                              | MANDATE_ALREADY_EXISTS                    |
| 80000       | 82222222   | RECEIVED (no delay)                 | N/A                                       |
| 90000       | 92222222   | N/A                                 | OPERATION_NOT_ALLOWED                     |

#### BS Agreements {#bs-agreements}

| PBS_NUMBER | CREDITOR_ID | OPERATION_ALLOWED |
|------------|-------------|-------------------|
| 40000002   | 40000       | True              |
| 50000002   | 50000       | True              |
| 60000002   | 60000       | True              |
| 70000002   | 70000       | True              |
| 80000002   | 80000       | True              |
| 90000002   | 90000       | False             |

#### Debtor Groups {#debtor-groups}

| PBS_NUMBER | DEBTOR_GROUP_NUMBER |
|------------|---------------------|
| 40000002   | 4000                |
| 50000002   | 5000                |
| 60000002   | 6000                |
| 70000002   | 7000                |
| 80000002   | 8000                |
| 90000002   | 9000                |

## Test Cases {#test-cases}

The following test cases are variations to the result of BS Creditor API requests.
Note: If you want to create many mandates for the same scenario, then don't forget to change uuid and creditorsDebtorReference in the next requests to some new values. UUID should be unique for each mandate, and creditorsDebtorReference should be a part of unique combination of (debtorGroupNumber, pbsNumber, creditorDebtorReference).

### Happy path {#hahahugoshortcodes1hbhb-happy-path}

Requests using debtor group number **4000** will succeed and the mock response will be **VALIDATED**.
First we need to create test mandate using group number **4000**. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates`

As a request body you can use this:

```json
  {
    "uuid": "0e90e6f9-9e8e-4e9d-9976-2460689dc136",
    "debtorIdentity": {
      "cprNumber": "0509821209"
    },
    "debtorAccountInformation": {
      "debtorRegNo": "0406",
      "debtorAccountNo": "4356789123"
    },
    "creditorsDebtorReference": "ØK4856124",
    "creditorData": {
      "pbsNumber": 40000002,
      "debtorGroupNumber": 4000
    }
  }
```

After sending request, you should expect following response:

```json
{
  "uuid": "0e90e6f9-9e8e-4e9d-9976-2460689dc136",
  "statusCode": "RECEIVED"
}
```

After we created a mandate, we can try and search for it. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates/searches`

As a request body you can use:

```json
{
  "uuids": [
    "0e90e6f9-9e8e-4e9d-9976-2460689dc136"
  ]
}
```

After sending request, you should expect following response:

```json
{
  "statuses": [
    {
      "uuid": "0e90e6f9-9e8e-4e9d-9976-2460689dc136",
      "statusCode": "VALIDATED",
      "mandateId": 2
    }
  ]
}
```

As we can see, **VALIDATED** mandate was successfully created, and we can retrieve its details.

### Failure scenario 1 {#hahahugoshortcodes4hbhb-failure-scenario-1}

Requests using debtor group number **5000** will fail and the mock response will be FAILED with the **DEBTOR_IDENTITY_IS_NOT_CORRECT** error code.
First we need to create test mandate using group number **5000**. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates`

As a request body you can use:

```json
  {
    "uuid": "56087f52-d512-4532-9266-6b1e51918763",
    "debtorIdentity": {
      "cprNumber": "0509821209"
    },
    "debtorAccountInformation": {
      "debtorRegNo": "0406",
      "debtorAccountNo": "4356789123"
    },
    "creditorsDebtorReference": "ØK4856124",
    "creditorData": {
      "pbsNumber": 50000002,
      "debtorGroupNumber": 5000
    }
  }
```

After sending request, you should expect following response:

```json
{
  "uuid": "56087f52-d512-4532-9266-6b1e51918763",
  "statusCode": "RECEIVED"
}
```

After we created a mandate, we can try and search for it. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates/searches`

As a request body you can use:

```json
{
  "uuids": [
    "56087f52-d512-4532-9266-6b1e51918763"
  ]
}
```

After sending request, you should expect following response:

```json
{
  "statuses": [
    {
      "uuid": "56087f52-d512-4532-9266-6b1e51918763",
      "statusCode": "FAILED",
      "errorCode": "DEBTOR_IDENTITY_IS_NOT_CORRECT",
      "errorDescription": "DEBTOR_IDENTITY_IS_NOT_CORRECT"
    }
  ]
}
```

As we can see, mandate creation has FAILED with **DEBTOR_IDENTITY_IS_NOT_CORRECT** error code, which is what we expected.

### Failure scenario 2 {#hahahugoshortcodes7hbhb-failure-scenario-2}

Requests using debtor group number **6000** will fail and the mock response will be FAILED with the **DEBTOR_ACCOUNT_INFORMATION_IS_NOT_CORRECT** error code.
First we need to create test mandate using group number **6000**. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates`

As a request body you can use:

```json
  {
    "uuid": "c71582c9-07ce-482a-b41c-1549b2483616",
    "debtorIdentity": {
      "cprNumber": "0509821209"
    },
    "debtorAccountInformation": {
      "debtorRegNo": "0406",
      "debtorAccountNo": "4356789123"
    },
    "creditorsDebtorReference": "ØK4856124",
    "creditorData": {
      "pbsNumber": 60000002,
      "debtorGroupNumber": 6000
    }
  }
```

After sending request, you should expect following response:

```json
{
  "uuid": "c71582c9-07ce-482a-b41c-1549b2483616",
  "statusCode": "RECEIVED"
}
```

After we created a mandate, we can try and search for it. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates/searches`

As a request body you can use:

```json
{
  "uuids": [
    "c71582c9-07ce-482a-b41c-1549b2483616"
  ]
}
```

After sending request, you should expect following response:

```json
{
  "statuses": [
    {
      "uuid": "c71582c9-07ce-482a-b41c-1549b2483616",
      "statusCode": "FAILED",
      "errorCode": "DEBTOR_ACCOUNT_INFORMATION_IS_NOT_CORRECT",
      "errorDescription": "DEBTOR_ACCOUNT_INFORMATION_IS_NOT_CORRECT"
    }
  ]
}
```

As we can see, mandate creation has FAILED with **DEBTOR_ACCOUNT_INFORMATION_IS_NOT_CORRECT** error code, which is what we expected.

### Failure scenario 3 {#hahahugoshortcodes10hbhb-failure-scenario-3}

Requests using debtor group number **7000** will fail and the mock response will be FAILED with the **MANDATE_ALREADY_EXISTS** error code.
First we need to create test mandate using group number **7000**. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates`

As a request body you can use:

```json
  {
    "uuid": "2b1d7ff6-81e2-4d2a-b56a-3e895f555183",
    "debtorIdentity": {
      "cprNumber": "0509821209"
    },
    "debtorAccountInformation": {
      "debtorRegNo": "0406",
      "debtorAccountNo": "4356789123"
    },
    "creditorsDebtorReference": "ØK4856124",
    "creditorData": {
      "pbsNumber": 70000002,
      "debtorGroupNumber": 7000
    }
  }
```

After sending request, you should expect following response:

```json
{
  "uuid": "2b1d7ff6-81e2-4d2a-b56a-3e895f555183",
  "statusCode": "RECEIVED"
}
```

After we created a mandate, we can try and search for it. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates/searches`

As a request body you can use:

```json
{
  "uuids": [
    "2b1d7ff6-81e2-4d2a-b56a-3e895f555183"
  ]
}
```

After sending request, you should expect following response:

```json
{
  "statuses": [
    {
      "uuid": "2b1d7ff6-81e2-4d2a-b56a-3e895f555183",
      "statusCode": "FAILED",
      "errorCode": "MANDATE_ALREADY_EXISTS",
      "errorDescription": "MANDATE_ALREADY_EXISTS"
    }
  ]
}
```

As we can see, mandate creation has FAILED with **MANDATE_ALREADY_EXISTS** error code, which is what we expected.

### Failure scenario 4 {#hahahugoshortcodes13hbhb-failure-scenario-4}

Requests using debtor group number **8000** will be stuck in **RECEIVED** status to simulate abnormal processing time or a persisting error within Betalingsservice.
First we need to create test mandate using group number **8000**. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates`

As a request body you can use:

```json
  {
    "uuid": "199efa0c-7a22-4760-ac9c-411929baed76",
    "debtorIdentity": {
      "cprNumber": "0509821209"
    },
    "debtorAccountInformation": {
      "debtorRegNo": "0406",
      "debtorAccountNo": "4356789123"
    },
    "creditorsDebtorReference": "ØK4856124",
    "creditorData": {
      "pbsNumber": 80000002,
      "debtorGroupNumber": 8000
    }
  }
```

After sending request, you should expect following response:

```json
{
  "uuid": "199efa0c-7a22-4760-ac9c-411929baed76",
  "statusCode": "RECEIVED"
}
```

After we created a mandate, we can try and search for it. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates/searches`

As a request body you can use:

```json
{
  "uuids": [
    "199efa0c-7a22-4760-ac9c-411929baed76"
  ]
}
```

After sending request, you should expect following response:

```json
{
  "statuses": [
    {
      "uuid": "199efa0c-7a22-4760-ac9c-411929baed76",
      "statusCode": "RECEIVED"
    }
  ]
}
```

As we can see, mandate creation got stuck in the **RECEIVED** state, which is what we expected.

### Failure scenario 5 {#hahahugoshortcodes16hbhb-failure-scenario-5}

Requests using debtor group number **9000** will fail with error **OPERATION_NOT_ALLOWED** indicating that the creditor does not have the appropriate service code.
We need to create test mandate using group number **9000**. For this, you need to send a request to the following sandbox endpoint:

**Post** `https://mtf.oauth.payments.mastercard.dk/betalingsservice/bs-creditor-api-sandbox/delegated-mandates`

As a request body you can use:

```json
  {
    "uuid": "678e1b2a-c43e-4f50-a0c0-431c6c68c116",
    "debtorIdentity": {
      "cprNumber": "0509821209"
    },
    "debtorAccountInformation": {
      "debtorRegNo": "0406",
      "debtorAccountNo": "4356789123"
    },
    "creditorsDebtorReference": "ØK4856124",
    "creditorData": {
      "pbsNumber": 90000002,
      "debtorGroupNumber": 9000
    }
  }
```

After sending request, you should expect following response:

```json
{
  "Errors": {
    "Error": [
      {
        "Source": "bs-creditorapi",
        "ReasonCode": "OPERATION_NOT_ALLOWED",
        "Description": "Operation not allowed for creditor based on service codes",
        "Recoverable": false,
        "Details": "OPERATION_NOT_ALLOWED 'Operation not allowed for creditor based on service codes'"
      }
    ]
  }
}
```

As we can see, mandate creation ended with an error **OPERATION_NOT_ALLOWED**, which is what we expected.
