diff --git a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebFluxApplicationContextTests.java b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebFluxApplicationContextTests.java index 1ba6d106..4d2aa0e6 100644 --- a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebFluxApplicationContextTests.java +++ b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebFluxApplicationContextTests.java @@ -20,7 +20,6 @@ import java.util.Collections; import java.util.function.Consumer; import graphql.schema.idl.TypeRuntimeWiring; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -37,6 +36,10 @@ import org.springframework.graphql.web.WebInterceptor; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; +import static org.hamcrest.Matchers.containsString; + +// @formatter:off + class WebFluxApplicationContextTests { private static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations.of( @@ -49,57 +52,97 @@ class WebFluxApplicationContextTests { @Test void query() { testWithWebClient((client) -> { - String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount" - + " author" + " }" + "}"; + String query = "{" + + " bookById(id: \\\"book-1\\\"){ " + + " id" + + " name" + + " pageCount" + + " author" + + " }" + + "}"; - client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}").exchange().expectStatus().isOk() - .expectBody().jsonPath("data.bookById.name").isEqualTo("GraphQL for beginners"); + client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}") + .exchange() + .expectStatus() + .isOk() + .expectBody() + .jsonPath("data.bookById.name") + .isEqualTo("GraphQL for beginners"); }); } @Test void queryMissing() { - testWithWebClient((client) -> client.post().uri("").bodyValue("{}").exchange().expectStatus().isBadRequest()); + testWithWebClient((client) -> + client.post().uri("").bodyValue("{}") + .exchange() + .expectStatus() + .isBadRequest()); } @Test void queryIsInvalidJson() { - testWithWebClient((client) -> client.post().uri("").bodyValue(":)").exchange().expectStatus().isBadRequest()); + testWithWebClient((client) -> + client.post().uri("").bodyValue(":)") + .exchange() + .expectStatus() + .isBadRequest()); } @Test void interceptedQuery() { testWithWebClient((client) -> { - String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount" - + " author" + " }" + "}"; + String query = "{" + + " bookById(id: \\\"book-1\\\"){ " + + " id" + + " name" + + " pageCount" + + " author" + + " }" + + "}"; - client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}").exchange().expectStatus().isOk() - .expectHeader().valueEquals("X-Custom-Header", "42"); + client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}") + .exchange() + .expectStatus() + .isOk() + .expectHeader() + .valueEquals("X-Custom-Header", "42"); }); } @Test void schemaEndpoint() { - testWithWebClient((client) -> client.get().uri("/schema").accept(MediaType.ALL).exchange().expectStatus().isOk() - .expectHeader().contentType(MediaType.TEXT_PLAIN).expectBody(String.class) - .value(Matchers.containsString("type Book"))); + testWithWebClient((client) -> + client.get().uri("/schema").accept(MediaType.ALL) + .exchange() + .expectStatus() + .isOk() + .expectHeader() + .contentType(MediaType.TEXT_PLAIN) + .expectBody(String.class) + .value(containsString("type Book"))); } private void testWithWebClient(Consumer consumer) { testWithApplicationContext((context) -> { - WebTestClient client = WebTestClient.bindToApplicationContext(context).configureClient() + WebTestClient client = WebTestClient.bindToApplicationContext(context) + .configureClient() .defaultHeaders((headers) -> { headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - }).baseUrl(BASE_URL).build(); + }) + .baseUrl(BASE_URL) + .build(); consumer.accept(client); }); } private void testWithApplicationContext(ContextConsumer consumer) { - new ReactiveWebApplicationContextRunner().withConfiguration(AUTO_CONFIGURATIONS) + new ReactiveWebApplicationContextRunner() + .withConfiguration(AUTO_CONFIGURATIONS) .withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class) - .withPropertyValues("spring.main.web-application-type=reactive", + .withPropertyValues( + "spring.main.web-application-type=reactive", "spring.graphql.schema.printer.enabled=true", "spring.graphql.schema.location=classpath:books/schema.graphqls") .run(consumer); @@ -110,8 +153,9 @@ class WebFluxApplicationContextTests { @Bean public RuntimeWiringCustomizer bookDataFetcher() { - return (runtimeWiring) -> runtimeWiring.type(TypeRuntimeWiring.newTypeWiring("Query") - .dataFetcher("bookById", GraphQlDataFetchers.getBookByIdDataFetcher())); + return (runtimeWiring) -> + runtimeWiring.type(TypeRuntimeWiring.newTypeWiring("Query") + .dataFetcher("bookById", GraphQlDataFetchers.getBookByIdDataFetcher())); } } @@ -121,8 +165,8 @@ class WebFluxApplicationContextTests { @Bean public WebInterceptor customWebInterceptor() { - return (input, next) -> next.handle(input) - .map((output) -> output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42"))); + return (input, next) -> next.handle(input).map((output) -> + output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42"))); } } diff --git a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebMvcApplicationContextTests.java b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebMvcApplicationContextTests.java index aae02ff9..b01f7860 100644 --- a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebMvcApplicationContextTests.java +++ b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebMvcApplicationContextTests.java @@ -32,7 +32,6 @@ import org.springframework.graphql.web.WebInterceptor; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch; @@ -43,6 +42,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +// @formatter:off + class WebMvcApplicationContextTests { public static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations.of( @@ -54,11 +55,17 @@ class WebMvcApplicationContextTests { @Test void endpointHandlesGraphQlQuery() { testWith((mockMvc) -> { - String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount" - + " author" + " }" + "}"; - MvcResult asyncResult = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}")) - .andReturn(); - mockMvc.perform(asyncDispatch(asyncResult)).andExpect(status().isOk()) + String query = "{" + + " bookById(id: \\\"book-1\\\"){ " + + " id" + + " name" + + " pageCount" + + " author" + + " }" + + "}"; + MvcResult result = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}")).andReturn(); + mockMvc.perform(asyncDispatch(result)) + .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("data.bookById.name").value("GraphQL for beginners")); }); @@ -77,32 +84,42 @@ class WebMvcApplicationContextTests { @Test void interceptedQuery() { testWith((mockMvc) -> { - String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount" - + " author" + " }" + "}"; - MvcResult asyncResult = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}")) - .andReturn(); - mockMvc.perform(asyncDispatch(asyncResult)).andExpect(status().isOk()) + String query = "{" + + " bookById(id: \\\"book-1\\\"){ " + + " id" + + " name" + + " pageCount" + + " author" + + " }" + + "}"; + MvcResult result = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}")).andReturn(); + mockMvc.perform(asyncDispatch(result)) + .andExpect(status().isOk()) .andExpect(header().string("X-Custom-Header", "42")); }); } @Test void schemaEndpoint() { - testWith((mockMvc) -> mockMvc.perform(get("/graphql/schema")).andExpect(status().isOk()) + testWith((mockMvc) -> mockMvc.perform(get("/graphql/schema")) + .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.TEXT_PLAIN)) .andExpect(content().string(Matchers.containsString("type Book")))); } private void testWith(MockMvcConsumer mockMvcConsumer) { - new WebApplicationContextRunner().withConfiguration(AUTO_CONFIGURATIONS) + new WebApplicationContextRunner() + .withConfiguration(AUTO_CONFIGURATIONS) .withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class) - .withPropertyValues("spring.main.web-application-type=servlet", + .withPropertyValues( + "spring.main.web-application-type=servlet", "spring.graphql.schema.printer.enabled=true", "spring.graphql.schema.location=classpath:books/schema.graphqls") .run((context) -> { - MockHttpServletRequestBuilder builder = post("/graphql").contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON); - MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).defaultRequest(builder).build(); + MediaType mediaType = MediaType.APPLICATION_JSON; + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context) + .defaultRequest(post("/graphql").contentType(mediaType).accept(mediaType)) + .build(); mockMvcConsumer.accept(mockMvc); }); } @@ -118,8 +135,8 @@ class WebMvcApplicationContextTests { @Bean public RuntimeWiringCustomizer bookDataFetcher() { - return (builder) -> builder.type(TypeRuntimeWiring.newTypeWiring("Query").dataFetcher("bookById", - GraphQlDataFetchers.getBookByIdDataFetcher())); + return (builder) -> builder.type(TypeRuntimeWiring.newTypeWiring("Query") + .dataFetcher("bookById", GraphQlDataFetchers.getBookByIdDataFetcher())); } } @@ -129,8 +146,8 @@ class WebMvcApplicationContextTests { @Bean public WebInterceptor customWebInterceptor() { - return (input, next) -> next.handle(input) - .map((output) -> output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42"))); + return (input, next) -> next.handle(input).map((output) -> + output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42"))); } } diff --git a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/actuate/metrics/GraphQlTagsTests.java b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/actuate/metrics/GraphQlTagsTests.java index e7ae182c..2f371c39 100644 --- a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/actuate/metrics/GraphQlTagsTests.java +++ b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/actuate/metrics/GraphQlTagsTests.java @@ -29,6 +29,8 @@ import org.springframework.graphql.test.tester.TestExecutionResult; import static org.assertj.core.api.Assertions.assertThat; +// @formatter:off + /** * Tests for {@link GraphQlTags} * @@ -44,30 +46,32 @@ class GraphQlTagsTests { @Test void executionOutcomeShouldErrorWhenExceptionThrown() { - Tag outcomeTag = GraphQlTags.executionOutcome(new TestExecutionResult(), - new IllegalArgumentException("test error")); - assertThat(outcomeTag.getValue()).isEqualTo("ERROR"); + Tag tag = GraphQlTags.executionOutcome(new TestExecutionResult(), new IllegalArgumentException("test error")); + assertThat(tag.getValue()).isEqualTo("ERROR"); } @Test void executionOutcomeShouldErrorWhenResponseErrors() { - ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder(); - builder.addError(GraphqlErrorBuilder.newError().message("Invalid query").build()); - Tag outcomeTag = GraphQlTags.executionOutcome(builder.build(), null); - assertThat(outcomeTag.getValue()).isEqualTo("ERROR"); + GraphQLError error = GraphqlErrorBuilder.newError().message("Invalid query").build(); + Tag tag = GraphQlTags.executionOutcome(ExecutionResultImpl.newExecutionResult().addError(error).build(), null); + assertThat(tag.getValue()).isEqualTo("ERROR"); } @Test void errorTypeShouldBeDefinedIfPresent() { - GraphQLError error = GraphqlErrorBuilder.newError().errorType(ErrorType.DataFetchingException) - .message("test error").build(); + GraphQLError error = GraphqlErrorBuilder.newError() + .errorType(ErrorType.DataFetchingException) + .message("test error") + .build(); Tag errorTypeTag = GraphQlTags.errorType(error); assertThat(errorTypeTag.getValue()).isEqualTo("DataFetchingException"); } @Test void errorPathShouldUseJsonPathFormat() { - GraphQLError error = GraphqlErrorBuilder.newError().path(Arrays.asList("project", "name")).message("test error") + GraphQLError error = GraphqlErrorBuilder.newError() + .path(Arrays.asList("project", "name")) + .message("test error") .build(); Tag errorPathTag = GraphQlTags.errorPath(error); assertThat(errorPathTag.getValue()).isEqualTo("$.project.name"); @@ -75,8 +79,10 @@ class GraphQlTagsTests { @Test void errorPathShouldUseJsonPathFormatForIndices() { - GraphQLError error = GraphqlErrorBuilder.newError().path(Arrays.asList("issues", "42", "title")) - .message("test error").build(); + GraphQLError error = GraphqlErrorBuilder.newError() + .path(Arrays.asList("issues", "42", "title")) + .message("test error") + .build(); Tag errorPathTag = GraphQlTags.errorPath(error); assertThat(errorPathTag.getValue()).isEqualTo("$.issues[*].title"); } diff --git a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/test/tester/GraphQlTesterAutoConfigurationTests.java b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/test/tester/GraphQlTesterAutoConfigurationTests.java index a4868548..2a39d143 100644 --- a/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/test/tester/GraphQlTesterAutoConfigurationTests.java +++ b/graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/test/tester/GraphQlTesterAutoConfigurationTests.java @@ -82,9 +82,11 @@ class GraphQlTesterAutoConfigurationTests { @Bean WebTestClient webTestClient() { - RouterFunction routerFunction = RouterFunctions.route() - .POST("/graphql", (request) -> ServerResponse.ok().build()).build(); - return WebTestClient.bindToRouterFunction(routerFunction).build(); + // @formatter:off + RouterFunction routes = + RouterFunctions.route().POST("/graphql", (request) -> ServerResponse.ok().build()).build(); + return WebTestClient.bindToRouterFunction(routes).build(); + // @formatter:on } @Bean