|
| 1 | +import graphql.annotations.AnnotationsSchemaCreator; |
| 2 | +import graphql.annotations.annotationTypes.GraphQLDescription; |
| 3 | +import graphql.annotations.annotationTypes.GraphQLField; |
| 4 | +import graphql.annotations.annotationTypes.GraphQLName; |
| 5 | +import graphql.annotations.directives.creation.DirectiveLocations; |
| 6 | +import graphql.annotations.processor.GraphQLAnnotations; |
| 7 | +import graphql.introspection.Introspection; |
| 8 | +import graphql.schema.GraphQLDirective; |
| 9 | +import graphql.schema.GraphQLObjectType; |
| 10 | +import graphql.schema.GraphQLSchema; |
| 11 | +import org.testng.annotations.BeforeMethod; |
| 12 | +import org.testng.annotations.Test; |
| 13 | + |
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; |
| 18 | +import static org.hamcrest.CoreMatchers.is; |
| 19 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | + |
| 22 | +public class AnnotationsSchemaCreatorTest { |
| 23 | + private AnnotationsSchemaCreator.Builder builder; |
| 24 | + private GraphQLAnnotations graphQLAnnotations; |
| 25 | + |
| 26 | + @BeforeMethod |
| 27 | + public void setUp() { |
| 28 | + graphQLAnnotations = new GraphQLAnnotations(); |
| 29 | + builder = newAnnotationsSchema().setAnnotationsProcessor(graphQLAnnotations); |
| 30 | + } |
| 31 | + |
| 32 | + @Test(expectedExceptions = AssertionError.class) |
| 33 | + public void testBuild_queryIsNotProvided_ExceptionIsThrown() { |
| 34 | + // act |
| 35 | + builder.build(); |
| 36 | + } |
| 37 | + |
| 38 | + @GraphQLDescription("query obj") |
| 39 | + public static class QueryTest { |
| 40 | + @GraphQLField |
| 41 | + public int getNum() { |
| 42 | + return 5; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testBuild_queryIsProvided_SchemaIsCreatedWithQuery() { |
| 48 | + // arrange + act |
| 49 | + GraphQLSchema schema = builder.query(QueryTest.class).build(); |
| 50 | + |
| 51 | + // assert |
| 52 | + GraphQLObjectType queryType = schema.getQueryType(); |
| 53 | + assertThat(queryType.getDescription(), is("query obj")); |
| 54 | + assertThat(queryType.getFieldDefinition("getNum"), notNullValue()); |
| 55 | + assertThat(queryType.getFieldDefinitions().size(), is(1)); |
| 56 | + } |
| 57 | + |
| 58 | + public static class MutationTest { |
| 59 | + @GraphQLField |
| 60 | + public int mutate() { |
| 61 | + return 4; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testBuild_mutation_SchemaIsCreatedWithMutation() { |
| 67 | + // arrange + act |
| 68 | + GraphQLSchema schema = builder.query(QueryTest.class).mutation(MutationTest.class).build(); |
| 69 | + |
| 70 | + /// assert |
| 71 | + GraphQLObjectType mutationType = schema.getMutationType(); |
| 72 | + assertThat(mutationType.getFieldDefinition("mutate"), notNullValue()); |
| 73 | + assertThat(mutationType.getFieldDefinitions().size(), is(1)); |
| 74 | + } |
| 75 | + |
| 76 | + public static class SubscriptionTest { |
| 77 | + @GraphQLField |
| 78 | + public int subscribe() { |
| 79 | + return 4; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testBuild_subscription_SchemaIsCreatedWithSubscription() { |
| 85 | + // arrange + act |
| 86 | + GraphQLSchema schema = builder.query(QueryTest.class).subscription(SubscriptionTest.class).build(); |
| 87 | + |
| 88 | + // assert |
| 89 | + GraphQLObjectType subscriptionType = schema.getSubscriptionType(); |
| 90 | + assertThat(subscriptionType.getFieldDefinition("subscribe"), notNullValue()); |
| 91 | + assertThat(subscriptionType.getFieldDefinitions().size(), is(1)); |
| 92 | + } |
| 93 | + |
| 94 | + @GraphQLName("testDirective") |
| 95 | + @DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION}) |
| 96 | + public static class DirectiveDefinitionTest { |
| 97 | + private boolean isActive = true; |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testBuild_directive_SchemaIsCreatedWithDirective() { |
| 102 | + // arrange + act |
| 103 | + GraphQLSchema schema = builder.query(QueryTest.class).directive(DirectiveDefinitionTest.class).build(); |
| 104 | + |
| 105 | + // assert |
| 106 | + GraphQLDirective testDirective = schema.getDirective("testDirective"); |
| 107 | + assertThat(testDirective, notNullValue()); |
| 108 | + assertThat(testDirective.getArguments().size(), is(1)); |
| 109 | + assertThat(testDirective.getArgument("isActive"), notNullValue()); |
| 110 | + } |
| 111 | + |
| 112 | + @GraphQLName("secondDirective") |
| 113 | + @DirectiveLocations(Introspection.DirectiveLocation.FIELD) |
| 114 | + public static class SecondDirective { |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testBuild_multipleDirectives_SchemaIsCreatedWithDirectives() { |
| 120 | + // arrange + act |
| 121 | + Set<Class<?>> directives = new HashSet<>(); |
| 122 | + directives.add(DirectiveDefinitionTest.class); |
| 123 | + directives.add(SecondDirective.class); |
| 124 | + GraphQLDirective directiveTest = graphQLAnnotations.directive(DirectiveDefinitionTest.class); |
| 125 | + GraphQLDirective secondDirective = graphQLAnnotations.directive(SecondDirective.class); |
| 126 | + GraphQLSchema schema = builder.query(QueryTest.class).directives(directives).build(); |
| 127 | + |
| 128 | + // assert |
| 129 | + assertThat(schema.getDirective("secondDirective"), notNullValue()); |
| 130 | + assertThat(schema.getDirective("testDirective"), notNullValue()); |
| 131 | + } |
| 132 | + |
| 133 | + @GraphQLName("additional") |
| 134 | + public static class AdditionalTypeTest { |
| 135 | + public int getI() { |
| 136 | + return 4; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testBuild_additionalType_SchemaIsCreatedWithAdditionalType() { |
| 142 | + // arrange + act |
| 143 | + GraphQLSchema schema = builder.query(QueryTest.class).additionalType(AdditionalTypeTest.class).build(); |
| 144 | + GraphQLObjectType additionalType = graphQLAnnotations.object(AdditionalTypeTest.class); |
| 145 | + |
| 146 | + // assert |
| 147 | + assertThat(schema.getType("additional"), notNullValue()); |
| 148 | + assertThat(schema.getType("additional").toString(), is(additionalType.toString())); |
| 149 | + } |
| 150 | +} |
0 commit comments