2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.dao.jsongraph;
23 import org.janusgraph.core.JanusGraph;
24 import org.janusgraph.core.JanusGraphVertex;
25 import fj.data.Either;
26 import mockit.Deencapsulation;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.apache.tinkerpop.gremlin.structure.Direction;
29 import org.apache.tinkerpop.gremlin.structure.Edge;
30 import org.apache.tinkerpop.gremlin.structure.Element;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
39 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
40 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
41 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
42 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
43 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
44 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.List;
51 public class JanusGraphDaoMockTest {
54 JanusGraphDao testSubject;
57 JanusGraphClient janusGraphClient;
60 public void setUp() throws Exception {
61 MockitoAnnotations.initMocks(this);
65 public void testCommit() throws Exception {
66 JanusGraphOperationStatus result;
69 result = testSubject.commit();
73 public void testRollback() throws Exception {
74 JanusGraphOperationStatus result;
77 result = testSubject.rollback();
81 public void testGetGraph() throws Exception {
83 Either<JanusGraph, JanusGraphOperationStatus> result;
87 result = testSubject.getGraph();
91 public void testCreateVertex() throws Exception {
93 GraphVertex graphVertex = new GraphVertex();
94 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
95 Either<GraphVertex, JanusGraphOperationStatus> result;
97 JanusGraph tg = Mockito.mock(JanusGraph.class);
98 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
100 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
101 Mockito.when(tg.addVertex()).thenReturn(value2);
102 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
103 result = testSubject.createVertex(graphVertex);
107 public void testCreateVertexErrorGetGraph() throws Exception {
109 GraphVertex graphVertex = new GraphVertex();
110 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
111 Either<GraphVertex, JanusGraphOperationStatus> result;
113 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
115 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
116 result = testSubject.createVertex(graphVertex);
120 public void testCreateVertexException() throws Exception {
122 GraphVertex graphVertex = new GraphVertex();
123 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
124 Either<GraphVertex, JanusGraphOperationStatus> result;
126 JanusGraph tg = Mockito.mock(JanusGraph.class);
127 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
129 Mockito.when(tg.addVertex()).thenThrow(RuntimeException.class);
130 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
131 result = testSubject.createVertex(graphVertex);
135 public void testGetVertexByPropertyAndLabel() throws Exception {
136 Either<GraphVertex, JanusGraphOperationStatus> result;
139 Mockito.when(janusGraphClient.getGraph()).thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
140 result = testSubject.getVertexByPropertyAndLabel(GraphPropertyEnum.COMPONENT_TYPE, "mock",
141 VertexTypeEnum.ADDITIONAL_INFORMATION);
145 public void testGetFirstFoundVertex() throws Exception {
146 Iterable<JanusGraphVertex> vertices = Mockito.mock(Iterable.class);
147 Either<GraphVertex, JanusGraphOperationStatus> result;
149 Iterator<JanusGraphVertex> value = Mockito.mock(Iterator.class);
150 Mockito.when(vertices.iterator()).thenReturn(value);
151 Mockito.when(value.hasNext()).thenReturn(true);
152 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
153 Mockito.when(value.next()).thenReturn(value2);
156 result = Deencapsulation.invoke(testSubject, "getFirstFoundVertex", JsonParseFlagEnum.NoParse, vertices);
160 public void testGetFirstFoundVertexNotFound() throws Exception {
161 Iterable<JanusGraphVertex> vertices = Mockito.mock(Iterable.class);
162 Either<GraphVertex, JanusGraphOperationStatus> result;
164 Iterator<JanusGraphVertex> value = Mockito.mock(Iterator.class);
165 Mockito.when(vertices.iterator()).thenReturn(value);
166 Mockito.when(value.hasNext()).thenReturn(false);
167 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
168 Mockito.when(value.next()).thenReturn(value2);
171 result = Deencapsulation.invoke(testSubject, "getFirstFoundVertex", JsonParseFlagEnum.NoParse, vertices);
175 public void testGetVertexById_1Exception() throws Exception {
178 Either<GraphVertex, JanusGraphOperationStatus> result;
180 JanusGraph tg = Mockito.mock(JanusGraph.class);
181 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
183 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
184 Mockito.when(tg.addVertex()).thenReturn(value2);
185 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
188 result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
189 // Assert.assertEquals(null, result);
193 public void testGetVertexById_1GraphClosed() throws Exception {
196 Either<GraphVertex, JanusGraphOperationStatus> result;
199 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
201 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
202 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
205 result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
206 // Assert.assertEquals(null, result);
210 public void testSetVertexProperties_1() throws Exception {
211 Vertex vertex = Mockito.mock(Vertex.class);
212 Map<String, Object> properties = new HashMap<>();
213 properties.put("mock", "mock");
216 testSubject.setVertexProperties(vertex, properties);
220 public void testCreateAndFill() throws Exception {
222 JanusGraphVertex vertex = Mockito.mock(JanusGraphVertex.class);
223 JsonParseFlagEnum parseFlag = null;
228 result = Deencapsulation.invoke(testSubject, "createAndFill", vertex, JsonParseFlagEnum.NoParse);
232 public void testParseVertexProperties() throws Exception {
234 GraphVertex graphVertex = new GraphVertex();
235 JanusGraphVertex vertex = Mockito.mock(JanusGraphVertex.class);
236 graphVertex.setVertex(vertex);
237 JsonParseFlagEnum parseFlag = null;
241 testSubject.parseVertexProperties(graphVertex, JsonParseFlagEnum.NoParse);
246 public void testCreateEdge() throws Exception {
248 GraphVertex from = Mockito.mock(GraphVertex.class);
249 GraphVertex to = Mockito.mock(GraphVertex.class);
251 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
252 Mockito.when(from.getVertex()).thenReturn(value);
253 Mockito.when(to.getVertex()).thenReturn(value);
254 Map<EdgePropertyEnum, Object> properties = new HashMap<>();
255 JanusGraphOperationStatus result;
259 result = testSubject.createEdge(from, to, EdgeLabelEnum.ADDITIONAL_INFORMATION, properties);
260 from = new GraphVertex();
261 to = new GraphVertex();
262 result = testSubject.createEdge(from, to, EdgeLabelEnum.ADDITIONAL_INFORMATION, properties);
266 public void testSetEdgeProperties() throws Exception {
268 Element element = Mockito.mock(Element.class);
269 Map<EdgePropertyEnum, Object> properties = new HashMap<>();
273 properties.put(EdgePropertyEnum.STATE, "mock");
274 testSubject.setEdgeProperties(element, properties);
278 public void testGetByCriteria() throws Exception {
279 Map<GraphPropertyEnum, Object> props = new HashMap<>();
280 Either<List<GraphVertex>, JanusGraphOperationStatus> result;
282 JanusGraph tg = Mockito.mock(JanusGraph.class);
283 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
284 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
285 Mockito.when(tg.addVertex()).thenReturn(value2);
286 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
289 result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props);
293 public void testGetByCriteria_1() throws Exception {
295 Map<GraphPropertyEnum, Object> props = new HashMap<>();
296 Either<List<GraphVertex>, JanusGraphOperationStatus> result;
298 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
299 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
300 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
303 result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props, JsonParseFlagEnum.NoParse);
307 public void testGetCatalogVerticies() throws Exception {
308 Either<Iterator<Vertex>, JanusGraphOperationStatus> result;
310 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
312 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
313 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
316 result = testSubject.getCatalogOrArchiveVerticies(true);
320 public void testGetChildVertex() throws Exception {
322 GraphVertex parentVertex = new GraphVertex();
323 EdgeLabelEnum edgeLabel = null;
324 JsonParseFlagEnum parseFlag = null;
325 Either<GraphVertex, JanusGraphOperationStatus> result;
327 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
328 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
329 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
332 result = testSubject.getChildVertex(parentVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION, JsonParseFlagEnum.NoParse);
336 public void testGetChildVertex_1() throws Exception {
338 Vertex parentVertex = null;
339 EdgeLabelEnum edgeLabel = null;
340 JsonParseFlagEnum parseFlag = null;
341 Either<Vertex, JanusGraphOperationStatus> result;
343 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
344 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
345 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
348 result = testSubject.getChildVertex(parentVertex, edgeLabel, parseFlag);
353 public void testGetParentVertex_1() throws Exception {
355 Vertex parentVertex = null;
356 EdgeLabelEnum edgeLabel = null;
357 JsonParseFlagEnum parseFlag = null;
358 Either<Vertex, JanusGraphOperationStatus> result;
362 result = testSubject.getParentVertex(parentVertex, edgeLabel, parseFlag);
366 public void testGetParentVertecies_1() throws Exception {
368 Vertex parentVertex = null;
369 EdgeLabelEnum edgeLabel = null;
370 JsonParseFlagEnum parseFlag = null;
371 Either<List<Vertex>, JanusGraphOperationStatus> result;
375 result = testSubject.getParentVertices(parentVertex, edgeLabel, parseFlag);
379 public void testGetAdjacentVerticies() throws Exception {
381 Vertex parentVertex = null;
382 EdgeLabelEnum edgeLabel = null;
383 JsonParseFlagEnum parseFlag = null;
384 Direction direction = null;
385 Either<List<Vertex>, JanusGraphOperationStatus> result;
387 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
388 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
389 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
391 result = Deencapsulation.invoke(testSubject, "getAdjacentVertices",
392 new Object[] { Vertex.class, EdgeLabelEnum.class, JsonParseFlagEnum.class, Direction.class });
396 public void testGetChildrenVertecies_1() throws Exception {
398 Vertex parentVertex = null;
399 EdgeLabelEnum edgeLabel = null;
400 JsonParseFlagEnum parseFlag = null;
401 Either<List<Vertex>, JanusGraphOperationStatus> result;
405 result = testSubject.getChildrenVertices(parentVertex, edgeLabel, parseFlag);
410 public void testDeleteBelongingEdgeByCriteria() throws Exception {
412 GraphVertex vertex = null;
413 EdgeLabelEnum label = null;
414 Map<GraphPropertyEnum, Object> properties = null;
415 Either<Edge, JanusGraphOperationStatus> result;
419 result = testSubject.deleteBelongingEdgeByCriteria(vertex, label, properties);
423 public void testDeleteEdge() throws Exception {
425 GraphVertex fromVertex = new GraphVertex();
426 GraphVertex toVertex = new GraphVertex();
427 Either<Edge, JanusGraphOperationStatus> result;
429 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
430 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
431 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
434 result = testSubject.deleteEdge(fromVertex, toVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION);
438 public void testDeleteEdgeByDirection() throws Exception {
439 GraphVertex fromVertex = new GraphVertex();
440 JanusGraphOperationStatus result;
443 result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
447 public void testDeleteEdgeByDirectionMock() throws Exception {
448 GraphVertex fromVertex = Mockito.mock(GraphVertex.class);
449 JanusGraphOperationStatus result;
451 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);;
452 Mockito.when(fromVertex.getVertex()).thenReturn(value);
453 Iterator<Edge> value2 = Mockito.mock(Iterator.class);;
454 Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(value2);
455 Mockito.when(value2.hasNext()).thenReturn(true, false);
456 Edge value3 = Mockito.mock(Edge.class);;
457 Mockito.when(value2.next()).thenReturn(value3);
459 result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
463 public void testUpdateVertex() throws Exception {
465 GraphVertex graphVertex = new GraphVertex();
466 Either<GraphVertex, JanusGraphOperationStatus> result;
470 result = testSubject.updateVertex(graphVertex);
474 public void testGetVerticesByUniqueIdAndParseFlag() throws Exception {
476 Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGet = new HashMap<>();
477 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> result;
480 result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
481 ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum> value3 = ImmutablePair.of(GraphPropertyEnum.COMPONENT_TYPE, JsonParseFlagEnum.NoParse);
482 verticesToGet.put("mock", value3);
484 result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
485 } catch (Exception e) {
486 // TODO Auto-generated catch block
492 public void testCreateEdge_2() throws Exception {
496 EdgeLabelEnum label = null;
497 Edge edgeToCopy = null;
498 JanusGraphOperationStatus result;
502 result = testSubject.createEdge(from, to, label, edgeToCopy);
507 public void testReplaceEdgeLabel() throws Exception {
509 Vertex fromVertex = null;
510 Vertex toVertex = null;
511 Edge prevEdge = null;
512 EdgeLabelEnum prevLabel = null;
513 EdgeLabelEnum newLabel = null;
514 JanusGraphOperationStatus result;
518 result = testSubject.replaceEdgeLabel(fromVertex, toVertex, prevEdge, prevLabel, newLabel);
522 public void testUpdateVertexMetadataPropertiesWithJson() throws Exception {
524 Vertex vertex = Mockito.mock(Vertex.class);;
525 Map<GraphPropertyEnum, Object> properties = new HashMap<>();
526 properties.put(GraphPropertyEnum.COMPONENT_TYPE, "mock");
527 JanusGraphOperationStatus result;
531 result = testSubject.updateVertexMetadataPropertiesWithJson(vertex, properties);
536 public void testDisassociateAndDeleteLast() throws Exception {
538 GraphVertex vertex = Mockito.mock(GraphVertex.class);
539 JanusGraphOperationStatus result;
541 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
542 Iterator<Edge> mockiter = Mockito.mock(Iterator.class);
543 Edge nextmock = Mockito.mock(Edge.class);
544 Mockito.when(vertex.getVertex()).thenReturn(value);
545 Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(mockiter);
546 Mockito.when(mockiter.hasNext()).thenReturn(true, false);
547 Mockito.when(mockiter.next()).thenReturn(nextmock);
548 Vertex secondVertex = Mockito.mock(Vertex.class);
549 Mockito.when(nextmock.outVertex()).thenReturn(secondVertex);
550 Mockito.when(nextmock.inVertex()).thenReturn(secondVertex);
551 Iterator<Edge> restOfEdges = Mockito.mock(Iterator.class);
552 Mockito.when(secondVertex.edges(Mockito.any(), Mockito.any())).thenReturn(restOfEdges);
553 Mockito.when(restOfEdges.hasNext()).thenReturn(false);
556 result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
560 public void testDisassociateAndDeleteLastOut() throws Exception {
562 GraphVertex vertex = Mockito.mock(GraphVertex.class);
563 JanusGraphOperationStatus result;
565 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
566 Iterator<Edge> mockiter = Mockito.mock(Iterator.class);
567 Edge nextmock = Mockito.mock(Edge.class);
568 Mockito.when(vertex.getVertex()).thenReturn(value);
569 Mockito.when(value.edges(Mockito.any(), Mockito.any())).thenReturn(mockiter);
570 Mockito.when(mockiter.hasNext()).thenReturn(true, false);
571 Mockito.when(mockiter.next()).thenReturn(nextmock);
572 Vertex secondVertex = Mockito.mock(Vertex.class);
573 Mockito.when(nextmock.outVertex()).thenReturn(secondVertex);
574 Mockito.when(nextmock.inVertex()).thenReturn(secondVertex);
575 Iterator<Edge> restOfEdges = Mockito.mock(Iterator.class);
576 Mockito.when(secondVertex.edges(Mockito.any(), Mockito.any())).thenReturn(restOfEdges);
577 Mockito.when(restOfEdges.hasNext()).thenReturn(false);
580 result = testSubject.disassociateAndDeleteLast(vertex, Direction.IN, EdgeLabelEnum.ADDITIONAL_INFORMATION);
584 public void testDisassociateAndDeleteLastException() throws Exception {
586 GraphVertex vertex = Mockito.mock(GraphVertex.class);
587 JanusGraphOperationStatus result;
589 Mockito.when(vertex.getVertex()).thenThrow(RuntimeException.class);
592 result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
596 public void testMoveEdge() throws Exception {
598 GraphVertex vertexA = new GraphVertex();
599 GraphVertex vertexB = new GraphVertex();
600 JanusGraphOperationStatus result;
604 result = testSubject.moveEdge(vertexA, vertexB, EdgeLabelEnum.ADDITIONAL_INFORMATION, Direction.BOTH);