Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions graphql-spring-boot-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
dependencies {
compile("org.springframework:spring-web:$LIB_SPRING_CORE_VER")
compile("org.springframework.boot:spring-boot-starter-test:$LIB_SPRING_BOOT_VER")
compile("com.fasterxml.jackson.core:jackson-databind:2.5.4")
compile("com.jayway.jsonpath:json-path:2.3.0")
compile("com.fasterxml.jackson.core:jackson-databind:2.10.2")
compile("com.jayway.jsonpath:json-path:2.4.0")
compileOnly("com.graphql-java:graphql-java:$LIB_GRAPHQL_JAVA_VER")
compileOnly("com.graphql-java-kickstart:graphql-java-servlet:$LIB_GRAPHQL_SERVLET_VER")
testImplementation("org.springframework.boot:spring-boot-starter-web:$LIB_SPRING_BOOT_VER")
}
repositories {
mavenCentral()
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

public class GraphQLResponse {

private ResponseEntity<String> responseEntity;
private ObjectMapper mapper;
private ReadContext context;
private final ResponseEntity<String> responseEntity;
private final ObjectMapper mapper;
private final ReadContext context;

public GraphQLResponse(ResponseEntity<String> responseEntity) {
public GraphQLResponse(ResponseEntity<String> responseEntity, ObjectMapper objectMapper) {
this.responseEntity = Objects.requireNonNull(responseEntity);
this.mapper = new ObjectMapper();
this.mapper = Objects.requireNonNull(objectMapper);

Objects.requireNonNull(responseEntity.getBody(),
() -> "Body is empty with status " + responseEntity.getStatusCodeValue());
Expand All @@ -31,11 +31,11 @@ public JsonNode readTree() throws IOException {
}

public String get(String path) {
return context.read(path);
return get(path, String.class);
}

public <T> T get(String path, Class<T> type) {
return context.read(path, type);
return mapper.convertValue(context.read(path, Object.class), type);
}

public <T> List<T> getList(String path, Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class GraphQLTestTemplate {
private TestRestTemplate restTemplate;
@Value("${graphql.servlet.mapping:/graphql}")
private String graphqlMapping;
@Autowired
private ObjectMapper objectMapper;

private ObjectMapper objectMapper = new ObjectMapper();
private HttpHeaders headers = new HttpHeaders();

private String createJsonQuery(String graphql, ObjectNode variables)
Expand Down Expand Up @@ -138,7 +139,7 @@ private GraphQLResponse post(String payload) {

private GraphQLResponse postRequest(HttpEntity<Object> request) {
ResponseEntity<String> response = restTemplate.exchange(graphqlMapping, HttpMethod.POST, request, String.class);
return new GraphQLResponse(response);
return new GraphQLResponse(response, objectMapper);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.graphql.spring.boot.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = TestApplication.class)
public class GraphQLResponseTest {

private static final String DATA_PATH = "$.data.test";

@Autowired
private ObjectMapper objectMapper;

private static Stream<Arguments> testGetStringArguments() {
return Stream.of(
Arguments.of("{\"data\": {\"test\": 2}}", "2"),
Arguments.of("{\"data\": {\"test\": \"2\"}}", "2"),
Arguments.of("{\"data\": {\"test\": \"2020-02-23\"}}", "2020-02-23")
);
}

private static Stream<Arguments> testGetArguments() {
return Stream.of(
Arguments.of("{\"data\": {\"test\": \"2\"}}", Integer.class, 2),
Arguments.of("{\"data\": {\"test\": \"2\"}}", String.class, "2"),
Arguments.of("{\"data\": {\"test\": \"2\"}}", BigDecimal.class, new BigDecimal("2")),
Arguments.of("{\"data\": {\"test\": \"2020-02-23\"}}", LocalDate.class, LocalDate.parse("2020-02-23")),
Arguments.of("{\"data\": {\"test\": {\"foo\": \"fizzBuzz\", \"bar\": 13.8 }}}", FooBar.class,
new FooBar("fizzBuzz", new BigDecimal("13.8")))
);
}

private static Stream<Arguments> testGetListArguments() {
return Stream.of(
Arguments.of("{\"data\": {\"test\": [\"2\", \"1\"]}}", Integer.class, Arrays.asList(2, 1)),
Arguments.of("{\"data\": {\"test\": [\"2\", \"1\"]}}", String.class, Arrays.asList("2", "1")),
Arguments.of("{\"data\": {\"test\": [\"2\", \"1\"]}}", BigDecimal.class,
Arrays.asList(new BigDecimal("2"), new BigDecimal("1"))),
Arguments.of("{\"data\": {\"test\": [\"2020-02-23\", \"2020-02-24\"]}}", LocalDate.class,
Arrays.asList(LocalDate.parse("2020-02-23"), LocalDate.parse("2020-02-24"))),
Arguments.of("{\"data\":{\"test\":[{\"foo\":\"fizz\",\"bar\":1.23},{\"foo\":\"buzz\",\"bar\":32.12}]}}",
FooBar.class,
Arrays.asList(
new FooBar("fizz", new BigDecimal("1.23")),
new FooBar("buzz", new BigDecimal("32.12"))
)
)
);
}

@DisplayName("Should get the JSON node's value as a String.")
@ParameterizedTest
@MethodSource("testGetStringArguments")
public void testGetString(
final String bodyString,
final String expected
) {
//GIVEN
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper);
//WHEN
final String actual = graphQLResponse.get(DATA_PATH);
//THEN
assertThat(actual).isEqualTo(expected);
}

@DisplayName("Should get the JSON node's value as an instance of a specified class.")
@ParameterizedTest
@MethodSource("testGetArguments")
public <T> void testGet(
final String bodyString,
final Class<T> clazz,
final T expected
) {
//GIVEN
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper);
//WHEN
final T actual = graphQLResponse.get(DATA_PATH, clazz);
//THEN
assertThat(actual).isInstanceOf(clazz).isEqualTo(expected);
}

@DisplayName("Should get the JSON node's value as a List.")
@ParameterizedTest
@MethodSource("testGetListArguments")
public <T> void testGetList(
final String bodyString,
final Class<T> clazz,
final List<T> expected
) {
//GIVEN
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper);
//WHEN
final List<T> actual = graphQLResponse.getList(DATA_PATH, clazz);
//THEN
assertThat(actual).containsExactlyElementsOf(expected);
}

@Data
@AllArgsConstructor
@NoArgsConstructor
private static class FooBar {
private String foo;
private BigDecimal bar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.graphql.spring.boot.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}