Skip to content

Commit 0ce48dc

Browse files
committed
Turn off auto-formatting in Boot starter tests
1 parent 16209d8 commit 0ce48dc

File tree

4 files changed

+129
-60
lines changed

4 files changed

+129
-60
lines changed

graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebFluxApplicationContextTests.java

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.function.Consumer;
2121

2222
import graphql.schema.idl.TypeRuntimeWiring;
23-
import org.hamcrest.Matchers;
2423
import org.junit.jupiter.api.Test;
2524

2625
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -37,6 +36,10 @@
3736
import org.springframework.http.MediaType;
3837
import org.springframework.test.web.reactive.server.WebTestClient;
3938

39+
import static org.hamcrest.Matchers.containsString;
40+
41+
// @formatter:off
42+
4043
class WebFluxApplicationContextTests {
4144

4245
private static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations.of(
@@ -49,57 +52,97 @@ class WebFluxApplicationContextTests {
4952
@Test
5053
void query() {
5154
testWithWebClient((client) -> {
52-
String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount"
53-
+ " author" + " }" + "}";
54-
55-
client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}").exchange().expectStatus().isOk()
56-
.expectBody().jsonPath("data.bookById.name").isEqualTo("GraphQL for beginners");
55+
String query = "{" +
56+
" bookById(id: \\\"book-1\\\"){ " +
57+
" id" +
58+
" name" +
59+
" pageCount" +
60+
" author" +
61+
" }" +
62+
"}";
63+
64+
client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}")
65+
.exchange()
66+
.expectStatus()
67+
.isOk()
68+
.expectBody()
69+
.jsonPath("data.bookById.name")
70+
.isEqualTo("GraphQL for beginners");
5771
});
5872
}
5973

6074
@Test
6175
void queryMissing() {
62-
testWithWebClient((client) -> client.post().uri("").bodyValue("{}").exchange().expectStatus().isBadRequest());
76+
testWithWebClient((client) ->
77+
client.post().uri("").bodyValue("{}")
78+
.exchange()
79+
.expectStatus()
80+
.isBadRequest());
6381
}
6482

6583
@Test
6684
void queryIsInvalidJson() {
67-
testWithWebClient((client) -> client.post().uri("").bodyValue(":)").exchange().expectStatus().isBadRequest());
85+
testWithWebClient((client) ->
86+
client.post().uri("").bodyValue(":)")
87+
.exchange()
88+
.expectStatus()
89+
.isBadRequest());
6890
}
6991

7092
@Test
7193
void interceptedQuery() {
7294
testWithWebClient((client) -> {
73-
String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount"
74-
+ " author" + " }" + "}";
75-
76-
client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}").exchange().expectStatus().isOk()
77-
.expectHeader().valueEquals("X-Custom-Header", "42");
95+
String query = "{" +
96+
" bookById(id: \\\"book-1\\\"){ " +
97+
" id" +
98+
" name" +
99+
" pageCount" +
100+
" author" +
101+
" }" +
102+
"}";
103+
104+
client.post().uri("").bodyValue("{ \"query\": \"" + query + "\"}")
105+
.exchange()
106+
.expectStatus()
107+
.isOk()
108+
.expectHeader()
109+
.valueEquals("X-Custom-Header", "42");
78110
});
79111
}
80112

81113
@Test
82114
void schemaEndpoint() {
83-
testWithWebClient((client) -> client.get().uri("/schema").accept(MediaType.ALL).exchange().expectStatus().isOk()
84-
.expectHeader().contentType(MediaType.TEXT_PLAIN).expectBody(String.class)
85-
.value(Matchers.containsString("type Book")));
115+
testWithWebClient((client) ->
116+
client.get().uri("/schema").accept(MediaType.ALL)
117+
.exchange()
118+
.expectStatus()
119+
.isOk()
120+
.expectHeader()
121+
.contentType(MediaType.TEXT_PLAIN)
122+
.expectBody(String.class)
123+
.value(containsString("type Book")));
86124
}
87125

88126
private void testWithWebClient(Consumer<WebTestClient> consumer) {
89127
testWithApplicationContext((context) -> {
90-
WebTestClient client = WebTestClient.bindToApplicationContext(context).configureClient()
128+
WebTestClient client = WebTestClient.bindToApplicationContext(context)
129+
.configureClient()
91130
.defaultHeaders((headers) -> {
92131
headers.setContentType(MediaType.APPLICATION_JSON);
93132
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
94-
}).baseUrl(BASE_URL).build();
133+
})
134+
.baseUrl(BASE_URL)
135+
.build();
95136
consumer.accept(client);
96137
});
97138
}
98139

