3232import java .util .List ;
3333import java .util .stream .Stream ;
3434
35- import static org .assertj .core .api .Assertions .assertThat ;
36- import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
35+ import static org .assertj .core .api .Assertions .*;
3736
3837@ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
3938public class GraphQLResponseTest {
4039
41- private static final String DATA_PATH = "$.data.test" ;
40+ private static final String DATA_PATH_TEST = "$.data.test" ;
41+ private static final String INNER_FOO_LIST_DATA_PATH = "$.data.externalList[*].fooList" ;
42+ private static final String NESTED_LIST_RESPONSE_FILE = "response-with-nested-list.json" ;
43+ private static final String EMPTY_OBJECT_RESPONSE = "{}" ;
4244
4345 @ Autowired
4446 private ObjectMapper objectMapper ;
@@ -80,84 +82,78 @@ private static Stream<Arguments> testGetListArguments() {
8082 );
8183 }
8284
85+ private static String loadClassPathResource (final String path ) {
86+ try (InputStream resourceStream = new ClassPathResource (path ).getInputStream ()) {
87+ return StreamUtils .copyToString (resourceStream , StandardCharsets .UTF_8 );
88+ } catch (IOException e ) {
89+ fail ("Test setup error - failed to load test resource." , e );
90+ return "" ;
91+ }
92+ }
93+
8394 @ DisplayName ("Should get the JSON node's value as a String." )
8495 @ ParameterizedTest
8596 @ MethodSource ("testGetStringArguments" )
86- public void testGetString (
87- final String bodyString ,
88- final String expected
89- ) {
97+ public void testGetString (final String bodyString , final String expected ) {
9098 //GIVEN
91- final GraphQLResponse graphQLResponse = new GraphQLResponse ( ResponseEntity . ok ( bodyString ), objectMapper );
99+ final GraphQLResponse graphQLResponse = createResponse ( bodyString );
92100 //WHEN
93- final String actual = graphQLResponse .get (DATA_PATH );
101+ final String actual = graphQLResponse .get (DATA_PATH_TEST );
94102 //THEN
95103 assertThat (actual ).isEqualTo (expected );
96104 }
97105
98106 @ DisplayName ("Should get the JSON node's value as an instance of a specified class." )
99107 @ ParameterizedTest
100108 @ MethodSource ("testGetArguments" )
101- public <T > void testGet (
102- final String bodyString ,
103- final Class <T > clazz ,
104- final T expected
105- ) {
109+ public <T > void testGet (final String bodyString , final Class <T > clazz , final T expected ) {
106110 //GIVEN
107- final GraphQLResponse graphQLResponse = new GraphQLResponse ( ResponseEntity . ok ( bodyString ), objectMapper );
111+ final GraphQLResponse graphQLResponse = createResponse ( bodyString );
108112 //WHEN
109- final T actual = graphQLResponse .get (DATA_PATH , clazz );
113+ final T actual = graphQLResponse .get (DATA_PATH_TEST , clazz );
110114 //THEN
111115 assertThat (actual ).isInstanceOf (clazz ).isEqualTo (expected );
112116 }
113117
114118 @ DisplayName ("Should get the JSON node's value as a List." )
115119 @ ParameterizedTest
116120 @ MethodSource ("testGetListArguments" )
117- public <T > void testGetList (
118- final String bodyString ,
119- final Class <T > clazz ,
120- final List <T > expected
121- ) {
121+ public <T > void testGetList (final String bodyString , final Class <T > clazz , final List <T > expected ) {
122122 //GIVEN
123- final GraphQLResponse graphQLResponse = new GraphQLResponse ( ResponseEntity . ok ( bodyString ), objectMapper );
123+ final GraphQLResponse graphQLResponse = createResponse ( bodyString );
124124 //WHEN
125- final List <T > actual = graphQLResponse .getList (DATA_PATH , clazz );
125+ final List <T > actual = graphQLResponse .getList (DATA_PATH_TEST , clazz );
126126 //THEN
127127 assertThat (actual ).containsExactlyElementsOf (expected );
128128 }
129129
130130 @ DisplayName ("Should get field as defined by Jackson's JavaType." )
131131 @ Test
132- public void testGetAsJavaType () throws IOException {
132+ public void testGetAsJavaType () {
133133 // GIVEN
134- final String dataPath = "$.data.externalList[*].fooList" ;
135- final String response = loadClassPathResource ("response-with-nested-list.json" );
136134 final List <List <String >> expected = Arrays .asList (Arrays .asList ("foo1" , "foo2" ),
137135 Collections .singletonList ("foo3" ));
138136 final JavaType stringList = objectMapper .getTypeFactory ().constructCollectionType (List .class , String .class );
139137 final JavaType listOfStringLists = objectMapper .getTypeFactory ().constructCollectionType (List .class ,
140138 stringList );
141139 // WHEN
142- final List <List <String >> actual = new GraphQLResponse ( ResponseEntity . ok ( response ), objectMapper )
143- .get (dataPath , listOfStringLists );
140+ final List <List <String >> actual = createResponse ( loadClassPathResource ( NESTED_LIST_RESPONSE_FILE ) )
141+ .get (INNER_FOO_LIST_DATA_PATH , listOfStringLists );
144142 // THEN
145143 assertThat (actual ).containsExactlyElementsOf (expected );
146144
147145 }
148146
149147 @ DisplayName ("Should throw illegal argument exception if type is incompatible" )
150148 @ Test
151- public void testGetAsJavaTypeConversionError () throws IOException {
149+ public void testGetAsJavaTypeConversionError () {
152150 // GIVEN
153- final String dataPath = "$.data.externalList[*].fooList" ;
154- final String response = loadClassPathResource ("response-with-nested-list.json" );
155151 final JavaType stringType = objectMapper .getTypeFactory ().constructType (String .class );
156152 // WHEN
157- final GraphQLResponse actual = new GraphQLResponse ( ResponseEntity . ok ( response ), objectMapper );
153+ final GraphQLResponse actual = createResponse ( loadClassPathResource ( NESTED_LIST_RESPONSE_FILE ) );
158154 // THEN
159155 assertThatExceptionOfType (IllegalArgumentException .class )
160- .isThrownBy (() -> actual .get (dataPath , stringType ));
156+ .isThrownBy (() -> actual .get (INNER_FOO_LIST_DATA_PATH , stringType ));
161157
162158 }
163159
@@ -170,7 +166,7 @@ public void testGetAsJavaTypeConversionError() throws IOException {
170166 @ DisplayName ("Should pass the assertion if no errors are present." )
171167 void testExpectNoErrorsPass (final String response ) {
172168 // WHEN
173- final GraphQLResponse graphQLResponse = new GraphQLResponse ( ResponseEntity . ok ( response ), objectMapper );
169+ final GraphQLResponse graphQLResponse = createResponse ( response );
174170 // THEN
175171 assertThat (graphQLResponse .assertThatNoErrorsArePresent ())
176172 .as ("Should not throw an exception. Should return GraphQL response instance." )
@@ -180,112 +176,116 @@ void testExpectNoErrorsPass(final String response) {
180176 @ Test
181177 @ DisplayName ("Should throw assertion error if an error is present in the response." )
182178 void testExpectNoErrorsFail () {
183- // WHEN
179+ // GIVEN
184180 final String response = "{\" errors\" : [{\" message\" : \" Test error.\" }], \" data\" : { \" foo\" :\" bar\" } }" ;
185- final GraphQLResponse graphQLResponse = new GraphQLResponse (ResponseEntity .ok (response ), objectMapper );
181+ // WHEN
182+ final GraphQLResponse graphQLResponse = createResponse (response );
186183 // THEN
187184 assertThatExceptionOfType (AssertionError .class )
188185 .isThrownBy (graphQLResponse ::assertThatNoErrorsArePresent )
189186 .withMessage ("Expected no GraphQL errors, but got 1: <Unspecified error>: Test error." );
190187 }
191188
192189 @ Test
193- @ DisplayName ("Should return assertion for the number of errors." )
190+ @ DisplayName ("Should return an assertion for the number of errors." )
194191 void testNumberOfErrorsAssertion () {
195- // WHEN
192+ // GIVEN
196193 final String response = "{\" errors\" : [{\" message\" : \" Test error.\" }, {\" message\" : \" Test error 2.\" }], "
197194 + "\" data\" : { \" foo\" :\" bar\" } }" ;
198- final GraphQLResponse graphQLResponse = new GraphQLResponse ( ResponseEntity . ok ( response ), objectMapper );
199- // THEN
195+ final GraphQLResponse graphQLResponse = createResponse ( response );
196+ // WHEN
200197 final NumberOfErrorsAssertion actual = graphQLResponse .assertThatNumberOfErrors ();
198+ // THEN
201199 assertThat (actual ).extracting ("actual" ).isEqualTo (2 );
202200 assertThat (actual .and ()).isSameAs (graphQLResponse );
203201 }
204202
205203 @ Test
206- @ DisplayName ("Should return assertion for the list of errors." )
204+ @ DisplayName ("Should return an assertion for the list of errors." )
207205 void testErrorListAssertion () {
208206 // GIVEN
209207 final String response = "{\" errors\" : [{\" message\" : \" Test error.\" , \" errorType\" : \" DataFetchingException\" }], "
210208 + "\" data\" : { \" foo\" :\" bar\" } }" ;
209+ final GraphQLResponse graphQLResponse = createResponse (response );
211210 // WHEN
212- final GraphQLResponse graphQLResponse = new GraphQLResponse (ResponseEntity .ok (response ), objectMapper );
213211 final GraphQLErrorListAssertion actual = graphQLResponse .assertThatListOfErrors ();
214212 // THEN
215213 assertThat (actual ).isNotNull ();
216214 assertThat (actual ).extracting ("actual" ).isEqualTo (Collections .singletonList (GraphQLTestError .builder ()
217- .message ("Test error." ).errorType (ErrorType .DataFetchingException ).build ()));
215+ .message ("Test error." ).errorType (ErrorType .DataFetchingException ).build ()));
218216 assertThat (actual .and ()).isSameAs (graphQLResponse );
219217 }
220218
221219 @ Test
222- @ DisplayName ("Should return a assertion for the json content of the response." )
220+ @ DisplayName ("Should return an assertion for the json content of the response." )
223221 void testFieldAssertion () {
224222 // GIVEN
225223 final String response = "{ \" data\" : { \" foo\" :\" bar\" } }" ;
224+ final GraphQLResponse graphQLResponse = createResponse (response );
226225 // WHEN
227- final GraphQLResponse graphQLResponse = new GraphQLResponse (ResponseEntity .ok (response ), objectMapper );
228226 final JsonContentAssert actual = graphQLResponse .assertThatJsonContent ();
229227 // THEN
230228 assertThat (actual ).isNotNull ();
231229 assertThat (actual ).extracting ("actual" ).isEqualTo (response );
232230 }
233231
234232 @ Test
235- @ DisplayName ("Should return a assertion for the data field." )
233+ @ DisplayName ("Should return an assertion for the data field." )
236234 void testDataFieldAssertion () {
235+ // GIVEN
236+ final GraphQLResponse response = createResponse (EMPTY_OBJECT_RESPONSE );
237237 // WHEN
238- final GraphQLResponse response = new GraphQLResponse (ResponseEntity .ok ("{}" ), objectMapper );
239238 final GraphQLFieldAssert actual = response .assertThatDataField ();
240239 // THEN
241- assertThat (actual ).isNotNull ();
242- assertThat (actual ).extracting ("graphQLResponse" , "jsonPath" )
243- .containsExactly (response , "$.data" );
240+ assertThatAssertionIsCorrect (actual , response , "$.data" );
244241 }
245242
246243 @ Test
247- @ DisplayName ("Should return a assertion for the errors field." )
244+ @ DisplayName ("Should return an assertion for the errors field." )
248245 void testErrorFieldAssertion () {
246+ // GIVEN
247+ final GraphQLResponse response = createResponse (EMPTY_OBJECT_RESPONSE );
249248 // WHEN
250- final GraphQLResponse response = new GraphQLResponse (ResponseEntity .ok ("{}" ), objectMapper );
251249 final GraphQLFieldAssert actual = response .assertThatErrorsField ();
252250 // THEN
253- assertThat (actual ).isNotNull ();
254- assertThat (actual ).extracting ("graphQLResponse" , "jsonPath" )
255- .containsExactly (response , "$.errors" );
251+ assertThatAssertionIsCorrect (actual , response , "$.errors" );
256252 }
257253
258254 @ Test
259- @ DisplayName ("Should return a assertion for the extensions field." )
255+ @ DisplayName ("Should return an assertion for the extensions field." )
260256 void testErrorExtensionsAssertion () {
257+ // GIVEN
258+ final GraphQLResponse response = createResponse (EMPTY_OBJECT_RESPONSE );
261259 // WHEN
262- final GraphQLResponse response = new GraphQLResponse (ResponseEntity .ok ("{}" ), objectMapper );
263260 final GraphQLFieldAssert actual = response .assertThatExtensionsField ();
264261 // THEN
265- assertThat (actual ).isNotNull ();
266- assertThat (actual ).extracting ("graphQLResponse" , "jsonPath" )
267- .containsExactly (response , "$.extensions" );
262+ assertThatAssertionIsCorrect (actual , response , "$.extensions" );
268263 }
269264
270265 @ Test
271266 @ DisplayName ("Should return a field assertion for the specific field." )
272267 void testJsonAssertion () {
273268 // GIVEN
274- final String response = "{}" ;
275269 final String path = "$any.path" ;
270+ final GraphQLResponse graphQLResponse = createResponse (EMPTY_OBJECT_RESPONSE );
276271 // WHEN
277- final GraphQLResponse graphQLResponse = new GraphQLResponse (ResponseEntity .ok (response ), objectMapper );
278272 final GraphQLFieldAssert actual = graphQLResponse .assertThatField (path );
279273 // THEN
280- assertThat (actual ).isNotNull ();
281- assertThat (actual ).extracting ("graphQLResponse" , "jsonPath" )
282- .containsExactly (graphQLResponse , path );
274+ assertThatAssertionIsCorrect (actual , graphQLResponse , path );
283275 }
284276
285- private String loadClassPathResource (String path ) throws IOException {
286- try (InputStream resourceStream = new ClassPathResource (path ).getInputStream ()) {
287- return StreamUtils .copyToString (resourceStream , StandardCharsets .UTF_8 );
288- }
277+ private GraphQLResponse createResponse (final String s ) {
278+ return new GraphQLResponse (ResponseEntity .ok (s ), objectMapper );
279+ }
280+
281+ private void assertThatAssertionIsCorrect (
282+ final GraphQLFieldAssert actual ,
283+ final GraphQLResponse expectedResponse ,
284+ final Object expectedPath
285+ ) {
286+ assertThat (actual ).isNotNull ();
287+ assertThat (actual ).extracting ("graphQLResponse" , "jsonPath" )
288+ .containsExactly (expectedResponse , expectedPath );
289289 }
290290
291291 @ Data
0 commit comments