Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit b891660

Browse files
feat: add GraphQL response field assert as JavaType methods
1 parent 99c80ce commit b891660

File tree

6 files changed

+215
-6
lines changed

6 files changed

+215
-6
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/assertions/GraphQLFieldAssert.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.graphql.spring.boot.test.assertions;
22

3+
import com.fasterxml.jackson.databind.JavaType;
34
import com.graphql.spring.boot.test.GraphQLResponse;
45
import com.jayway.jsonpath.PathNotFoundException;
56
import lombok.RequiredArgsConstructor;
@@ -158,13 +159,35 @@ public GraphQLStringAssert asString() {
158159

159160
/**
160161
* Returns an assertion for the content of the field as an instance of the specified class.
162+
* @param clazz The class of the object. to assert
161163
* @return a {@link GraphQLGenericObjectAssert} instance
162164
* @throws AssertionError if the path does not exist or the content could not be converted to the specified class
163165
*/
164166
public <T> GraphQLGenericObjectAssert<T> as(final Class<T> clazz) {
165167
return new GraphQLGenericObjectAssert<>(graphQLResponse, getFieldAs(clazz));
166168
}
167169

170+
/**
171+
* Returns an assertion for the content of the field as an instance of the specified type.
172+
* @param javaType The java type definition.
173+
* @return a {@link GraphQLGenericObjectAssert} instance
174+
* @throws AssertionError if the path does not exist or the content could not be converted to the specified class
175+
*/
176+
public <T> GraphQLGenericObjectAssert<T> as(final JavaType javaType) {
177+
return new GraphQLGenericObjectAssert<>(graphQLResponse, getFieldAs(javaType));
178+
}
179+
180+
/**
181+
* Returns an assertion for the content of the field as an instance of the specified list type.
182+
* @param javaListType The java type definition. Expected to define a list type.
183+
* @return a {@link GraphQLListAssert} instance
184+
* @throws AssertionError if the path does not exist or the content could not be converted to the specified class
185+
* or if the provided type is not a list type.
186+
*/
187+
public <T> GraphQLListAssert<T> asList(final JavaType javaListType) {
188+
return new GraphQLListAssert<>(graphQLResponse, getFieldAs(javaListType));
189+
}
190+
168191
/**
169192
* Returns an assertion for the content of the field as list of objects.
170193
* @param elementClass the type of objects in the list
@@ -195,6 +218,18 @@ private <T> T getFieldAs(final Class<T> targetClass) {
195218
}
196219
}
197220

221+
private <T> T getFieldAs(final JavaType javaType) {
222+
try {
223+
return graphQLResponse.get(jsonPath, javaType);
224+
} catch (PathNotFoundException e) {
225+
fail(String.format("Expected field %s to be present.", jsonPath), e);
226+
return null;
227+
} catch (IllegalArgumentException e) {
228+
fail(String.format("Expected that content of field %s can be converted to %s.", jsonPath, javaType), e);
229+
return null;
230+
}
231+
}
232+
198233
private <T> List<T> getFieldAsList(final Class<T> targetClass) {
199234
try {
200235
return graphQLResponse.getList(jsonPath, targetClass);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import com.fasterxml.jackson.databind.JavaType;
4+
import com.fasterxml.jackson.databind.type.TypeFactory;
5+
import com.graphql.spring.boot.test.assertions.GraphQLFieldAssert;
6+
import com.graphql.spring.boot.test.assertions.GraphQLListAssert;
7+
import com.jayway.jsonpath.PathNotFoundException;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.Mock;
11+
12+
import java.util.Arrays;
13+
import java.util.List;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
17+
import static org.mockito.BDDMockito.given;
18+
19+
public class GraphQLFieldAssertAsJavaListTypeTest extends GraphQLFieldAssertTestBase {
20+
21+
private static final JavaType STRING_LIST_TYPE = TypeFactory.defaultInstance()
22+
.constructCollectionLikeType(List.class, String.class);
23+
24+
@Test
25+
@DisplayName("Should return a String assertion (value at specific path is valid list).")
26+
void shouldReturnStringAssertIfFieldIsNonNull() {
27+
// GIVEN
28+
final List<String> values = Arrays.asList("value1", "value2");
29+
given(graphQLResponse.get(MOCK_PATH, STRING_LIST_TYPE)).willReturn(values);
30+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
31+
// WHEN
32+
final GraphQLListAssert<String> actual = graphQLFieldAssert.asList(STRING_LIST_TYPE);
33+
// THEN
34+
assertThat(actual).isNotNull();
35+
assertThat(actual.and()).isSameAs(graphQLResponse);
36+
assertThat(actual.containsExactlyElementsOf(values).and()).isSameAs(graphQLResponse);
37+
assertThat(actual).extracting("actual").isSameAs(values);
38+
}
39+
40+
@Test
41+
@DisplayName("Should return a String assertion (value at specific path is null).")
42+
void shouldReturnStringAssertIfFieldIsNull() {
43+
// GIVEN
44+
given(graphQLResponse.get(MOCK_PATH, STRING_LIST_TYPE)).willReturn(null);
45+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
46+
// WHEN
47+
final GraphQLListAssert<String> actual = graphQLFieldAssert.asList(STRING_LIST_TYPE);
48+
// THEN
49+
assertThat(actual).isNotNull();
50+
assertThat(actual.and()).isSameAs(graphQLResponse);
51+
assertThat(actual).extracting("actual").isSameAs(null);
52+
}
53+
54+
@Test
55+
@DisplayName("Should fail if the value at the provided path is missing.")
56+
void shouldFailIfPathNotFound(final @Mock PathNotFoundException pathNotFoundException) {
57+
// GIVEN
58+
given(graphQLResponse.get(MOCK_PATH, STRING_LIST_TYPE)).willThrow(pathNotFoundException);
59+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
60+
// WHEN - THEN
61+
assertThatExceptionOfType(AssertionError.class)
62+
.isThrownBy(() -> graphQLFieldAssert.asList(STRING_LIST_TYPE))
63+
.withMessage("Expected field %s to be present.", MOCK_PATH)
64+
.withCause(pathNotFoundException);
65+
}
66+
67+
@Test
68+
@DisplayName("Should fail if the value at the provided path cannot be converted.")
69+
void shouldFailIfCannotBeConverted(final @Mock IllegalArgumentException illegalArgumentException) {
70+
// GIVEN
71+
given(graphQLResponse.get(MOCK_PATH, STRING_LIST_TYPE)).willThrow(illegalArgumentException);
72+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
73+
// WHEN - THEN
74+
assertThatExceptionOfType(AssertionError.class)
75+
.isThrownBy(() -> graphQLFieldAssert.asList(STRING_LIST_TYPE))
76+
.withMessage("Expected that content of field %s can be converted to %s.", MOCK_PATH,
77+
STRING_LIST_TYPE)
78+
.withCause(illegalArgumentException);
79+
}
80+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import com.fasterxml.jackson.databind.JavaType;
4+
import com.fasterxml.jackson.databind.type.TypeFactory;
5+
import com.graphql.spring.boot.test.assertions.GraphQLFieldAssert;
6+
import com.graphql.spring.boot.test.assertions.GraphQLGenericObjectAssert;
7+
import com.jayway.jsonpath.PathNotFoundException;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.Mock;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
14+
import static org.mockito.BDDMockito.given;
15+
16+
public class GraphQLFieldAssertAsJavaTypeTest extends GraphQLFieldAssertTestBase {
17+
18+
private static final JavaType FOO_TYPE = TypeFactory.defaultInstance().constructType(Foo.class);
19+
20+
@Test
21+
@DisplayName("Should return a generic assertion (value at specific path is non-null).")
22+
void shouldReturnGenericObjectAssertIfFieldIsNonNull() {
23+
// GIVEN
24+
final Foo foo = new Foo("fooBar");
25+
given(graphQLResponse.get(MOCK_PATH, FOO_TYPE)).willReturn(foo);
26+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
27+
// WHEN
28+
final GraphQLGenericObjectAssert<GraphQLFieldAssertAsTest.Foo>
29+
actual = graphQLFieldAssert.as(FOO_TYPE);
30+
// THEN
31+
assertThat(actual).isNotNull();
32+
assertThat(actual.and()).isSameAs(graphQLResponse);
33+
assertThat(actual.isEqualTo(foo).and()).isSameAs(graphQLResponse);
34+
assertThat(actual).extracting("actual").isSameAs(foo);
35+
}
36+
37+
@Test
38+
@DisplayName("Should return a generic assertion (value at specific path is null).")
39+
void shouldReturnGenericObjectAssertIfFieldIsNull() {
40+
// GIVEN
41+
given(graphQLResponse.get(MOCK_PATH, FOO_TYPE)).willReturn(null);
42+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
43+
// WHEN
44+
final GraphQLGenericObjectAssert<Foo> actual = graphQLFieldAssert.as(FOO_TYPE);
45+
// THEN
46+
assertThat(actual).isNotNull();
47+
assertThat(actual.and()).isSameAs(graphQLResponse);
48+
assertThat(actual).extracting("actual").isSameAs(null);
49+
}
50+
51+
@Test
52+
@DisplayName("Should fail if the value at the provided path is missing.")
53+
void shouldFailIfPathNotFound(final @Mock PathNotFoundException pathNotFoundException) {
54+
// GIVEN
55+
given(graphQLResponse.get(MOCK_PATH, FOO_TYPE)).willThrow(pathNotFoundException);
56+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
57+
// WHEN - THEN
58+
assertThatExceptionOfType(AssertionError.class)
59+
.isThrownBy(() -> graphQLFieldAssert.as(FOO_TYPE))
60+
.withMessage("Expected field %s to be present.", MOCK_PATH)
61+
.withCause(pathNotFoundException);
62+
}
63+
64+
@Test
65+
@DisplayName("Should fail if the value at the provided path cannot be converted.")
66+
void shouldFailIfCannotBeConverted(final @Mock IllegalArgumentException illegalArgumentException) {
67+
// GIVEN
68+
given(graphQLResponse.get(MOCK_PATH, FOO_TYPE)).willThrow(illegalArgumentException);
69+
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);
70+
// WHEN - THEN
71+
assertThatExceptionOfType(AssertionError.class)
72+
.isThrownBy(() -> graphQLFieldAssert.as(FOO_TYPE))
73+
.withMessage("Expected that content of field %s can be converted to %s.", MOCK_PATH,
74+
FOO_TYPE)
75+
.withCause(illegalArgumentException);
76+
}
77+
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,4 @@ void shouldFailIfIsNotNull(final @Mock IllegalArgumentException illegalArgumentE
7171
Foo.class)
7272
.withCause(illegalArgumentException);
7373
}
74-
75-
@Data
76-
@AllArgsConstructor
77-
private static class Foo {
78-
private String bar;
79-
}
8074
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertTestBase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.graphql.spring.boot.test;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
35
import org.junit.jupiter.api.extension.ExtendWith;
46
import org.mockito.Mock;
57
import org.mockito.junit.jupiter.MockitoExtension;
@@ -12,4 +14,10 @@ public class GraphQLFieldAssertTestBase {
1214

1315
@Mock
1416
protected GraphQLResponse graphQLResponse;
17+
18+
@Data
19+
@AllArgsConstructor
20+
protected static class Foo {
21+
private String bar;
22+
}
1523
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLResponseTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ public void testGetAsJavaType() throws IOException {
146146

147147
}
148148

149+
@DisplayName("Should throw illegal argument exception if type is incompatible")
150+
@Test
151+
public void testGetAsJavaTypeConversionError() throws IOException {
152+
// GIVEN
153+
final String dataPath = "$.data.externalList[*].fooList";
154+
final String response = loadClassPathResource("response-with-nested-list.json");
155+
final JavaType stringType = objectMapper.getTypeFactory().constructType(String.class);
156+
// WHEN
157+
final GraphQLResponse actual = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
158+
// THEN
159+
assertThatExceptionOfType(IllegalArgumentException.class)
160+
.isThrownBy(() -> actual.get(dataPath, stringType));
161+
162+
}
163+
149164
@ParameterizedTest
150165
@ValueSource(strings = {
151166
"{\"data\": { \"foo\":\"bar\" } }",

0 commit comments

Comments
 (0)