From 853cfbf97a18256b6beab40be6f3509ca5092087 Mon Sep 17 00:00:00 2001 From: bohub12 Date: Tue, 12 Sep 2023 01:40:21 +0900 Subject: [PATCH] Feat: Test code for GraphQL --- spring-for-graphql/README.md | 41 ++++++ spring-for-graphql/build.gradle | 5 + .../com/example/demo/DemoApplication.java | 12 +- .../MemberGraphQLControllerTest.java | 120 ++++++++++++++++++ .../getMemberExpectedResponse.json | 9 ++ .../getMemberListExpectedResponse.json | 31 +++++ .../saveMemberExpectedResponse.json | 9 ++ .../resources/graphql-test/getMember.graphql | 7 + .../graphql-test/getMemberList.graphql | 7 + .../resources/graphql-test/saveMember.graphql | 7 + 10 files changed, 242 insertions(+), 6 deletions(-) create mode 100644 spring-for-graphql/README.md create mode 100644 spring-for-graphql/src/test/java/com/example/demo/controller/MemberGraphQLControllerTest.java create mode 100644 spring-for-graphql/src/test/resources/graphql-response/getMemberExpectedResponse.json create mode 100644 spring-for-graphql/src/test/resources/graphql-response/getMemberListExpectedResponse.json create mode 100644 spring-for-graphql/src/test/resources/graphql-response/saveMemberExpectedResponse.json create mode 100644 spring-for-graphql/src/test/resources/graphql-test/getMember.graphql create mode 100644 spring-for-graphql/src/test/resources/graphql-test/getMemberList.graphql create mode 100644 spring-for-graphql/src/test/resources/graphql-test/saveMember.graphql diff --git a/spring-for-graphql/README.md b/spring-for-graphql/README.md new file mode 100644 index 0000000..6d3551f --- /dev/null +++ b/spring-for-graphql/README.md @@ -0,0 +1,41 @@ + + +```graphql +mutation { + saveMember(body: { + name: "멤버" + age: 15 + role: ADMIN + }) { + id + age + name + role + } +} + +query { + getMember(id:1) { + id + name + age + role + } +} + +query { + getMemberList { + id + name + age + role + } +} +``` + + +# _Reference_ +- [GraphQL official documentation](https://graphql.org/learn) +- [GraphQL official documentation - schema](https://graphql.org/learn/schema/) +- [Baeldung Blog - Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql) +- [tech blog1](https://danawalab.github.io/spring/2022/06/06/Spring-for-GraphQL.html) \ No newline at end of file diff --git a/spring-for-graphql/build.gradle b/spring-for-graphql/build.gradle index 918b994..5d46ad0 100644 --- a/spring-for-graphql/build.gradle +++ b/spring-for-graphql/build.gradle @@ -30,9 +30,14 @@ dependencies { // graph ql - supported various scalars implementation 'com.graphql-java:graphql-java-extended-scalars:21.0' implementation 'org.springframework.boot:spring-boot-starter-web' + testImplementation 'org.projectlombok:lombok' + compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' + annotationProcessor 'org.projectlombok:lombok' + testAnnotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework:spring-webflux' testImplementation 'org.springframework.graphql:spring-graphql-test' diff --git a/spring-for-graphql/src/main/java/com/example/demo/DemoApplication.java b/spring-for-graphql/src/main/java/com/example/demo/DemoApplication.java index eecec99..ca99aae 100644 --- a/spring-for-graphql/src/main/java/com/example/demo/DemoApplication.java +++ b/spring-for-graphql/src/main/java/com/example/demo/DemoApplication.java @@ -1,8 +1,8 @@ package com.example.demo; import com.example.demo.domain.Member; -import com.example.demo.repository.MemberRepository; import com.example.demo.domain.MemberRole; +import com.example.demo.repository.MemberRepository; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -18,11 +18,11 @@ public static void main(String[] args) { @Bean public CommandLineRunner runner(MemberRepository repository) { return args -> { - repository.save(new Member(1L, "member1", MemberRole.NORMAL, 10)); - repository.save(new Member(2L, "member2", MemberRole.NORMAL, 20)); - repository.save(new Member(3L, "member3", MemberRole.NORMAL, 30)); - repository.save(new Member(4L, "member4", MemberRole.ADMIN, 40)); - repository.save(new Member(5L, "member5", MemberRole.ADMIN, 50)); + repository.save(new Member("member1", MemberRole.NORMAL, 10)); + repository.save(new Member("member2", MemberRole.NORMAL, 20)); + repository.save(new Member("member3", MemberRole.NORMAL, 30)); + repository.save(new Member("member4", MemberRole.ADMIN, 40)); + repository.save(new Member("member5", MemberRole.ADMIN, 50)); }; } diff --git a/spring-for-graphql/src/test/java/com/example/demo/controller/MemberGraphQLControllerTest.java b/spring-for-graphql/src/test/java/com/example/demo/controller/MemberGraphQLControllerTest.java new file mode 100644 index 0000000..537395c --- /dev/null +++ b/spring-for-graphql/src/test/java/com/example/demo/controller/MemberGraphQLControllerTest.java @@ -0,0 +1,120 @@ +package com.example.demo.controller; + +import com.example.demo.config.GraphQlConfig; +import com.example.demo.domain.Member; +import com.example.demo.domain.MemberRole; +import com.example.demo.repository.MemberRepository; +import lombok.SneakyThrows; +import org.junit.jupiter.api.AfterEach; +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.graphql.tester.AutoConfigureGraphQlTester; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.graphql.test.tester.GraphQlTester; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + + +@SpringBootTest +@AutoConfigureGraphQlTester +@Import(GraphQlConfig.class) +class MemberGraphQLControllerTest { + + @Autowired + private GraphQlTester graphQlTester; + + @Autowired + private MemberRepository repository; + + + @BeforeEach + void addDummyData() { + repository.save(new Member("member1", MemberRole.NORMAL, 10)); + repository.save(new Member("member2", MemberRole.NORMAL, 20)); + repository.save(new Member("member3", MemberRole.NORMAL, 30)); + repository.save(new Member("member4", MemberRole.ADMIN, 40)); + repository.save(new Member("member5", MemberRole.ADMIN, 50)); + } + + @AfterEach + void clear() { + repository.deleteAll(); + } + + + @Test + void getMember_thenReturnResponse1() { + long id = repository.findAll().stream().findFirst().orElseThrow().getId(); + + String documentName = "getMember"; + + graphQlTester.documentName(documentName) + .variable("id", id) + .execute() + .path("$") + .matchesJson(expected(documentName)); + } + + @Test + void getMember_thenReturnResponse2() { + long id = repository.findAll().stream().findFirst().orElseThrow().getId(); + + String query = String.format("{ getMember(id: %d) { id name age role }}", id); + Member member = graphQlTester.document(query) + .execute() + .path("data.getMember") + .entity(Member.class) + .get(); + + assertThat(member).isNotNull(); + assertThat(member.getAge()).isEqualTo(10); + } + + + @Test + void getMemberList_thenReturnResponse1() { + String documentName = "getMemberList"; + + List memberList = graphQlTester.documentName(documentName) + .execute() + .path("data.getMemberList[*]") + .entityList(Member.class) + .get(); + + assertThat(memberList.size()).isEqualTo(5); + } + + @Test + void saveMember_thenReturnResponse1() { + String name = "멤버"; + MemberRole role = MemberRole.ADMIN; + int age = 15; + + HashMap map = new HashMap<>(); + map.put("name", name); + map.put("role", role); + map.put("age", age); + + String documentName = "saveMember"; + + graphQlTester.documentName(documentName) + .variable("body", map) + .execute() + .path("$") + .matchesJson(expected(documentName)); + } + + @SneakyThrows + public static String expected(String fileName) { + Path path = Paths.get("src/test/resources/graphql-response/" + fileName + "ExpectedResponse.json"); + return new String(Files.readAllBytes(path)); + } +} \ No newline at end of file diff --git a/spring-for-graphql/src/test/resources/graphql-response/getMemberExpectedResponse.json b/spring-for-graphql/src/test/resources/graphql-response/getMemberExpectedResponse.json new file mode 100644 index 0000000..728c27c --- /dev/null +++ b/spring-for-graphql/src/test/resources/graphql-response/getMemberExpectedResponse.json @@ -0,0 +1,9 @@ +{ + "data": { + "getMember": { + "name": "member1", + "age": 10, + "role": "NORMAL" + } + } +} \ No newline at end of file diff --git a/spring-for-graphql/src/test/resources/graphql-response/getMemberListExpectedResponse.json b/spring-for-graphql/src/test/resources/graphql-response/getMemberListExpectedResponse.json new file mode 100644 index 0000000..1973404 --- /dev/null +++ b/spring-for-graphql/src/test/resources/graphql-response/getMemberListExpectedResponse.json @@ -0,0 +1,31 @@ +{ + "data": { + "getMemberList": [ + { + "name": "member1", + "age": 10, + "role": "NORMAL" + }, + { + "name": "member2", + "age": 20, + "role": "NORMAL" + }, + { + "name": "member3", + "age": 30, + "role": "NORMAL" + }, + { + "name": "member4", + "age": 40, + "role": "ADMIN" + }, + { + "name": "member5", + "age": 50, + "role": "ADMIN" + } + ] + } +} \ No newline at end of file diff --git a/spring-for-graphql/src/test/resources/graphql-response/saveMemberExpectedResponse.json b/spring-for-graphql/src/test/resources/graphql-response/saveMemberExpectedResponse.json new file mode 100644 index 0000000..f1b10b8 --- /dev/null +++ b/spring-for-graphql/src/test/resources/graphql-response/saveMemberExpectedResponse.json @@ -0,0 +1,9 @@ +{ + "data": { + "saveMember": { + "age": 15, + "name": "멤버", + "role": "ADMIN" + } + } +} \ No newline at end of file diff --git a/spring-for-graphql/src/test/resources/graphql-test/getMember.graphql b/spring-for-graphql/src/test/resources/graphql-test/getMember.graphql new file mode 100644 index 0000000..4d953f8 --- /dev/null +++ b/spring-for-graphql/src/test/resources/graphql-test/getMember.graphql @@ -0,0 +1,7 @@ +query getMember ($id: Long!) { + getMember(id: $id) { + name + age + role + } +} \ No newline at end of file diff --git a/spring-for-graphql/src/test/resources/graphql-test/getMemberList.graphql b/spring-for-graphql/src/test/resources/graphql-test/getMemberList.graphql new file mode 100644 index 0000000..bc262cb --- /dev/null +++ b/spring-for-graphql/src/test/resources/graphql-test/getMemberList.graphql @@ -0,0 +1,7 @@ +query getMemberList { + getMemberList { + name + age + role + } +} \ No newline at end of file diff --git a/spring-for-graphql/src/test/resources/graphql-test/saveMember.graphql b/spring-for-graphql/src/test/resources/graphql-test/saveMember.graphql new file mode 100644 index 0000000..141f879 --- /dev/null +++ b/spring-for-graphql/src/test/resources/graphql-test/saveMember.graphql @@ -0,0 +1,7 @@ +mutation saveMember ($body: CreateMemberRequestDto!) { + saveMember(body: $body) { + age + name + role + } +} \ No newline at end of file