Fix: Listing archived catalog resources fails randomly
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaElementOperationCatalogTest.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.model.jsonjanusgraph.operations;
22
23 import fj.data.Either;
24 import org.apache.tinkerpop.gremlin.structure.Direction;
25 import org.apache.tinkerpop.gremlin.structure.Edge;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
28 import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyVertexProperty;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
36 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
37 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
38 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
39 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
40
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.List;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertTrue;
47 import static org.mockito.Mockito.when;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class ToscaElementOperationCatalogTest {
51
52     private static final String UPDATER_ID = "m08740";
53     private ArrayList<Vertex> vertexList = new ArrayList<>();
54
55     @Mock Edge edge;
56     @Mock Vertex vertex;
57     @Mock Vertex outVertex;
58     @Mock Iterator<Edge> edges;
59     @Mock JanusGraphDao janusGraphDao;
60     @Mock VertexProperty<Object> property;
61     @Mock VertexProperty<Object> updaterProperty;
62
63     @InjectMocks
64     private ToscaElementOperation toscaOperation = new TopologyTemplateOperation();
65
66     @Before
67     public void setUp() {
68         vertexList.add(vertex);
69         when(janusGraphDao.getCatalogOrArchiveVertices(true)).thenReturn(Either.left(vertexList.iterator()));
70         when(janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY))
71                 .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
72         when(vertex.property(GraphPropertiesDictionary.METADATA.getProperty())).thenReturn(property);
73     }
74
75     private void stubEmptyUpdater() {
76         when(vertex.edges(Direction.IN, EdgeLabelEnum.STATE.name())).thenReturn(edges);
77         when(edges.hasNext()).thenReturn(false);
78     }
79
80     private void stubExistingUpdater() {
81         when(vertex.edges(Direction.IN, EdgeLabelEnum.STATE.name())).thenReturn(edges);
82         when(edges.hasNext()).thenReturn(true);
83         when(edges.next()).thenReturn(edge);
84         when(edge.outVertex()).thenReturn(outVertex);
85         when(updaterProperty.value()).thenReturn(UPDATER_ID);
86         when(outVertex.property(GraphPropertiesDictionary.USERID.getProperty())).thenReturn(updaterProperty);
87     }
88
89     @Test
90     public void getComponentFromCatalogWhenDeleteIsTrue() {
91         final String vertexJsonIsDeletedTrue = "{\"lifecycleState\":\"CERTIFIED\",\"componentType\":\"RESOURCE\",\"vendorRelease\":\"1\",\"contactId\":\"ah7840\",\"lastUpdateDate\":1496119811038,\"icon\":\"att\",\"description\":\"Cloud\",\"creationDate\":1459432094781,\"vendorName\":\"AT&T\",\"mandatory\":false,\"version\":\"1.0\",\"tags\":[\"Cloud\"],\"highestVersion\":true,\"systemName\":\"Cloud\",\"name\":\"Cloud\",\"isDeleted\":true,\"invariantUuid\":\"ab5263fd-115c-41f2-8d0c-8b9279bce2b2\",\"UUID\":\"208cf7df-68a1-4ae7-afc9-409ea8012332\",\"normalizedName\":\"cloud\",\"toscaResourceName\":\"org.openecomp.resource.Endpoint.Cloud\",\"uniqueId\":\"9674e7e1-bc1a-41fe-b503-fbe996801475\",\"resourceType\":\"VFC\"}";
92         when(property.value()).thenReturn(vertexJsonIsDeletedTrue);
93         List<CatalogComponent> componentList = toscaOperation.getElementCatalogData(true, null).left().value();
94         assertTrue(componentList.isEmpty());
95     }
96
97     @Test
98     public void getComponentFromCatalogWhenDeleteNotFound() {
99         stubEmptyUpdater();
100         final String vertexJsonIsDeletedNotFound = "{\"lifecycleState\":\"CERTIFIED\",\"componentType\":\"RESOURCE\",\"vendorRelease\":\"1\",\"contactId\":\"ah7840\",\"lastUpdateDate\":1496119811038,\"icon\":\"att\",\"description\":\"Cloud\",\"creationDate\":1459432094781,\"vendorName\":\"AT&T\",\"mandatory\":false,\"version\":\"1.0\",\"tags\":[\"Cloud\"],\"highestVersion\":true,\"systemName\":\"Cloud\",\"name\":\"Cloud\",\"invariantUuid\":\"ab5263fd-115c-41f2-8d0c-8b9279bce2b2\",\"UUID\":\"208cf7df-68a1-4ae7-afc9-409ea8012332\",\"normalizedName\":\"cloud\",\"toscaResourceName\":\"org.openecomp.resource.Endpoint.Cloud\",\"uniqueId\":\"9674e7e1-bc1a-41fe-b503-fbe996801475\",\"resourceType\":\"VFC\"}";
101         when(property.value()).thenReturn(vertexJsonIsDeletedNotFound);
102         List<CatalogComponent> componentList = toscaOperation.getElementCatalogData(true, null).left().value();
103         assertEquals(1, componentList.size());
104     }
105
106     @Test
107     public void getComponentFromCatalogWhenDeleteIsFalse() {
108         stubExistingUpdater();
109         final String vertexJsonIsDeletedFalse = "{\"lifecycleState\":\"CERTIFIED\",\"componentType\":\"RESOURCE\",\"vendorRelease\":\"1\",\"contactId\":\"ah7840\",\"lastUpdateDate\":1496119811038,\"icon\":\"att\",\"description\":\"Cloud\",\"creationDate\":1459432094781,\"vendorName\":\"AT&T\",\"mandatory\":false,\"version\":\"1.0\",\"tags\":[\"Cloud\"],\"highestVersion\":true,\"systemName\":\"Cloud\",\"name\":\"Cloud\",\"isDeleted\":false,\"invariantUuid\":\"ab5263fd-115c-41f2-8d0c-8b9279bce2b2\",\"UUID\":\"208cf7df-68a1-4ae7-afc9-409ea8012332\",\"normalizedName\":\"cloud\",\"toscaResourceName\":\"org.openecomp.resource.Endpoint.Cloud\",\"uniqueId\":\"9674e7e1-bc1a-41fe-b503-fbe996801475\",\"resourceType\":\"VFC\"}";
110         when(property.value()).thenReturn(vertexJsonIsDeletedFalse);
111         List<CatalogComponent> componentList = toscaOperation.getElementCatalogData(true, null).left().value();
112         assertEquals(1, componentList.size());
113         assertEquals(UPDATER_ID, componentList.get(0).getLastUpdaterUserId());
114     }
115
116     @Test
117     public void thatVertexIsSkippedIfItHasNoMetadataProperty() {
118         VertexProperty<Object> emptyVertexProperty = new EmptyVertexProperty<>();
119         when(vertex.property(GraphPropertiesDictionary.METADATA.getProperty())).thenReturn(emptyVertexProperty);
120
121         List<CatalogComponent> componentList = toscaOperation.getElementCatalogData(true, null).left().value();
122         assertEquals(0, componentList.size());
123     }
124
125 }