Feature/bael 5756 choose api (#13055)

* BAEL-5756: Rest controller

* BAEL-5756: GraphQL controller (without tests)

* BAEL-5756: Fix GraphQL test

* BAEL-5756: Fix GraphQL test 2

* BAEL-5756: GRPC working

* BAEL-5756: GRPC with Spring Boot

* BAEL-5756: Books proto service

* BAEL-5756: Fix grpc integration test

* BAEL-5756: Refactor

* BAEL-5756: Revert some changes
This commit is contained in:
Daniel Strmecki
2022-12-03 16:00:24 +01:00
committed by GitHub
parent 5f939afaf0
commit 0f3c81c248
18 changed files with 525 additions and 1 deletions
@@ -0,0 +1,33 @@
package com.baeldung.chooseapi.controllers;
import com.baeldung.chooseapi.ChooseApiApp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.graphql.test.tester.HttpGraphQlTester;
import org.springframework.test.context.ActiveProfiles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ChooseApiApp.class)
@ActiveProfiles("chooseapi")
class BooksControllerGraphQLIntegrationTest {
@Autowired
private HttpGraphQlTester graphQlTester;
@Test
void givenBooksServiceThatReturnThreeBooks_whenCallingGraphQLEndpoint_thenThreeBooksAreReturned() throws Exception {
String document = "query { books { title year author { firstName lastName }}}";
Path expectedResponse = Paths.get("src/test/resources/graphql-test/books_expected_response.json");
String expectedJson = new String(Files.readAllBytes(expectedResponse));
this.graphQlTester.document(document)
.execute()
.path("books")
.matchesJson(expectedJson);
}
}
@@ -0,0 +1,36 @@
package com.baeldung.chooseapi.controllers;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@SpringBootTest
@AutoConfigureMockMvc
class BooksControllerRestIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
void givenBooksServiceThatReturnThreeBooks_whenCallingRestEndpoint_thenThreeBooksAreReturned() throws Exception {
Path expectedResponse = Paths.get("src/test/resources/graphql-test/books_expected_response.json");
String expectedJson = new String(Files.readAllBytes(expectedResponse));
this.mockMvc.perform(get("/rest/books"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().json(expectedJson));
}
}
@@ -0,0 +1,55 @@
package com.baeldung.chooseapi.grpc;
import com.baeldung.chooseapi.dtos.Book;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.baeldung.chooseapi.BooksServiceOuterClass.BooksRequest;
import com.baeldung.chooseapi.BooksServiceOuterClass.BooksResponse;
import com.baeldung.chooseapi.BooksServiceGrpc.BooksServiceBlockingStub;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
@SpringBootTest(properties = {
"grpc.server.inProcessName=test", // Enable inProcess server
"grpc.server.port=-1", // Disable external server
"grpc.client.inProcess.address=in-process:test" // Configure the client to connect to the inProcess server
})
@SpringJUnitConfig(GrpcIntegrationTestConfig.class)
@DirtiesContext // Ensures that the grpc-server is properly shutdown after each test
class BooksServiceGrpcIntegrationTest {
private static final ObjectMapper objectMapper = new ObjectMapper();
@GrpcClient("inProcess")
private BooksServiceBlockingStub booksServiceGrpc;
@Test
@DirtiesContext
void givenBooksServiceThatReturnThreeBooks_whenCallingGrpcEndpoint_thenThreeBooksAreReturned() throws IOException, JSONException {
Path expectedResponse = Paths.get("src/test/resources/graphql-test/books_expected_response.json");
String expectedJson = new String(Files.readAllBytes(expectedResponse));
BooksRequest request = BooksRequest.newBuilder().build();
BooksResponse response = booksServiceGrpc.books(request);
List<Book> books = response.getBookList().stream()
.map(GrpcBooksMapper::mapProtoToBook)
.collect(Collectors.toList());
JSONAssert.assertEquals(objectMapper.writeValueAsString(books), expectedJson, true);
}
}
@@ -0,0 +1,28 @@
package com.baeldung.chooseapi.grpc;
import com.baeldung.chooseapi.services.BooksService;
import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration;
import net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration;
import net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ImportAutoConfiguration({
GrpcServerAutoConfiguration.class, // Create required server beans
GrpcServerFactoryAutoConfiguration.class, // Select server implementation
GrpcClientAutoConfiguration.class}) // Support @GrpcClient annotation
class GrpcIntegrationTestConfig {
@Bean
BooksService booksService() {
return new BooksService();
}
@Bean
BooksServiceGrpc booksServiceGrpc() {
return new BooksServiceGrpc(booksService());
}
}
@@ -0,0 +1,26 @@
[
{
"title": "Philosopher's Stone",
"year": 1997,
"author": {
"firstName": "Joanne",
"lastName": "Rowling"
}
},
{
"title": "Goblet of Fire",
"year": 2000,
"author": {
"firstName": "Joanne",
"lastName": "Rowling"
}
},
{
"title": "Deathly Hallows",
"year": 2007,
"author": {
"firstName": "Joanne",
"lastName": "Rowling"
}
}
]