From a6a0e43c01d9efd3fac7c7ee0f7373f0cd66d4e4 Mon Sep 17 00:00:00 2001 From: Andrew Olenik Date: Fri, 30 Mar 2018 19:26:22 +0300 Subject: [PATCH 1/2] Two tests added. 1) Does e2e test, i.e. goes to https://api.icndb.com 2) Does the same but acts against mocked ICNDB client --- .../tpl/bff/connectors/icndb/ICNDBJoke.java | 3 + .../connectors/icndb/ICNDBJokeEnvelope.java | 2 + .../bff/graphql/GraphQLIntegrationTest.java | 61 ++++++++++++ .../tpl/bff/graphql/GraphQLQueryTest.java | 94 +++++++++++++++++++ .../bff/graphql/GraphQLTestController.java | 23 +++++ api/src/test/resources/application.yml | 7 +- 6 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java create mode 100644 api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java create mode 100644 api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLTestController.java diff --git a/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJoke.java b/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJoke.java index ca73f62..6957b9a 100644 --- a/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJoke.java +++ b/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJoke.java @@ -1,9 +1,12 @@ package lv.ctco.tpl.bff.connectors.icndb; import lombok.Getter; +import lombok.Setter; + import java.util.List; @Getter +@Setter public class ICNDBJoke { String id; diff --git a/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJokeEnvelope.java b/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJokeEnvelope.java index 6d19996..e90df9f 100644 --- a/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJokeEnvelope.java +++ b/api/src/main/java/lv/ctco/tpl/bff/connectors/icndb/ICNDBJokeEnvelope.java @@ -1,8 +1,10 @@ package lv.ctco.tpl.bff.connectors.icndb; import lombok.Getter; +import lombok.Setter; @Getter +@Setter public class ICNDBJokeEnvelope { String type; diff --git a/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java new file mode 100644 index 0000000..2a75230 --- /dev/null +++ b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java @@ -0,0 +1,61 @@ +package lv.ctco.tpl.bff.graphql; + +import lombok.SneakyThrows; +import lombok.val; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +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.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@RunWith(SpringRunner.class) +@AutoConfigureMockMvc +public class GraphQLIntegrationTest { + + private static final String GRAPHQL_PATH = "/graphql"; + + @Autowired + private MockMvc mockMvc; + + @Test + @SneakyThrows + public void testGetJokeById() { + val joke = + "{" + + " jokes {" + + " jokeById(id: \"77\") {" + + " id," + + " text" + + " }" + + " }" + + "}"; + + val result = graphQLPost(joke); + + result.andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").doesNotExist()) + .andExpect(jsonPath("$.data.jokes.jokeById.id").value("77")) + .andExpect(jsonPath("$.data.jokes.jokeById.text").value("Chuck Norris can divide by zero.")); + } + + @SneakyThrows + private ResultActions graphQLPost(String query) { + return mockMvc.perform(post(GRAPHQL_PATH).content(request(query))); + } + + @SneakyThrows + private String request(String query) { + val jsonQuery = new JSONObject(); + jsonQuery.put("query", query); + return jsonQuery.toString(); + } +} diff --git a/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java new file mode 100644 index 0000000..de8df7a --- /dev/null +++ b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java @@ -0,0 +1,94 @@ +package lv.ctco.tpl.bff.graphql; + +import com.netflix.hystrix.HystrixCommand; +import com.netflix.hystrix.HystrixCommandGroupKey; +import lombok.SneakyThrows; +import lombok.val; +import lv.ctco.tpl.bff.configuration.AppProperties; +import lv.ctco.tpl.bff.connectors.icndb.ICNDB; +import lv.ctco.tpl.bff.connectors.icndb.ICNDBJoke; +import lv.ctco.tpl.bff.connectors.icndb.ICNDBJokeEnvelope; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +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.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; + +import static org.mockito.Mockito.doReturn; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@RunWith(SpringRunner.class) +@AutoConfigureMockMvc +public class GraphQLQueryTest { + + private static final String GRAPHQL_PATH = "/graphql"; + + @Autowired + private AppProperties appProperties; + + @Autowired + private MockMvc mockMvc; + + @Mock + private ICNDB icndb; + + @Test + @SneakyThrows + public void testGetJokeById() { + + val joke = + "{" + + " jokes {" + + " jokeById(id: \"77\") {" + + " id," + + " text" + + " }" + + " }" + + "}"; + + doReturn(mockResponse()) + .when(icndb).getJokeById("77"); + + val result = graphQLPost(joke); + + result.andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").doesNotExist()) + .andExpect(jsonPath("$.data.jokes.jokeById.id").value("77")) + .andExpect(jsonPath("$.data.jokes.jokeById.text").value("Chuck Norris can divide by zero.")); + } + + @SneakyThrows + private ResultActions graphQLPost(String query) { + return mockMvc.perform(post(GRAPHQL_PATH).content(request(query))); + } + + @SneakyThrows + private String request(String query) { + val jsonQuery = new JSONObject(); + jsonQuery.put("query", query); + return jsonQuery.toString(); + } + + private HystrixCommand mockResponse() { + return new HystrixCommand(HystrixCommandGroupKey.Factory.asKey(appProperties.getIcndbUrl())) { + @Override + protected ICNDBJokeEnvelope run() throws Exception { + ICNDBJokeEnvelope icndbJokeEnvelope = new ICNDBJokeEnvelope(); + icndbJokeEnvelope.setType("success"); + ICNDBJoke value = new ICNDBJoke(); + value.setId("77"); + value.setJoke("Chuck Norris can divide by zero."); + icndbJokeEnvelope.setValue(value); + return icndbJokeEnvelope; + } + }; + } +} diff --git a/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLTestController.java b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLTestController.java new file mode 100644 index 0000000..7af3a08 --- /dev/null +++ b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLTestController.java @@ -0,0 +1,23 @@ +package lv.ctco.tpl.bff.graphql; + +import graphql.servlet.GraphQLServlet; +import lombok.SneakyThrows; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Controller +public class GraphQLTestController { + + @Autowired + private GraphQLServlet graphQLServlet; + + @RequestMapping(value = "${graphql.servlet.mapping:/graphql}") + @SneakyThrows + public void service(HttpServletRequest request, HttpServletResponse response) { + graphQLServlet.service(request, response); + } +} diff --git a/api/src/test/resources/application.yml b/api/src/test/resources/application.yml index 0aee696..641c899 100644 --- a/api/src/test/resources/application.yml +++ b/api/src/test/resources/application.yml @@ -1,2 +1,7 @@ app: - icndb-url: https://example.com + icndb-url: https://api.icndb.com + +logging: + level: + feign: DEBUG + graphql: DEBUG \ No newline at end of file From 966e221005859f7a732e1442281c59b4111befb0 Mon Sep 17 00:00:00 2001 From: Andrew Olenik Date: Fri, 30 Mar 2018 19:51:50 +0300 Subject: [PATCH 2/2] style check fixes --- .../bff/graphql/GraphQLIntegrationTest.java | 30 +++++++-------- .../tpl/bff/graphql/GraphQLQueryTest.java | 38 ++++++++----------- 2 files changed, 29 insertions(+), 39 deletions(-) diff --git a/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java index 2a75230..78950fe 100644 --- a/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java +++ b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLIntegrationTest.java @@ -23,39 +23,35 @@ public class GraphQLIntegrationTest { private static final String GRAPHQL_PATH = "/graphql"; + private static final String QUERY = "{jokes {jokeById(id: \"77\"){id,text}}}"; + + private static final String JOKE_ID = "77"; + + private static final String JOKE_TEXT = "Chuck Norris can divide by zero."; + @Autowired private MockMvc mockMvc; @Test @SneakyThrows public void testGetJokeById() { - val joke = - "{" + - " jokes {" + - " jokeById(id: \"77\") {" + - " id," + - " text" + - " }" + - " }" + - "}"; - - val result = graphQLPost(joke); + val result = graphQLPost(); result.andExpect(status().isOk()) .andExpect(jsonPath("$.errors").doesNotExist()) - .andExpect(jsonPath("$.data.jokes.jokeById.id").value("77")) - .andExpect(jsonPath("$.data.jokes.jokeById.text").value("Chuck Norris can divide by zero.")); + .andExpect(jsonPath("$.data.jokes.jokeById.id").value(JOKE_ID)) + .andExpect(jsonPath("$.data.jokes.jokeById.text").value(JOKE_TEXT)); } @SneakyThrows - private ResultActions graphQLPost(String query) { - return mockMvc.perform(post(GRAPHQL_PATH).content(request(query))); + private ResultActions graphQLPost() { + return mockMvc.perform(post(GRAPHQL_PATH).content(request())); } @SneakyThrows - private String request(String query) { + private String request() { val jsonQuery = new JSONObject(); - jsonQuery.put("query", query); + jsonQuery.put("query", QUERY); return jsonQuery.toString(); } } diff --git a/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java index de8df7a..28d553c 100644 --- a/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java +++ b/api/src/test/java/lv/ctco/tpl/bff/graphql/GraphQLQueryTest.java @@ -31,6 +31,12 @@ public class GraphQLQueryTest { private static final String GRAPHQL_PATH = "/graphql"; + private static final String QUERY = "{jokes {jokeById(id: \"77\"){id,text}}}"; + + private static final String JOKE_ID = "77"; + + private static final String JOKE_TEXT = "Chuck Norris can divide by zero."; + @Autowired private AppProperties appProperties; @@ -43,37 +49,25 @@ public class GraphQLQueryTest { @Test @SneakyThrows public void testGetJokeById() { + doReturn(mockResponse()).when(icndb).getJokeById(JOKE_ID); - val joke = - "{" + - " jokes {" + - " jokeById(id: \"77\") {" + - " id," + - " text" + - " }" + - " }" + - "}"; - - doReturn(mockResponse()) - .when(icndb).getJokeById("77"); - - val result = graphQLPost(joke); + val result = graphQLPost(); result.andExpect(status().isOk()) .andExpect(jsonPath("$.errors").doesNotExist()) - .andExpect(jsonPath("$.data.jokes.jokeById.id").value("77")) - .andExpect(jsonPath("$.data.jokes.jokeById.text").value("Chuck Norris can divide by zero.")); + .andExpect(jsonPath("$.data.jokes.jokeById.id").value(JOKE_ID)) + .andExpect(jsonPath("$.data.jokes.jokeById.text").value(JOKE_TEXT)); } @SneakyThrows - private ResultActions graphQLPost(String query) { - return mockMvc.perform(post(GRAPHQL_PATH).content(request(query))); + private ResultActions graphQLPost() { + return mockMvc.perform(post(GRAPHQL_PATH).content(request())); } @SneakyThrows - private String request(String query) { + private String request() { val jsonQuery = new JSONObject(); - jsonQuery.put("query", query); + jsonQuery.put("query", QUERY); return jsonQuery.toString(); } @@ -84,8 +78,8 @@ protected ICNDBJokeEnvelope run() throws Exception { ICNDBJokeEnvelope icndbJokeEnvelope = new ICNDBJokeEnvelope(); icndbJokeEnvelope.setType("success"); ICNDBJoke value = new ICNDBJoke(); - value.setId("77"); - value.setJoke("Chuck Norris can divide by zero."); + value.setId(JOKE_ID); + value.setJoke(JOKE_TEXT); icndbJokeEnvelope.setValue(value); return icndbJokeEnvelope; }