Improve testing stability
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / dao / janusgraph / JanusGraphDaoMockTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.dao.janusgraph;
22
23 import fj.data.Either;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import org.apache.commons.lang3.tuple.ImmutablePair;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.apache.tinkerpop.gremlin.structure.Edge;
31 import org.apache.tinkerpop.gremlin.structure.Element;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.janusgraph.core.JanusGraph;
34 import org.janusgraph.core.JanusGraphVertex;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
42 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
43 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
44 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
45 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
46 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
47 import org.openecomp.sdc.be.utils.DAOConfDependentTest;
48
49 class JanusGraphDaoMockTest extends DAOConfDependentTest {
50
51     @InjectMocks
52     private JanusGraphDao testSubject;
53
54     @Mock
55     private JanusGraphClient janusGraphClient;
56
57     @BeforeEach
58     void BeforeEach() throws Exception {
59         MockitoAnnotations.openMocks(this);
60     }
61
62     @Test
63     void testCommit() throws Exception {
64         JanusGraphOperationStatus result;
65
66         // default test
67         result = testSubject.commit();
68     }
69
70     @Test
71     void testRollback() throws Exception {
72         JanusGraphOperationStatus result;
73
74         // default test
75         result = testSubject.rollback();
76     }
77
78     @Test
79     void testGetGraph() throws Exception {
80
81         Either<JanusGraph, JanusGraphOperationStatus> result;
82
83         // default test
84
85         result = testSubject.getGraph();
86     }
87
88     @Test
89     void testCreateVertex() throws Exception {
90
91         GraphVertex graphVertex = new GraphVertex();
92         graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
93         Either<GraphVertex, JanusGraphOperationStatus> result;
94
95         JanusGraph tg = Mockito.mock(JanusGraph.class);
96         Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
97         // default test
98         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
99         Mockito.when(tg.addVertex()).thenReturn(value2);
100         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
101         result = testSubject.createVertex(graphVertex);
102     }
103
104     @Test
105     void testCreateVertexErrorGetGraph() throws Exception {
106
107         GraphVertex graphVertex = new GraphVertex();
108         graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
109         Either<GraphVertex, JanusGraphOperationStatus> result;
110
111         Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
112         // default test
113         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
114         result = testSubject.createVertex(graphVertex);
115     }
116
117     @Test
118     void testCreateVertexException() throws Exception {
119
120         GraphVertex graphVertex = new GraphVertex();
121         graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
122         Either<GraphVertex, JanusGraphOperationStatus> result;
123
124         JanusGraph tg = Mockito.mock(JanusGraph.class);
125         Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
126         // default test
127         Mockito.when(tg.addVertex()).thenThrow(RuntimeException.class);
128         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
129         result = testSubject.createVertex(graphVertex);
130     }
131
132     @Test
133     void testGetVertexByPropertyAndLabel() throws Exception {
134         Either<GraphVertex, JanusGraphOperationStatus> result;
135
136         // default test
137         Mockito.when(janusGraphClient.getGraph()).thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
138         result = testSubject.getVertexByPropertyAndLabel(GraphPropertyEnum.COMPONENT_TYPE, "mock",
139             VertexTypeEnum.ADDITIONAL_INFORMATION);
140     }
141
142     @Test
143     void testGetVertexById_1Exception() throws Exception {
144
145         String id = "mock";
146         Either<GraphVertex, JanusGraphOperationStatus> result;
147
148         JanusGraph tg = Mockito.mock(JanusGraph.class);
149         Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
150         // default test
151         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
152         Mockito.when(tg.addVertex()).thenReturn(value2);
153         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
154
155         // test 1
156         result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
157         // Assert.assertEquals(null, result);
158     }
159
160     @Test
161     void testGetVertexById_1GraphClosed() throws Exception {
162
163         String id = "mock";
164         Either<GraphVertex, JanusGraphOperationStatus> result;
165
166         Object b;
167         Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
168         // default test
169         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
170         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
171
172         // test 1
173         result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
174         // Assert.assertEquals(null, result);
175     }
176
177     @Test
178     void testSetVertexProperties_1() throws Exception {
179         Vertex vertex = Mockito.mock(Vertex.class);
180         Map<String, Object> properties = new HashMap<>();
181         properties.put("mock", "mock");
182
183         // default test
184         testSubject.setVertexProperties(vertex, properties);
185     }
186
187     @Test
188     void testParseVertexProperties() throws Exception {
189
190         GraphVertex graphVertex = new GraphVertex();
191         JanusGraphVertex vertex = Mockito.mock(JanusGraphVertex.class);
192         graphVertex.setVertex(vertex);
193         JsonParseFlagEnum parseFlag = null;
194
195         // default test
196
197         testSubject.parseVertexProperties(graphVertex, JsonParseFlagEnum.NoParse);
198     }
199
200     @Test
201     void testCreateEdge() throws Exception {
202
203         GraphVertex from = Mockito.mock(GraphVertex.class);
204         GraphVertex to = Mockito.mock(GraphVertex.class);
205
206         JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
207         Mockito.when(from.getVertex()).thenReturn(value);
208         Mockito.when(to.getVertex()).thenReturn(value);
209         Map<EdgePropertyEnum, Object> properties = new HashMap<>();
210         JanusGraphOperationStatus result;
211
212         // default test
213
214         result = testSubject.createEdge(from, to, EdgeLabelEnum.ADDITIONAL_INFORMATION, properties);
215         from = new GraphVertex();
216         to = new GraphVertex();
217         result = testSubject.createEdge(from, to, EdgeLabelEnum.ADDITIONAL_INFORMATION, properties);
218     }
219
220     @Test
221     void testSetEdgeProperties() throws Exception {
222
223         Element element = Mockito.mock(Element.class);
224         Map<EdgePropertyEnum, Object> properties = new HashMap<>();
225
226         // test 1
227
228         properties.put(EdgePropertyEnum.STATE, "mock");
229         testSubject.setEdgeProperties(element, properties);
230     }
231
232     @Test
233     void testGetByCriteria() throws Exception {
234         Map<GraphPropertyEnum, Object> props = new HashMap<>();
235         Either<List<GraphVertex>, JanusGraphOperationStatus> result;
236
237         JanusGraph tg = Mockito.mock(JanusGraph.class);
238         Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
239         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
240         Mockito.when(tg.addVertex()).thenReturn(value2);
241         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
242
243         // default test
244         result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props);
245     }
246
247     @Test
248     void testGetByCriteria_1() throws Exception {
249
250         Map<GraphPropertyEnum, Object> props = new HashMap<>();
251         Either<List<GraphVertex>, JanusGraphOperationStatus> result;
252
253         Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
254         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
255         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
256
257         // default test
258         result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props, JsonParseFlagEnum.NoParse);
259     }
260
261     @Test
262     void testGetCatalogVerticies() throws Exception {
263         Either<Iterator<Vertex>, JanusGraphOperationStatus> result;
264
265         Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
266         // default test
267         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
268         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
269
270         // default test
271         result = testSubject.getCatalogOrArchiveVerticies(true);
272     }
273
274     @Test
275     void testGetChildVertex() throws Exception {
276
277         GraphVertex parentVertex = new GraphVertex();
278         EdgeLabelEnum edgeLabel = null;
279         JsonParseFlagEnum parseFlag = null;
280         Either<GraphVertex, JanusGraphOperationStatus> result;
281
282         Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
283         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
284         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
285
286         // default test
287         result = testSubject.getChildVertex(parentVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION, JsonParseFlagEnum.NoParse);
288     }
289
290     @Test
291     void testGetChildVertex_1() throws Exception {
292
293         Vertex parentVertex = null;
294         EdgeLabelEnum edgeLabel = null;
295         JsonParseFlagEnum parseFlag = null;
296         Either<Vertex, JanusGraphOperationStatus> result;
297
298         Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
299         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
300         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
301
302         // default test
303         result = testSubject.getChildVertex(parentVertex, edgeLabel, parseFlag);
304     }
305
306     @Test
307     void testGetParentVertex_1() throws Exception {
308
309         Vertex parentVertex = null;
310         EdgeLabelEnum edgeLabel = null;
311         JsonParseFlagEnum parseFlag = null;
312         Either<Vertex, JanusGraphOperationStatus> result;
313
314         // default test
315
316         result = testSubject.getParentVertex(parentVertex, edgeLabel, parseFlag);
317     }
318
319     @Test
320     void testGetParentVertecies_1() throws Exception {
321
322         Vertex parentVertex = null;
323         EdgeLabelEnum edgeLabel = null;
324         JsonParseFlagEnum parseFlag = null;
325         Either<List<Vertex>, JanusGraphOperationStatus> result;
326
327         // default test
328
329         result = testSubject.getParentVertices(parentVertex, edgeLabel, parseFlag);
330     }
331
332     @Test
333     void testGetChildrenVertecies_1() throws Exception {
334
335         Vertex parentVertex = null;
336         EdgeLabelEnum edgeLabel = null;
337         JsonParseFlagEnum parseFlag = null;
338         Either<List<Vertex>, JanusGraphOperationStatus> result;
339
340         // default test
341
342         result = testSubject.getChildrenVertices(parentVertex, edgeLabel, parseFlag);
343     }
344
345     @Test
346     void testDeleteBelongingEdgeByCriteria() throws Exception {
347
348         GraphVertex vertex = null;
349         EdgeLabelEnum label = null;
350         Map<GraphPropertyEnum, Object> properties = null;
351         Either<Edge, JanusGraphOperationStatus> result;
352
353         // default test
354
355         result = testSubject.deleteBelongingEdgeByCriteria(vertex, label, properties);
356     }
357
358     @Test
359     void testDeleteEdge() throws Exception {
360
361         GraphVertex fromVertex = new GraphVertex();
362         GraphVertex toVertex = new GraphVertex();
363         Either<Edge, JanusGraphOperationStatus> result;
364
365         Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
366         JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
367         Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
368
369         // default test
370         result = testSubject.deleteEdge(fromVertex, toVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION);
371     }
372
373     @Test
374     void testDeleteEdgeByDirection() throws Exception {
375         GraphVertex fromVertex = new GraphVertex();
376         JanusGraphOperationStatus result;
377
378         // default test
379         result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
380     }
381
382     @Test
383     void testDeleteEdgeByDirectionMock() throws Exception {
384         GraphVertex fromVertex = Mockito.mock(GraphVertex.class);
385         JanusGraphOperationStatus result;
386
387         JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
388         ;
389         Mockito.when(fromVertex.getVertex()).thenReturn(value);
390         Iterator<Edge> value2 = Mockito.mock(Iterator.class);
391         ;
392         Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(value2);
393         Mockito.when(value2.hasNext()).thenReturn(true, false);
394         Edge value3 = Mockito.mock(Edge.class);
395         ;
396         Mockito.when(value2.next()).thenReturn(value3);
397         // default test
398         result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
399     }
400
401     @Test
402     void testUpdateVertex() throws Exception {
403
404         GraphVertex graphVertex = new GraphVertex();
405         Either<GraphVertex, JanusGraphOperationStatus> result;
406
407         // default test
408
409         result = testSubject.updateVertex(graphVertex);
410     }
411
412     @Test
413     void testGetVerticesByUniqueIdAndParseFlag() throws Exception {
414
415         Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGet = new HashMap<>();
416         Either<Map<String, GraphVertex>, JanusGraphOperationStatus> result;
417
418         // default test
419         result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
420         ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum> value3 = ImmutablePair.of(GraphPropertyEnum.COMPONENT_TYPE, JsonParseFlagEnum.NoParse);
421         verticesToGet.put("mock", value3);
422         try {
423             result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
424         } catch (Exception e) {
425             // TODO Auto-generated catch block
426             e.printStackTrace();
427         }
428     }
429
430     @Test
431     void testCreateEdge_2() throws Exception {
432
433         Vertex from = null;
434         Vertex to = null;
435         EdgeLabelEnum label = null;
436         Edge edgeToCopy = null;
437         JanusGraphOperationStatus result;
438
439         // default test
440
441         result = testSubject.createEdge(from, to, label, edgeToCopy);
442     }
443
444     @Test
445     void testReplaceEdgeLabel() throws Exception {
446
447         Vertex fromVertex = null;
448         Vertex toVertex = null;
449         Edge prevEdge = null;
450         EdgeLabelEnum prevLabel = null;
451         EdgeLabelEnum newLabel = null;
452         JanusGraphOperationStatus result;
453
454         // default test
455
456         result = testSubject.replaceEdgeLabel(fromVertex, toVertex, prevEdge, prevLabel, newLabel);
457     }
458
459     @Test
460     void testUpdateVertexMetadataPropertiesWithJson() throws Exception {
461
462         Vertex vertex = Mockito.mock(Vertex.class);
463         ;
464         Map<GraphPropertyEnum, Object> properties = new HashMap<>();
465         properties.put(GraphPropertyEnum.COMPONENT_TYPE, "mock");
466         JanusGraphOperationStatus result;
467
468         // default test
469
470         result = testSubject.updateVertexMetadataPropertiesWithJson(vertex, properties);
471     }
472
473     //TODO Last
474     @Test
475     void testDisassociateAndDeleteLast() throws Exception {
476
477         GraphVertex vertex = Mockito.mock(GraphVertex.class);
478         JanusGraphOperationStatus result;
479
480         JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
481         Iterator<Edge> mockiter = Mockito.mock(Iterator.class);
482         Edge nextmock = Mockito.mock(Edge.class);
483         Mockito.when(vertex.getVertex()).thenReturn(value);
484         Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(mockiter);
485         Mockito.when(mockiter.hasNext()).thenReturn(true, false);
486         Mockito.when(mockiter.next()).thenReturn(nextmock);
487         Vertex secondVertex = Mockito.mock(Vertex.class);
488         Mockito.when(nextmock.outVertex()).thenReturn(secondVertex);
489         Mockito.when(nextmock.inVertex()).thenReturn(secondVertex);
490         Iterator<Edge> restOfEdges = Mockito.mock(Iterator.class);
491         Mockito.when(secondVertex.edges(Mockito.any(), Mockito.any())).thenReturn(restOfEdges);
492         Mockito.when(restOfEdges.hasNext()).thenReturn(false);
493
494         // default test
495         result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
496     }
497
498     @Test
499     void testDisassociateAndDeleteLastOut() throws Exception {
500
501         GraphVertex vertex = Mockito.mock(GraphVertex.class);
502         JanusGraphOperationStatus result;
503
504         JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
505         Iterator<Edge> mockiter = Mockito.mock(Iterator.class);
506         Edge nextmock = Mockito.mock(Edge.class);
507         Mockito.when(vertex.getVertex()).thenReturn(value);
508         Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(mockiter);
509         Mockito.when(mockiter.hasNext()).thenReturn(true, false);
510         Mockito.when(mockiter.next()).thenReturn(nextmock);
511         Vertex secondVertex = Mockito.mock(Vertex.class);
512         Mockito.when(nextmock.outVertex()).thenReturn(secondVertex);
513         Mockito.when(nextmock.inVertex()).thenReturn(secondVertex);
514         Iterator<Edge> restOfEdges = Mockito.mock(Iterator.class);
515         Mockito.when(secondVertex.edges(Mockito.any(), Mockito.any())).thenReturn(restOfEdges);
516         Mockito.when(restOfEdges.hasNext()).thenReturn(false);
517
518         // default test
519         result = testSubject.disassociateAndDeleteLast(vertex, Direction.IN, EdgeLabelEnum.ADDITIONAL_INFORMATION);
520     }
521
522     @Test
523     void testDisassociateAndDeleteLastException() throws Exception {
524
525         GraphVertex vertex = Mockito.mock(GraphVertex.class);
526         JanusGraphOperationStatus result;
527
528         Mockito.when(vertex.getVertex()).thenThrow(RuntimeException.class);
529
530         // default test
531         result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
532     }
533
534     @Test
535     void testMoveEdge() throws Exception {
536
537         GraphVertex vertexA = new GraphVertex();
538         GraphVertex vertexB = new GraphVertex();
539         JanusGraphOperationStatus result;
540
541         // default test
542
543         result = testSubject.moveEdge(vertexA, vertexB, EdgeLabelEnum.ADDITIONAL_INFORMATION, Direction.BOTH);
544     }
545 }