Skip to content

Commit

Permalink
chore(#100): add sample with exploded query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed May 14, 2024
1 parent 2b2b4ef commit 63e767f
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.citrusframework.simulator.sample.scenario;

import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.simulator.scenario.AbstractSimulatorScenario;
import org.citrusframework.simulator.scenario.Scenario;
import org.citrusframework.simulator.scenario.ScenarioRunner;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

import static org.citrusframework.actions.EchoAction.Builder.echo;

@Scenario("Parameter")
@RequestMapping(value = "/services/rest/simulator/parameter", method = RequestMethod.GET)
public class ParameterScenario extends AbstractSimulatorScenario {

@Override
public void run(ScenarioRunner scenario) {
scenario.$(scenario.http()
.receive()
.get()
.getMessageBuilderSupport()
.extract((message, testContext) -> {
if (message instanceof HttpMessage httpMessage) {
testContext.addVariables(Map.of("pulver", httpMessage.getQueryParams().get("exploded")));
}
}));

scenario.$(echo("Extracted dangerous parameters: ${pulver}"));

scenario.$(scenario.http()
.send()
.response(HttpStatus.OK)
.message()
.body(
// language=json
"""
{
"pulver": ${pulver}
}
"""
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public class SimulatorRestIT extends TestNGCitrusSpringSupport {

private final String defaultResponse = "<DefaultResponse>This is a default response!</DefaultResponse>";

/** Test Http REST client */
/**
* Test Http REST client
*/
@Autowired
private HttpClient simulatorClient;

Expand All @@ -59,21 +61,21 @@ public class SimulatorRestIT extends TestNGCitrusSpringSupport {
@CitrusTest
public void testHelloRequest() {
$(http().client(simulatorClient)
.send()
.post("hello")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<Hello xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Say Hello!" +
"</Hello>"));
.send()
.post("hello")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<Hello xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Say Hello!" +
"</Hello>"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body("<HelloResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Hi there!" +
"</HelloResponse>"));
.receive()
.response(HttpStatus.OK)
.message()
.body("<HelloResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Hi there!" +
"</HelloResponse>"));
}

/**
Expand All @@ -82,46 +84,45 @@ public void testHelloRequest() {
@CitrusTest
public void testHowdyRequest() {
$(http().client(simulatorClient)
.send()
.post("howdy")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<Hello xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Say Hello!" +
"</Hello>"));
.send()
.post("howdy")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<Hello xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Say Hello!" +
"</Hello>"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body("<HelloResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Howdy partner!" +
"</HelloResponse>"));
.receive()
.response(HttpStatus.OK)
.message()
.body("<HelloResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Howdy partner!" +
"</HelloResponse>"));
}



/**
* Sends goodbye request to server expecting positive response message.
*/
@CitrusTest
public void testGoodByeRequest() {
$(http().client(simulatorClient)
.send()
.post("goodbye")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<GoodBye xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Say GoodBye!" +
"</GoodBye>"));
.send()
.post("goodbye")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<GoodBye xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Say GoodBye!" +
"</GoodBye>"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body("<GoodByeResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Bye bye!" +
"</GoodByeResponse>"));
.receive()
.response(HttpStatus.OK)
.message()
.body("<GoodByeResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Bye bye!" +
"</GoodByeResponse>"));
}

/**
Expand All @@ -130,19 +131,19 @@ public void testGoodByeRequest() {
@CitrusTest
public void testDefaultRequest() {
$(http().client(simulatorClient)
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<Default>" +
"Should trigger default scenario" +
"</Default>"));
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<Default>" +
"Should trigger default scenario" +
"</Default>"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body(defaultResponse));
.receive()
.response(HttpStatus.OK)
.message()
.body(defaultResponse));
}

/**
Expand All @@ -153,57 +154,57 @@ public void testInterveningRequest() {
variable("correlationId", "citrus:randomNumber(10)");

$(http().client(simulatorClient)
.send()
.post("goodnight")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<GoodNight xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Go to sleep!" +
"</GoodNight>")
.header("x-correlationid", "${correlationId}"));
.send()
.post("goodnight")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<GoodNight xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Go to sleep!" +
"</GoodNight>")
.header("x-correlationid", "${correlationId}"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body("<GoodNightResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Good Night!" +
"</GoodNightResponse>"));
.receive()
.response(HttpStatus.OK)
.message()
.body("<GoodNightResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
"Good Night!" +
"</GoodNightResponse>"));

//
// Should be handled by default scenario
//

$(http().client(simulatorClient)
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<InterveningRequest>No match correlation!</InterveningRequest>"));
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<InterveningRequest>No match correlation!</InterveningRequest>"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body(defaultResponse));
.receive()
.response(HttpStatus.OK)
.message()
.body(defaultResponse));

//
// Should be handled by good-night scenario
//

$(http().client(simulatorClient)
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<InterveningRequest>In between!</InterveningRequest>")
.header("x-correlationid", "${correlationId}"));
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<InterveningRequest>In between!</InterveningRequest>")
.header("x-correlationid", "${correlationId}"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body("<InterveningResponse>In between!</InterveningResponse>"));
.receive()
.response(HttpStatus.OK)
.message()
.body("<InterveningResponse>In between!</InterveningResponse>"));

$(sleep().milliseconds(2000L));

Expand All @@ -212,18 +213,39 @@ public void testInterveningRequest() {
//

$(http().client(simulatorClient)
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<InterveningRequest>After correlation!</InterveningRequest>")
.header("x-correlationid", "${correlationId}"));
.send()
.post()
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.body("<InterveningRequest>After correlation!</InterveningRequest>")
.header("x-correlationid", "${correlationId}"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body(defaultResponse));
.receive()
.response(HttpStatus.OK)
.message()
.body(defaultResponse));
}

@CitrusTest
public void testSimulationWithExplodingQueryParams() {
$(http().client(simulatorClient)
.send()
.get("parameter")
.queryParam("exploded", "1")
.queryParam("exploded", "2"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body(
"""
{
"pulver": [1, 2]
}
"""
));
}

/**
Expand Down Expand Up @@ -293,8 +315,8 @@ public XsdSchemaRepository schemaRepository() {
@Bean
public HttpClient simulatorClient() {
return CitrusEndpoints.http().client()
.requestUrl(String.format("http://localhost:%s/services/rest/simulator", 8080))
.build();
.requestUrl(String.format("http://localhost:%s/services/rest/simulator", 8080))
.build();
}

@Bean
Expand Down

0 comments on commit 63e767f

Please sign in to comment.