Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor ws autoconfiguration as well #277

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public class SimulatorEndpointAdapter extends RequestDispatchingEndpointAdapter
private final ApplicationContext applicationContext;
private final CorrelationHandlerRegistry handlerRegistry;
private final ScenarioExecutorService scenarioExecutorService;
private final SimulatorConfigurationProperties configuration;
private final SimulatorConfigurationProperties simulatorConfiguration;

@Getter
@Setter
private boolean handleResponse = true;

public SimulatorEndpointAdapter(ApplicationContext applicationContext, CorrelationHandlerRegistry handlerRegistry, ScenarioExecutorService scenarioExecutorService, SimulatorConfigurationProperties configuration) {
public SimulatorEndpointAdapter(ApplicationContext applicationContext, CorrelationHandlerRegistry handlerRegistry, ScenarioExecutorService scenarioExecutorService, SimulatorConfigurationProperties simulatorConfiguration) {
this.applicationContext = applicationContext;
this.handlerRegistry = handlerRegistry;
this.scenarioExecutorService = scenarioExecutorService;
this.configuration = configuration;
this.simulatorConfiguration = simulatorConfiguration;
}

private static ResponseStatusException getResponseStatusException(Throwable e) {
Expand Down Expand Up @@ -90,7 +90,7 @@ public Message dispatchMessage(Message message, String mappingName) {

SimulatorScenario scenario;
if (!hasText(scenarioName) || !applicationContext.containsBean(scenarioName)) {
scenarioName = configuration.getDefaultScenario();
scenarioName = simulatorConfiguration.getDefaultScenario();
logger.info("Unable to find scenario for mapping '{}' - using default scenario '{}'", mappingName, scenarioName);
}
scenario = applicationContext.getBean(scenarioName, SimulatorScenario.class);
Expand All @@ -112,7 +112,7 @@ public Message dispatchMessage(Message message, String mappingName) {
private Message awaitResponseOrThrowException(CompletableFuture<Message> responseFuture, String scenarioName) {
try {
if (handleResponse) {
var message = responseFuture.get(configuration.getDefaultTimeout(), MILLISECONDS);
var message = responseFuture.get(simulatorConfiguration.getDefaultTimeout(), MILLISECONDS);

if (EXCEPTION_TYPE.equals(message.getType())) {
throw getResponseStatusException(message.getPayload(Throwable.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import org.citrusframework.endpoint.EndpointAdapter;
import org.citrusframework.endpoint.adapter.EmptyResponseEndpointAdapter;
import org.citrusframework.http.controller.HttpMessageController;
Expand Down Expand Up @@ -61,6 +62,11 @@
import java.util.List;
import java.util.Map;

import static java.util.Collections.addAll;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static lombok.AccessLevel.PROTECTED;

@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(SimulatorAutoConfiguration.class)
Expand All @@ -70,20 +76,24 @@ public class SimulatorRestAutoConfiguration {

public static final String REST_ENDPOINT_ADAPTER_BEAN_NAME = "simulatorRestEndpointAdapter";

@Getter(PROTECTED)
private final ApplicationContext applicationContext;

@Getter(PROTECTED)
private final SimulatorRestConfigurationProperties simulatorRestConfiguration;

private @Nullable SimulatorRestConfigurer configurer;
@Getter(PROTECTED)
private final @Nullable SimulatorRestConfigurer restConfigurer;

/**
* Target Citrus Http controller
*/
private HttpMessageController restController;

public SimulatorRestAutoConfiguration(ApplicationContext applicationContext, SimulatorRestConfigurationProperties simulatorRestConfiguration, @Autowired(required = false) @Nullable SimulatorRestConfigurer configurer) {
public SimulatorRestAutoConfiguration(ApplicationContext applicationContext, SimulatorRestConfigurationProperties simulatorRestConfiguration, @Autowired(required = false) @Nullable SimulatorRestConfigurer restConfigurer) {
this.applicationContext = applicationContext;
this.simulatorRestConfiguration = simulatorRestConfiguration;
this.configurer = configurer;
this.restConfigurer = restConfigurer;
}

@Bean
Expand Down Expand Up @@ -169,8 +179,8 @@ public SimulatorEndpointAdapter simulatorRestEndpointAdapter(CorrelationHandlerR

@Bean
public EndpointAdapter simulatorRestFallbackEndpointAdapter() {
if (configurer != null) {
return configurer.fallbackEndpointAdapter();
if (nonNull(restConfigurer)) {
return restConfigurer.fallbackEndpointAdapter();
}

return new EmptyResponseEndpointAdapter();
Expand All @@ -180,7 +190,7 @@ public EndpointAdapter simulatorRestFallbackEndpointAdapter() {
* Gets the Citrus Http REST controller.
*/
protected HttpMessageController createRestController(SimulatorEndpointAdapter simulatorRestEndpointAdapter) {
if (restController == null) {
if (isNull(restController)) {
restController = new HttpMessageController();

simulatorRestEndpointAdapter.setMappingKeyExtractor(simulatorRestScenarioMapper());
Expand All @@ -194,8 +204,8 @@ protected HttpMessageController createRestController(SimulatorEndpointAdapter si

@Bean
public ScenarioMapper simulatorRestScenarioMapper() {
if (configurer != null) {
return configurer.scenarioMapper();
if (nonNull(restConfigurer)) {
return restConfigurer.scenarioMapper();
}

return new HttpRequestAnnotationScenarioMapper();
Expand All @@ -218,13 +228,11 @@ public HttpScenarioGenerator simulatorRestScenarioGenerator() {
/**
* Gets the url pattern to map this Http rest controller to. Clients must use this
* context path in order to access the Http REST support on the simulator.
*
* @return
*/
@NotNull
protected List<String> getUrlMappings() {
if (configurer != null) {
List<String> configuredUrls = configurer.urlMappings(simulatorRestConfiguration);
if (nonNull(restConfigurer)) {
List<String> configuredUrls = restConfigurer.urlMappings(simulatorRestConfiguration);
return configuredUrls != null ? configuredUrls : Collections.emptyList();
}

Expand All @@ -233,13 +241,11 @@ protected List<String> getUrlMappings() {

/**
* Provides list of endpoint interceptors.
*
* @return
*/
protected HandlerInterceptor[] interceptors(MessageListeners messageListeners, SimulatorMessageListener simulatorMessageListener) {
List<HandlerInterceptor> interceptors = new ArrayList<>();
if (configurer != null) {
Collections.addAll(interceptors, configurer.interceptors());
if (nonNull(restConfigurer)) {
addAll(interceptors, restConfigurer.interceptors());
}
interceptors.add(new LoggingHandlerInterceptor());
interceptors.add(httpInterceptor(messageListeners, simulatorMessageListener));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import jakarta.annotation.Nullable;
import jakarta.jms.ConnectionFactory;
import lombok.Getter;
import org.citrusframework.context.TestContextFactory;
import org.citrusframework.endpoint.EndpointAdapter;
import org.citrusframework.endpoint.adapter.EmptyResponseEndpointAdapter;
Expand Down Expand Up @@ -48,6 +49,8 @@
import org.springframework.util.StringUtils;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static lombok.AccessLevel.PROTECTED;

@Configuration
@AutoConfigureAfter(SimulatorAutoConfiguration.class)
Expand All @@ -57,22 +60,26 @@ public class SimulatorJmsAutoConfiguration {

public static final String JMS_ENDPOINT_ADAPTER_BEAN_NAME = "simulatorJmsEndpointAdapter";

@Getter(PROTECTED)
private final SimulatorConfigurationProperties simulatorConfiguration;

@Getter(PROTECTED)
private final SimulatorJmsConfigurationProperties simulatorJmsConfiguration;

private @Nullable SimulatorJmsConfigurer configurer;
@Getter(PROTECTED)
private final @Nullable SimulatorJmsConfigurer jmsConfigurer;

public SimulatorJmsAutoConfiguration(SimulatorConfigurationProperties simulatorConfiguration, SimulatorJmsConfigurationProperties simulatorJmsConfiguration, @Autowired(required = false) @Nullable SimulatorJmsConfigurer configurer) {
public SimulatorJmsAutoConfiguration(SimulatorConfigurationProperties simulatorConfiguration, SimulatorJmsConfigurationProperties simulatorJmsConfiguration, @Autowired(required = false) @Nullable SimulatorJmsConfigurer jmsConfigurer) {
this.simulatorConfiguration = simulatorConfiguration;
this.simulatorJmsConfiguration = simulatorJmsConfiguration;
this.configurer = configurer;
this.jmsConfigurer = jmsConfigurer;
}

@Bean
@ConditionalOnMissingBean
public ConnectionFactory connectionFactory() {
if (configurer != null) {
return configurer.connectionFactory();
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.connectionFactory();
}

return new SingleConnectionFactory();
Expand Down Expand Up @@ -104,14 +111,14 @@ protected JmsEndpoint simulatorJmsInboundEndpoint(ConnectionFactory connectionFa
}

@Bean(JMS_ENDPOINT_ADAPTER_BEAN_NAME)
public SimulatorEndpointAdapter simulatorJmsEndpointAdapter(ApplicationContext applicationContext, CorrelationHandlerRegistry handlerRegistry, ScenarioExecutorService scenarioExecutorService, SimulatorConfigurationProperties configuration) {
return new SimulatorEndpointAdapter(applicationContext, handlerRegistry, scenarioExecutorService, configuration);
public SimulatorEndpointAdapter simulatorJmsEndpointAdapter(ApplicationContext applicationContext, CorrelationHandlerRegistry handlerRegistry, ScenarioExecutorService scenarioExecutorService) {
return new SimulatorEndpointAdapter(applicationContext, handlerRegistry, scenarioExecutorService, getSimulatorConfiguration());
}

@Bean
public ScenarioMapper simulatorJmsScenarioMapper() {
if (configurer != null) {
return configurer.scenarioMapper();
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.scenarioMapper();
}

return new ContentBasedXPathScenarioMapper().addXPathExpression("local-name(/*)");
Expand Down Expand Up @@ -149,83 +156,74 @@ public SimulatorEndpointPoller simulatorJmsEndpointPoller(ConnectionFactory conn

@Bean
public EndpointAdapter simulatorJmsFallbackEndpointAdapter() {
if (configurer != null) {
return configurer.fallbackEndpointAdapter();
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.fallbackEndpointAdapter();
}

return new EmptyResponseEndpointAdapter();
}

/**
* Gets the destination name to receive messages from.
*
* @return
*/
protected String getInboundDestination() {
if (configurer != null) {
return configurer.inboundDestination(simulatorJmsConfiguration);
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.inboundDestination(simulatorJmsConfiguration);
}

return simulatorJmsConfiguration.getInboundDestination();
}

/**
* Gets the destination name to send messages to.
*
* @return
*/
protected String getReplyDestination() {
if (configurer != null) {
return configurer.replyDestination(simulatorJmsConfiguration);
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.replyDestination(simulatorJmsConfiguration);
}

return simulatorJmsConfiguration.getReplyDestination();
}

/**
* Should the endpoint use synchronous reply communication.
* @return
*/
protected boolean isSynchronous() {
if (configurer != null) {
return configurer.synchronous(simulatorJmsConfiguration);
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.synchronous(simulatorJmsConfiguration);
}

return simulatorJmsConfiguration.isSynchronous();
}

/**
* Should the endpoint use SOAP envelope handling.
* @return
*/
protected boolean useSoap() {
if (configurer != null) {
return configurer.useSoap(simulatorJmsConfiguration);
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.useSoap(simulatorJmsConfiguration);
}

return simulatorJmsConfiguration.isUseSoap();
}

/**
* Should the endpoint use pub sub domain.
* @return
* Should the endpoint use pub-sub domain.
*/
protected boolean isPubSubDomain() {
if (configurer != null) {
return configurer.pubSubDomain(simulatorJmsConfiguration);
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.pubSubDomain(simulatorJmsConfiguration);
}

return simulatorJmsConfiguration.isPubSubDomain();
}

/**
* Gets the endpoint polling exception delay.
* @param simulatorConfiguration
* @return
*/
protected Long exceptionDelay(SimulatorConfigurationProperties simulatorConfiguration) {
if (configurer != null) {
return configurer.exceptionDelay(simulatorConfiguration);
if (nonNull(jmsConfigurer)) {
return jmsConfigurer.exceptionDelay(simulatorConfiguration);
}

return simulatorConfiguration.getExceptionDelay();
Expand Down
Loading
Loading