forked from citrusframework/citrus-simulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(citrusframework#285): implement validation and refine scenario g…
…eneration
- Loading branch information
Thorsten Schlathoelter
committed
Jun 21, 2024
1 parent
f32e976
commit a75b86d
Showing
22 changed files
with
2,532 additions
and
240 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
121 changes: 121 additions & 0 deletions
121
...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,121 @@ | ||
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.OpenApiServerActionBuilder; | ||
import org.citrusframework.openapi.actions.OpenApiServerResponseActionBuilder; | ||
import org.citrusframework.simulator.http.HttpResponseActionBuilderProvider; | ||
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; | ||
|
||
private static final TriFunction<OpenApiServerActionBuilder, OasOperation, HttpMessage, HttpServerResponseActionBuilder> NULL_RESPONSE = SpecificPingResponseMessageBuilder::createNull; | ||
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 the random generator | ||
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( | ||
OpenApiServerActionBuilder openApiServerActionBuilder, OasOperation oasOperation, | ||
HttpMessage receivedMessage) { | ||
return SPECIFC_BUILDER_MAP.getOrDefault(getIdFromPingRequest(receivedMessage), NULL_RESPONSE).apply(openApiServerActionBuilder, oasOperation, 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.