Skip to content

Commit

Permalink
chore: jdk17 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Oct 20, 2023
1 parent 8c7f11a commit f17953d
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package org.citrusframework.simulator.controller;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.citrusframework.simulator.model.ScenarioParameter;
import org.citrusframework.simulator.service.ScenarioExecutionService;
import org.citrusframework.simulator.service.ScenarioLookupService;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -43,30 +47,13 @@ public ScenarioController(ScenarioExecutionService scenarioExecutionService, Sce
this.scenarios = getScenarioList(scenarioLookupService);
}

@Data
public static class Scenario {
public enum ScenarioType {
STARTER,
MESSAGE_TRIGGERED;
}

private final String name;
private final ScenarioType type;
}

private static List<Scenario> getScenarioList(ScenarioLookupService scenarioLookupService) {
final List<Scenario> scenarios = new ArrayList<>();
scenarioLookupService.getScenarioNames().forEach(name -> scenarios.add(new Scenario(name, Scenario.ScenarioType.MESSAGE_TRIGGERED)));
scenarioLookupService.getStarterNames().forEach(name -> scenarios.add(new Scenario(name, Scenario.ScenarioType.STARTER)));
return scenarios;
}

@Data
@NoArgsConstructor
public static class ScenarioFilter {
private String name;
}

/**
* Get a list of scenarios
*
Expand All @@ -76,13 +63,13 @@ public static class ScenarioFilter {
@RequestMapping(method = RequestMethod.POST)
public Collection<Scenario> getScenarioNames(@RequestBody(required = false) ScenarioFilter filter) {
return scenarios.stream()
.filter(scenario -> {
if (filter != null && StringUtils.hasText(filter.getName())) {
return scenario.getName().contains(filter.getName());
}
return true;
})
.sorted(Comparator.comparing(Scenario::getName)).toList();
.filter(scenario -> {
if (filter != null && StringUtils.hasText(filter.getName())) {
return scenario.name().contains(filter.getName());
}
return true;
})
.sorted(Comparator.comparing(Scenario::name)).toList();
}

/**
Expand All @@ -104,9 +91,23 @@ public Collection<ScenarioParameter> getScenarioParameters(@PathVariable("name")
*/
@RequestMapping(method = RequestMethod.POST, value = "/launch/{name}")
public Long launchScenario(
@PathVariable("name") String name,
@RequestBody(required = false) List<ScenarioParameter> scenarioParameters) {
@PathVariable("name") String name,
@RequestBody(required = false) List<ScenarioParameter> scenarioParameters) {
return scenarioExecutionService.run(name, scenarioParameters);
}

public record Scenario(String name, ScenarioController.Scenario.ScenarioType type) {
public enum ScenarioType {
STARTER,
MESSAGE_TRIGGERED
}

}

