-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#285): implement validation and refine scenario generation
- Loading branch information
Showing
24 changed files
with
2,553 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...rc/main/java/org/citrusframework/simulator/sample/SpecificPingResponseMessageBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package org.citrusframework.simulator.sample; | ||
|
||
import static java.lang.String.format; | ||
|
||
import io.apicurio.datamodels.openapi.models.OasOperation; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.commons.lang3.function.TriFunction; | ||
import org.citrusframework.http.actions.HttpServerResponseActionBuilder; | ||
import org.citrusframework.http.message.HttpMessage; | ||
import org.citrusframework.http.message.HttpMessageHeaders; | ||
import org.citrusframework.message.MessageType; | ||
import org.citrusframework.openapi.actions.OpenApiActionBuilder; | ||
import org.citrusframework.openapi.actions.OpenApiServerActionBuilder; | ||
import org.citrusframework.openapi.actions.OpenApiServerResponseActionBuilder; | ||
import org.citrusframework.simulator.http.HttpOperationScenario; | ||
import org.citrusframework.simulator.http.HttpResponseActionBuilderProvider; | ||
import org.citrusframework.simulator.scenario.ScenarioRunner; | ||
import org.citrusframework.simulator.scenario.SimulatorScenario; | ||
import org.springframework.http.MediaType; | ||
|
||
/** | ||
* {@link HttpResponseActionBuilderProvider} that provides specific responses for dedicated ping | ||
* calls. Shows, how to use a {@link HttpResponseActionBuilderProvider} to control the random | ||
* message generation. | ||
*/ | ||
public class SpecificPingResponseMessageBuilder implements HttpResponseActionBuilderProvider { | ||
|
||
private static final int MISSING_ID = Integer.MIN_VALUE; | ||
|
||
/** | ||
* Function that returns null to indicate, that the provider does not provide a builder for the given scenario. | ||
*/ | ||
private static final TriFunction<OpenApiServerActionBuilder, OasOperation, HttpMessage, HttpServerResponseActionBuilder> NULL_RESPONSE = SpecificPingResponseMessageBuilder::createNull; | ||
|
||
/** | ||
* Map to store specific functions per ping id. | ||
*/ | ||
private static final Map<Integer, TriFunction<OpenApiServerActionBuilder, OasOperation, HttpMessage, HttpServerResponseActionBuilder>> SPECIFC_BUILDER_MAP = new HashMap<>(); | ||
|
||
// Specific responses for some ids, all others will be handled by returning null and letting the random generator do its work. | ||
static { | ||
SPECIFC_BUILDER_MAP.put(15000, | ||
SpecificPingResponseMessageBuilder::createResponseWithDedicatedRequiredHeader); | ||
SPECIFC_BUILDER_MAP.put(10000, | ||
SpecificPingResponseMessageBuilder::createResponseWithMessageAndHeaders); | ||
SPECIFC_BUILDER_MAP.put(5000, SpecificPingResponseMessageBuilder::createResponseWithSpecificBody); | ||
SPECIFC_BUILDER_MAP.put(4000, | ||
SpecificPingResponseMessageBuilder::createResponseWithRandomGenerationSuppressed); | ||
} | ||
|
||
@Override | ||
public HttpServerResponseActionBuilder provideHttpServerResponseActionBuilder( | ||
ScenarioRunner scenarioRunner, SimulatorScenario simulatorScenario, | ||
HttpMessage receivedMessage) { | ||
|
||
if (!(simulatorScenario instanceof HttpOperationScenario httpOperationScenario)) { | ||
return null; | ||
} | ||
|
||
OpenApiServerActionBuilder openApiServerActionBuilder = new OpenApiActionBuilder( | ||
httpOperationScenario.getOpenApiSpecification()).server(scenarioRunner.getScenarioEndpoint()); | ||
|
||
return SPECIFC_BUILDER_MAP.getOrDefault(getIdFromPingRequest(receivedMessage), NULL_RESPONSE).apply(openApiServerActionBuilder, httpOperationScenario.getOperation(), receivedMessage); | ||
} | ||
|
||
private static Integer getIdFromPingRequest(HttpMessage httpMessage) { | ||
String uri = httpMessage.getUri(); | ||
Pattern pattern = Pattern.compile("/pingapi/v1/ping/(\\d*)"); | ||
Matcher matcher = pattern.matcher(uri); | ||
if (matcher.matches()) { | ||
return Integer.parseInt(matcher.group(1)); | ||
} | ||
return MISSING_ID; | ||
} | ||
|
||
/** | ||
* Sample to prove, that random data generation can be suppressed. Note that the generated | ||
* response is thus invalid and will result in an error. | ||
*/ | ||
private static OpenApiServerResponseActionBuilder createResponseWithRandomGenerationSuppressed( | ||
OpenApiServerActionBuilder openApiServerActionBuilder, OasOperation oasOperation, | ||
HttpMessage receivedMessage) { | ||
OpenApiServerResponseActionBuilder sendMessageBuilder = openApiServerActionBuilder.send( | ||
oasOperation.operationId, "200").enableRandomGeneration(false); | ||
sendMessageBuilder.message().body(format("{\"id\": %d, \"pingTime\": %d}", | ||
getIdFromPingRequest(receivedMessage), System.currentTimeMillis())); | ||
return sendMessageBuilder; | ||
} | ||
|
||
/** | ||
* Sample to prove, that the body content can be controlled, while headers will be generated by | ||
* random generator. | ||
*/ | ||
private static OpenApiServerResponseActionBuilder createResponseWithSpecificBody( | ||
OpenApiServerActionBuilder openApiServerActionBuilder, OasOperation oasOperation, | ||
HttpMessage receivedMessage) { | ||
OpenApiServerResponseActionBuilder sendMessageBuilder = openApiServerActionBuilder.send( | ||
oasOperation.operationId, "200"); | ||
sendMessageBuilder.message().body(format("{\"id\": %d, \"pingCount\": %d}", | ||
getIdFromPingRequest(receivedMessage), System.currentTimeMillis())); | ||
return sendMessageBuilder; | ||
} | ||
|
||
/** | ||
* Sample to prove, that the status, response and headers can be controlled and are not | ||
* overwritten by random generator. | ||
*/ | ||
private static OpenApiServerResponseActionBuilder createResponseWithMessageAndHeaders( | ||
OpenApiServerActionBuilder openApiServerActionBuilder, OasOperation oasOperation, | ||
HttpMessage receivedMessage) { | ||
OpenApiServerResponseActionBuilder sendMessageBuilder = openApiServerActionBuilder.send( | ||
oasOperation.operationId, "400", receivedMessage.getAccept()); | ||
sendMessageBuilder.message().type(MessageType.PLAINTEXT) | ||
.header(HttpMessageHeaders.HTTP_CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE) | ||
.header("Ping-Time", "1").body("Requests with id == 10000 cannot be processed!"); | ||
return sendMessageBuilder; | ||
} | ||
|
||
/** | ||
* Sample to prove, that a preset header can be controlled, while generating a valid random | ||
* response. | ||
*/ | ||
private static OpenApiServerResponseActionBuilder createResponseWithDedicatedRequiredHeader( | ||
OpenApiServerActionBuilder openApiServerActionBuilder, OasOperation oasOperation, | ||
HttpMessage receivedMessage) { | ||
OpenApiServerResponseActionBuilder sendMessageBuilder = openApiServerActionBuilder.send( | ||
oasOperation.operationId, "200", receivedMessage.getAccept()); | ||
sendMessageBuilder.message().header("Ping-Time", "0"); | ||
return sendMessageBuilder; | ||
} | ||
|
||
private static OpenApiServerResponseActionBuilder createNull( | ||
OpenApiServerActionBuilder ignoreOpenApiServerActionBuilder, | ||
OasOperation ignoreOasOperation, HttpMessage ignoreReceivedMessage) { | ||
return null; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
simulator-samples/sample-swagger/src/main/resources/openapi/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Note, that the petstore-v3.json has been slightly modified from its original version. | ||
OK messages have been added, where missing, to be able to activate the response validation feature. |
Oops, something went wrong.