Merge remote-tracking branch 'origin/psemenkov' into psemenkov

# Conflicts:
#	clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/account/BankAccountControllerTest.java
#	clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/config/BankAccountControllerConfig.java
#	clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/config/HazelcastServiceTestConfiguration.java
#	clearing-parent/backend-api/src/test/ru/spcex/clearing/backendapi/controller/queue/config/IOperator.java
This commit is contained in:
psemenkov 2022-09-26 18:52:45 +03:00
commit dd7610432a
4 changed files with 48 additions and 24 deletions

View file

@ -1,13 +1,18 @@
package ru.spcex.clearing.backendapi.controller.queue.account; package ru.spcex.clearing.backendapi.controller.queue.account;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NestedExceptionUtils; import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 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.spcex.clearing.backendapi.config.WebConfig; import ru.spcex.clearing.backendapi.config.WebConfig;
import ru.spcex.clearing.backendapi.controller.queue.AbstractControllerTest;
import ru.spcex.clearing.backendapi.controller.queue.config.*; 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.BankAccountNewAction;
import ru.spcex.clearing.backendapi.controller.request.cud.account.BankAccountUpdateAction; 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.CudResponse;
@ -17,6 +22,9 @@ import ru.spcex.clearing.backendapi.errors.ActionValidationException;
import ru.spcex.clearing.platform.messaging.domain.ActionType; import ru.spcex.clearing.platform.messaging.domain.ActionType;
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest; 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.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; 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.content;
@ -30,8 +38,31 @@ import static ru.spcex.clearing.backendapi.controller.queue.utils.JsonUtil.write
KafkaConfig.class, KafkaConfig.class,
HazelcastServiceTestConfiguration.class, HazelcastServiceTestConfiguration.class,
Jackson2HttpConverterConfig.class}) Jackson2HttpConverterConfig.class})
class BankAccountControllerTest extends AbstractControllerTest { class BankAccountControllerTest {
public static final MatcherFactory.Matcher<CudResponse> CUD_RESPONSE_MATCHER = MatcherFactory.usingIgnoringFieldsComparator(CudResponse.class);
private static final String REST_URL = "/securities/bank-accounts/"; 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
private WebApplicationContext webApplicationContext;
@PostConstruct
private void postConstruct() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.addFilter(CHARACTER_ENCODING_FILTER)
// .apply(springSecurity())
.build();
}
/** /**
* {@link BankAccountController#add(BankAccountNewAction)}<br> * {@link BankAccountController#add(BankAccountNewAction)}<br>
@ -66,9 +97,9 @@ class BankAccountControllerTest extends AbstractControllerTest {
extended.setMessage("success"); extended.setMessage("success");
extended.setPayload(new QueueSuccessResponse(ActionType.NEW, currentId.getAndIncrement())); extended.setPayload(new QueueSuccessResponse(ActionType.NEW, currentId.getAndIncrement()));
//ACT //ACT
perform(MockMvcRequestBuilders.post(REST_URL) mockMvc.perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(writeValue(bankAccountNewAction))) .content(writeValue(bankAccountNewAction)))
.andDo(print())//output to the log request and response .andDo(print())//output to the log request and response
// ASSERT // ASSERT
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -136,9 +167,9 @@ class BankAccountControllerTest extends AbstractControllerTest {
extended.setMessage("success"); extended.setMessage("success");
extended.setPayload(new QueueSuccessResponse(ActionType.UPDATE, currentId.getAndIncrement())); extended.setPayload(new QueueSuccessResponse(ActionType.UPDATE, currentId.getAndIncrement()));
//ACT //ACT
perform(MockMvcRequestBuilders.put(REST_URL + "0") mockMvc.perform(MockMvcRequestBuilders.put(REST_URL + "0")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(writeValue(bankAccountNewAction))) .content(writeValue(bankAccountNewAction)))
.andDo(print())//output to the log request and response .andDo(print())//output to the log request and response
// ASSERT // ASSERT
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -160,8 +191,8 @@ class BankAccountControllerTest extends AbstractControllerTest {
extended.setMessage("success"); extended.setMessage("success");
extended.setPayload(new QueueSuccessResponse(ActionType.DELETE, currentId.getAndIncrement())); extended.setPayload(new QueueSuccessResponse(ActionType.DELETE, currentId.getAndIncrement()));
//ACT //ACT
perform(MockMvcRequestBuilders.delete(REST_URL + "0") mockMvc.perform(MockMvcRequestBuilders.delete(REST_URL + "0")
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andDo(print())//output to the log request and response .andDo(print())//output to the log request and response
// ASSERT // ASSERT
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -172,7 +203,7 @@ class BankAccountControllerTest extends AbstractControllerTest {
private void assertThrowsFor(IAction<?> iAction) { private void assertThrowsFor(IAction<?> iAction) {
assertThrows(ActionValidationException.class, () -> { assertThrows(ActionValidationException.class, () -> {
try { try {
perform(MockMvcRequestBuilders.post(REST_URL).contentType(MediaType.APPLICATION_JSON).content(writeValue(iAction))); mockMvc.perform(MockMvcRequestBuilders.post(REST_URL).contentType(MediaType.APPLICATION_JSON).content(writeValue(iAction)));
} catch (Exception e) { } catch (Exception e) {
Throwable rootCause = NestedExceptionUtils.getRootCause(e); Throwable rootCause = NestedExceptionUtils.getRootCause(e);
throw rootCause != null ? rootCause : e; throw rootCause != null ? rootCause : e;

View file

@ -1,26 +1,18 @@
package ru.spcex.clearing.backendapi.controller.queue.config; package ru.spcex.clearing.backendapi.controller.queue.config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import ru.spcex.clearing.backendapi.controller.queue.account.BankAccountController; import ru.spcex.clearing.backendapi.controller.queue.account.BankAccountController;
import ru.spcex.clearing.backendapi.service.IOperator; import ru.spcex.clearing.backendapi.service.IOperator;
import ru.spcex.clearing.backendapi.service.impl.StateLoaderImpl;
@Configuration @Configuration
public class BankAccountControllerConfig { public class BankAccountControllerConfig {
@Autowired @Autowired
@Qualifier("iOperator")
private IOperator operator;
@Autowired
@Qualifier("stateLoaderImpl")
private StateLoaderImpl stateLoader;
@Bean @Bean
public BankAccountController createBankAccountController() { public BankAccountController createBankAccountController(IOperator operator) {
return new BankAccountController(operator, stateLoader); return new BankAccountController(operator);
} }
} }

View file

@ -40,7 +40,7 @@ public class HazelcastServiceTestConfiguration {
joinConfig.setTcpIpConfig(new TcpIpConfig().setEnabled(true).setMembers(List.of("127.0.0.1"))); joinConfig.setTcpIpConfig(new TcpIpConfig().setEnabled(true).setMembers(List.of("127.0.0.1")));
networkConfig.setJoin(joinConfig); networkConfig.setJoin(joinConfig);
cfg.setNetworkConfig(networkConfig); cfg.setNetworkConfig(networkConfig);
hazelcastInstance = Hazelcast.getOrCreateHazelcastInstance(cfg); hazelcastInstance = Hazelcast.newHazelcastInstance(cfg);
HazelcastHelper.otcSystem_setStorageState(true, hazelcastInstance); HazelcastHelper.otcSystem_setStorageState(true, hazelcastInstance);
return new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, params); return new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, params);
} }

View file

@ -16,8 +16,9 @@ public class IOperator {
private HazelcastService hazelcastServiceTest; private HazelcastService hazelcastServiceTest;
@Autowired @Autowired
@Bean(name = "iOperator") @Bean
public OperatorImpl createIOperator(Producer<String, Object> kafka) { public OperatorImpl createIOperator(Producer<String, Object> kafka) {
return new OperatorImpl(kafka, hazelcastServiceTest); return new OperatorImpl(kafka, hazelcastServiceTest);
} }
} }