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.
1515package graphql .annotations ;
1616
1717import graphql .annotations .annotationTypes .GraphQLDescription ;
18+ import graphql .annotations .annotationTypes .GraphQLDirectiveDefinition ;
1819import graphql .annotations .annotationTypes .GraphQLName ;
1920import graphql .annotations .directives .AnnotationsDirectiveWiring ;
21+ import graphql .annotations .directives .creation .DirectiveAnnotation ;
2022import graphql .annotations .directives .creation .DirectiveLocations ;
2123import graphql .annotations .directives .creation .DirectiveWiring ;
2224import graphql .annotations .processor .GraphQLAnnotations ;
2628import org .testng .annotations .BeforeMethod ;
2729import org .testng .annotations .Test ;
2830
31+ import java .util .Set ;
32+
2933import static graphql .Scalars .GraphQLBoolean ;
3034import static graphql .Scalars .GraphQLString ;
3135import static org .testng .AssertJUnit .*;
@@ -40,7 +44,11 @@ public void setUp() {
4044 this .graphQLAnnotations = new GraphQLAnnotations ();
4145 }
4246
43- public static class GeneralWiring implements AnnotationsDirectiveWiring {
47+ /**
48+ * Defining of directives through a class (@Deprecated)
49+ */
50+
51+ public static class GeneralWiring implements AnnotationsDirectiveWiring {
4452
4553 }
4654
@@ -58,7 +66,7 @@ public static class UpperDirective {
5866 }
5967
6068 @ Test
61- public void test_directive_creation () {
69+ public void directive_suppliedDirectiveClass_returnCorrectDirective () {
6270 // Act
6371 GraphQLDirective directive = this .graphQLAnnotations .directive (UpperDirective .class );
6472
@@ -83,4 +91,93 @@ public void test_directive_creation() {
8391 assertNotNull (noDefaultValue );
8492 assertNull (noDefaultValue .getDefaultValue ());
8593 }
94+
95+ /**
96+ * Defining of directives through a method
97+ */
98+
99+ static class Wiring implements AnnotationsDirectiveWiring {
100+
101+ }
102+
103+ static class DirectivesMethodsContainer {
104+ @ GraphQLName ("upper" )
105+ @ GraphQLDescription ("upper directive" )
106+ @ GraphQLDirectiveDefinition (wiring = Wiring .class )
107+ @ DirectiveLocations ({Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection .DirectiveLocation .INTERFACE })
108+ public void upperDirective (@ GraphQLName ("isActive" ) @ GraphQLDescription ("is active" ) boolean isActive ) {
109+ }
110+
111+ @ GraphQLName ("suffix" )
112+ @ GraphQLDescription ("suffix directive" )
113+ @ GraphQLDirectiveDefinition (wiring = Wiring .class )
114+ @ DirectiveLocations ({Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection .DirectiveLocation .INTERFACE })
115+ public void suffixDirective (@ GraphQLName ("suffix" ) @ GraphQLDescription ("the suffix" ) String suffix ) {
116+ }
117+ }
118+
119+
120+ @ Test
121+ public void directive_suppliedDirectiveMethodContainer_returnCorrectDirective () {
122+ // Act
123+ Set <GraphQLDirective > directive = this .graphQLAnnotations .directives (DirectivesMethodsContainer .class );
124+
125+ GraphQLDirective upper = (GraphQLDirective ) directive .toArray ()[1 ];
126+ GraphQLDirective suffix = (GraphQLDirective ) directive .toArray ()[0 ];
127+
128+ // Assert
129+ assertEquals (upper .getName (), "upper" );
130+ assertEquals (upper .getDescription (), "upper directive" );
131+ assertArrayEquals (upper .validLocations ().toArray (), new Introspection .DirectiveLocation []{Introspection .DirectiveLocation .FIELD_DEFINITION ,
132+ Introspection .DirectiveLocation .INTERFACE });
133+ GraphQLArgument isActive = upper .getArgument ("isActive" );
134+ assertNotNull (isActive );
135+ assertEquals (isActive .getName (), "isActive" );
136+ assertEquals (isActive .getType (), GraphQLBoolean );
137+ assertNull (isActive .getDefaultValue ());
138+
139+ GraphQLArgument suffixToAdd = suffix .getArgument ("suffix" );
140+ assertNotNull (suffixToAdd );
141+ assertEquals (suffixToAdd .getType (), GraphQLString );
142+ assertEquals ("the suffix" , suffixToAdd .getDescription ());
143+ assertNull (suffixToAdd .getDefaultValue ());
144+ }
145+
146+
147+ /**
148+ * Defining of directives through a java annotation
149+ */
150+
151+ @ GraphQLName ("upper" )
152+ @ GraphQLDescription ("the upper" )
153+ @ DirectiveAnnotation (Wiring .class )
154+ @ DirectiveLocations ({Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection .DirectiveLocation .INTERFACE })
155+ @interface UpperAnnotation {
156+ @ GraphQLName ("isActive" )
157+ @ GraphQLDescription ("is active" )
158+ boolean isActive () default true ;
159+ }
160+
161+ @ GraphQLName ("bla" )
162+ @interface NoDirectiveAnnotation {
163+ boolean isActive () default true ;
164+ }
165+
166+ @ Test
167+ public void directive_suppliedDirectiveAnnotation_returnCorrectDirective () {
168+ // Act
169+ GraphQLDirective upper = this .graphQLAnnotations .directiveViaAnnotation (UpperAnnotation .class );
170+
171+ // Assert
172+ assertEquals (upper .getName (), "upper" );
173+ assertEquals (upper .getDescription (), "the upper" );
174+ assertArrayEquals (upper .validLocations ().toArray (), new Introspection .DirectiveLocation []{Introspection .DirectiveLocation .FIELD_DEFINITION ,
175+ Introspection .DirectiveLocation .INTERFACE });
176+ GraphQLArgument isActive = upper .getArgument ("isActive" );
177+ assertNotNull (isActive );
178+ assertEquals (isActive .getName (), "isActive" );
179+ assertEquals (isActive .getType (), GraphQLBoolean );
180+ assertEquals (true ,isActive .getDefaultValue ());
181+ }
182+
86183}
0 commit comments