-
Notifications
You must be signed in to change notification settings - Fork 46
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
open api scenario generator #315
Open
bbortt
wants to merge
6
commits into
citrusframework:main
Choose a base branch
from
postfinance:issue/285/open_api_scenario_generator_TS_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d03458e
feat(#285): support OpenAPI 3.0 from HttpOperationScenario
7c34468
feat(#285): implement validation and refine scenario generation
58d37e5
chore: adjustments due to openapi feature changes
3bd51b8
chore: adjustments due to openapi feature changes
fbb616d
chore: PF RC3
449dd45
chore: update spring.boot version
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
141 changes: 141 additions & 0 deletions
141
...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,141 @@ | ||||||
package org.citrusframework.simulator.sample; | ||||||
|
||||||
import static java.lang.String.format; | ||||||
import static org.citrusframework.openapi.OpenApiSettings.getResponseAutoFillRandomValues; | ||||||
|
||||||
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(getResponseAutoFillRandomValues()); | ||||||
sendMessageBuilder.message().body(format("{\"id\": %d, \"pingTime\": %d}", | ||||||
getIdFromPingRequest(receivedMessage), System.currentTimeMillis())); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
please use static import for this. |
||||||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be reverted