Skip to content

Commit f819de4

Browse files
author
vkarthikeya
committed
Hactoberfest2023 : Multiple Directive Coexist validation
1 parent 73dba53 commit f819de4

File tree

3 files changed

+109
-5
lines changed

3 files changed

+109
-5
lines changed

src/main/java/com/intuit/graphql/orchestrator/authorization/ValidateMultipleDirectivesCoexist.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@
1010

1111
public class ValidateMultipleDirectivesCoexist {
1212

13+
public ValidateMultipleDirectivesCoexist() {
14+
}
15+
1316
public void validate(List<Directive> directives) {
1417
List<String> directiveNames = directives.stream()
1518
.map(d -> d.getDefinition().getName())
1619
.collect(Collectors.toList());
1720

1821
if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "external"))) {
19-
throw new InvalidDirectivePairingException("resolver", "external");
22+
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
2023
}
2124

2225
if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "provides"))) {
23-
throw new InvalidDirectivePairingException("resolver", "provides");
26+
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
2427
}
2528

2629
if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "requires"))) {
27-
throw new InvalidDirectivePairingException("resolver", "requires");
30+
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
2831
}
2932

3033
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.intuit.graphql.orchestrator.stitching;
22

33

4+
import java.util.List;
5+
46
public class InvalidDirectivePairingException extends StitchingException {
57

68
private static final String ERR_MSG = "Field %s in container type %s with resolver directive not allowed "
79
+ "to have argument definitions.";
810

9-
public InvalidDirectivePairingException(String fieldName, String containerName) {
10-
super(String.format(ERR_MSG, fieldName, containerName));
11+
public InvalidDirectivePairingException(List<String> directiveNames) {
12+
super(String.format(ERR_MSG, directiveNames.get(0), directiveNames.get(1)));
1113
}
1214
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.intuit.graphql.orchestrator.authorization
2+
3+
import com.intuit.graphql.orchestrator.stitching.InvalidDirectivePairingException
4+
import spock.lang.Specification;
5+
import com.intuit.graphql.graphQL.Directive
6+
7+
import com.intuit.graphql.graphQL.DirectiveDefinition
8+
import static com.intuit.graphql.orchestrator.XtextObjectCreationUtil.buildDirective
9+
import static com.intuit.graphql.orchestrator.XtextObjectCreationUtil.buildDirectiveDefinition;
10+
11+
class ValidateMultipleDirectiveCoexistSpec extends Specification {
12+
private Directive resolverDirective
13+
14+
private Directive providesDirective
15+
16+
private Directive externalDirective
17+
18+
private Directive requiresDirective
19+
20+
private Directive skipDirective
21+
22+
private Directive includesDirective
23+
24+
25+
26+
def setup() {
27+
DirectiveDefinition resolverDirectiveDefinition = buildDirectiveDefinition("resolver")
28+
DirectiveDefinition externalDirectiveDefinition = buildDirectiveDefinition("external")
29+
DirectiveDefinition requiresDirectiveDefinition = buildDirectiveDefinition("requires")
30+
DirectiveDefinition providesDirectiveDefinition = buildDirectiveDefinition("provides")
31+
DirectiveDefinition skipDirectiveDefinition = buildDirectiveDefinition("skip")
32+
DirectiveDefinition includesDirectiveDefinition = buildDirectiveDefinition("include")
33+
resolverDirective = buildDirective(resolverDirectiveDefinition, Collections.emptyList())
34+
providesDirective = buildDirective(providesDirectiveDefinition, Collections.emptyList())
35+
externalDirective = buildDirective(externalDirectiveDefinition, Collections.emptyList())
36+
requiresDirective = buildDirective(requiresDirectiveDefinition, Collections.emptyList())
37+
skipDirective = buildDirective(skipDirectiveDefinition, Collections.emptyList())
38+
includesDirective = buildDirective(includesDirectiveDefinition, Collections.emptyList())
39+
}
40+
41+
def "should throw exception for invalid directive pairing: resolver and external"() {
42+
given:
43+
def directives = [
44+
resolverDirective,
45+
externalDirective
46+
]
47+
48+
when:
49+
new ValidateMultipleDirectivesCoexist().validate(directives)
50+
51+
then:
52+
thrown InvalidDirectivePairingException.class
53+
}
54+
55+
def "should throw exception for invalid directive pairing: resolver and provides"() {
56+
given:
57+
def directives = [
58+
resolverDirective,
59+
providesDirective
60+
]
61+
62+
when:
63+
new ValidateMultipleDirectivesCoexist().validate(directives)
64+
65+
then:
66+
thrown InvalidDirectivePairingException.class
67+
}
68+
69+
def "should throw exception for invalid directive pairing: resolver and requires"() {
70+
given:
71+
def directives = [
72+
resolverDirective,
73+
requiresDirective
74+
]
75+
76+
when:
77+
new ValidateMultipleDirectivesCoexist().validate(directives)
78+
79+
then:
80+
thrown InvalidDirectivePairingException.class
81+
}
82+
83+
def "should not throw exception for valid directives"() {
84+
given:
85+
def directives = [
86+
requiresDirective,
87+
skipDirective,
88+
includesDirective
89+
]
90+
91+
when:
92+
new ValidateMultipleDirectivesCoexist().validate(directives)
93+
94+
then:
95+
noExceptionThrown()
96+
}
97+
}
98+
99+

0 commit comments

Comments
 (0)