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.janusgraph;
23 import fj.data.Either;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
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;
49 class JanusGraphDaoMockTest extends DAOConfDependentTest {
52 private JanusGraphDao testSubject;
55 private JanusGraphClient janusGraphClient;
58 void BeforeEach() throws Exception {
59 MockitoAnnotations.openMocks(this);
63 void testCommit() throws Exception {
64 JanusGraphOperationStatus result;
67 result = testSubject.commit();
71 void testRollback() throws Exception {
72 JanusGraphOperationStatus result;
75 result = testSubject.rollback();
79 void testGetGraph() throws Exception {
81 Either<JanusGraph, JanusGraphOperationStatus> result;
85 result = testSubject.getGraph();
89 void testCreateVertex() throws Exception {
91 GraphVertex graphVertex = new GraphVertex();
92 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
93 Either<GraphVertex, JanusGraphOperationStatus> result;
95 JanusGraph tg = Mockito.mock(JanusGraph.class);
96 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
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);
105 void testCreateVertexErrorGetGraph() throws Exception {
107 GraphVertex graphVertex = new GraphVertex();
108 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
109 Either<GraphVertex, JanusGraphOperationStatus> result;
111 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
113 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
114 result = testSubject.createVertex(graphVertex);
118 void testCreateVertexException() throws Exception {
120 GraphVertex graphVertex = new GraphVertex();
121 graphVertex.setLabel(VertexTypeEnum.ADDITIONAL_INFORMATION);
122 Either<GraphVertex, JanusGraphOperationStatus> result;
124 JanusGraph tg = Mockito.mock(JanusGraph.class);
125 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
127 Mockito.when(tg.addVertex()).thenThrow(RuntimeException.class);
128 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
129 result = testSubject.createVertex(graphVertex);
133 void testGetVertexByPropertyAndLabel() throws Exception {
134 Either<GraphVertex, JanusGraphOperationStatus> result;
137 Mockito.when(janusGraphClient.getGraph()).thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
138 result = testSubject.getVertexByPropertyAndLabel(GraphPropertyEnum.COMPONENT_TYPE, "mock",
139 VertexTypeEnum.ADDITIONAL_INFORMATION);
143 void testGetVertexById_1Exception() throws Exception {
146 Either<GraphVertex, JanusGraphOperationStatus> result;
148 JanusGraph tg = Mockito.mock(JanusGraph.class);
149 Either<JanusGraph, JanusGraphOperationStatus> value = Either.left(tg);
151 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
152 Mockito.when(tg.addVertex()).thenReturn(value2);
153 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
156 result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
157 // Assert.assertEquals(null, result);
161 void testGetVertexById_1GraphClosed() throws Exception {
164 Either<GraphVertex, JanusGraphOperationStatus> result;
167 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
169 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
170 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
173 result = testSubject.getVertexById(id, JsonParseFlagEnum.NoParse);
174 // Assert.assertEquals(null, result);
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");
184 testSubject.setVertexProperties(vertex, properties);
188 void testParseVertexProperties() throws Exception {
190 GraphVertex graphVertex = new GraphVertex();
191 JanusGraphVertex vertex = Mockito.mock(JanusGraphVertex.class);
192 graphVertex.setVertex(vertex);
193 JsonParseFlagEnum parseFlag = null;
197 testSubject.parseVertexProperties(graphVertex, JsonParseFlagEnum.NoParse);
201 void testCreateEdge() throws Exception {
203 GraphVertex from = Mockito.mock(GraphVertex.class);
204 GraphVertex to = Mockito.mock(GraphVertex.class);
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;
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);
221 void testSetEdgeProperties() throws Exception {
223 Element element = Mockito.mock(Element.class);
224 Map<EdgePropertyEnum, Object> properties = new HashMap<>();
228 properties.put(EdgePropertyEnum.STATE, "mock");
229 testSubject.setEdgeProperties(element, properties);
233 void testGetByCriteria() throws Exception {
234 Map<GraphPropertyEnum, Object> props = new HashMap<>();
235 Either<List<GraphVertex>, JanusGraphOperationStatus> result;
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);
244 result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props);
248 void testGetByCriteria_1() throws Exception {
250 Map<GraphPropertyEnum, Object> props = new HashMap<>();
251 Either<List<GraphVertex>, JanusGraphOperationStatus> result;
253 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
254 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
255 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
258 result = testSubject.getByCriteria(VertexTypeEnum.ADDITIONAL_INFORMATION, props, JsonParseFlagEnum.NoParse);
262 void testGetCatalogVerticies() throws Exception {
263 Either<Iterator<Vertex>, JanusGraphOperationStatus> result;
265 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
267 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
268 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
271 result = testSubject.getCatalogOrArchiveVerticies(true);
275 void testGetChildVertex() throws Exception {
277 GraphVertex parentVertex = new GraphVertex();
278 EdgeLabelEnum edgeLabel = null;
279 JsonParseFlagEnum parseFlag = null;
280 Either<GraphVertex, JanusGraphOperationStatus> result;
282 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
283 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
284 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
287 result = testSubject.getChildVertex(parentVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION, JsonParseFlagEnum.NoParse);
291 void testGetChildVertex_1() throws Exception {
293 Vertex parentVertex = null;
294 EdgeLabelEnum edgeLabel = null;
295 JsonParseFlagEnum parseFlag = null;
296 Either<Vertex, 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.getChildVertex(parentVertex, edgeLabel, parseFlag);
307 void testGetParentVertex_1() throws Exception {
309 Vertex parentVertex = null;
310 EdgeLabelEnum edgeLabel = null;
311 JsonParseFlagEnum parseFlag = null;
312 Either<Vertex, JanusGraphOperationStatus> result;
316 result = testSubject.getParentVertex(parentVertex, edgeLabel, parseFlag);
320 void testGetParentVertecies_1() throws Exception {
322 Vertex parentVertex = null;
323 EdgeLabelEnum edgeLabel = null;
324 JsonParseFlagEnum parseFlag = null;
325 Either<List<Vertex>, JanusGraphOperationStatus> result;
329 result = testSubject.getParentVertices(parentVertex, edgeLabel, parseFlag);
333 void testGetChildrenVertecies_1() throws Exception {
335 Vertex parentVertex = null;
336 EdgeLabelEnum edgeLabel = null;
337 JsonParseFlagEnum parseFlag = null;
338 Either<List<Vertex>, JanusGraphOperationStatus> result;
342 result = testSubject.getChildrenVertices(parentVertex, edgeLabel, parseFlag);
346 void testDeleteBelongingEdgeByCriteria() throws Exception {
348 GraphVertex vertex = null;
349 EdgeLabelEnum label = null;
350 Map<GraphPropertyEnum, Object> properties = null;
351 Either<Edge, JanusGraphOperationStatus> result;
355 result = testSubject.deleteBelongingEdgeByCriteria(vertex, label, properties);
359 void testDeleteEdge() throws Exception {
361 GraphVertex fromVertex = new GraphVertex();
362 GraphVertex toVertex = new GraphVertex();
363 Either<Edge, JanusGraphOperationStatus> result;
365 Either<JanusGraph, JanusGraphOperationStatus> value = Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
366 JanusGraphVertex value2 = Mockito.mock(JanusGraphVertex.class);
367 Mockito.when(janusGraphClient.getGraph()).thenReturn(value);
370 result = testSubject.deleteEdge(fromVertex, toVertex, EdgeLabelEnum.ADDITIONAL_INFORMATION);
374 void testDeleteEdgeByDirection() throws Exception {
375 GraphVertex fromVertex = new GraphVertex();
376 JanusGraphOperationStatus result;
379 result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
383 void testDeleteEdgeByDirectionMock() throws Exception {
384 GraphVertex fromVertex = Mockito.mock(GraphVertex.class);
385 JanusGraphOperationStatus result;
387 JanusGraphVertex value = Mockito.mock(JanusGraphVertex.class);
389 Mockito.when(fromVertex.getVertex()).thenReturn(value);
390 Iterator<Edge> value2 = Mockito.mock(Iterator.class);
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);
396 Mockito.when(value2.next()).thenReturn(value3);
398 result = testSubject.deleteEdgeByDirection(fromVertex, Direction.BOTH, EdgeLabelEnum.ADDITIONAL_INFORMATION);
402 void testUpdateVertex() throws Exception {
404 GraphVertex graphVertex = new GraphVertex();
405 Either<GraphVertex, JanusGraphOperationStatus> result;
409 result = testSubject.updateVertex(graphVertex);
413 void testGetVerticesByUniqueIdAndParseFlag() throws Exception {
415 Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGet = new HashMap<>();
416 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> result;
419 result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
420 ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum> value3 = ImmutablePair.of(GraphPropertyEnum.COMPONENT_TYPE, JsonParseFlagEnum.NoParse);
421 verticesToGet.put("mock", value3);
423 result = testSubject.getVerticesByUniqueIdAndParseFlag(verticesToGet);
424 } catch (Exception e) {
425 // TODO Auto-generated catch block
431 void testCreateEdge_2() throws Exception {
435 EdgeLabelEnum label = null;
436 Edge edgeToCopy = null;
437 JanusGraphOperationStatus result;
441 result = testSubject.createEdge(from, to, label, edgeToCopy);
445 void testReplaceEdgeLabel() throws Exception {
447 Vertex fromVertex = null;
448 Vertex toVertex = null;
449 Edge prevEdge = null;
450 EdgeLabelEnum prevLabel = null;
451 EdgeLabelEnum newLabel = null;
452 JanusGraphOperationStatus result;
456 result = testSubject.replaceEdgeLabel(fromVertex, toVertex, prevEdge, prevLabel, newLabel);
460 void testUpdateVertexMetadataPropertiesWithJson() throws Exception {
462 Vertex vertex = Mockito.mock(Vertex.class);
464 Map<GraphPropertyEnum, Object> properties = new HashMap<>();
465 properties.put(GraphPropertyEnum.COMPONENT_TYPE, "mock");
466 JanusGraphOperationStatus result;
470 result = testSubject.updateVertexMetadataPropertiesWithJson(vertex, properties);
475 void testDisassociateAndDeleteLast() throws Exception {
477 GraphVertex vertex = Mockito.mock(GraphVertex.class);
478 JanusGraphOperationStatus result;
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);
495 result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
499 void testDisassociateAndDeleteLastOut() throws Exception {
501 GraphVertex vertex = Mockito.mock(GraphVertex.class);
502 JanusGraphOperationStatus result;
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);
519 result = testSubject.disassociateAndDeleteLast(vertex, Direction.IN, EdgeLabelEnum.ADDITIONAL_INFORMATION);
523 void testDisassociateAndDeleteLastException() throws Exception {
525 GraphVertex vertex = Mockito.mock(GraphVertex.class);
526 JanusGraphOperationStatus result;
528 Mockito.when(vertex.getVertex()).thenThrow(RuntimeException.class);
531 result = testSubject.disassociateAndDeleteLast(vertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
535 void testMoveEdge() throws Exception {
537 GraphVertex vertexA = new GraphVertex();
538 GraphVertex vertexB = new GraphVertex();
539 JanusGraphOperationStatus result;
543 result = testSubject.moveEdge(vertexA, vertexB, EdgeLabelEnum.ADDITIONAL_INFORMATION, Direction.BOTH);