Skip to content

Commit

Permalink
feat: easy json ok response helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Nov 30, 2023
1 parent 6f5e857 commit c204d66
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,19 @@ public void run(ScenarioRunner scenario) {

scenario.run(calculateIban());

scenario.$(scenario.http()
.send()
.response(HttpStatus.OK)
.message()
.body(JSON_RESPONSE.placeholder())
.contentType(MediaType.APPLICATION_JSON_VALUE));
scenario.$(
scenario.http()
.sendOkJson(JSON_RESPONSE.placeholder())
);
}

private TestAction calculateIban() {
return new AbstractTestAction() {
@Override
public void doExecute(TestContext context) {
final String queryParams = context.getVariable(QUERY_PARAMS.name(), String.class);
final String sortCode = queryParameterService.getSortCode(queryParams);
final String bankAccountNumber = queryParameterService.getBankAccountNumber(queryParams);
final String jsonResponse = bankService.calculate(sortCode, bankAccountNumber).asJson();
context.setVariable(JSON_RESPONSE.name(), jsonResponse);
}
return (TestContext context) -> {
final String queryParams = context.getVariable(QUERY_PARAMS.name(), String.class);
final String sortCode = queryParameterService.getSortCode(queryParams);
final String bankAccountNumber = queryParameterService.getBankAccountNumber(queryParams);
final String jsonResponse = bankService.calculate(sortCode, bankAccountNumber).asJson();
context.setVariable(JSON_RESPONSE.name(), jsonResponse);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ public void run(ScenarioRunner scenario) {

scenario.run(validateIban());

scenario.$(scenario.http()
.send()
.response(HttpStatus.OK)
.message()
.body(JSON_RESPONSE.placeholder())
.contentType(MediaType.APPLICATION_JSON_VALUE));
scenario.$(
scenario.http()
.sendOkJson(JSON_RESPONSE.placeholder())
);
}

private TestAction validateIban() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ public OutboundXmlDataDictionary(SimulatorConfigurationProperties simulatorConfi

@Override
public <T> T translate(Node node, T value, TestContext context) {
if (value instanceof String) {
String toTranslate;
if (value instanceof String stringValue) {
String toTranslate = stringValue;
if (!mappings.isEmpty()) {
toTranslate = (String) super.translate(node, value, context);
} else {
toTranslate = (String) value;
}

if (toTranslate.equals(value)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package org.citrusframework.simulator.http;

import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.http.actions.HttpActionBuilder;
import org.citrusframework.http.actions.HttpServerActionBuilder;
import org.citrusframework.http.actions.HttpServerResponseActionBuilder.HttpMessageBuilderSupport;
import org.citrusframework.simulator.scenario.ScenarioEndpoint;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

/**
* @author Christoph Deppisch
*/
public class HttpScenarioActionBuilder extends HttpActionBuilder {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().enable(INDENT_OUTPUT);

/** Scenario endpoint */
private final ScenarioEndpoint scenarioEndpoint;

Expand All @@ -31,4 +41,36 @@ public HttpServerActionBuilder.HttpServerReceiveActionBuilder receive() {
public HttpServerActionBuilder.HttpServerSendActionBuilder send() {
return server(scenarioEndpoint).send();
}

/**
* Send scenario {@code application/json} response operation.
* @return
*/
public HttpMessageBuilderSupport sendOkJson(String json) {
return server(scenarioEndpoint)
.send()
.response(HttpStatus.OK)
.message()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(json);
}

/**
* Send scenario {@code application/json} response operation from serialized {@link Object}.
* @return
*/
public HttpMessageBuilderSupport sendOkJson(Object jsonObject) {
HttpMessageBuilderSupport httpMessageBuilderSupport = server(scenarioEndpoint).send()
.response(HttpStatus.OK)
.message()
.contentType(MediaType.APPLICATION_JSON_VALUE);

try {
httpMessageBuilderSupport.body(OBJECT_MAPPER.writeValueAsString(jsonObject));
} catch (JsonProcessingException e) {
throw new CitrusRuntimeException(e);
}

return httpMessageBuilderSupport;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.citrusframework.simulator.http;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.citrusframework.http.actions.HttpServerResponseActionBuilder.HttpMessageBuilderSupport;
import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.simulator.scenario.ScenarioEndpoint;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;

@ExtendWith(MockitoExtension.class)
class HttpScenarioActionBuilderTest {

private static final TestJsonObject JSON_OBJECT_REPRESENTATION = new TestJsonObject("value");
private static final String JSON_STRING_REPRESENTATION = """
{
"property" : "value"
}""";

@Mock
private ScenarioEndpoint scenarioEndpointMock;

private HttpScenarioActionBuilder fixture;

private static void verifyOkJsonResponse(HttpMessageBuilderSupport httpMessageBuilderSupport) {
HttpMessage httpMessage = (HttpMessage) ReflectionTestUtils.getField(httpMessageBuilderSupport, "httpMessage");
assertNotNull(httpMessage);

assertEquals(HttpStatus.OK, httpMessage.getStatusCode());
assertEquals(MediaType.APPLICATION_JSON_VALUE, httpMessage.getContentType());
assertEquals(JSON_STRING_REPRESENTATION, httpMessage.getPayload(String.class).replace("\r\n", "\n"));
}

@BeforeEach
void beforeEachSetup() {
fixture = new HttpScenarioActionBuilder(scenarioEndpointMock);
}

@Test
void sendOkJsonFromString() {
HttpMessageBuilderSupport httpMessageBuilderSupport = fixture.sendOkJson(JSON_STRING_REPRESENTATION);
verifyOkJsonResponse(httpMessageBuilderSupport);
}

@Test
void sendOkJsonFromObject() {
HttpMessageBuilderSupport httpMessageBuilderSupport = fixture.sendOkJson(JSON_OBJECT_REPRESENTATION);
verifyOkJsonResponse(httpMessageBuilderSupport);
}

private record TestJsonObject(String property) {
}
}

0 comments on commit c204d66

Please sign in to comment.