fixing bug in test

This commit is contained in:
psemenkov 2022-12-01 17:57:43 +03:00
parent 42529a8eed
commit 40c716d159
3 changed files with 122 additions and 78 deletions

View file

@ -1,7 +1,12 @@
package ru.spcex.clearing.backendapi.controller.queue;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
@ -10,20 +15,25 @@ import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import ru.spcex.clearing.backendapi.controller.queue.company.DeleteCompanyController;
import ru.spcex.clearing.backendapi.controller.queue.config.*;
import ru.spcex.clearing.backendapi.meta.GetResponseFactory;
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicLong;
@SpringJUnitWebConfig(classes = {
@ContextConfiguration(classes = {
BankAccountControllerConfig.class,
DeleteCompanyController.class,
WebTestConfig.class,
IOperator.class,
StateLoaderImplConfig.class,
DeleteCompanyController.class,
BankAccountControllerConfig.class,
KafkaTestConfig.class,
HazelcastServiceTestConfiguration.class,
StateLoaderImplConfig.class,
Jackson2HttpConverterTestConfig.class})
@ExtendWith(SpringExtension.class)
@WebMvcTest//(controllers = DeleteCompanyController.class)
@TestPropertySource(properties = "spring.config.location=D:/repo/mfd/clearing/clearing-parent/backend-api/src/main/resources/")
public abstract class AbstractControllerTest {
protected static final AtomicLong currentId = new AtomicLong();
private static final CharacterEncodingFilter CHARACTER_ENCODING_FILTER = new CharacterEncodingFilter();
@ -33,8 +43,13 @@ public abstract class AbstractControllerTest {
CHARACTER_ENCODING_FILTER.setForceEncoding(true);
}
@Autowired
@Qualifier("hazelcastServiceTest")
protected HazelcastService hazelcastServiceTest;
@Autowired
@Qualifier("responseFactoryTest")
protected GetResponseFactory responseFactory;
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;

View file

@ -2,27 +2,16 @@ package ru.spcex.clearing.backendapi.controller.queue.account;
import com.hazelcast.core.IMap;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import ru.clearing.classes.statics.data.account.BankAccount;
import ru.spcex.clearing.backendapi.controller.queue.config.*;
import ru.spcex.clearing.backendapi.controller.queue.utils.MatcherFactory;
import ru.spcex.clearing.backendapi.controller.queue.AbstractControllerTest;
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.controller.response.entity.CommonGetAllResponse;
import ru.spcex.clearing.backendapi.controller.response.entity.account.BankAccountBackendGetById;
import ru.spcex.clearing.backendapi.controller.response.entity.account.BankAccountBackendGetFields;
import ru.spcex.clearing.backendapi.domain.actions.IAction;
@ -30,10 +19,9 @@ import ru.spcex.clearing.backendapi.errors.ActionValidationException;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.ActionType;
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest;
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Collection;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@ -41,46 +29,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static ru.spcex.clearing.backendapi.controller.queue.utils.JsonUtil.writeValue;
@ContextConfiguration(classes = {
WebTestConfig.class,
IOperator.class,
BankAccountControllerConfig.class,
KafkaTestConfig.class,
HazelcastServiceTestConfiguration.class,
StateLoaderImplConfig.class,
Jackson2HttpConverterTestConfig.class})
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = BankAccountController.class)
@TestPropertySource(properties = "spring.config.location=D:/repo/mfd/clearing/clearing-parent/backend-api/src/main/resources/")
class BankAccountControllerTest {
public static final MatcherFactory.Matcher<CudResponse> CUD_RESPONSE_MATCHER = MatcherFactory.usingIgnoringFieldsComparator(CudResponse.class);
class BankAccountControllerTest extends AbstractControllerTest {
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");
CHARACTER_ENCODING_FILTER.setForceEncoding(true);
}
private MockMvc mockMvc;
@Autowired
@Qualifier("hazelcastServiceTest")
private HazelcastService hazelcastServiceTest;
@Autowired
private WebApplicationContext webApplicationContext;
@PostConstruct
private void postConstruct() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.addFilter(CHARACTER_ENCODING_FILTER)
// .apply(springSecurity())
.build();
}
/**
* {@link BankAccountController#add(BankAccountNewAction)}<br>
@ -115,9 +65,9 @@ class BankAccountControllerTest {
expected.setMessage("success");
expected.setPayload(new QueueSuccessResponse(ActionType.NEW, currentId.getAndIncrement()));
//ACT
mockMvc.perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(writeValue(bankAccountNewAction)))
perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(writeValue(bankAccountNewAction)))
.andDo(print())//output to the log request and response
//ASSERT
.andExpect(status().isOk())
@ -185,9 +135,9 @@ class BankAccountControllerTest {
expected.setMessage("success");
expected.setPayload(new QueueSuccessResponse(ActionType.UPDATE, currentId.getAndIncrement()));
//ACT
mockMvc.perform(MockMvcRequestBuilders.put(REST_URL + "0")
.contentType(MediaType.APPLICATION_JSON)
.content(writeValue(bankAccountNewAction)))
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())
@ -195,11 +145,10 @@ class BankAccountControllerTest {
.andExpect(content().json(writeValue(expected)));
}
/**
* {@link BankAccountController#delete(Long)} <br>
* Тест проверяет получение id сущности {@link Long} по REST API и отправку в Apache Kafka.<br>
* Входной запрос {@link Long}: - 0L<br>
* Входной запрос /securities/bank-accounts/{@link Long}: - 0L<br>
*/
@Test
void delete() throws Exception {
@ -209,15 +158,21 @@ class BankAccountControllerTest {
expected.setMessage("success");
expected.setPayload(new QueueSuccessResponse(ActionType.DELETE, currentId.getAndIncrement()));
//ACT
mockMvc.perform(MockMvcRequestBuilders.delete(REST_URL + "0")
.contentType(MediaType.APPLICATION_JSON))
perform(MockMvcRequestBuilders.delete(REST_URL + "0")
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())//output to the log request and response
// ASSERT
//ASSERT
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(content().json(writeValue(expected)));
}
/**
* {@link BankAccountController#getById(Long)} <br>
* Тест проверяет получение запроса по REST API и отправку всех записей из таблицы hazelcast Map_BankAccount.<br>
* Входной запрос /securities/bank-accounts/{@link Long}: - 0L <br>
* Ответ BankAccountBackendGetById <br>
*/
@Test
void getById() throws Exception {
//ARRANGE
@ -250,8 +205,8 @@ class BankAccountControllerTest {
payload.setId(existBankAccount.getId());
expected.setPayload(payload);
//ACT
mockMvc.perform(MockMvcRequestBuilders.get(REST_URL + existBankAccount.getId())
.contentType(MediaType.APPLICATION_JSON))
perform(MockMvcRequestBuilders.get(REST_URL + existBankAccount.getId())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())//output to the log request and response
// ASSERT
.andExpect(status().isOk())
@ -259,14 +214,48 @@ class BankAccountControllerTest {
.andExpect(content().json(writeValue(expected)));
}
/**
* {@link BankAccountController#getAll()} <br>
* Тест проверяет получение запроса по REST API и отправку всех записей из таблицы hazelcast Map_BankAccount.<br>
* Входной запрос /securities/bank-accounts/ <br>
* Ответ CommonGetAllResponse <br>
*/
@Test
void getAll() {
void getAll() throws Exception {
//ARRANGE
BankAccount existBankAccount = new BankAccount();
existBankAccount.setBankName("ooo tinkoff");
existBankAccount.setBankIdentificationCode("99999");
existBankAccount.setCorrespondentAccount("9294189285498598598");
existBankAccount.setCorrespondentAccountName("BIK OF TINKOFF");
existBankAccount.setCurrency("RUB");
existBankAccount.setDestination("OOO ROGA I KOPITA");
existBankAccount.setTaxpayerIdentificationNumber("848484848484");
existBankAccount.setTaxRegistrationReasonCode("886886");
existBankAccount.setAccount("123456789123");
existBankAccount.setId(currentId.get());
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
iMap.put(existBankAccount.getId(), existBankAccount);
Collection<BankAccount> values = iMap.values();
Collection<Map<String, Object>> all = responseFactory.responseFromObjectCollection(values);
CommonGetAllResponse expected = new CommonGetAllResponse();
expected.fromEntity(all);
//ACT
perform(MockMvcRequestBuilders.get(REST_URL)
.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(expected)));
}
private void assertThrowsFor(IAction<?> iAction) {
assertThrows(ActionValidationException.class, () -> {
try {
mockMvc.perform(MockMvcRequestBuilders.post(REST_URL).contentType(MediaType.APPLICATION_JSON).content(writeValue(iAction)));
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;

View file

@ -1,13 +1,20 @@
package ru.spcex.clearing.backendapi.controller.queue.company;
import com.hazelcast.core.IMap;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import ru.clearing.classes.statics.data.company.Company;
import ru.spcex.clearing.backendapi.controller.queue.AbstractControllerTest;
import ru.spcex.clearing.backendapi.controller.response.cud.CudResponse;
import ru.spcex.clearing.backendapi.controller.response.cud.QueueSuccessResponse;
import ru.spcex.clearing.backendapi.controller.response.entity.CommonGetAllResponse;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.ActionType;
import java.util.Collection;
import java.util.Map;
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;
@ -19,7 +26,7 @@ class DeleteCompanyControllerTest extends AbstractControllerTest {
/**
* {@link DeleteCompanyController#delete(Long)} <br>
* Тест проверяет получение id сущности {@link Long} по REST API и отправку в Apache Kafka.<br>
* Входной запрос {@link Long}: - 0L<br>
* Входной запрос /companies/{@link Long}: - 0L<br>
*/
@Test
void delete() throws Exception {
@ -32,9 +39,42 @@ class DeleteCompanyControllerTest extends AbstractControllerTest {
perform(MockMvcRequestBuilders.delete(REST_URL + "0")
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())//output to the log request and response
// ASSERT
// ASSERT
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(content().json(writeValue(extended)));
}
/**
* {@link DeleteCompanyController#getAll()} <br>
* Тест проверяет получение запроса по REST API и отправку всех записей из таблицы hazelcast Map_Company.<br>
* Входной запрос /companies <br>
*/
@Test
void getAll() throws Exception {
//ARRANGE
Company existCompany = new Company();
existCompany.setTradingCode("ooo tinkoff");
existCompany.setClearingCode("99999");
existCompany.setFullName("BIK OF TINKOFF");
existCompany.setShortName("OOO ROGA I KOPITA");
existCompany.setTradingCode("848484848484");
existCompany.setId(currentId.get());
IMap<Long, Company> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_Company);
iMap.put(existCompany.getId(), existCompany);
Collection<Company> values = iMap.values();
Collection<Map<String, Object>> all = responseFactory.responseFromObjectCollection(values);
CommonGetAllResponse expected = new CommonGetAllResponse();
expected.fromEntity(all);
//ACT
perform(MockMvcRequestBuilders.get(REST_URL)
.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(expected)));
}
}