|
| 1 | +/* |
| 2 | + * Copyright 2020-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.graphql.client; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import mockwebserver3.MockResponse; |
| 24 | +import mockwebserver3.MockWebServer; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.Arguments; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | + |
| 31 | +import org.springframework.graphql.Book; |
| 32 | +import org.springframework.graphql.MediaTypes; |
| 33 | +import org.springframework.http.MediaType; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.junit.jupiter.api.Named.named; |
| 37 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration tests for {@link HttpGraphQlClient}. |
| 41 | + */ |
| 42 | +class HttpGraphQlClientTests { |
| 43 | + |
| 44 | + private MockWebServer server; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setup() throws IOException { |
| 48 | + this.server = new MockWebServer(); |
| 49 | + this.server.start(); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + void tearDown() throws IOException { |
| 54 | + this.server.close(); |
| 55 | + } |
| 56 | + |
| 57 | + static Stream<Arguments> graphQlClientTypes() { |
| 58 | + return Stream.of( |
| 59 | + arguments(named("HttpGraphQlClient", ClientType.ASYNC)), |
| 60 | + arguments(named("HttpSyncGraphQlClient", ClientType.SYNC)) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @ParameterizedTest |
| 65 | + @MethodSource("graphQlClientTypes") |
| 66 | + void toEntityListSync(ClientType clientType) { |
| 67 | + prepareOkResponse(""" |
| 68 | + { |
| 69 | + "data": { |
| 70 | + "favoriteBooks": [ |
| 71 | + {"id":"42","name":"Hitchhiker's Guide to the Galaxy"}, |
| 72 | + {"id":"53","name":"Breaking Bad"} |
| 73 | + ] |
| 74 | + } |
| 75 | + } |
| 76 | + """); |
| 77 | + List<Book> favoriteBooks = createClient(clientType).document("{ favoriteBooks() { id name } }") |
| 78 | + .retrieveSync("favoriteBooks") |
| 79 | + .toEntityList(Book.class); |
| 80 | + |
| 81 | + assertThat(favoriteBooks).hasSize(2); |
| 82 | + assertThat(favoriteBooks.get(0)).isInstanceOf(Book.class); |
| 83 | + assertThat(favoriteBooks).extracting("id").contains(42L, 53L); |
| 84 | + } |
| 85 | + |
| 86 | + private GraphQlClient createClient(ClientType clientType) { |
| 87 | + return switch (clientType) { |
| 88 | + case ASYNC -> HttpGraphQlClient.builder().url(server.url("/").toString()).build(); |
| 89 | + case SYNC -> HttpSyncGraphQlClient.builder().url(server.url("/").toString()).build(); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + void prepareOkResponse(String body) { |
| 94 | + prepareResponse(200, MediaTypes.APPLICATION_GRAPHQL_RESPONSE, body); |
| 95 | + } |
| 96 | + |
| 97 | + private void prepareResponse(int status, MediaType contentType, String body) { |
| 98 | + MockResponse mockResponse = new MockResponse.Builder().code(status) |
| 99 | + .setHeader("Content-Type", contentType.toString()) |
| 100 | + .body(body) |
| 101 | + .build(); |
| 102 | + this.server.enqueue(mockResponse); |
| 103 | + } |
| 104 | + |
| 105 | + enum ClientType { |
| 106 | + ASYNC, SYNC |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments