|
| 1 | +package com.adyen; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.ArgumentMatchers.eq; |
| 5 | +import static org.mockito.Mockito.verify; |
| 6 | + |
| 7 | +import com.adyen.constants.ApiConstants; |
| 8 | +import com.adyen.model.balanceplatform.*; |
| 9 | +import com.adyen.service.balanceplatform.DirectDebitMandatesApi; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.mockito.ArgumentCaptor; |
| 14 | + |
| 15 | +public class DirectDebitMandatesTest extends BaseTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void getMandateByIdTest() throws Exception { |
| 19 | + Client client = createMockClientFromFile("mocks/balancePlatform/Mandate.json"); |
| 20 | + DirectDebitMandatesApi service = new DirectDebitMandatesApi(client); |
| 21 | + |
| 22 | + Mandate response = service.getMandateById("MNDT000000000000000000000001"); |
| 23 | + |
| 24 | + assertNotNull(response); |
| 25 | + assertEquals("MNDT000000000000000000000001", response.getId()); |
| 26 | + assertEquals("BA000000000000000000000001", response.getBalanceAccountId()); |
| 27 | + assertEquals("PI000000000000000000000001", response.getPaymentInstrumentId()); |
| 28 | + assertEquals(MandateStatus.APPROVED, response.getStatus()); |
| 29 | + assertEquals(MandateType.BACS, response.getType()); |
| 30 | + verify(client.getHttpClient()) |
| 31 | + .request( |
| 32 | + "https://balanceplatform-api-test.adyen.com/bcl/v2/mandates/MNDT000000000000000000000001", |
| 33 | + null, |
| 34 | + client.getConfig(), |
| 35 | + false, |
| 36 | + null, |
| 37 | + ApiConstants.HttpMethod.GET, |
| 38 | + null); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void getMandateCounterpartyTest() throws Exception { |
| 43 | + Client client = createMockClientFromFile("mocks/balancePlatform/Mandate.json"); |
| 44 | + DirectDebitMandatesApi service = new DirectDebitMandatesApi(client); |
| 45 | + |
| 46 | + Mandate response = service.getMandateById("MNDT000000000000000000000001"); |
| 47 | + |
| 48 | + assertNotNull(response.getCounterparty()); |
| 49 | + assertNotNull(response.getCounterparty().getAccountHolder()); |
| 50 | + assertEquals("Albert Klassens", response.getCounterparty().getAccountHolder().getFullName()); |
| 51 | + |
| 52 | + MandateAccountIdentification accountIdentification = |
| 53 | + response.getCounterparty().getAccountIdentification(); |
| 54 | + assertNotNull(accountIdentification); |
| 55 | + assertInstanceOf(UKLocalMandateAccountIdentification.class, accountIdentification); |
| 56 | + |
| 57 | + UKLocalMandateAccountIdentification ukIdentification = |
| 58 | + (UKLocalMandateAccountIdentification) accountIdentification; |
| 59 | + assertEquals("ukLocal", ukIdentification.getType()); |
| 60 | + assertEquals("10809699", ukIdentification.getAccountNumber()); |
| 61 | + assertEquals("405081", ukIdentification.getSortCode()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void getListOfMandatesTest() throws Exception { |
| 66 | + Client client = createMockClientFromFile("mocks/balancePlatform/ListMandatesResponse.json"); |
| 67 | + DirectDebitMandatesApi service = new DirectDebitMandatesApi(client); |
| 68 | + |
| 69 | + ListMandatesResponse response = service.getListOfMandates(); |
| 70 | + |
| 71 | + assertNotNull(response); |
| 72 | + assertNotNull(response.getMandates()); |
| 73 | + assertEquals(1, response.getMandates().size()); |
| 74 | + assertEquals("MNDT000000000000000000000001", response.getMandates().get(0).getId()); |
| 75 | + assertNotNull(response.getLink()); |
| 76 | + verify(client.getHttpClient()) |
| 77 | + .request( |
| 78 | + "https://balanceplatform-api-test.adyen.com/bcl/v2/mandates", |
| 79 | + null, |
| 80 | + client.getConfig(), |
| 81 | + false, |
| 82 | + null, |
| 83 | + ApiConstants.HttpMethod.GET, |
| 84 | + new HashMap<>()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void getListOfMandatesWithQueryParamsTest() throws Exception { |
| 89 | + Client client = createMockClientFromFile("mocks/balancePlatform/ListMandatesResponse.json"); |
| 90 | + DirectDebitMandatesApi service = new DirectDebitMandatesApi(client); |
| 91 | + |
| 92 | + ListMandatesResponse response = |
| 93 | + service.getListOfMandates( |
| 94 | + "BA000000000000000000000001", "PI000000000000000000000001", null, null); |
| 95 | + |
| 96 | + assertNotNull(response); |
| 97 | + assertEquals(1, response.getMandates().size()); |
| 98 | + |
| 99 | + ArgumentCaptor<Map<String, String>> queryParamsCaptor = ArgumentCaptor.captor(); |
| 100 | + verify(client.getHttpClient()) |
| 101 | + .request( |
| 102 | + eq("https://balanceplatform-api-test.adyen.com/bcl/v2/mandates"), |
| 103 | + eq(null), |
| 104 | + eq(client.getConfig()), |
| 105 | + eq(false), |
| 106 | + eq(null), |
| 107 | + eq(ApiConstants.HttpMethod.GET), |
| 108 | + queryParamsCaptor.capture()); |
| 109 | + |
| 110 | + Map<String, String> queryParams = queryParamsCaptor.getValue(); |
| 111 | + assertEquals(2, queryParams.size()); |
| 112 | + assertEquals("BA000000000000000000000001", queryParams.get("balanceAccountId")); |
| 113 | + assertEquals("PI000000000000000000000001", queryParams.get("paymentInstrumentId")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void getListOfMandatesWithCursorTest() throws Exception { |
| 118 | + Client client = createMockClientFromFile("mocks/balancePlatform/ListMandatesResponse.json"); |
| 119 | + DirectDebitMandatesApi service = new DirectDebitMandatesApi(client); |
| 120 | + |
| 121 | + service.getListOfMandates(null, null, "nextCursor", null); |
| 122 | + |
| 123 | + ArgumentCaptor<Map<String, String>> queryParamsCaptor = ArgumentCaptor.captor(); |
| 124 | + verify(client.getHttpClient()) |
| 125 | + .request( |
| 126 | + eq("https://balanceplatform-api-test.adyen.com/bcl/v2/mandates"), |
| 127 | + eq(null), |
| 128 | + eq(client.getConfig()), |
| 129 | + eq(false), |
| 130 | + eq(null), |
| 131 | + eq(ApiConstants.HttpMethod.GET), |
| 132 | + queryParamsCaptor.capture()); |
| 133 | + |
| 134 | + Map<String, String> queryParams = queryParamsCaptor.getValue(); |
| 135 | + assertEquals(1, queryParams.size()); |
| 136 | + assertEquals("nextCursor", queryParamsCaptor.getValue().get("cursor")); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void cancelMandateTest() throws Exception { |
| 141 | + Client client = createMockClientFromResponse(""); |
| 142 | + DirectDebitMandatesApi service = new DirectDebitMandatesApi(client); |
| 143 | + |
| 144 | + service.cancelMandate("MNDT000000000000000000000001"); |
| 145 | + |
| 146 | + verify(client.getHttpClient()) |
| 147 | + .request( |
| 148 | + "https://balanceplatform-api-test.adyen.com/bcl/v2/mandates/MNDT000000000000000000000001/cancel", |
| 149 | + null, |
| 150 | + client.getConfig(), |
| 151 | + false, |
| 152 | + null, |
| 153 | + ApiConstants.HttpMethod.POST, |
| 154 | + null); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void updateMandateTest() throws Exception { |
| 159 | + Client client = createMockClientFromResponse(""); |
| 160 | + DirectDebitMandatesApi service = new DirectDebitMandatesApi(client); |
| 161 | + |
| 162 | + MandateUpdate request = new MandateUpdate().paymentInstrumentId("PI000000000000000000000002"); |
| 163 | + service.updateMandate("MNDT000000000000000000000001", request); |
| 164 | + |
| 165 | + ArgumentCaptor<String> requestBodyCaptor = ArgumentCaptor.forClass(String.class); |
| 166 | + verify(client.getHttpClient()) |
| 167 | + .request( |
| 168 | + eq( |
| 169 | + "https://balanceplatform-api-test.adyen.com/bcl/v2/mandates/MNDT000000000000000000000001"), |
| 170 | + requestBodyCaptor.capture(), |
| 171 | + eq(client.getConfig()), |
| 172 | + eq(false), |
| 173 | + eq(null), |
| 174 | + eq(ApiConstants.HttpMethod.PATCH), |
| 175 | + eq(null)); |
| 176 | + |
| 177 | + assertEquals(request.toJson(), requestBodyCaptor.getValue()); |
| 178 | + } |
| 179 | +} |
0 commit comments