Skip to content

Commit c75b8ae

Browse files
Merge pull request #178 from irintu/assert_for_outgoing_queries_202310
test: add assert for outgoing queries in integration tests - (Issue #175)
2 parents 5dfcf7c + dce3d3f commit c75b8ae

File tree

6 files changed

+92
-18
lines changed

6 files changed

+92
-18
lines changed

src/test/groovy/com/intuit/graphql/orchestrator/integration/AliasFragmentSpreadWithNestedSpec.groovy

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ class AliasFragmentSpreadWithNestedSpec extends BaseIntegrationTestSpecification
5959
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
6060

6161
then:
62+
//query QUERY {
63+
// a {b {foo:c(a:"foo") {d {f1}}}}
64+
// a {b {bar:c(a:"bar") {d {f2}}}}
65+
// }
66+
compareQueryToExecutionInput(null,
67+
"queryQUERY{a{b{foo:c(a:\"foo\"){d{f1}}bar:c(a:\"bar\"){d{f2}}}}}", service1)
68+
compareQueryToExecutionInput(null,null, service2)
69+
6270
executionResult.getErrors().isEmpty()
6371
Map<String, Object> data = executionResult.getData()
6472
data.a?.b?.foo?.d?.f1 instanceof String && data.a?.b?.foo?.d?.f1 == "foo1"
6573
data.a?.b?.bar?.d?.f2 instanceof String && data.a?.b?.bar?.d?.f2 == "bar2"
6674

67-
//query QUERY {
68-
// a {b {foo:c(a:"foo") {d {f1}}}}
69-
// a {b {bar:c(a:"bar") {d {f2}}}}
70-
// }
7175
}
7276

7377
def "Fragments used at the type-merge level with arguments"() {
@@ -92,6 +96,15 @@ class AliasFragmentSpreadWithNestedSpec extends BaseIntegrationTestSpecification
9296
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
9397

9498
then:
99+
// fragment fr on C {d {f1 f2}}
100+
//
101+
// query QUERY {
102+
// a {b {foo:c(a:"foo") {...fr}}}
103+
// a {b {bar:c(a:"bar") {...fr}}}
104+
// }
105+
compareQueryToExecutionInput(null,
106+
"fragmentfronC{d{f1f2}}queryQUERY{a{b{foo:c(a:\"foo\"){...fr}bar:c(a:\"bar\"){...fr}}}}", service1)
107+
compareQueryToExecutionInput(null,null, service2)
95108
executionResult.getErrors().isEmpty()
96109
Map<String, Object> data = executionResult.getData()
97110
data.a?.b?.foo?.d?.f1 instanceof String && data.a?.b?.foo?.d?.f1 == "foo1"
@@ -100,12 +113,7 @@ class AliasFragmentSpreadWithNestedSpec extends BaseIntegrationTestSpecification
100113
data.a?.b?.bar?.d?.f2 instanceof String && data.a?.b?.bar?.d?.f2 == "bar2"
101114
}
102115

103-
// fragment fr on C {d {f1 f2}}
104-
//
105-
// query QUERY {
106-
// a {b {foo:c(a:"foo") {...fr}}}
107-
// a {b {bar:c(a:"bar") {...fr}}}
108-
// }
116+
109117
def "Basic Nested"() {
110118
given:
111119
def mockServiceResponseBasic1 = [
@@ -150,6 +158,15 @@ class AliasFragmentSpreadWithNestedSpec extends BaseIntegrationTestSpecification
150158
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
151159

152160
then:
161+
// fragment fr on C {d {f1 f2}}
162+
//
163+
// query QUERY {
164+
// a {b {foo:c(a:"foo") {...fr}}}
165+
// a {b {bar:c(a:"bar") {...fr}}}
166+
// }
167+
compareQueryToExecutionInput(null,
168+
"queryQUERY{a{b{c(a:\"foo\"){d{f1f2}}}e{f1}}}", service1)
169+
compareQueryToExecutionInput(null,"queryQUERY{a{b{c(a:\"foo\"){f{f1f2}}}}}", service2)
153170
executionResult.getErrors().isEmpty()
154171
Map<String, Object> data = executionResult.getData()
155172
data.a?.b?.c?.d?.f1 instanceof String && data.a?.b?.c?.d?.f1 == "foo1"
@@ -158,11 +175,5 @@ class AliasFragmentSpreadWithNestedSpec extends BaseIntegrationTestSpecification
158175
data.a?.b?.c?.f?.f2 instanceof String && data.a?.b?.c?.f?.f2 == "bar2"
159176
data.a?.e?.f1 instanceof String && data.a?.e?.f1 == "eoo"
160177
}
161-
162-
// fragment fr on C {d {f1 f2}}
163-
//
164-
// query QUERY {
165-
// a {b {foo:c(a:"foo") {...fr}}}
166-
// a {b {bar:c(a:"bar") {...fr}}}
167-
// }
178+
168179
}