@Data
@NoArgsConstructor
public static class ScenarioFilter {
private String name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ public class CorrelationHandlerRegistry {
/**
* Map of active handlers
*/
private ConcurrentMap<CorrelationHandler, TestContext> registeredHandlers = new ConcurrentHashMap<>();

/**
* Maximum capacity of active tests
*/
private int queueCapacity = 1000;
private final ConcurrentMap<CorrelationHandler, TestContext> registeredHandlers = new ConcurrentHashMap<>();

/**
* Add new correlation manager to registry.
* @param handler
*/
public void register(CorrelationHandler handler, TestContext context) {
if (!registeredHandlers.keySet().contains(handler)) {
register(handler, context, 1000);
}

public void register(CorrelationHandler handler, TestContext context, int queueCapacity) {
if (!registeredHandlers.containsKey(handler)) {
registeredHandlers.put(handler, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.citrusframework.simulator.endpoint;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.citrusframework.context.TestContext;
import org.citrusframework.context.TestContextFactory;
import org.citrusframework.endpoint.Endpoint;
Expand All @@ -26,7 +27,6 @@
import org.citrusframework.messaging.Producer;
import org.citrusframework.messaging.ReplyProducer;
import org.citrusframework.simulator.exception.SimulatorException;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
Expand All @@ -35,7 +35,12 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;

import java.util.concurrent.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* @author Christoph Deppisch
Expand Down Expand Up @@ -186,14 +191,14 @@ public void stop() {
}

@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
if (autoStart) {
start();
}
}

@Override
public void destroy() throws Exception {
public void destroy() {
stop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,24 @@ public void run(ScenarioRunner scenario) {
scenario.name(operation.getOperationId());
scenario.$(echo("Generated scenario from swagger operation: " + operation.getOperationId()));

HttpServerRequestActionBuilder requestBuilder;
switch (method) {
case GET:
requestBuilder = scenario.http()
HttpServerRequestActionBuilder requestBuilder = switch (method) {
case GET -> scenario.http()
.receive()
.get();
break;
case POST:
requestBuilder = scenario.http()
case POST -> scenario.http()
.receive()
.post();
break;
case PUT:
requestBuilder = scenario.http()
case PUT -> scenario.http()
.receive()
.put();
break;
case HEAD:
requestBuilder = scenario.http()
case HEAD -> scenario.http()
.receive()
.head();
break;
case DELETE:
requestBuilder = scenario.http()
case DELETE -> scenario.http()
.receive()
.delete();
break;
default:
throw new SimulatorException("Unsupported request method: " + method.name());
}
default -> throw new SimulatorException("Unsupported request method: " + method.name());
};

requestBuilder
.message()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@

package org.citrusframework.simulator.http;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import jakarta.annotation.Nullable;
import lombok.Builder;
import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.message.Message;
import org.citrusframework.simulator.config.SimulatorConfigurationProperties;
import org.citrusframework.simulator.scenario.Scenario;
import org.citrusframework.simulator.scenario.ScenarioListAware;
import org.citrusframework.simulator.scenario.SimulatorScenario;
import org.citrusframework.simulator.scenario.mapper.AbstractScenarioMapper;
import lombok.Builder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Scenario mapper performs mapping logic on request mapping annotations on given scenarios. Scenarios match on request method as well as
* request path pattern matching.
Expand All @@ -41,11 +41,11 @@
*/
public class HttpRequestAnnotationScenarioMapper extends AbstractScenarioMapper implements ScenarioListAware {

@Autowired(required = false)
private List<SimulatorScenario> scenarioList = new ArrayList<>();

private final HttpRequestAnnotationMatcher httpRequestAnnotationMatcher = HttpRequestAnnotationMatcher.instance();

@Autowired(required = false)
private @Nullable List<SimulatorScenario> scenarioList;

@Override
protected String getMappingKey(Message request) {
if (request instanceof HttpMessage) {
Expand All @@ -55,48 +55,37 @@ protected String getMappingKey(Message request) {
return super.getMappingKey(request);
}

@Data
@Builder
private static final class EnrichedScenarioWithRequestMapping {
final SimulatorScenario scenario;
final RequestMapping requestMapping;

public boolean hasRequestMapping() {
return requestMapping != null;
}

public String name() {
return scenario.getClass().getAnnotation(Scenario.class).value();
}
}

protected String getMappingKeyForHttpMessage(HttpMessage httpMessage) {
Optional<String> mapping = scenarioList.stream()
List<SimulatorScenario> nullSafeList = Optional.ofNullable(scenarioList).orElse(Collections.emptyList());

// First look for exact match
Optional<String> mapping = nullSafeList.stream()
.map(scenario -> EnrichedScenarioWithRequestMapping.builder()
.scenario(scenario)
.requestMapping(AnnotationUtils.findAnnotation(scenario.getClass(), RequestMapping.class))
.build()
)
.filter(EnrichedScenarioWithRequestMapping::hasRequestMapping)
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestPathSupported(httpMessage, swrm.requestMapping(), true))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestMethodSupported(httpMessage, swrm.requestMapping()))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestQueryParamsSupported(httpMessage, swrm.requestMapping()))
.map(EnrichedScenarioWithRequestMapping::name)
.findFirst();

// If that didn't help, look for inecaxt match
if (mapping.isEmpty()) {
mapping = nullSafeList.stream()
.map(scenario -> EnrichedScenarioWithRequestMapping.builder()
.scenario(scenario)
.requestMapping(AnnotationUtils.findAnnotation(scenario.getClass(), RequestMapping.class))
.build()
.scenario(scenario)
.requestMapping(AnnotationUtils.findAnnotation(scenario.getClass(), RequestMapping.class))
.build()
)
.filter(EnrichedScenarioWithRequestMapping::hasRequestMapping)
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestPathSupported(httpMessage, swrm.getRequestMapping(), true))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestMethodSupported(httpMessage, swrm.getRequestMapping()))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestQueryParamsSupported(httpMessage, swrm.getRequestMapping()))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestPathSupported(httpMessage, swrm.requestMapping(), false))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestMethodSupported(httpMessage, swrm.requestMapping()))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestQueryParamsSupported(httpMessage, swrm.requestMapping()))
.map(EnrichedScenarioWithRequestMapping::name)
.findFirst();

if (!mapping.isPresent()) {
mapping = scenarioList.stream()
.map(scenario -> EnrichedScenarioWithRequestMapping.builder()
.scenario(scenario)
.requestMapping(AnnotationUtils.findAnnotation(scenario.getClass(), RequestMapping.class))
.build()
)
.filter(EnrichedScenarioWithRequestMapping::hasRequestMapping)
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestPathSupported(httpMessage, swrm.getRequestMapping(), false))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestMethodSupported(httpMessage, swrm.getRequestMapping()))
.filter(swrm -> httpRequestAnnotationMatcher.checkRequestQueryParamsSupported(httpMessage, swrm.getRequestMapping()))
.map(EnrichedScenarioWithRequestMapping::name)
.findFirst();
}

return mapping.orElseGet(() -> super.getMappingKey(httpMessage));
Expand Down Expand Up @@ -142,4 +131,15 @@ public SimulatorConfigurationProperties getConfiguration() {
public void setConfiguration(SimulatorConfigurationProperties configuration) {
this.setSimulatorConfigurationProperties(configuration);
}

@Builder
private record EnrichedScenarioWithRequestMapping(SimulatorScenario scenario, RequestMapping requestMapping) {
public boolean hasRequestMapping() {
return requestMapping != null;
}

public String name() {
return scenario.getClass().getAnnotation(Scenario.class).value();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.citrusframework.simulator.http;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.citrusframework.http.controller.HttpMessageController;
import org.citrusframework.message.RawMessage;
import org.citrusframework.report.MessageListeners;
Expand All @@ -25,8 +27,6 @@
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
Expand All @@ -50,14 +50,14 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
if (messageListeners != null) {
messageListeners.onOutboundMessage(new RawMessage(getResponseContent(request, response, handler)), null);
}
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

}

Expand Down
Loading

0 comments on commit f17953d

Please sign in to comment.