Skip to content

Commit

Permalink
feat(citrus-http): easy json response helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Dec 7, 2023
1 parent f65052d commit edebb47
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
9 changes: 9 additions & 0 deletions endpoints/citrus-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@

package org.citrusframework.http.actions;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.citrusframework.TestAction;
import org.citrusframework.TestActionBuilder;
import org.citrusframework.endpoint.Endpoint;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;
import org.citrusframework.util.ObjectHelper;
import org.citrusframework.util.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

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

/**
* Action executes http server operations such as receiving requests and sending response messages.
Expand All @@ -34,6 +40,8 @@
*/
public class HttpServerActionBuilder implements TestActionBuilder.DelegatingTestActionBuilder<TestAction>, ReferenceResolverAware {

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

/** Bean reference resolver */
private ReferenceResolver referenceResolver;

Expand Down Expand Up @@ -73,6 +81,32 @@ public HttpServerResponseActionBuilder respond(HttpStatus status) {
return new HttpServerSendActionBuilder().response(status);
}

/**
* Generic response builder for sending JSON response messages to client with response status 200 (OK).
*
* @return
*/
public HttpServerResponseActionBuilder.HttpMessageBuilderSupport respondOkJson(String json) {
return new HttpServerSendActionBuilder()
.response(HttpStatus.OK)
.message()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(json);
}

/**
* Generic response builder for sending JSON response messages to client with response status 200 (OK).
*
* @return
*/
public HttpServerResponseActionBuilder.HttpMessageBuilderSupport respondOkJson(Object json) {
try {
return respondOkJson(OBJECT_MAPPER.writeValueAsString(json));
} catch (JsonProcessingException e) {
throw new CitrusRuntimeException("Failed to write JSON body as string!", e);
}
}

/**
* Receive Http requests as server.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.citrusframework.http.actions;

import org.citrusframework.endpoint.Endpoint;
import org.citrusframework.http.message.HttpMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Mockito.mock;
import static org.springframework.test.util.ReflectionTestUtils.getField;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

public class HttpServerActionBuilderTest {

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

private HttpServerActionBuilder fixture;

private static void verifyOkJsonResponse(HttpServerResponseActionBuilder.HttpMessageBuilderSupport httpMessageBuilderSupport) {
Object responseMessage = getField(httpMessageBuilderSupport, "httpMessage");
assertTrue(responseMessage instanceof HttpMessage);

HttpMessage httpMessage = (HttpMessage) responseMessage;

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

@BeforeMethod
public void beforeMethodSetup() {
fixture = new HttpServerActionBuilder(mock(Endpoint.class));
}

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

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

private record TestJsonObject(String property) {
}
}

0 comments on commit edebb47

Please sign in to comment.