Skip to content

Commit 3588a59

Browse files
committed
Add spock and update existing tests.
1 parent f56485e commit 3588a59

File tree

5 files changed

+242
-237
lines changed

5 files changed

+242
-237
lines changed

build.gradle

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ plugins {
1414
}
1515

1616
apply plugin: 'java'
17-
compileJava {
18-
sourceCompatibility = '1.8'
19-
targetCompatibility = '1.8'
20-
}
17+
sourceCompatibility = '1.8'
18+
targetCompatibility = '1.8'
19+
20+
// Tests
21+
apply plugin: 'groovy'
2122

2223
repositories {
2324
mavenLocal()
@@ -31,8 +32,10 @@ dependencies {
3132
compile 'com.google.guava:guava:20.0'
3233

3334
// Unit testing
34-
testCompile 'org.testng:testng:6.9.10'
35-
testCompile 'org.mockito:mockito-core:1.10.19'
35+
testCompile "org.codehaus.groovy:groovy-all:2.4.1"
36+
testCompile "org.spockframework:spock-core:1.1-groovy-2.4-rc-3"
37+
testRuntime "cglib:cglib-nodep:3.2.4"
38+
testRuntime "org.objenesis:objenesis:2.5.1"
3639
testCompile 'org.slf4j:slf4j-simple:1.7.24'
3740

3841
// Remove boilerplate
@@ -166,8 +169,6 @@ bintray {
166169
}
167170
}
168171

169-
test.useTestNG()
170-
171172
idea {
172173
project {
173174
languageLevel = '1.8'
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.servlet
16+
17+
import graphql.annotations.GraphQLAnnotations
18+
import graphql.annotations.GraphQLField
19+
import graphql.annotations.GraphQLName
20+
import graphql.schema.GraphQLObjectType
21+
import graphql.schema.GraphQLSchema
22+
import lombok.SneakyThrows
23+
import spock.lang.Specification
24+
25+
class GraphQLVariablesSpec extends Specification {
26+
27+
static class ComplexQueryProvider implements GraphQLQueryProvider {
28+
29+
static class Data {
30+
@GraphQLField
31+
String field1
32+
@GraphQLField
33+
String field2
34+
}
35+
36+
static class DataInput {
37+
@GraphQLField
38+
String field1
39+
@GraphQLField
40+
String field2
41+
}
42+
43+
@GraphQLName("data")
44+
static class DataQuery {
45+
@GraphQLField
46+
Data echo(DataInput data) {
47+
return new Data(field1: data.field1, field2: data.field2)
48+
}
49+
}
50+
51+
@Override
52+
@SneakyThrows
53+
GraphQLObjectType getQuery() {
54+
return GraphQLAnnotations.object(DataQuery.class)
55+
}
56+
57+
@Override
58+
Object context() {
59+
return new DataQuery()
60+
}
61+
}
62+
63+
GraphQLSchema schema
64+
65+
def setup() {
66+
OsgiGraphQLServlet servlet = new OsgiGraphQLServlet()
67+
ComplexQueryProvider queryProvider = new ComplexQueryProvider()
68+
servlet.bindQueryProvider(queryProvider)
69+
schema = servlet.getSchema()
70+
}
71+
72+
private static final String QUERY = 'query Q($d: Data) { data { echo(data: $d) { field1 field2 } } }'
73+
74+
def "variables are automatically coerced into correct specific types"() {
75+
when:
76+
GraphQLVariables variables = new GraphQLVariables(schema, QUERY, [
77+
d: [
78+
field1: "1",
79+
field2: "2"
80+
]
81+
])
82+
Object d = variables.get("d")
83+
84+
then:
85+
d != null
86+
d instanceof ComplexQueryProvider.Data
87+
((ComplexQueryProvider.Data) d).getField1() == "1"
88+
((ComplexQueryProvider.Data) d).getField2() == "2"
89+
}
90+
91+
private static final String NON_NULL_QUERY = 'query Q($d: Data!) { data { echo(data: $d) { field1 field2 } } }'
92+
93+
def "non-null variables are automatically coerced into correct specific types"() {
94+
when:
95+
GraphQLVariables variables = new GraphQLVariables(schema, NON_NULL_QUERY, [
96+
d: [
97+
field1: "1",
98+
field2: "2"
99+
]
100+
])
101+
Object d = variables.get("d")
102+
103+
then:
104+
d != null
105+
d instanceof ComplexQueryProvider.Data
106+
((ComplexQueryProvider.Data) d).getField1() == "1"
107+
((ComplexQueryProvider.Data) d).getField2() == "2"
108+
}
109+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.servlet
16+
17+
import graphql.annotations.GraphQLAnnotations
18+
import graphql.annotations.GraphQLField
19+
import graphql.annotations.GraphQLName
20+
import graphql.schema.GraphQLFieldDefinition
21+
import graphql.schema.GraphQLObjectType
22+
import lombok.SneakyThrows
23+
import spock.lang.Specification
24+
25+
import static graphql.Scalars.GraphQLInt
26+
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
27+
28+
class OsgiGraphQLServletSpec extends Specification {
29+
30+
static class TestQueryProvider implements GraphQLQueryProvider {
31+
32+
@GraphQLName("query")
33+
static class Query {
34+
@GraphQLField
35+
public String field;
36+
}
37+
38+
@Override
39+
@SneakyThrows
40+
GraphQLObjectType getQuery() {
41+
return GraphQLAnnotations.object(Query.class);
42+
}
43+
44+
@Override
45+
Object context() {
46+
return new Query();
47+
}
48+
}
49+
50+
def "query provider adds query objects"() {
51+
setup:
52+
OsgiGraphQLServlet servlet = new OsgiGraphQLServlet()
53+
TestQueryProvider queryProvider = new TestQueryProvider()
54+
servlet.bindQueryProvider(queryProvider)
55+
GraphQLFieldDefinition query
56+
57+
when:
58+
query = servlet.getSchema().getQueryType().getFieldDefinition("query")
59+
then:
60+
query.getType().getName() == "query"
61+
62+
when:
63+
query = servlet.getReadOnlySchema().getQueryType().getFieldDefinition("query")
64+
then:
65+
query.getType().getName() == "query"
66+
67+
when:
68+
servlet.unbindQueryProvider(queryProvider)
69+
then:
70+
servlet.getSchema().getQueryType().getFieldDefinitions().isEmpty()
71+
servlet.getReadOnlySchema().getQueryType().getFieldDefinitions().isEmpty()
72+
}
73+
74+
static class TestMutationProvider implements GraphQLMutationProvider {
75+
@Override
76+
Collection<GraphQLFieldDefinition> getMutations() {
77+
return Collections.singletonList(newFieldDefinition().name("int").type(GraphQLInt).staticValue(1).build());
78+
}
79+
}
80+
81+
def "mutation provider adds mutation objects"() {
82+
setup:
83+
OsgiGraphQLServlet servlet = new OsgiGraphQLServlet();
84+
TestMutationProvider mutationProvider = new TestMutationProvider();
85+
86+
when:
87+
servlet.bindMutationProvider(mutationProvider)
88+
then:
89+
servlet.getSchema().getMutationType().getFieldDefinition("int").getType() == GraphQLInt
90+
servlet.getReadOnlySchema().getMutationType() == null
91+
92+
when:
93+
servlet.unbindMutationProvider(mutationProvider)
94+
then:
95+
servlet.getSchema().getMutationType() == null
96+
}
97+
98+
// @Test
99+
// @SneakyThrows
100+
// public void schema() {
101+
// OsgiGraphQLServlet servlet = new OsgiGraphQLServlet();
102+
//
103+
// HttpServletRequest req = mock(HttpServletRequest.class);
104+
// when(req.getPathInfo()).thenReturn("/schema.json");
105+
// HttpServletResponse resp = mock(HttpServletResponse.class);
106+
//
107+
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
108+
// PrintWriter writer = new PrintWriter(outputStream, true);
109+
// when(resp.getWriter()).thenReturn(writer);
110+
//
111+
// verify(resp, times(0)).setStatus(anyInt());
112+
//
113+
// servlet.bindQueryProvider(new TestQueryProvider());
114+
// servlet.doGet(req, resp);
115+
//
116+
// writer.flush();
117+
//
118+
// Map<String, Object> response = new ObjectMapper().readValue(outputStream.toByteArray(), new TypeReference<Map<String, Object>>() {
119+
// });
120+
// assertTrue(response.containsKey("data"));
121+
// assertFalse(response.containsKey("errors"));
122+
// }
123+
124+
}

src/test/java/graphql/servlet/GraphQLServletTest.java

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)