99140
private void testWithApplicationContext(ContextConsumer<ApplicationContext> consumer) {
100-
new ReactiveWebApplicationContextRunner().withConfiguration(AUTO_CONFIGURATIONS)
141+
new ReactiveWebApplicationContextRunner()
142+
.withConfiguration(AUTO_CONFIGURATIONS)
101143
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class)
102-
.withPropertyValues("spring.main.web-application-type=reactive",
144+
.withPropertyValues(
145+
"spring.main.web-application-type=reactive",
103146
"spring.graphql.schema.printer.enabled=true",
104147
"spring.graphql.schema.location=classpath:books/schema.graphqls")
105148
.run(consumer);
@@ -110,8 +153,9 @@ public static class DataFetchersConfiguration {
110153

111154
@Bean
112155
public RuntimeWiringCustomizer bookDataFetcher() {
113-
return (runtimeWiring) -> runtimeWiring.type(TypeRuntimeWiring.newTypeWiring("Query")
114-
.dataFetcher("bookById", GraphQlDataFetchers.getBookByIdDataFetcher()));
156+
return (runtimeWiring) ->
157+
runtimeWiring.type(TypeRuntimeWiring.newTypeWiring("Query")
158+
.dataFetcher("bookById", GraphQlDataFetchers.getBookByIdDataFetcher()));
115159
}
116160

117161
}
@@ -121,8 +165,8 @@ public static class CustomWebInterceptor {
121165

122166
@Bean
123167
public WebInterceptor customWebInterceptor() {
124-
return (input, next) -> next.handle(input)
125-
.map((output) -> output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42")));
168+
return (input, next) -> next.handle(input).map((output) ->
169+
output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42")));
126170
}
127171

128172
}

graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/WebMvcApplicationContextTests.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.http.MediaType;
3333
import org.springframework.test.web.servlet.MockMvc;
3434
import org.springframework.test.web.servlet.MvcResult;
35-
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
3635
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
3736

3837
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
@@ -43,6 +42,8 @@
4342
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
4443
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
4544

45+
// @formatter:off
46+
4647
class WebMvcApplicationContextTests {
4748

4849
public static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations.of(
@@ -54,11 +55,17 @@ class WebMvcApplicationContextTests {
5455
@Test
5556
void endpointHandlesGraphQlQuery() {
5657
testWith((mockMvc) -> {
57-
String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount"
58-
+ " author" + " }" + "}";
59-
MvcResult asyncResult = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}"))
60-
.andReturn();
61-
mockMvc.perform(asyncDispatch(asyncResult)).andExpect(status().isOk())
58+
String query = "{" +
59+
" bookById(id: \\\"book-1\\\"){ " +
60+
" id" +
61+
" name" +
62+
" pageCount" +
63+
" author" +
64+
" }" +
65+
"}";
66+
MvcResult result = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}")).andReturn();
67+
mockMvc.perform(asyncDispatch(result))
68+
.andExpect(status().isOk())
6269
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
6370
.andExpect(jsonPath("data.bookById.name").value("GraphQL for beginners"));
6471
});
@@ -77,32 +84,42 @@ void invalidJson() {
7784
@Test
7885
void interceptedQuery() {
7986
testWith((mockMvc) -> {
80-
String query = "{" + " bookById(id: \\\"book-1\\\"){ " + " id" + " name" + " pageCount"
81-
+ " author" + " }" + "}";
82-
MvcResult asyncResult = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}"))
83-
.andReturn();
84-
mockMvc.perform(asyncDispatch(asyncResult)).andExpect(status().isOk())
87+
String query = "{" +
88+
" bookById(id: \\\"book-1\\\"){ " +
89+
" id" +
90+
" name" +
91+
" pageCount" +
92+
" author" +
93+
" }" +
94+
"}";
95+
MvcResult result = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}")).andReturn();
96+
mockMvc.perform(asyncDispatch(result))
97+
.andExpect(status().isOk())
8598
.andExpect(header().string("X-Custom-Header", "42"));
8699
});
87100
}
88101

89102
@Test
90103
void schemaEndpoint() {
91-
testWith((mockMvc) -> mockMvc.perform(get("/graphql/schema")).andExpect(status().isOk())
104+
testWith((mockMvc) -> mockMvc.perform(get("/graphql/schema"))
105+
.andExpect(status().isOk())
92106
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
93107
.andExpect(content().string(Matchers.containsString("type Book"))));
94108
}
95109

96110
private void testWith(MockMvcConsumer mockMvcConsumer) {
97-
new WebApplicationContextRunner().withConfiguration(AUTO_CONFIGURATIONS)
111+
new WebApplicationContextRunner()
112+
.withConfiguration(AUTO_CONFIGURATIONS)
98113
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class)
99-
.withPropertyValues("spring.main.web-application-type=servlet",
114+
.withPropertyValues(
115+
"spring.main.web-application-type=servlet",
100116
"spring.graphql.schema.printer.enabled=true",
101117
"spring.graphql.schema.location=classpath:books/schema.graphqls")
102118
.run((context) -> {
103-
MockHttpServletRequestBuilder builder = post("/graphql").contentType(MediaType.APPLICATION_JSON)
104-
.accept(MediaType.APPLICATION_JSON);
105-
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).defaultRequest(builder).build();
119+
MediaType mediaType = MediaType.APPLICATION_JSON;
120+
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context)
121+
.defaultRequest(post("/graphql").contentType(mediaType).accept(mediaType))
122+
.build();
106123
mockMvcConsumer.accept(mockMvc);
107124
});
108125
}
@@ -118,8 +135,8 @@ public static class DataFetchersConfiguration {
118135

119136
@Bean
120137
public RuntimeWiringCustomizer bookDataFetcher() {
121-
return (builder) -> builder.type(TypeRuntimeWiring.newTypeWiring("Query").dataFetcher("bookById",
122-
GraphQlDataFetchers.getBookByIdDataFetcher()));
138+
return (builder) -> builder.type(TypeRuntimeWiring.newTypeWiring("Query")
139+
.dataFetcher("bookById", GraphQlDataFetchers.getBookByIdDataFetcher()));
123140
}
124141

125142
}
@@ -129,8 +146,8 @@ public static class CustomWebInterceptor {
129146

130147
@Bean
131148
public WebInterceptor customWebInterceptor() {
132-
return (input, next) -> next.handle(input)
133-
.map((output) -> output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42")));
149+
return (input, next) -> next.handle(input).map((output) ->
150+
output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42")));
134151
}
135152

136153
}

graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/actuate/metrics/GraphQlTagsTests.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import static org.assertj.core.api.Assertions.assertThat;
3131

32+
// @formatter:off
33+
3234
/**
3335
* Tests for {@link GraphQlTags}
3436
*
@@ -44,39 +46,43 @@ void executionOutcomeShouldSucceed() {
4446

4547
@Test
4648
void executionOutcomeShouldErrorWhenExceptionThrown() {
47-
Tag outcomeTag = GraphQlTags.executionOutcome(new TestExecutionResult(),
48-
new IllegalArgumentException("test error"));
49-
assertThat(outcomeTag.getValue()).isEqualTo("ERROR");
49+
Tag tag = GraphQlTags.executionOutcome(new TestExecutionResult(), new IllegalArgumentException("test error"));
50+
assertThat(tag.getValue()).isEqualTo("ERROR");
5051
}
5152

5253
@Test
5354
void executionOutcomeShouldErrorWhenResponseErrors() {
54-
ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder();
55-
builder.addError(GraphqlErrorBuilder.newError().message("Invalid query").build());
56-
Tag outcomeTag = GraphQlTags.executionOutcome(builder.build(), null);
57-
assertThat(outcomeTag.getValue()).isEqualTo("ERROR");
55+
GraphQLError error = GraphqlErrorBuilder.newError().message("Invalid query").build();
56+
Tag tag = GraphQlTags.executionOutcome(ExecutionResultImpl.newExecutionResult().addError(error).build(), null);
57+
assertThat(tag.getValue()).isEqualTo("ERROR");
5858
}
5959

6060
@Test
6161
void errorTypeShouldBeDefinedIfPresent() {
62-
GraphQLError error = GraphqlErrorBuilder.newError().errorType(ErrorType.DataFetchingException)
63-
.message("test error").build();
62+
GraphQLError error = GraphqlErrorBuilder.newError()
63+
.errorType(ErrorType.DataFetchingException)
64+
.message("test error")
65+
.build();
6466
Tag errorTypeTag = GraphQlTags.errorType(error);
6567
assertThat(errorTypeTag.getValue()).isEqualTo("DataFetchingException");
6668
}
6769

6870
@Test
6971
void errorPathShouldUseJsonPathFormat() {
70-
GraphQLError error = GraphqlErrorBuilder.newError().path(Arrays.asList("project", "name")).message("test error")
72+
GraphQLError error = GraphqlErrorBuilder.newError()
73+
.path(Arrays.asList("project", "name"))
74+
.message("test error")
7175
.build();
7276
Tag errorPathTag = GraphQlTags.errorPath(error);
7377
assertThat(errorPathTag.getValue()).isEqualTo("$.project.name");
7478
}
7579

7680
@Test
7781
void errorPathShouldUseJsonPathFormatForIndices() {
78-
GraphQLError error = GraphqlErrorBuilder.newError().path(Arrays.asList("issues", "42", "title"))
79-
.message("test error").build();
82+
GraphQLError error = GraphqlErrorBuilder.newError()
83+
.path(Arrays.asList("issues", "42", "title"))
84+
.message("test error")
85+
.build();
8086
Tag errorPathTag = GraphQlTags.errorPath(error);
8187
assertThat(errorPathTag.getValue()).isEqualTo("$.issues[*].title");
8288
}

graphql-spring-boot-starter/src/test/java/org/springframework/graphql/boot/test/tester/GraphQlTesterAutoConfigurationTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ static class WebTestClientConfiguration {
8282

8383
@Bean
8484
WebTestClient webTestClient() {
85-
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route()
86-
.POST("/graphql", (request) -> ServerResponse.ok().build()).build();
87-
return WebTestClient.bindToRouterFunction(routerFunction).build();
85+
// @formatter:off
86+
RouterFunction<ServerResponse> routes =
87+
RouterFunctions.route().POST("/graphql", (request) -> ServerResponse.ok().build()).build();
88+
return WebTestClient.bindToRouterFunction(routes).build();
89+
// @formatter:on
8890
}
8991

9092
@Bean

0 commit comments

Comments
 (0)