From 11eb548d79cab177712ac53945ad69ab16c5f215 Mon Sep 17 00:00:00 2001 From: psemenkov Date: Mon, 26 Sep 2022 14:00:34 +0300 Subject: [PATCH] http://git.mfd.msk/mfd/clearing/issues/7 --- added test for BankAccountController with validation test for add method. For methods update and delete didn't do the check validation empty id because without id REST request for path "/securities/bank-accounts/" not work. --- .../account/BankAccountControllerTest.java | 162 ++++++++++++++++-- 1 file changed, 148 insertions(+), 14 deletions(-) diff --git a/clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/account/BankAccountControllerTest.java b/clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/account/BankAccountControllerTest.java index d89b18d34..e0705f548 100644 --- a/clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/account/BankAccountControllerTest.java +++ b/clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/account/BankAccountControllerTest.java @@ -2,6 +2,7 @@ package ru.spcex.clearing.backendapi.controller.queue.account; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.NestedExceptionUtils; import org.springframework.http.MediaType; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.web.servlet.MockMvc; @@ -13,13 +14,18 @@ import ru.spcex.clearing.backendapi.config.WebConfig; import ru.spcex.clearing.backendapi.controller.queue.config.*; import ru.spcex.clearing.backendapi.controller.queue.utils.MatcherFactory; import ru.spcex.clearing.backendapi.controller.request.cud.account.BankAccountNewAction; +import ru.spcex.clearing.backendapi.controller.request.cud.account.BankAccountUpdateAction; import ru.spcex.clearing.backendapi.controller.response.cud.CudResponse; import ru.spcex.clearing.backendapi.controller.response.cud.QueueSuccessResponse; +import ru.spcex.clearing.backendapi.domain.actions.IAction; +import ru.spcex.clearing.backendapi.errors.ActionValidationException; import ru.spcex.clearing.platform.messaging.domain.ActionType; import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest; import javax.annotation.PostConstruct; +import java.util.concurrent.atomic.AtomicLong; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -37,6 +43,7 @@ class BankAccountControllerTest { public static final MatcherFactory.Matcher CUD_RESPONSE_MATCHER = MatcherFactory.usingIgnoringFieldsComparator(CudResponse.class); private static final String REST_URL = "/securities/bank-accounts/"; private static final CharacterEncodingFilter CHARACTER_ENCODING_FILTER = new CharacterEncodingFilter(); + private static final AtomicLong currentId = new AtomicLong(); static { CHARACTER_ENCODING_FILTER.setEncoding("UTF-8"); @@ -74,22 +81,21 @@ class BankAccountControllerTest { @Test void add() throws Exception { //ARRANGE - BankAccountNewAction bankAccountNewAction = new BankAccountNewAction(); - bankAccountNewAction.setBankIdentificationCode("044525776"); - bankAccountNewAction.setBankName("Beta Money Bank"); - bankAccountNewAction.setCorrespondentAccount("30101111111111111776"); - bankAccountNewAction.setCorrespondentAccountName("correspondent"); - bankAccountNewAction.setCurrency("RUB"); - bankAccountNewAction.setDestination("destination"); - bankAccountNewAction.setTaxpayerIdentificationNumber("3664011397"); - bankAccountNewAction.setTaxRegistrationReasonCode("01"); - bankAccountNewAction.setAccount("11111222223333344444"); + BankAccountNewAction bankAccountNewAction = getBankAccountNewAction( + "044525776", + "Beta Money Bank", + "30101111111111111776", + "correspondent", + "RUB", + "destination", + "3664011397", + "01", + "11111222223333344444"); CudResponse extended = new CudResponse(); extended.setCode(0L); extended.setMessage("success"); - extended.setPayload(new QueueSuccessResponse(ActionType.NEW, 0L)); - + extended.setPayload(new QueueSuccessResponse(ActionType.NEW, currentId.getAndIncrement())); //ACT mockMvc.perform(MockMvcRequestBuilders.post(REST_URL) .contentType(MediaType.APPLICATION_JSON) @@ -101,11 +107,139 @@ class BankAccountControllerTest { .andExpect(content().json(writeValue(extended))); } + /** + * {@link BankAccountController#add(BankAccountNewAction)}
+ * Тест проверяет работу валидации сущности {@link BankAccountNewAction} принятой по REST API для отправку в Apache Kafka.
+ * Входной запрос {@link BankAccountNewRequest}:
+ * {@link BankAccountNewRequest#bankIdentificationCode} - 044525776 или ""
+ * {@link BankAccountNewRequest#bankName} - Beta Money Bank или ""
+ * {@link BankAccountNewRequest#correspondentAccount} - 30101111111111111776 или ""
+ * {@link BankAccountNewRequest#correspondentAccountName} - correspondent или ""
+ * {@link BankAccountNewRequest#currency} - RUB или ""
+ * {@link BankAccountNewRequest#destination} - destinatio или ""n
+ * {@link BankAccountNewRequest#taxpayerIdentificationNumber} - 3664011397 или ""
+ * {@link BankAccountNewRequest#taxRegistrationReasonCode} - 01 или ""
+ * {@link BankAccountNewRequest#account} - 11111222223333344444 или ""
+ */ @Test - void update() { + void addWithException() { + assertThrowsFor(getBankAccountNewAction("", "Beta Money Bank", "30101111111111111776", "correspondent", "RUB", "destination", "3664011397", "01", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "", "30101111111111111776", "correspondent", "RUB", "destination", "3664011397", "01", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "Beta Money Bank", "", "correspondent", "RUB", "destination", "3664011397", "01", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "Beta Money Bank", "30101111111111111776", "", "RUB", "destination", "3664011397", "01", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "Beta Money Bank", "30101111111111111776", "correspondent", "", "destination", "3664011397", "01", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "Beta Money Bank", "30101111111111111776", "correspondent", "RUB", "", "3664011397", "01", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "Beta Money Bank", "30101111111111111776", "correspondent", "RUB", "destination", "", "01", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "Beta Money Bank", "30101111111111111776", "correspondent", "RUB", "destination", "3664011397", "", "11111222223333344444")); + assertThrowsFor(getBankAccountNewAction("044525776", "Beta Money Bank", "30101111111111111776", "correspondent", "RUB", "destination", "3664011397", "01", "")); } + /** + * {@link BankAccountController#update(Long, BankAccountUpdateAction)}
+ * Тест проверяет получение сущности {@link BankAccountUpdateAction} по REST API и отправку в Apache Kafka.
+ * Входной запрос {@link BankAccountUpdateAction}:
+ * {@link BankAccountUpdateAction#bankIdentificationCode} - 044525776
+ * {@link BankAccountUpdateAction#bankName} - Beta Money Bank
+ * {@link BankAccountUpdateAction#correspondentAccount} - 30101111111111111776
+ * {@link BankAccountUpdateAction#correspondentAccountName} - correspondent
+ * {@link BankAccountUpdateAction#currency} - RUB
+ * {@link BankAccountUpdateAction#destination} - destination
+ * {@link BankAccountUpdateAction#taxpayerIdentificationNumber} - 3664011397
+ * {@link BankAccountUpdateAction#taxRegistrationReasonCode} - 01
+ * {@link BankAccountUpdateAction#account} - 11111222223333344444
+ */ @Test - void delete() { + void update() throws Exception { + //ARRANGE + BankAccountUpdateAction bankAccountNewAction = getBankAccountUpdateAction( + "044525776", + "Beta Money Bank", + "30101111111111111776", + "correspondent", + "RUB", + "destination", + "3664011397", + "01", + "11111222223333344444"); + + CudResponse extended = new CudResponse(); + extended.setCode(0L); + extended.setMessage("success"); + extended.setPayload(new QueueSuccessResponse(ActionType.UPDATE, currentId.getAndIncrement())); + //ACT + mockMvc.perform(MockMvcRequestBuilders.put(REST_URL + "0") + .contentType(MediaType.APPLICATION_JSON) + .content(writeValue(bankAccountNewAction))) + .andDo(print())//output to the log request and response +// ASSERT + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(content().json(writeValue(extended))); + } + + + /** + * {@link BankAccountController#delete(Long)}
+ * Тест проверяет получение id сущности {@link Long} по REST API и отправку в Apache Kafka.
+ * Входной запрос {@link Long}: - 0L
+ */ + @Test + void delete() throws Exception { + //ARRANGE + CudResponse extended = new CudResponse(); + extended.setCode(0L); + extended.setMessage("success"); + extended.setPayload(new QueueSuccessResponse(ActionType.DELETE, currentId.getAndIncrement())); + //ACT + mockMvc.perform(MockMvcRequestBuilders.delete(REST_URL + "0") + .contentType(MediaType.APPLICATION_JSON)) + .andDo(print())//output to the log request and response +// ASSERT + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(content().json(writeValue(extended))); + } + + private void assertThrowsFor(IAction iAction) { + assertThrows(ActionValidationException.class, () -> { + try { + mockMvc.perform(MockMvcRequestBuilders.post(REST_URL).contentType(MediaType.APPLICATION_JSON).content(writeValue(iAction))); + } catch (Exception e) { + Throwable rootCause = NestedExceptionUtils.getRootCause(e); + throw rootCause != null ? rootCause : e; + } + }); + } + + private BankAccountNewAction getBankAccountNewAction(String BankIdentificationCode, String BankName, String CorrespondentAccount, String CorrespondentAccountName, + String Currency, String Destination, String TaxpayerIdentificationNumber, String TaxRegistrationReasonCode, + String Account) { + BankAccountNewAction bankAccountNewAction = new BankAccountNewAction(); + bankAccountNewAction.setBankIdentificationCode(BankIdentificationCode); + bankAccountNewAction.setBankName(BankName); + bankAccountNewAction.setCorrespondentAccount(CorrespondentAccount); + bankAccountNewAction.setCorrespondentAccountName(CorrespondentAccountName); + bankAccountNewAction.setCurrency(Currency); + bankAccountNewAction.setDestination(Destination); + bankAccountNewAction.setTaxpayerIdentificationNumber(TaxpayerIdentificationNumber); + bankAccountNewAction.setTaxRegistrationReasonCode(TaxRegistrationReasonCode); + bankAccountNewAction.setAccount(Account); + return bankAccountNewAction; + } + + private BankAccountUpdateAction getBankAccountUpdateAction(String BankIdentificationCode, String BankName, String CorrespondentAccount, String CorrespondentAccountName, + String Currency, String Destination, String TaxpayerIdentificationNumber, String TaxRegistrationReasonCode, + String Account) { + BankAccountUpdateAction bankAccountUpdateAction = new BankAccountUpdateAction(); + bankAccountUpdateAction.setBankIdentificationCode(BankIdentificationCode); + bankAccountUpdateAction.setBankName(BankName); + bankAccountUpdateAction.setCorrespondentAccount(CorrespondentAccount); + bankAccountUpdateAction.setCorrespondentAccountName(CorrespondentAccountName); + bankAccountUpdateAction.setCurrency(Currency); + bankAccountUpdateAction.setDestination(Destination); + bankAccountUpdateAction.setTaxpayerIdentificationNumber(TaxpayerIdentificationNumber); + bankAccountUpdateAction.setTaxRegistrationReasonCode(TaxRegistrationReasonCode); + bankAccountUpdateAction.setAccount(Account); + return bankAccountUpdateAction; } } \ No newline at end of file