forked from Enigmatis/graphql-java-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLIterableAndArrayTest.java
More file actions
147 lines (119 loc) · 5.55 KB
/
GraphQLIterableAndArrayTest.java
File metadata and controls
147 lines (119 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Copyright 2016 Yurii Rashkovskii
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.annotations;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.annotations.annotationTypes.GraphQLDataFetcher;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLSchema;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class GraphQLIterableAndArrayTest {
private GraphQLAnnotations graphQLAnnotations;
@BeforeMethod
public void init() {
this.graphQLAnnotations = new GraphQLAnnotations();
}
static class TestMappedObject {
@GraphQLField
public String name;
@GraphQLField
public String foo;
}
static class TestObjectDB {
private String foo;
private String name;
TestObjectDB(String name, String foo) {
this.name = name;
this.foo = foo;
}
}
public static class IterableAndArrayTestQuery {
@GraphQLField
@GraphQLDataFetcher(ArrayFetcher.class)
public TestMappedObject[] array;
@GraphQLField
@GraphQLDataFetcher(ListFetcher.class)
public List<TestMappedObject> list;
@GraphQLField
@GraphQLDataFetcher(ArrayWithinArrayFetcher.class)
public TestMappedObject[][] arrayWithinArray;
}
public static class ArrayFetcher implements DataFetcher<TestObjectDB[]> {
@Override
public TestObjectDB[] get(DataFetchingEnvironment environment) {
return new TestObjectDB[]{new TestObjectDB("hello", "world")};
}
}
public static class ListFetcher implements DataFetcher<List<TestObjectDB>> {
@Override
public List<TestObjectDB> get(DataFetchingEnvironment environment) {
return Collections.singletonList(new TestObjectDB("test name", "test foo"));
}
}
public static class ArrayWithinArrayFetcher implements DataFetcher<TestObjectDB[][]> {
@Override
public TestObjectDB[][] get(DataFetchingEnvironment environment) {
return new TestObjectDB[][]{
{new TestObjectDB("hello", "world")},
{new TestObjectDB("a", "b"), new TestObjectDB("c", "d")}
};
}
}
@Test
public void queryWithArray() {
GraphQLSchema schema = newAnnotationsSchema().query(IterableAndArrayTestQuery.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{array {name foo}}");
assertTrue(result.getErrors().isEmpty());
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "array", 0)).get("name"), "hello");
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "array", 0)).get("foo"), "world");
}
@Test
public void queryWithList() {
GraphQLSchema schema = newAnnotationsSchema().query(IterableAndArrayTestQuery.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{list {name foo}}");
assertTrue(result.getErrors().isEmpty());
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "list", 0)).get("name"), "test name");
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "list", 0)).get("foo"), "test foo");
}
@Test
public void queryWithArrayWithinAnArray() {
GraphQLSchema schema = newAnnotationsSchema().query(IterableAndArrayTestQuery.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{arrayWithinArray {name foo}}");
assertTrue(result.getErrors().isEmpty());
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 0, 0)).get("name"), "hello");
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 0, 0)).get("foo"), "world");
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 0)).get("name"), "a");
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 0)).get("foo"), "b");
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 1)).get("name"), "c");
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 1)).get("foo"), "d");
}
private Object getQueryResultAtIndex(ExecutionResult result, String queryName, int index) {
return ((List) (((LinkedHashMap) result.getData()).get(queryName))).get(index);
}
private Object getQueryResultAtCell(ExecutionResult result, String queryName, int rowIndex, int columnIndex) {
return (((List) (getQueryResultAtIndex(result, queryName, rowIndex))).get(columnIndex));
}
}