1 package org.openecomp.sdc.be.dao.jsongraph;
3 import org.janusgraph.core.JanusGraph;
4 import org.janusgraph.core.JanusGraphVertex;
6 import mockit.Deencapsulation;
7 import org.apache.commons.lang3.tuple.ImmutablePair;
8 import org.apache.tinkerpop.gremlin.structure.Direction;
9 import org.apache.tinkerpop.gremlin.structure.Edge;
10 import org.apache.tinkerpop.gremlin.structure.Element;
11 import org.apache.tinkerpop.gremlin.structure.Vertex;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.InjectMocks;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.MockitoAnnotations;
18 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
19 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
20 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
21 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
22 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
23 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
24 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
31 public class JanusGraphDaoMockTest {
34 JanusGraphDao testSubject;
37 JanusGraphClient janusGraphClient;
40 public void setUp() throws Exception {
41 MockitoAnnotations.initMocks(this);
45 public void testCommit() throws Exception {
46 JanusGraphOperationStatus result;
49 result = testSubject.commit();
53 public void testRollback() throws Exception {
54 JanusGraphOperationStatus result;
57 result = testSubject.rollback();
61 public void testGetGraph() throws Exception {
63 Either<JanusGraph, JanusGraphOperationStatus> result;
67 result = testSubject.getGraph();
71 public void testCreateVertex() throws Exception {
73 GraphVertex graphVertex = new GraphVertex();
74 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
75 Either<GraphVertex, JanusGraphOperationStatus> result;
77 JanusGraph tg = Mockito.mock(JanusGraph.class);
78 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
80 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
81 Mockito.when(tg.addVertex()).thenReturn(value2);
82 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
83 result = testSubject.createVertex(graphVertex);
87 public void testCreateVertexErrorGetGraph() throws Exception {
89 GraphVertex graphVertex = new GraphVertex();
90 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
91 Either<GraphVertex, JanusGraphOperationStatus> result;
93 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
95 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
96 result = testSubject.createVertex(graphVertex);
100 public void testCreateVertexException() throws Exception {
102 GraphVertex graphVertex = new GraphVertex();
103 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
104 Either<GraphVertex, JanusGraphOperationStatus> result;
106 JanusGraph tg = Mockito.mock(JanusGraph.class);
107 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
109 Mockito.when(tg.addVertex()).thenThrow(RuntimeException.class);
110 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
111 result = testSubject.createVertex(graphVertex);
115 public void testGetVertexByPropertyAndLabel() throws Exception {
116 Either<GraphVertex, JanusGraphOperationStatus> result;
119 Mockito.when(janusGraphClient.getGraph()).thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
120 result = testSubject.getVertexByPropertyAndLabel(GraphPropertyEnum.COMPONENT_TYPE, "mock",
121 VertexTypeEnum.ADDITIONAL_INFORMATION);
125 public void testGetFirstFoundVertex() throws Exception {
126 Iterable<JanusGraphVertex> vertices = Mockito.mock(Iterable.class);
127 Either<GraphVertex, JanusGraphOperationStatus> result;
129 Iterator<JanusGraphVertex> value = Mockito.mock(Iterator.class);
130 Mockito.when(vertices.iterator()).thenReturn(value);
131 Mockito.when(value.hasNext()).thenReturn(true);
132 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
133 Mockito.when(value.next()).thenReturn(value2);
136 result = Deencapsulation.invoke(testSubject, "getFirstFoundVertex", JsonParseFlagEnum.NoParse, vertices);
140 public void testGetFirstFoundVertexNotFound() throws Exception {
141 Iterable<JanusGraphVertex> vertices = Mockito.mock(Iterable.class);
142 Either<GraphVertex, JanusGraphOperationStatus> result;
144 Iterator<JanusGraphVertex> value = Mockito.mock(Iterator.class);
145 Mockito.when(vertices.iterator()).thenReturn(value);
146 Mockito.when(value.hasNext()).thenReturn(false);
147 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
148 Mockito.when(value.next()).thenReturn(value2);
151 result = Deencapsulation.invoke(testSubject, "getFirstFoundVertex", JsonParseFlagEnum.NoParse, vertices);
155 public void testGetVertexById_1Exception() throws Exception {
158 Either<GraphVertex, JanusGraphOperationStatus> result;
160 JanusGraph tg = Mockito.mock(JanusGraph.class);
161 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
163 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
164 Mockito.when(tg.addVertex()).thenReturn(value2);
165 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
168 result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
169 // Assert.assertEquals(null, result);
173 public void testGetVertexById_1GraphClosed() throws Exception {
176 Either<GraphVertex, JanusGraphOperationStatus> result;
179 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
181 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
182 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
185 result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
186 // Assert.assertEquals(null, result);
190 public void testSetVertexProperties_1() throws Exception {
191 Vertex vertex = Mockito.mock(Vertex.class);
192 Map<String, Object> properties = new HashMap<>();
193 properties.put("mock", "mock");
196 testSubject.setVertexProperties(vertex, properties);
200 public void testCreateAndFill() throws Exception {
202 JanusGraphVertex vertex = Mockito.mock(JanusGraphVertex.class);
203 JsonParseFlagEnum parseFlag = null;
208 result = Deencapsulation.invoke(testSubject, "createAndFill", vertex, JsonParseFlagEnum.NoParse);
212 public void testParseVertexProperties() throws Exception {
214 GraphVertex graphVertex = new GraphVertex();
215 JanusGraphVertex vertex = Mockito.mock(JanusGraphVertex.class);
216 graphVertex.setVertex(vertex);
217 JsonParseFlagEnum parseFlag = null;
221 testSubject.parseVertexProperties(graphVertex, JsonParseFlagEnum.NoParse);
226 public void testCreateEdge() throws Exception {
228 GraphVertex from = Mockito.mock(GraphVertex.class);
229 GraphVertex to = Mockito.mock(GraphVertex.class);
231 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
232 Mockito.when(from.getVertex()).thenReturn(value);
233 Mockito.when(to.getVertex()).thenReturn(value);
234 Map<EdgePropertyEnum, Object> properties = new HashMap<>();
235 JanusGraphOperationStatus result;
239 result = testSubject.createEdge(from, to, EdgeLabelEnum.ADDITIONAL_INFORMATION, properties);
240 from = new GraphVertex();
241 to = new GraphVertex();
242 result = testSubject.createEdge(from, to, EdgeLabelEnum.ADDITIONAL_INFORMATION, properties);
246 public void testSetEdgeProperties() throws Exception {
248 Element element = Mockito.mock(Element.class);
249 Map<EdgePropertyEnum, Object> properties = new HashMap<>();
253 properties.put(EdgePropertyEnum.STATE, "mock");
254 testSubject.setEdgeProperties(element, properties);
258 public void testGetByCriteria() throws Exception {
259 Map<GraphPropertyEnum, Object> props = new HashMap<>();
260 Either<List<GraphVertex>, JanusGraphOperationStatus> result;
262 JanusGraph tg = Mockito.mock(JanusGraph.class);
263 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
264 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
265 Mockito.when(tg.addVertex()).thenReturn(value2);
266 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
269 result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props);
273 public void testGetByCriteria_1() throws Exception {
275 Map<GraphPropertyEnum, Object> props = new HashMap<>();
276 Either<List<GraphVertex>, JanusGraphOperationStatus> result;
278 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
279 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
280 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
283 result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props, JsonParseFlagEnum.NoParse);
287 public void testGetCatalogVerticies() throws Exception {
288 Either<Iterator<Vertex>, JanusGraphOperationStatus> result;
290 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
292 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
293 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
296 result = testSubject.getCatalogOrArchiveVerticies(true);
300 public void testGetChildVertex() throws Exception {
302 GraphVertex parentVertex = new GraphVertex();
303 EdgeLabelEnum edgeLabel = null;
304 JsonParseFlagEnum parseFlag = null;
305 Either<GraphVertex, JanusGraphOperationStatus> result;
307 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
308 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
309 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
312 result = testSubject.getChildVertex(parentVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION, JsonParseFlagEnum.NoParse);
316 public void testGetChildVertex_1() throws Exception {
318 Vertex parentVertex = null;
319 EdgeLabelEnum edgeLabel = null;
320 JsonParseFlagEnum parseFlag = null;
321 Either<Vertex, JanusGraphOperationStatus> result;
323 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
324 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
325 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
328 result = testSubject.getChildVertex(parentVertex, edgeLabel, parseFlag);
333 public void testGetParentVertex_1() throws Exception {
335 Vertex parentVertex = null;
336 EdgeLabelEnum edgeLabel = null;
337 JsonParseFlagEnum parseFlag = null;
338 Either<Vertex, JanusGraphOperationStatus> result;
342 result = testSubject.getParentVertex(parentVertex, edgeLabel, parseFlag);
346 public void testGetParentVertecies_1() throws Exception {
348 Vertex parentVertex = null;
349 EdgeLabelEnum edgeLabel = null;
350 JsonParseFlagEnum parseFlag = null;
351 Either<List<Vertex>, JanusGraphOperationStatus> result;
355 result = testSubject.getParentVertecies(parentVertex, edgeLabel, parseFlag);
359 public void testGetAdjacentVerticies() throws Exception {
361 Vertex parentVertex = null;
362 EdgeLabelEnum edgeLabel = null;
363 JsonParseFlagEnum parseFlag = null;
364 Direction direction = null;
365 Either<List<Vertex>, JanusGraphOperationStatus> result;
367 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
368 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
369 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
371 result = Deencapsulation.invoke(testSubject, "getAdjacentVerticies",
372 new Object[] { Vertex.class, EdgeLabelEnum.class, JsonParseFlagEnum.class, Direction.class });
376 public void testGetChildrenVertecies_1() throws Exception {
378 Vertex parentVertex = null;
379 EdgeLabelEnum edgeLabel = null;
380 JsonParseFlagEnum parseFlag = null;
381 Either<List<Vertex>, JanusGraphOperationStatus> result;
385 result = testSubject.getChildrenVertecies(parentVertex, edgeLabel, parseFlag);
390 public void testDeleteBelongingEdgeByCriteria() throws Exception {
392 GraphVertex vertex = null;
393 EdgeLabelEnum label = null;
394 Map<GraphPropertyEnum, Object> properties = null;
395 Either<Edge, JanusGraphOperationStatus> result;
399 result = testSubject.deleteBelongingEdgeByCriteria(vertex, label, properties);
403 public void testDeleteEdge() throws Exception {
405 GraphVertex fromVertex = new GraphVertex();
406 GraphVertex toVertex = new GraphVertex();
407 Either<Edge, JanusGraphOperationStatus> result;
409 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
410 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
411 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
414 result = testSubject.deleteEdge(fromVertex, toVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION);
418 public void testDeleteEdgeByDirection() throws Exception {
419 GraphVertex fromVertex = new GraphVertex();
420 JanusGraphOperationStatus result;
423 result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
427 public void testDeleteEdgeByDirectionMock() throws Exception {
428 GraphVertex fromVertex = Mockito.mock(GraphVertex.class);
429 JanusGraphOperationStatus result;
431 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);;
432 Mockito.when(fromVertex.getVertex()).thenReturn(value);
433 Iterator<Edge> value2 = Mockito.mock(Iterator.class);;
434 Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(value2);
435 Mockito.when(value2.hasNext()).thenReturn(true, false);
436 Edge value3 = Mockito.mock(Edge.class);;
437 Mockito.when(value2.next()).thenReturn(value3);
439 result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
443 public void testUpdateVertex() throws Exception {
445 GraphVertex graphVertex = new GraphVertex();
446 Either<GraphVertex, JanusGraphOperationStatus> result;
450 result = testSubject.updateVertex(graphVertex);
454 public void testGetVerticesByUniqueIdAndParseFlag() throws Exception {
456 Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGet = new HashMap<>();
457 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> result;
460 result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
461 ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum> value3 = ImmutablePair.of(GraphPropertyEnum.COMPONENT_TYPE, JsonParseFlagEnum.NoParse);
462 verticesToGet.put("mock", value3);
464 result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
465 } catch (Exception e) {
466 // TODO Auto-generated catch block
472 public void testCreateEdge_2() throws Exception {
476 EdgeLabelEnum label = null;
477 Edge edgeToCopy = null;
478 JanusGraphOperationStatus result;
482 result = testSubject.createEdge(from, to, label, edgeToCopy);
487 public void testReplaceEdgeLabel() throws Exception {
489 Vertex fromVertex = null;
490 Vertex toVertex = null;
491 Edge prevEdge = null;
492 EdgeLabelEnum prevLabel = null;
493 EdgeLabelEnum newLabel = null;
494 JanusGraphOperationStatus result;
498 result = testSubject.replaceEdgeLabel(fromVertex, toVertex, prevEdge, prevLabel, newLabel);
502 public void testUpdateVertexMetadataPropertiesWithJson() throws Exception {
504 Vertex vertex = Mockito.mock(Vertex.class);;
505 Map<GraphPropertyEnum, Object> properties = new HashMap<>();
506 properties.put(GraphPropertyEnum.COMPONENT_TYPE, "mock");
507 JanusGraphOperationStatus result;
511 result = testSubject.updateVertexMetadataPropertiesWithJson(vertex, properties);
516 public void testDisassociateAndDeleteLast() throws Exception {
518 GraphVertex vertex = Mockito.mock(GraphVertex.class);
519 JanusGraphOperationStatus result;
521 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
522 Iterator<Edge> mockiter = Mockito.mock(Iterator.class);
523 Edge nextmock = Mockito.mock(Edge.class);
524 Mockito.when(vertex.getVertex()).thenReturn(value);
525 Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(mockiter);
526 Mockito.when(mockiter.hasNext()).thenReturn(true, false);
527 Mockito.when(mockiter.next()).thenReturn(nextmock);
528 Vertex secondVertex = Mockito.mock(Vertex.class);
529 Mockito.when(nextmock.outVertex()).thenReturn(secondVertex);
530 Mockito.when(nextmock.inVertex()).thenReturn(secondVertex);
531 Iterator<Edge> restOfEdges = Mockito.mock(Iterator.class);
532 Mockito.when(secondVertex.edges(Mockito.any(), Mockito.any())).thenReturn(restOfEdges);
533 Mockito.when(restOfEdges.hasNext()).thenReturn(false);
536 result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
540 public void testDisassociateAndDeleteLastOut() throws Exception {
542 GraphVertex vertex = Mockito.mock(GraphVertex.class);
543 JanusGraphOperationStatus result;
545 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
546 Iterator<Edge> mockiter = Mockito.mock(Iterator.class);
547 Edge nextmock = Mockito.mock(Edge.class);
548 Mockito.when(vertex.getVertex()).thenReturn(value);
549 Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(mockiter);
550 Mockito.when(mockiter.hasNext()).thenReturn(true, false);
551 Mockito.when(mockiter.next()).thenReturn(nextmock);
552 Vertex secondVertex = Mockito.mock(Vertex.class);
553 Mockito.when(nextmock.outVertex()).thenReturn(secondVertex);
554 Mockito.when(nextmock.inVertex()).thenReturn(secondVertex);
555 Iterator<Edge> restOfEdges = Mockito.mock(Iterator.class);
556 Mockito.when(secondVertex.edges(Mockito.any(), Mockito.any())).thenReturn(restOfEdges);
557 Mockito.when(restOfEdges.hasNext()).thenReturn(false);
560 result = testSubject.disassociateAndDeleteLast(vertex, Direction.IN, EdgeLabelEnum.ADDITIONAL_INFORMATION);
564 public void testDisassociateAndDeleteLastException() throws Exception {
566 GraphVertex vertex = Mockito.mock(GraphVertex.class);
567 JanusGraphOperationStatus result;
569 Mockito.when(vertex.getVertex()).thenThrow(RuntimeException.class);
572 result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
576 public void testMoveEdge() throws Exception {
578 GraphVertex vertexA = new GraphVertex();
579 GraphVertex vertexB = new GraphVertex();
580 JanusGraphOperationStatus result;
584 result = testSubject.moveEdge(vertexA, vertexB, EdgeLabelEnum.ADDITIONAL_INFORMATION, Direction.BOTH);