diff --git a/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java b/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java index c535c7d7..60e14f91 100644 --- a/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java +++ b/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java @@ -1,30 +1,26 @@ package io.spring.sample.graphql; -import org.junit.jupiter.api.BeforeEach; 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.http.MediaType; -import org.springframework.test.web.reactive.server.WebTestClient; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.client.MockMvcWebTestClient; +import org.springframework.graphql.boot.test.tester.AutoConfigureGraphQlTester; +import org.springframework.graphql.execution.ErrorType; +import org.springframework.graphql.test.tester.WebGraphQlTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; // @formatter:off @SpringBootTest @AutoConfigureMockMvc +@AutoConfigureGraphQlTester class SampleApplicationTests { - private WebTestClient client; + @Autowired + private WebGraphQlTester graphQlTester; - @BeforeEach - public void setUp(@Autowired MockMvc mockMvc) { - this.client = MockMvcWebTestClient.bindTo(mockMvc) - .baseUrl("/graphql") - .defaultHeaders(headers -> headers.setContentType(MediaType.APPLICATION_JSON)) - .build(); - } @Test void printError() { @@ -35,18 +31,14 @@ class SampleApplicationTests { " }" + "}"; - - client.post().uri("") - .bodyValue("{ \"query\": \"" + query + "\"}") - .exchange() - .expectStatus().isOk() - .expectBody(String.class) - .consumeWith(System.out::println); - + this.graphQlTester.query(query) + .execute() + .errors() + .satisfy(System.out::println); } @Test - void anonoymousThenUnauthorized() { + void anonymousThenUnauthorized() { String query = "{" + " employees{ " + " name" + @@ -54,12 +46,14 @@ class SampleApplicationTests { " }" + "}"; - client.post().uri("") - .bodyValue("{ \"query\": \"" + query + "\"}") - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("errors[0].extensions.classification").isEqualTo("UNAUTHORIZED"); - + this.graphQlTester.query(query) + .execute() + .errors() + .satisfy(errors -> { + assertThat(errors).hasSize(1); + assertThat(errors.get(0).getExtensions().get("classification")) + .isEqualTo(ErrorType.UNAUTHORIZED.name()); + }); } @Test @@ -71,12 +65,15 @@ class SampleApplicationTests { " }" + "}"; - client.post().uri("") - .headers(h -> h.setBasicAuth("rob", "rob")) - .bodyValue("{ \"query\": \"" + query + "\"}") - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("errors[0].extensions.classification").isEqualTo("FORBIDDEN"); + this.graphQlTester.query(query) + .headers(headers -> headers.setBasicAuth("rob", "rob")) + .execute() + .errors() + .satisfy(errors -> { + assertThat(errors).hasSize(1); + assertThat(errors.get(0).getExtensions().get("classification")) + .isEqualTo(ErrorType.FORBIDDEN.name()); + }); } @Test @@ -87,13 +84,9 @@ class SampleApplicationTests { " }" + "}"; - - client.post().uri("") - .bodyValue("{ \"query\": \"" + query + "\"}") - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("data.employees[0].name").isEqualTo("Andi"); - + this.graphQlTester.query(query) + .execute() + .path("employees[0].name").entity(String.class).isEqualTo("Andi"); } @Test @@ -105,15 +98,14 @@ class SampleApplicationTests { " }" + "}"; - - client.post().uri("") - .bodyValue("{ \"query\": \"" + query + "\"}") - .exchange() - .expectStatus().isOk() - .expectBody() - .jsonPath("data.employees[0].name").isEqualTo("Andi") - .jsonPath("data.employees[0].salary").doesNotExist(); - + this.graphQlTester.query(query) + .execute() + .errors() + .satisfy(errors -> { + assertThat(errors).hasSize(1); + assertThat(errors.get(0).getExtensions().get("classification")) + .isEqualTo(ErrorType.UNAUTHORIZED.name()); + }); } @Test @@ -125,16 +117,11 @@ class SampleApplicationTests { " }" + "}"; - - client.post().uri("") - .headers(h -> h.setBasicAuth("admin", "admin")) - .bodyValue("{ \"query\": \"" + query + "\"}") - .exchange() - .expectStatus().isOk() - .expectBody() - .jsonPath("data.employees[0].name").isEqualTo("Andi") - .jsonPath("data.employees[0].salary").isEqualTo("42"); - + this.graphQlTester.query(query) + .headers(headers -> headers.setBasicAuth("admin", "admin")) + .execute() + .path("employees[0].name").entity(String.class).isEqualTo("Andi") + .path("employees[0].salary").entity(int.class).isEqualTo(42); } @Test @@ -146,14 +133,10 @@ class SampleApplicationTests { " }" + "}"; - - client.post().uri("") - .headers(h -> h.setBasicAuth("admin", "INVALID")) - .bodyValue("{ \"query\": \"" + query + "\"}") - .exchange() - .expectStatus().isUnauthorized() - .expectBody() - .isEmpty(); - + assertThatThrownBy(() -> + this.graphQlTester.query(query) + .headers(headers -> headers.setBasicAuth("admin", "INVALID")) + .executeAndVerify()) + .hasMessage("Status expected:<200 OK> but was:<401 UNAUTHORIZED>"); } } \ No newline at end of file