Skip to content

Commit 7e14584

Browse files
committed
Adding tests for Database Creation
1 parent 3a87bb7 commit 7e14584

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
package com.arangodb.graphql.create;
2+
3+
import com.arangodb.ArangoCollection;
4+
import com.arangodb.ArangoDB;
5+
import com.arangodb.ArangoDatabase;
6+
import com.arangodb.entity.CollectionType;
7+
import com.arangodb.model.CollectionCreateOptions;
8+
import com.arangodb.model.HashIndexOptions;
9+
import graphql.schema.*;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.mockito.ArgumentCaptor;
14+
import org.mockito.ArgumentMatchers;
15+
import org.mockito.Mock;
16+
import org.mockito.junit.MockitoJUnitRunner;
17+
18+
import java.util.Arrays;
19+
20+
import static graphql.Scalars.GraphQLString;
21+
import static org.hamcrest.Matchers.*;
22+
import static org.hamcrest.core.IsEqual.equalTo;
23+
import static org.junit.Assert.*;
24+
import static org.mockito.Mockito.*;
25+
26+
@RunWith(MockitoJUnitRunner.class)
27+
public class DatabaseObjectCreatorTest {
28+
29+
@Mock
30+
private ArangoDB arangoDB;
31+
32+
@Mock
33+
private GraphQLSchema graphQLSchema;
34+
35+
@Mock
36+
private ArangoDatabase arangoDatabase;
37+
38+
@Mock
39+
private GraphQLObjectType mockType;
40+
41+
@Mock
42+
private GraphQLDirective mockDirective;
43+
44+
@Mock
45+
private GraphQLDirective mockEdgeDirective;
46+
47+
@Mock
48+
private GraphQLArgument mockArgument;
49+
50+
@Mock
51+
private ArangoCollection mockCollection;
52+
53+
@Mock
54+
private ArangoCollection mockEdgeCollection;
55+
56+
@Mock
57+
private GraphQLFieldDefinition mockFieldDefinition;
58+
59+
@Mock
60+
private GraphQLArgument mockEdgeArgument;
61+
62+
@Mock
63+
private GraphQLObjectType mockQueryType;
64+
65+
@Mock
66+
private GraphQLFieldDefinition mockQueryFieldDefinition;
67+
68+
@Mock
69+
private GraphQLArgument mockMandatoryArgument;
70+
71+
@Mock
72+
private GraphQLArgument mockOptionalArgument;
73+
74+
private String databaseName = "myDatabase";
75+
76+
private String collectionName = "myCollection";
77+
78+
private String edgeCollectionName = "myEdgeCollection";
79+
80+
private DatabaseObjectCreator databaseObjectCreator;
81+
private String mandatoryArgumentName = "mandatoryArg";
82+
private String optionalArgumentName = "optionalArg";
83+
84+
private void attachVertexDirectiveToMockType() {
85+
when(mockType.getDirective("vertex")).thenReturn(mockDirective);
86+
when(mockDirective.getArgument("collection")).thenReturn(mockArgument);
87+
when(mockArgument.getValue()).thenReturn(collectionName);
88+
}
89+
90+
private void attachEdgeDirectiveToMockType() {
91+
attachVertexDirectiveToMockType();
92+
93+
when(mockFieldDefinition.getDirective("edge")).thenReturn(mockEdgeDirective);
94+
when(mockEdgeDirective.getArgument("collection")).thenReturn(mockEdgeArgument);
95+
when(mockEdgeArgument.getValue()).thenReturn(edgeCollectionName);
96+
}
97+
98+
@Before
99+
public void setup() {
100+
//Arango
101+
when(arangoDB.db(databaseName)).thenReturn(arangoDatabase);
102+
when(arangoDatabase.collection(collectionName)).thenReturn(mockCollection);
103+
when(arangoDatabase.collection(edgeCollectionName)).thenReturn(mockEdgeCollection);
104+
105+
//GraphQL Schema
106+
when(graphQLSchema.getAllTypesAsList()).thenReturn(Arrays.asList(mockType));
107+
when(graphQLSchema.getQueryType()).thenReturn(mockQueryType);
108+
when(mockQueryType.getFieldDefinitions()).thenReturn(Arrays.asList(mockQueryFieldDefinition));
109+
when(mockQueryFieldDefinition.getType()).thenReturn(mockType);
110+
when(mockMandatoryArgument.getName()).thenReturn(mandatoryArgumentName);
111+
when(mockMandatoryArgument.getType()).thenReturn(new GraphQLNonNull(GraphQLString));
112+
when(mockOptionalArgument.getName()).thenReturn(optionalArgumentName);
113+
114+
115+
//Type Setup
116+
when(mockType.getFieldDefinitions()).thenReturn(Arrays.asList(mockFieldDefinition));
117+
118+
databaseObjectCreator = new DatabaseObjectCreator(arangoDB, databaseName, graphQLSchema);
119+
}
120+
121+
@Test
122+
public void createDatabase() {
123+
when(arangoDatabase.exists()).thenReturn(false);
124+
databaseObjectCreator.createDatabase();
125+
verify(arangoDatabase).create();
126+
}
127+
128+
@Test
129+
public void doNotCreateExistingDatabase() {
130+
when(arangoDatabase.exists()).thenReturn(true);
131+
databaseObjectCreator.createDatabase();
132+
verify(arangoDatabase, never()).create();
133+
}
134+
135+
@Test
136+
public void createCollectionForVertexDirectiveType() {
137+
attachVertexDirectiveToMockType();
138+
when(mockCollection.exists()).thenReturn(false);
139+
140+
databaseObjectCreator.createCollections();
141+
142+
ArgumentCaptor<CollectionCreateOptions> captor = ArgumentCaptor.forClass(CollectionCreateOptions.class);
143+
verify(mockCollection).create(captor.capture());
144+
assertThat(captor.getValue().getType(), equalTo(CollectionType.DOCUMENT));
145+
146+
}
147+
148+
@Test
149+
public void doNotCreateExistingCollectionForVertexDirectiveType() {
150+
attachVertexDirectiveToMockType();
151+
when(mockCollection.exists()).thenReturn(true);
152+
153+
databaseObjectCreator.createCollections();
154+
155+
verify(mockCollection, never()).create(ArgumentMatchers.any(CollectionCreateOptions.class));
156+
157+
}
158+
159+
@Test
160+
public void doNotCreateCollectionForTypeWithNoDirective() {
161+
162+
databaseObjectCreator.createCollections();
163+
verify(mockCollection, never()).create(ArgumentMatchers.any(CollectionCreateOptions.class));
164+
165+
}
166+
167+
@Test
168+
public void createCollectionForEdgeDirectiveType() {
169+
attachEdgeDirectiveToMockType();
170+
171+
when(mockEdgeCollection.exists()).thenReturn(false);
172+
173+
databaseObjectCreator.createCollections();
174+
175+
ArgumentCaptor<CollectionCreateOptions> captor = ArgumentCaptor.forClass(CollectionCreateOptions.class);
176+
verify(mockEdgeCollection).create(captor.capture());
177+
assertThat(captor.getValue().getType(), equalTo(CollectionType.EDGES));
178+
179+
}
180+
181+
@Test
182+
public void doNotCreateExistingCollectionForEdgeDirectiveType() {
183+
attachEdgeDirectiveToMockType();
184+
185+
when(mockEdgeCollection.exists()).thenReturn(true);
186+
187+
databaseObjectCreator.createCollections();
188+
189+
verify(mockEdgeCollection, never()).create(ArgumentMatchers.any(CollectionCreateOptions.class));
190+
191+
}
192+
193+
@Test
194+
public void doNotCreateCollectionForFieldWithNoEdgeDirectiveType() {
195+
attachVertexDirectiveToMockType();
196+
197+
databaseObjectCreator.createCollections();
198+
199+
verify(mockEdgeCollection, never()).create(ArgumentMatchers.any(CollectionCreateOptions.class));
200+
201+
}
202+
203+
@Test
204+
public void createIndexes() {
205+
attachVertexDirectiveToMockType();
206+
when(mockQueryFieldDefinition.getArguments()).thenReturn(Arrays.asList(mockMandatoryArgument, mockOptionalArgument));
207+
208+
databaseObjectCreator.createIndexes();
209+
210+
ArgumentCaptor<Iterable<String>> captor = ArgumentCaptor.forClass(Iterable.class);
211+
212+
verify(mockCollection).ensureHashIndex(captor.capture(), ArgumentMatchers.any(HashIndexOptions.class));
213+
214+
assertThat(captor.getValue(), contains(mandatoryArgumentName, optionalArgumentName));
215+
216+
}
217+
218+
@Test
219+
public void createIndexesWithOptionalFirstArg() {
220+
attachVertexDirectiveToMockType();
221+
when(mockQueryFieldDefinition.getArguments()).thenReturn(Arrays.asList(mockOptionalArgument, mockMandatoryArgument));
222+
223+
databaseObjectCreator.createIndexes();
224+
225+
ArgumentCaptor<Iterable<String>> captor = ArgumentCaptor.forClass(Iterable.class);
226+
227+
verify(mockCollection).ensureHashIndex(captor.capture(), ArgumentMatchers.any(HashIndexOptions.class));
228+
229+
assertThat(captor.getValue(), contains(mandatoryArgumentName, optionalArgumentName));
230+
231+
}
232+
233+
@Test
234+
public void createIndexesWithNoMandatoryArgs() {
235+
attachVertexDirectiveToMockType();
236+
when(mockQueryFieldDefinition.getArguments()).thenReturn(Arrays.asList(mockOptionalArgument));
237+
238+
databaseObjectCreator.createIndexes();
239+
240+
ArgumentCaptor<Iterable<String>> captor = ArgumentCaptor.forClass(Iterable.class);
241+
242+
verify(mockCollection).ensureHashIndex(captor.capture(), ArgumentMatchers.any(HashIndexOptions.class));
243+
244+
assertThat(captor.getValue(), hasItem(optionalArgumentName));
245+
246+
}
247+
248+
249+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.arangodb.graphql.create;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
6+
import static org.hamcrest.core.IsEqual.equalTo;
7+
import static org.junit.Assert.*;
8+
9+
public class TypeCollectionMappingTest {
10+
11+
@Test
12+
public void addEdgeCollection() {
13+
final String testVertexCollection = "banana";
14+
final String testEdgeCollection = "apple";
15+
TypeCollectionMapping mapping = new TypeCollectionMapping(testVertexCollection);
16+
mapping.addEdgeCollection(testEdgeCollection);
17+
assertThat(mapping.getEdgeCollections(), contains(testEdgeCollection));
18+
assertThat(mapping.getEdgeCollections().size(), equalTo(1));
19+
}
20+
21+
@Test
22+
public void vertexCollection() {
23+
final String testVertexCollection = "banana";
24+
TypeCollectionMapping mapping = new TypeCollectionMapping(testVertexCollection);
25+
assertThat(mapping.getVertexCollection(), equalTo(testVertexCollection));
26+
}
27+
}

0 commit comments

Comments
 (0)