src/test/groovy/com/intuit/graphql/orchestrator/integration/InterfaceImplementInterfaceSpec.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.intuit.graphql.orchestrator.integration
22

33
import com.intuit.graphql.orchestrator.GraphQLOrchestrator
4+
import com.intuit.graphql.orchestrator.testhelpers.SimpleMockServiceProvider
45
import graphql.ExecutionInput
56
import graphql.ExecutionResult
67
import graphql.schema.GraphQLInterfaceType
@@ -130,6 +131,9 @@ class InterfaceImplementInterfaceSpec extends BaseIntegrationTestSpecification {
130131
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
131132

132133
then:
134+
compareQueryToExecutionInput(null,
135+
"queryQUERY{pets{edges{...onDogEdge{node{idnameisServiceDog__typename}__typename}__typename}__typename}}",
136+
(SimpleMockServiceProvider) testService)
133137
executionResult.getErrors().isEmpty()
134138
Map<String, Object> data = executionResult.getData()
135139

src/test/groovy/com/intuit/graphql/orchestrator/integration/JavaPrimitiveScalarSpec.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.intuit.graphql.orchestrator.integration
22

33
import com.intuit.graphql.orchestrator.GraphQLOrchestrator
4+
import com.intuit.graphql.orchestrator.schema.Operation
5+
import com.intuit.graphql.orchestrator.testhelpers.SimpleMockServiceProvider
46
import graphql.ExecutionInput
57
import graphql.ExecutionResult
68
import helpers.BaseIntegrationTestSpecification
@@ -57,6 +59,7 @@ class JavaPrimitiveScalarSpec extends BaseIntegrationTestSpecification {
5759
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
5860

5961
then:
62+
compareQueryToExecutionInput(Operation.QUERY, graphqlQuery, (SimpleMockServiceProvider) testService);
6063
executionResult.getErrors().isEmpty()
6164
Map<String, Object> data = executionResult.getData()
6265
data.a instanceof Long && data.a == Long.MAX_VALUE

src/test/groovy/com/intuit/graphql/orchestrator/integration/NestedFieldResolverSpec.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ class NestedFieldResolverSpec extends BaseIntegrationTestSpecification {
120120
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
121121

122122
then:
123+
specUnderTest.runtimeGraph.codeRegistry.dataFetcherMap.size() == 6
124+
checkIfKeyExistsInDataFetcherMap(specUnderTest, "BObjectType.bObjectField")
125+
checkIfKeyExistsInDataFetcherMap(specUnderTest, "Query.cTopField")
126+
checkIfKeyExistsInDataFetcherMap(specUnderTest, "Query._namespace")
127+
checkIfKeyExistsInDataFetcherMap(specUnderTest, "Query.arootField")
128+
checkIfKeyExistsInDataFetcherMap(specUnderTest, "Query.bTopField")
129+
checkIfKeyExistsInDataFetcherMap(specUnderTest, "AObjectType.cTopField")
130+
123131
executionResult.getErrors().isEmpty()
124132
executionResult?.data?.bTopField?.size() == 2
125133
executionResult?.data?.bTopField[0]?.size() == 2

src/test/groovy/com/intuit/graphql/orchestrator/integration/TopLevelStitchingSpec.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import com.google.common.collect.ImmutableMap
44
import com.intuit.graphql.orchestrator.ServiceProvider
55
import com.intuit.graphql.orchestrator.TestHelper
66
import com.intuit.graphql.orchestrator.TestServiceProvider
7+
import com.intuit.graphql.orchestrator.schema.Operation
78
import com.intuit.graphql.orchestrator.stitching.StitchingException
9+
import com.intuit.graphql.orchestrator.testhelpers.SimpleMockServiceProvider
810
import graphql.ExecutionInput
911
import graphql.ExecutionResult
1012
import helpers.BaseIntegrationTestSpecification
@@ -85,6 +87,8 @@ class TopLevelStitchingSpec extends BaseIntegrationTestSpecification {
8587
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
8688

8789
then:
90+
compareQueryToExecutionInput(Operation.QUERY, graphqlQuery, (SimpleMockServiceProvider) personService)
91+
compareQueryToExecutionInput(null, null, (SimpleMockServiceProvider) epsService)
8892
executionResult.getErrors().isEmpty()
8993
Map<String, Object> data = executionResult.getData()
9094
data.person?.name instanceof String && data.person?.name == "Test Name"
@@ -104,25 +108,31 @@ class TopLevelStitchingSpec extends BaseIntegrationTestSpecification {
104108
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
105109

106110
then:
111+
compareQueryToExecutionInput(Operation.QUERY, graphqlQuery, (SimpleMockServiceProvider) epsService)
112+
compareQueryToExecutionInput(null, null, (SimpleMockServiceProvider) personService)
107113
executionResult.getErrors().isEmpty()
108114
Map<String, Object> data = executionResult.getData()
109115
data.Profile?.prefFirstName instanceof String && data.Profile?.prefFirstName == "First Name"
110116
data.Profile?.prefLastName instanceof String && data.Profile?.prefLastName == "Last Name"
111117
data.Profile?.version instanceof Integer && data.Profile?.version == Short.MAX_VALUE
118+
112119
}
113120

114121
def "Eps service is stitched and mutation query is successful"() {
115122
given:
116123
specUnderTest = createGraphQLOrchestrator([epsMutationService, personService])
117124

118-
def graphqlQuery = "mutation { upsertProfile(corpId: \"test corp\", input: {version: ${Short.MAX_VALUE}}) { prefFirstName prefLastName version } }"
125+
def baseGraphqlQuery = "{ upsertProfile(corpId: \"test corp\", input: {version: ${Short.MAX_VALUE}}) { prefFirstName prefLastName version } }"
126+
def graphqlQuery = "mutation " + baseGraphqlQuery
119127

120128
ExecutionInput executionInput = createExecutionInput(graphqlQuery)
121129

122130
when:
123131
ExecutionResult executionResult = specUnderTest.execute(executionInput).get()
124132

125133
then:
134+
compareQueryToExecutionInput(Operation.MUTATION, baseGraphqlQuery, (SimpleMockServiceProvider) epsMutationService)
135+
compareQueryToExecutionInput(null, null, (SimpleMockServiceProvider) personService)
126136
executionResult.getErrors().isEmpty()
127137
Map<String, Object> data = executionResult.getData()
128138
data.upsertProfile?.prefFirstName instanceof String && data.upsertProfile?.prefFirstName == "First Name"

src/test/groovy/helpers/BaseIntegrationTestSpecification.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package helpers
22

33
import com.intuit.graphql.orchestrator.GraphQLOrchestrator
44
import com.intuit.graphql.orchestrator.ServiceProvider
5+
import com.intuit.graphql.orchestrator.schema.Operation
56
import com.intuit.graphql.orchestrator.schema.RuntimeGraph
67
import com.intuit.graphql.orchestrator.stitching.SchemaStitcher
78
import com.intuit.graphql.orchestrator.testhelpers.MockServiceProvider
@@ -110,6 +111,43 @@ class BaseIntegrationTestSpecification extends Specification {
110111
createExecutionInput(graphqlQuery, Collections.emptyMap())
111112
}
112113

114+
static boolean compareQueryToExecutionInput(Operation operation, String query,
115+
SimpleMockServiceProvider simpleMockServiceProvider) {
116+
if (query == null) {
117+
return getExecutionInputQueryNoSpace(simpleMockServiceProvider) == null
118+
}
119+
else {
120+
String graphQuery = (operation != null) ? operation.toString().toLowerCase() +
121+
operation.toString().toUpperCase() + query
122+
: query
123+
graphQuery = graphQuery.replaceAll("\\s", "");
124+
return (graphQuery) == getExecutionInputQueryNoSpace(simpleMockServiceProvider)
125+
}
126+
}
127+
128+
static String getExecutionInputQueryNoSpace(SimpleMockServiceProvider simpleMockServiceProvider) {
129+
String query = getExecutionInputQuery(simpleMockServiceProvider)
130+
return query != null ? query.replaceAll("\\s", "") : null
131+
}
132+
133+
static String getExecutionInputQuery(SimpleMockServiceProvider simpleMockServiceProvider) {
134+
try {
135+
Object executionInputArgs = simpleMockServiceProvider.executionInputArgumentCaptor.capturingMatcher.arguments
136+
return (executionInputArgs != null && !executionInputArgs.isEmpty())
137+
? executionInputArgs.get(0).query
138+
: null;
139+
} catch (NullPointerException e) {
140+
return null; // NPE hidden as its expected to be thrown
141+
}
142+
}
143+
144+
static String checkIfKeyExistsInDataFetcherMap(GraphQLOrchestrator graphQLOrchestrator, String key){
145+
return graphQLOrchestrator.runtimeGraph.codeRegistry.dataFetcherMap.entrySet().stream().anyMatch(
146+
{ entry ->
147+
(String.format("%s.%s", entry.getKey().getTypeName(), entry.getKey().getFieldName().toString())
148+
!= key) })
149+
}
150+
113151
ExecutionInput getCapturedDownstreamExecutionInput() {
114152
return testService.getExecutionInputArgumentCaptor().getValue()
115153
}

0 commit comments

Comments
 (0)