Skip to content

Commit 15e47b8

Browse files
committed
added tests
1 parent 0e08a7c commit 15e47b8

File tree

2 files changed

+190
-19
lines changed

2 files changed

+190
-19
lines changed

src/main/java/graphql/annotations/AnnotationsSchemaCreator.java

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,13 +21,10 @@
2121
import graphql.schema.GraphQLSchema;
2222
import graphql.schema.GraphQLType;
2323

24-
import java.util.ArrayList;
25-
import java.util.List;
24+
import java.util.HashSet;
2625
import java.util.Set;
2726
import java.util.stream.Collectors;
2827

29-
import static graphql.schema.GraphQLSchema.newSchema;
30-
3128
public class AnnotationsSchemaCreator {
3229

3330
public static Builder newAnnotationsSchema() {
@@ -38,9 +35,20 @@ public static class Builder {
3835
private Class<?> queryObject;
3936
private Class<?> mutationObject;
4037
private Class<?> subscriptionObject;
41-
private List<Class<?>> directivesObjectList = new ArrayList<>();
42-
private List<Class<?>> additionalTypesList = new ArrayList<>();
43-
private GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations();
38+
private Set<Class<?>> directivesObjectList = new HashSet<>();
39+
private Set<Class<?>> additionalTypesList = new HashSet<>();
40+
private GraphQLAnnotations graphQLAnnotations;
41+
private GraphQLSchema.Builder graphqlSchemaBuilder;
42+
43+
public Builder setGraphQLSchemaBuilder(GraphQLSchema.Builder schemaBuilder) {
44+
this.graphqlSchemaBuilder = schemaBuilder;
45+
return this;
46+
}
47+
48+
public Builder setAnnotationsProcessor(GraphQLAnnotations annotationsProcessor) {
49+
this.graphQLAnnotations = annotationsProcessor;
50+
return this;
51+
}
4452

4553
public Builder query(Class<?> object) {
4654
this.queryObject = object;
@@ -57,7 +65,7 @@ public Builder subscription(Class<?> subscriptionObject) {
5765
return this;
5866
}
5967

60-
public Builder directives(List<Class<?>> directivesObjectList) {
68+
public Builder directives(Set<Class<?>> directivesObjectList) {
6169
this.directivesObjectList.addAll(directivesObjectList);
6270
return this;
6371
}
@@ -72,6 +80,11 @@ public Builder additionalType(Class<?> additionalObject) {
7280
return this;
7381
}
7482

83+
public Builder additionalTypes(Set<Class<?>> additionalTypes) {
84+
this.additionalTypesList.addAll(additionalTypes);
85+
return this;
86+
}
87+
7588
public Builder typeExtension(Class<?> typeExtension) {
7689
this.graphQLAnnotations.registerTypeExtension(typeExtension);
7790
return this;
@@ -99,23 +112,31 @@ public GraphQLAnnotations getGraphQLAnnotations() {
99112
public GraphQLSchema build() {
100113
assert this.queryObject != null;
101114

115+
if (graphQLAnnotations == null) {
116+
graphQLAnnotations = new GraphQLAnnotations();
117+
}
118+
119+
if (graphqlSchemaBuilder == null) {
120+
graphqlSchemaBuilder = new GraphQLSchema.Builder();
121+
}
122+
102123
Set<GraphQLDirective> directives = directivesObjectList.stream().map(dir -> graphQLAnnotations.directive(dir)).collect(Collectors.toSet());
103124
Set<GraphQLType> additionalTypes = additionalTypesList.stream().map(x -> x.isInterface() ?
104125
graphQLAnnotations.generateInterface(x) : graphQLAnnotations.object(x)).collect(Collectors.toSet());
105-
GraphQLSchema.Builder builder = newSchema();
106-
builder.query(graphQLAnnotations.object(queryObject));
126+
127+
graphqlSchemaBuilder.query(graphQLAnnotations.object(queryObject));
107128
if (this.mutationObject != null) {
108-
builder.mutation(graphQLAnnotations.object(mutationObject));
129+
graphqlSchemaBuilder.mutation(graphQLAnnotations.object(mutationObject));
109130
}
110131
if (this.subscriptionObject != null) {
111-
builder.subscription(graphQLAnnotations.object(subscriptionObject));
132+
graphqlSchemaBuilder.subscription(graphQLAnnotations.object(subscriptionObject));
112133
}
113134
if (!this.directivesObjectList.isEmpty()) {
114-
builder.additionalDirectives(directives);
135+
graphqlSchemaBuilder.additionalDirectives(directives);
115136
}
116-
builder.additionalTypes(additionalTypes).additionalType(Relay.pageInfoType)
137+
graphqlSchemaBuilder.additionalTypes(additionalTypes).additionalType(Relay.pageInfoType)
117138
.codeRegistry(graphQLAnnotations.getContainer().getCodeRegistryBuilder().build());
118-
return builder.build();
139+
return graphqlSchemaBuilder.build();
119140
}
120141
}
121142
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)