BAEL-5307: Move code from new module to existing (#11868)
This commit is contained in:
parent
ce22b5b69c
commit
c87c79e035
@ -12,8 +12,24 @@
|
|||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>jitpack.io</id>
|
||||||
|
<url>https://jitpack.io</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
<id>central</id>
|
||||||
|
<name>Central Repository</name>
|
||||||
|
<url>https://repo.maven.apache.org/maven2</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.graphql-java</groupId>
|
<groupId>com.graphql-java</groupId>
|
||||||
@ -25,11 +41,114 @@
|
|||||||
<artifactId>ratpack-core</artifactId>
|
<artifactId>ratpack-core</artifactId>
|
||||||
<version>${ratpack-core.version}</version>
|
<version>${ratpack-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.americanexpress.nodes</groupId>
|
||||||
|
<artifactId>nodes</artifactId>
|
||||||
|
<version>0.5.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.graphql-java</groupId>
|
||||||
|
<artifactId>graphql-java</artifactId>
|
||||||
|
<version>9.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.graphql-java</groupId>
|
||||||
|
<artifactId>graphql-java-tools</artifactId>
|
||||||
|
<version>5.2.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.graphql-java</groupId>
|
||||||
|
<artifactId>graphql-java-servlet</artifactId>
|
||||||
|
<version>6.1.3</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>3.0.1</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.12.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mock-server</groupId>
|
||||||
|
<artifactId>mockserver-netty</artifactId>
|
||||||
|
<version>5.11.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mock-server</groupId>
|
||||||
|
<artifactId>mockserver-client-java</artifactId>
|
||||||
|
<version>5.11.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.graphql-java-generator</groupId>
|
||||||
|
<artifactId>graphql-java-runtime</artifactId>
|
||||||
|
<version>${graphql.java.generator.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>5.8.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.22.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source}</source>
|
||||||
|
<target>${maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-maven-plugin</artifactId>
|
||||||
|
<version>10.0.7</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.graphql-java-generator</groupId>
|
||||||
|
<artifactId>graphql-maven-plugin</artifactId>
|
||||||
|
<version>${graphql.java.generator.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>generateClientCode</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<packageName>com.baeldung.graphql.generated</packageName>
|
||||||
|
<copyRuntimeSources>false</copyRuntimeSources>
|
||||||
|
<generateDeprecatedRequestResponse>false</generateDeprecatedRequestResponse>
|
||||||
|
<separateUtilityClasses>true</separateUtilityClasses>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<graphql-java-annotations.version>3.0.3</graphql-java-annotations.version>
|
<graphql-java-annotations.version>3.0.3</graphql-java-annotations.version>
|
||||||
<ratpack-core.version>1.4.6</ratpack-core.version>
|
<ratpack-core.version>1.4.6</ratpack-core.version>
|
||||||
|
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<graphql.java.generator.version>1.18</graphql.java.generator.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.graphql.clients;
|
||||||
|
|
||||||
|
import com.baeldung.graphql.data.Data;
|
||||||
|
import io.aexp.nodes.graphql.GraphQLRequestEntity;
|
||||||
|
import io.aexp.nodes.graphql.GraphQLResponseEntity;
|
||||||
|
import io.aexp.nodes.graphql.GraphQLTemplate;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class AmericanExpressNodes {
|
||||||
|
|
||||||
|
public static GraphQLResponseEntity<Data> callGraphQLService(String url, String query) throws IOException {
|
||||||
|
GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
|
||||||
|
|
||||||
|
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
|
||||||
|
.url(StringUtils.join(url, "?query=", query))
|
||||||
|
.request(Data.class)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return graphQLTemplate.query(requestEntity, Data.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.graphql.clients;
|
||||||
|
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
public class ApacheHttpClient {
|
||||||
|
|
||||||
|
public static HttpResponse callGraphQLService(String url, String query) throws URISyntaxException, IOException {
|
||||||
|
HttpClient client = HttpClientBuilder.create().build();
|
||||||
|
HttpGet request = new HttpGet(url);
|
||||||
|
URI uri = new URIBuilder(request.getURI())
|
||||||
|
.addParameter("query", query)
|
||||||
|
.build();
|
||||||
|
request.setURI(uri);
|
||||||
|
return client.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.graphql.data;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
public class Author {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String surname;
|
||||||
|
|
||||||
|
public Author() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Author(String name, String surname) {
|
||||||
|
this.name = name;
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFullName() {
|
||||||
|
return StringUtils.join(getName(), " ", getSurname());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.graphql.data;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
private Author author;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String title, Author author) {
|
||||||
|
this.title = title;
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Author getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.graphql.data;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class BookRepository {
|
||||||
|
|
||||||
|
private static final List<Book> books = Stream.of(
|
||||||
|
new Book("Title 1", new Author("Pero", "Peric")),
|
||||||
|
new Book("Title 2", new Author("Marko", "Maric"))
|
||||||
|
).collect(Collectors.toList());
|
||||||
|
|
||||||
|
public List<Book> getAllBooks() {
|
||||||
|
return Collections.unmodifiableList(books);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.graphql.data;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Data {
|
||||||
|
|
||||||
|
private List<Book> allBooks;
|
||||||
|
|
||||||
|
public Data() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Data(List<Book> allBooks) {
|
||||||
|
this.allBooks = allBooks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Book> getAllBooks() {
|
||||||
|
return Collections.unmodifiableList(allBooks);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.graphql.data;
|
||||||
|
|
||||||
|
public class Response {
|
||||||
|
|
||||||
|
private Data data;
|
||||||
|
|
||||||
|
public Response() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Response(Data data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Data getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.graphql.server;
|
||||||
|
|
||||||
|
import com.baeldung.graphql.data.BookRepository;
|
||||||
|
import com.coxautodev.graphql.tools.SchemaParser;
|
||||||
|
import graphql.schema.GraphQLSchema;
|
||||||
|
import graphql.servlet.SimpleGraphQLHttpServlet;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@WebServlet(urlPatterns = "/graphql")
|
||||||
|
public class GraphQLEndpoint extends HttpServlet {
|
||||||
|
|
||||||
|
private SimpleGraphQLHttpServlet graphQLServlet;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
graphQLServlet.service(req, resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
GraphQLSchema schema = SchemaParser.newParser()
|
||||||
|
.resolvers(new GraphQLQuery(new BookRepository()))
|
||||||
|
.file("schema.graphqls")
|
||||||
|
.build()
|
||||||
|
.makeExecutableSchema();
|
||||||
|
graphQLServlet = SimpleGraphQLHttpServlet
|
||||||
|
.newBuilder(schema)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.graphql.server;
|
||||||
|
|
||||||
|
import com.baeldung.graphql.data.Book;
|
||||||
|
import com.baeldung.graphql.data.BookRepository;
|
||||||
|
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GraphQLQuery implements GraphQLQueryResolver {
|
||||||
|
|
||||||
|
private final BookRepository repository;
|
||||||
|
|
||||||
|
public GraphQLQuery(BookRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Book> allBooks() {
|
||||||
|
return repository.getAllBooks();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
graphql/graphql-java/src/main/resources/schema.graphqls
Normal file
17
graphql/graphql-java/src/main/resources/schema.graphqls
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
type Book {
|
||||||
|
title: String!
|
||||||
|
author: Author
|
||||||
|
}
|
||||||
|
|
||||||
|
type Author {
|
||||||
|
name: String!
|
||||||
|
surname: String!
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query {
|
||||||
|
allBooks: [Book]
|
||||||
|
}
|
||||||
|
|
||||||
|
schema {
|
||||||
|
query: Query
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
package com.baeldung.graphql;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.mockserver.client.MockServerClient;
|
||||||
|
import org.mockserver.integration.ClientAndServer;
|
||||||
|
import org.mockserver.model.HttpStatusCode;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
|
||||||
|
import static org.mockserver.matchers.Times.exactly;
|
||||||
|
import static org.mockserver.model.HttpRequest.request;
|
||||||
|
import static org.mockserver.model.HttpResponse.response;
|
||||||
|
|
||||||
|
public class GraphQLMockServer {
|
||||||
|
|
||||||
|
public static ClientAndServer mockServer;
|
||||||
|
public static String serviceUrl;
|
||||||
|
|
||||||
|
private static int serverPort;
|
||||||
|
|
||||||
|
public static final String SERVER_ADDRESS = "127.0.0.1";
|
||||||
|
public static final String HTTP_GET_POST = "GET";
|
||||||
|
public static final String PATH = "/graphql";
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void startServer() throws IOException, URISyntaxException {
|
||||||
|
serverPort = getFreePort();
|
||||||
|
serviceUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH;
|
||||||
|
mockServer = startClientAndServer(serverPort);
|
||||||
|
mockAllBooksTitleRequest();
|
||||||
|
mockAllBooksTitleAuthorRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void stopServer() {
|
||||||
|
mockServer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void mockAllBooksTitleAuthorRequest() {
|
||||||
|
String requestQuery = "{allBooks{title,author{name,surname}}}";
|
||||||
|
String responseJson = "{\"data\":{\"allBooks\":[{\"title\":\"Title 1\",\"author\":{\"name\":\"Pero\",\"surname\":\"Peric\"}},{\"title\":\"Title 2\",\"author\":{\"name\":\"Marko\",\"surname\":\"Maric\"}}]}}";
|
||||||
|
|
||||||
|
new MockServerClient(SERVER_ADDRESS, serverPort)
|
||||||
|
.when(
|
||||||
|
request()
|
||||||
|
.withPath(PATH)
|
||||||
|
.withQueryStringParameter("query", requestQuery),
|
||||||
|
exactly(1)
|
||||||
|
)
|
||||||
|
.respond(
|
||||||
|
response()
|
||||||
|
.withStatusCode(HttpStatusCode.OK_200.code())
|
||||||
|
.withBody(responseJson)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void mockAllBooksTitleRequest() {
|
||||||
|
String requestQuery = "{allBooks{title}}";
|
||||||
|
String responseJson = "{\"data\":{\"allBooks\":[{\"title\":\"Title 1\"},{\"title\":\"Title 2\"}]}}";
|
||||||
|
|
||||||
|
new MockServerClient(SERVER_ADDRESS, serverPort)
|
||||||
|
.when(
|
||||||
|
request()
|
||||||
|
.withPath(PATH)
|
||||||
|
.withQueryStringParameter("query", requestQuery),
|
||||||
|
exactly(1)
|
||||||
|
)
|
||||||
|
.respond(
|
||||||
|
response()
|
||||||
|
.withStatusCode(HttpStatusCode.OK_200.code())
|
||||||
|
.withBody(responseJson)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getFreePort () throws IOException {
|
||||||
|
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
||||||
|
return serverSocket.getLocalPort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.graphql.clients;
|
||||||
|
|
||||||
|
import com.baeldung.graphql.GraphQLMockServer;
|
||||||
|
import com.baeldung.graphql.data.Data;
|
||||||
|
import io.aexp.nodes.graphql.GraphQLResponseEntity;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||||
|
|
||||||
|
class AmericanExpressNodesUnitTest extends GraphQLMockServer {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGraphQLEndpoint_whenRequestingAllBooksWithTitle_thenExpectedJsonIsReturned() throws IOException {
|
||||||
|
GraphQLResponseEntity<Data> responseEntity = AmericanExpressNodes.callGraphQLService(serviceUrl, "{allBooks{title}}");
|
||||||
|
|
||||||
|
assertAll(
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks()).hasSize(2),
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks().get(0).getTitle()).isEqualTo("Title 1"),
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks().get(1).getTitle()).isEqualTo("Title 2")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGraphQLEndpoint_whenRequestingAllBooksWithTitleAndAuthor_thenExpectedJsonIsReturned() throws IOException {
|
||||||
|
GraphQLResponseEntity<Data> responseEntity = AmericanExpressNodes.callGraphQLService(serviceUrl, "{allBooks{title,author{name,surname}}}");
|
||||||
|
|
||||||
|
assertAll(
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks()).hasSize(2),
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks().get(0).getTitle()).isEqualTo("Title 1"),
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks().get(0).getAuthor().getFullName()).isEqualTo("Pero Peric"),
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks().get(1).getTitle()).isEqualTo("Title 2"),
|
||||||
|
() -> assertThat(responseEntity.getResponse().getAllBooks().get(1).getAuthor().getFullName()).isEqualTo("Marko Maric")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.graphql.clients;
|
||||||
|
|
||||||
|
import com.baeldung.graphql.GraphQLMockServer;
|
||||||
|
import com.baeldung.graphql.data.Response;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||||
|
|
||||||
|
class ApacheHttpClientUnitTest extends GraphQLMockServer {
|
||||||
|
|
||||||
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGraphQLEndpoint_whenRequestingAllBooksWithTitle_thenExpectedJsonIsReturned() throws IOException, URISyntaxException {
|
||||||
|
HttpResponse httpResponse = ApacheHttpClient.callGraphQLService(serviceUrl, "{allBooks{title}}");
|
||||||
|
String actualResponse = IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
|
||||||
|
Response parsedResponse = objectMapper.readValue(actualResponse, Response.class);
|
||||||
|
|
||||||
|
assertAll(
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks()).hasSize(2),
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks().get(0).getTitle()).isEqualTo("Title 1"),
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks().get(1).getTitle()).isEqualTo("Title 2")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGraphQLEndpoint_whenRequestingAllBooksWithTitleAndAuthor_thenExpectedJsonIsReturned() throws IOException, URISyntaxException {
|
||||||
|
HttpResponse httpResponse = ApacheHttpClient.callGraphQLService(serviceUrl, "{allBooks{title,author{name,surname}}}");
|
||||||
|
String actualResponse = IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
|
||||||
|
Response parsedResponse = objectMapper.readValue(actualResponse, Response.class);
|
||||||
|
|
||||||
|
assertAll(
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks()).hasSize(2),
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks().get(0).getTitle()).isEqualTo("Title 1"),
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks().get(0).getAuthor().getFullName()).isEqualTo("Pero Peric"),
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks().get(1).getTitle()).isEqualTo("Title 2"),
|
||||||
|
() -> assertThat(parsedResponse.getData().getAllBooks().get(1).getAuthor().getFullName()).isEqualTo("Marko Maric")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user