|
| 1 | +package com.introproventures.graphql.jpa.query.spring; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport; |
| 6 | +import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder; |
| 7 | +import com.introproventures.graphql.jpa.query.schema.model.book.Book; |
| 8 | +import graphql.schema.GraphQLSchema; |
| 9 | +import jakarta.persistence.EntityManager; |
| 10 | +import java.util.Map; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.SpringBootConfiguration; |
| 14 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 15 | +import org.springframework.boot.autoconfigure.domain.EntityScan; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.context.annotation.Bean; |
| 18 | +import org.springframework.graphql.ExecutionGraphQlService; |
| 19 | +import org.springframework.graphql.execution.DefaultExecutionGraphQlService; |
| 20 | +import org.springframework.graphql.execution.GraphQlSource; |
| 21 | +import org.springframework.graphql.test.tester.ExecutionGraphQlServiceTester; |
| 22 | +import org.springframework.graphql.test.tester.GraphQlTester; |
| 23 | + |
| 24 | +@SpringBootTest(properties = "spring.sql.init.data-locations=classpath:books.sql") |
| 25 | +public class ExecutionGraphQlServiceTest extends AbstractSpringBootTestSupport { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private GraphQlTester graphQlTester; |
| 29 | + |
| 30 | + @SpringBootConfiguration |
| 31 | + @EnableAutoConfiguration |
| 32 | + @EntityScan(basePackageClasses = Book.class) |
| 33 | + static class Application { |
| 34 | + |
| 35 | + @Bean |
| 36 | + public GraphQLSchema graphQLSchema(final EntityManager entityManager) { |
| 37 | + return new GraphQLJpaSchemaBuilder(entityManager) |
| 38 | + .name("GraphQLBooks") |
| 39 | + .description("Books JPA test schema") |
| 40 | + .build(); |
| 41 | + } |
| 42 | + |
| 43 | + @Bean |
| 44 | + ExecutionGraphQlService executionGraphQlService(GraphQLSchema graphQLSchema) { |
| 45 | + GraphQlSource graphQlSource = GraphQlSource.builder(graphQLSchema).build(); |
| 46 | + |
| 47 | + return new DefaultExecutionGraphQlService(graphQlSource); |
| 48 | + } |
| 49 | + |
| 50 | + @Bean |
| 51 | + GraphQlTester graphQlTester(ExecutionGraphQlService executionGraphQlService) { |
| 52 | + return ExecutionGraphQlServiceTester.create(executionGraphQlService); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void graphQlServiceTester() { |
| 58 | + graphQlTester |
| 59 | + .document("{Books{select{id title}}}") |
| 60 | + .execute() |
| 61 | + .path(".select[*]") |
| 62 | + .matchesJson( |
| 63 | + """ |
| 64 | + [ |
| 65 | + {"id":2,"title":"War and Peace"}, |
| 66 | + {"id":3,"title":"Anna Karenina"}, |
| 67 | + {"id":5,"title":"The Cherry Orchard"}, |
| 68 | + {"id":6,"title":"The Seagull"}, |
| 69 | + {"id":7,"title":"Three Sisters"} |
| 70 | + ] |
| 71 | + """ |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void graphQlServiceTesterMap() { |
| 77 | + var query = "{Books {select {id title}}}"; |
| 78 | + |
| 79 | + var expected = |
| 80 | + "[{id=2, title=War and Peace}, {id=3, title=Anna Karenina}, {id=5, title=The Cherry Orchard}, {id=6, title=The Seagull}, {id=7, title=Three Sisters}]"; |
| 81 | + |
| 82 | + graphQlTester |
| 83 | + .document(query) |
| 84 | + .execute() |
| 85 | + .errors() |
| 86 | + .verify() |
| 87 | + .path(".select[*]") |
| 88 | + .entityList(Map.class) |
| 89 | + .satisfies(result -> assertThat(result.toString()).isEqualTo(expected)); |
| 90 | + } |
| 91 | +} |
0 commit comments