Merge branch 'eugenp:master' into master

This commit is contained in:
Ulisses Lima
2022-03-05 22:23:01 -03:00
committed by GitHub
62 changed files with 1273 additions and 135 deletions
@@ -0,0 +1,67 @@
package com.baeldung.poi.excel.lastrow;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
public class LastRowUnitTest {
private static final String FILE_NAME = "lastRowTest.xlsx";
private String fileLocation;
@Before
public void setup() throws URISyntaxException {
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
}
@Test
public void givenExampleGrid_whenGetRow_thenReturnRowObjectIfModified() throws IOException {
Workbook workbook = new XSSFWorkbook(fileLocation);
Sheet sheet = workbook.getSheetAt(0);
assertEquals(7, sheet.getLastRowNum());
assertEquals(6, sheet.getPhysicalNumberOfRows());
assertNotNull(sheet.getRow(0));
assertNotNull(sheet.getRow(1));
assertNotNull(sheet.getRow(2));
assertNotNull(sheet.getRow(3));
assertNull(sheet.getRow(4));
assertNull(sheet.getRow(5));
assertNotNull(sheet.getRow(6));
assertNotNull(sheet.getRow(7));
assertNull(sheet.getRow(8));
assertSame(sheet.getRow(7), getLastRowFromSheet(sheet));
}
@Test
public void givenEmptySheet_whenGetRow_thenReturnNull() throws IOException {
Workbook workbook = new XSSFWorkbook(fileLocation);
Sheet sheet = workbook.getSheetAt(1);
assertEquals(-1, sheet.getLastRowNum());
assertEquals(0, sheet.getPhysicalNumberOfRows());
assertNull(sheet.getRow(0));
assertSame(sheet.getRow(0), getLastRowFromSheet(sheet));
}
public static Row getLastRowFromSheet(Sheet sheet) {
Row lastRow = null;
int lastRowNum = sheet.getLastRowNum();
if (lastRowNum >= 0) {
lastRow = sheet.getRow(lastRowNum);
}
return lastRow;
}
}
Binary file not shown.
@@ -6,3 +6,5 @@ This module contains articles about Map data structures in Java.
- [Using a Custom Class as a Key in a Java HashMap](https://www.baeldung.com/java-custom-class-map-key)
- [Nested HashMaps Examples in Java](https://www.baeldung.com/java-nested-hashmaps)
- [Java HashMap With Different Value Types](https://www.baeldung.com/java-hashmap-different-value-types)
- [Difference Between Map and HashMap in Java](https://www.baeldung.com/java-map-vs-hashmap)
- [How to Create a New Entry in a Map](https://www.baeldung.com/java-map-new-entry)
@@ -4,3 +4,4 @@
- [Bad Practices With Synchronization](https://www.baeldung.com/java-synchronization-bad-practices)
- [Start Two Threads at the Exact Same Time in Java](https://www.baeldung.com/java-start-two-threads-at-same-time)
- [Volatile Variables and Thread Safety](https://www.baeldung.com/java-volatile-variables-thread-safety)
- [Producer-Consumer Problem With Example in Java](https://www.baeldung.com/java-producer-consumer-problem)
@@ -1,3 +1,4 @@
### Relevant Articles:
- [Java ArrayIndexOutOfBoundsException](https://www.baeldung.com/java-arrayindexoutofboundsexception)
- [Java Missing Return Statement](https://www.baeldung.com/java-missing-return-statement)
@@ -1,45 +0,0 @@
package com.baeldung.exception.variablemightnothavebeeninitialized;
public class VariableMightNotHaveBeenInitialized {
private static int instanceVariableCount;
/**
* Method would not compile if lines 14 and 18 are uncommented.
*/
public static void countEven() {
//uninstantiated
int count;
int[] arr = new int[]{23, 56, 89, 12, 23};
for (int i = 0; i < arr.length; i++) {
if ((arr[i] % 2) == 0) {
// count++;
}
}
// System.out.println("Total Even Numbers : " + count);
}
public static int countEvenUsingInstanceVariable(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if ((arr[i] % 2) == 0) {
instanceVariableCount++;
}
}
return instanceVariableCount;
}
public static int countEvenUsingIfElse(int[] arr, int args) {
int count;
count = args > 0 ? args : 0;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] % 2) == 0) {
count++;
}
}
return count;
}
}
@@ -1,24 +0,0 @@
package com.baeldung.exception.variablemightnothavebeeninitialized;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class VariableMightNotHaveBeenInitializedUnitTest {
@Test
public void usingInstanceVariable_returnCount() {
int[] arr = new int[]{1, 2, 3, 4, 5, 6};
int value = VariableMightNotHaveBeenInitialized.countEvenUsingInstanceVariable(arr);
assertEquals(3, value);
}
@Test
public void usingArgumentsAndIfElse_returnCount() {
int[] arr = new int[]{1, 2, 3, 4, 5, 6};
int value = VariableMightNotHaveBeenInitialized.countEvenUsingIfElse(arr, 2);
assertEquals(5, value);
}
}
@@ -0,0 +1,83 @@
package com.baeldung.filetomap;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileToHashMap {
enum DupKeyOption {
OVERWRITE, DISCARD
}
public static Map<String, String> byBufferedReader(String filePath, DupKeyOption dupKeyOption) {
HashMap<String, String> map = new HashMap<>();
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
while ((line = reader.readLine()) != null) {
String[] keyValuePair = line.split(":", 2);
if (keyValuePair.length > 1) {
String key = keyValuePair[0];
String value = keyValuePair[1];
if (DupKeyOption.OVERWRITE == dupKeyOption) {
map.put(key, value);
} else if (DupKeyOption.DISCARD == dupKeyOption) {
map.putIfAbsent(key, value);
}
} else {
System.out.println("No Key:Value found in line, ignoring: " + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
public static Map<String, String> byStream(String filePath, DupKeyOption dupKeyOption) {
Map<String, String> map = new HashMap<>();
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
lines.filter(line -> line.contains(":"))
.forEach(line -> {
String[] keyValuePair = line.split(":", 2);
String key = keyValuePair[0];
String value = keyValuePair[1];
if (DupKeyOption.OVERWRITE == dupKeyOption) {
map.put(key, value);
} else if (DupKeyOption.DISCARD == dupKeyOption) {
map.putIfAbsent(key, value);
}
});
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
public static Map<String, List<String>> aggregateByKeys(String filePath) {
Map<String, List<String>> map = new HashMap<>();
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
lines.filter(line -> line.contains(":"))
.forEach(line -> {
String[] keyValuePair = line.split(":", 2);
String key = keyValuePair[0];
String value = keyValuePair[1];
if (map.containsKey(key)) {
map.get(key).add(value);
} else {
map.put(key, Stream.of(value).collect(Collectors.toList()));
}
});
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
@@ -0,0 +1,66 @@
package com.baeldung.filetomap;
import org.junit.Before;
import org.junit.Test;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
public class FileToHashMapUnitTest {
private String filePath;
private static final Map<String, String> EXPECTED_MAP_DISCARD = Stream.of(new String[][]{
{"title", "The Lord of the Rings: The Return of the King"},
{"director", "Peter Jackson"},
{"actor", "Sean Astin"}
}).collect(Collectors.toMap(data -> data[0], data -> data[1]));
private static final Map<String, String> EXPECTED_MAP_OVERWRITE = Stream.of(new String[][]{
{"title", "The Lord of the Rings: The Return of the King"},
{"director", "Peter Jackson"},
{"actor", "Ian McKellen"}
}).collect(Collectors.toMap(data -> data[0], data -> data[1]));
private static final Map<String, List<String>> EXPECTED_MAP_AGGREGATE = Stream.of(new String[][]{
{"title", "The Lord of the Rings: The Return of the King"},
{"director", "Peter Jackson"},
{"actor", "Sean Astin", "Ian McKellen"}
}).collect(Collectors.toMap(arr -> arr[0], arr -> Arrays.asList(Arrays.copyOfRange(arr, 1, arr.length))));
@Before
public void setPath() throws URISyntaxException {
if (filePath == null) {
filePath = Paths.get(ClassLoader.getSystemResource("filetomap/theLordOfRings.txt").toURI()).toString();
}
}
@Test
public void givenInputFile_whenInvokeByBufferedReaderPriorToJava8_shouldGetExpectedMap() {
Map<String, String> mapOverwrite = FileToHashMap.byBufferedReader(filePath, FileToHashMap.DupKeyOption.OVERWRITE);
Map<String, String> mapDiscard = FileToHashMap.byBufferedReader(filePath, FileToHashMap.DupKeyOption.DISCARD);
assertThat(mapOverwrite).isEqualTo(EXPECTED_MAP_OVERWRITE);
assertThat(mapDiscard).isEqualTo(EXPECTED_MAP_DISCARD);
}
@Test
public void givenInputFile_whenInvokeByStream_shouldGetExpectedMap() {
Map<String, String> mapOverwrite = FileToHashMap.byStream(filePath, FileToHashMap.DupKeyOption.OVERWRITE);
Map<String, String> mapDiscard = FileToHashMap.byStream(filePath, FileToHashMap.DupKeyOption.DISCARD);
assertThat(mapOverwrite).isEqualTo(EXPECTED_MAP_OVERWRITE);
assertThat(mapDiscard).isEqualTo(EXPECTED_MAP_DISCARD);
}
@Test
public void givenInputFile_whenInvokeAggregateByKeys_shouldGetExpectedMap() {
Map<String, List<String>> mapAgg = FileToHashMap.aggregateByKeys(filePath);
assertThat(mapAgg).isEqualTo(EXPECTED_MAP_AGGREGATE);
}
}
@@ -0,0 +1,6 @@
title:The Lord of the Rings: The Return of the King
director:Peter Jackson
actor:Sean Astin
actor:Ian McKellen
Gandalf and Aragorn lead the World of Men against Sauron's
army to draw his gaze from Frodo and Sam as they approach Mount Doom with the One Ring.
+1
View File
@@ -7,3 +7,4 @@
- [Dockerfile Strategies for Git](https://www.baeldung.com/ops/dockerfile-git-strategies)
- [How to Get Docker-Compose to Always Use the Latest Image](https://www.baeldung.com/ops/docker-compose-latest-image)
- [How to Include Files Outside of Docker’s Build Context](https://www.baeldung.com/ops/docker-include-files-outside-build-context)
- [Caching Maven Dependencies with Docker](https://www.baeldung.com/ops/docker-cache-maven-dependencies)
+119
View File
@@ -12,8 +12,24 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</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>
<dependency>
<groupId>com.graphql-java</groupId>
@@ -25,11 +41,114 @@
<artifactId>ratpack-core</artifactId>
<version>${ratpack-core.version}</version>
</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>
<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>
<graphql-java-annotations.version>3.0.3</graphql-java-annotations.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>
</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();
}
}
@@ -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")
);
}
}
+1
View File
@@ -3,3 +3,4 @@
This module contains articles about Servlets.
### Relevant Articles:
- [Check if a User Is Logged-in With Servlets and JSP](https://www.baeldung.com/servlets-jsp-check-user-login)
@@ -0,0 +1,56 @@
package com.baeldung.lombok.tostring;
import lombok.ToString;
@ToString
public class Account {
private String name;
// render this field before any others (the highest ranked)
@ToString.Include(rank = 1)
private String id;
@ToString.Exclude
private String accountNumber;
// automatically excluded
private String $ignored;
@ToString.Include
String description() {
return "Account description";
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String get$ignored() {
return $ignored;
}
public void set$ignored(String value) {
this.$ignored = value;
}
}
@@ -0,0 +1,9 @@
package com.baeldung.lombok.tostring;
import lombok.ToString;
@ToString
public enum AccountType {
CHECKING,
SAVING
}
@@ -0,0 +1,27 @@
package com.baeldung.lombok.tostring;
import lombok.ToString;
@ToString
public class RewardAccount extends Account {
private String rewardAccountId;
private Object[] relatedAccounts;
public String getRewardAccountId() {
return rewardAccountId;
}
public void setRewardAccountId(String rewardAccountId) {
this.rewardAccountId = rewardAccountId;
}
public Object[] getRelatedAccounts() {
return relatedAccounts;
}
public void setRelatedAccounts(Object[] relatedAccounts) {
this.relatedAccounts = relatedAccounts;
}
}

Some files were not shown because too many files have changed in this diff Show More