Fix test cases failing incorrectly
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ExternalReferencesOperationTest.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 static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.when;
25
26 import fj.data.Either;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import javax.annotation.Resource;
33 import org.junit.jupiter.api.Assertions;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mockito;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
40 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
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.JsonParseFlagEnum;
44 import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
45 import org.openecomp.sdc.be.model.ModelTestBase;
46 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
47 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper;
48 import org.openecomp.sdc.be.model.operations.StorageException;
49 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
50
51 /**
52  * Created by yavivi on 26/01/2018.
53  */
54 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
55 public class ExternalReferencesOperationTest extends ModelTestBase {
56
57     private static final String COMPONENT_ID = "ci-MyComponentName";
58     private static final String COMPONENT2_ID = "ci-MyComponentName2";
59     private static final String MONITORING_OBJECT_TYPE = "monitoring";
60     private static final String WORKFLOW_OBJECT_TYPE = "workflow";
61     private static final String REF_1 = "ref1";
62     private static final String REF_2 = "ref2";
63     private static final String REF_3 = "ref3";
64     private static final String REF_4 = "ref4";
65     private static final String REF_5 = "ref5";
66     //workflow
67     private static final String REF_6 = "ref6";
68
69     @Resource
70     private ExternalReferencesOperation externalReferenceOperation;
71
72     @Resource
73     private JanusGraphDao janusGraphDao;
74
75     private boolean isInitialized;
76
77     private GraphVertex serviceVertex;
78     private GraphVertex serviceVertex2;
79     private GraphVertex serviceVertex3;
80
81     private String serviceVertexUuid;
82     private String serviceVertex2Uuid;
83     private String serviceVertex3Uuid;
84
85     private IdMapper idMapper;
86
87     @BeforeAll
88     public static void initTest() {
89         ModelTestBase.init();
90     }
91
92     @BeforeEach
93     public void beforeTest() {
94         idMapper = Mockito.mock(IdMapper.class);
95         this.externalReferenceOperation.setIdMapper(idMapper);
96         when(idMapper.mapComponentNameToUniqueId(Mockito.anyString(), Mockito.any(GraphVertex.class))).thenReturn(COMPONENT_ID);
97         if (!isInitialized) {
98             GraphTestUtils.clearGraph(janusGraphDao);
99             initGraphForTest();
100             isInitialized = true;
101         }
102     }
103
104     @Test
105     public void testAddComponentInstanceExternalRef() {
106         Either<String, ActionStatus> addResult = externalReferenceOperation.addExternalReference(this.serviceVertexUuid, COMPONENT_ID,
107             MONITORING_OBJECT_TYPE, REF_4);
108         assertThat(addResult.isLeft()).isEqualTo(true);
109
110         //commit changes to janusgraph
111         final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
112         assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
113
114         assertThat(getServiceExternalRefs()).contains(REF_1, REF_2, REF_3, REF_4);
115     }
116
117     @Test
118     public void testAddExternalReferences_success() {
119         Map<String, List<String>> refsMap = Collections.singletonMap(MONITORING_OBJECT_TYPE, Arrays.asList(REF_1, REF_2));
120         externalReferenceOperation.addAllExternalReferences(serviceVertex3Uuid, COMPONENT_ID, refsMap);
121         Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex3Uuid, COMPONENT_ID);
122         assertThat(allExternalReferences.size()).isEqualTo(1);
123         assertThat(allExternalReferences).flatExtracting(MONITORING_OBJECT_TYPE).containsExactly(REF_1, REF_2);
124         externalReferenceOperation.addAllExternalReferences(serviceVertex3Uuid, COMPONENT2_ID, refsMap);
125         Map<String, List<String>> allExternalReferences2 = externalReferenceOperation.getAllExternalReferences(serviceVertex3Uuid, COMPONENT2_ID);
126         assertThat(allExternalReferences2.size()).isEqualTo(1);
127         assertThat(allExternalReferences2).flatExtracting(MONITORING_OBJECT_TYPE).containsExactly(REF_1, REF_2);
128     }
129
130     @Test
131     public void testGetAllCIExternalRefs_success() {
132         Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertexUuid, COMPONENT_ID);
133         assertThat(allExternalReferences.size()).isEqualTo(2);
134         assertThat(allExternalReferences).flatExtracting(WORKFLOW_OBJECT_TYPE).containsExactly(REF_6);
135         assertThat(allExternalReferences).flatExtracting(MONITORING_OBJECT_TYPE).containsExactly(REF_1, REF_2, REF_3, REF_5);
136     }
137
138     @Test
139     public void testGetAllCIExternalRefs_noRefsExist() {
140         Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, COMPONENT_ID);
141         assertThat(allExternalReferences.size()).isZero();
142     }
143
144     @Test
145     public void testGetAllCIExternalRefs_noSuchComponentInstance() {
146         Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, "FAKE");
147         assertThat(allExternalReferences.size()).isZero();
148     }
149
150     @Test
151     public void testGetAllCIExternalRefs_nonExitingService_throwsException() {
152         Assertions.assertThrows(StorageException.class, () -> externalReferenceOperation.getAllExternalReferences("FAKE", COMPONENT_ID));
153     }
154
155     @Test
156     public void testGetComponentInstanceExternalRef() {
157         assertThat(
158             externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(
159             REF_1, REF_2, REF_3, REF_5);
160         assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE).left()
161             .value()).containsExactly(REF_6);
162     }
163
164     @Test
165     public void testGetComponentInstanceExternalRefForNonExistingObjectId() {
166         assertThat(
167             externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(
168             REF_1, REF_2, REF_3, REF_5);
169         Either<List<String>, ActionStatus> getResult = externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID,
170             "FAKE_OBJECT_TYPE");
171         assertThat(getResult.left().value()).isEmpty();
172     }
173
174     @Test
175     public void testDeleteComponentInstanceExternalRef() {
176         //Test the precondition
177         assertThat(getServiceExternalRefs()).contains(REF_5);
178
179         //Remove REF 5
180         Either<String, ActionStatus> deleteStatus = externalReferenceOperation.deleteExternalReference(this.serviceVertexUuid, COMPONENT_ID,
181             MONITORING_OBJECT_TYPE, REF_5);
182         assertThat(deleteStatus.isLeft()).isEqualTo(true);
183
184         //commit changes to janusgraph
185         final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
186         assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
187
188         //Check that ref does not exist anymore
189         assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3);
190     }
191
192     @Test
193     public void testUpdateComponentInstanceExternalRef() {
194         //Test the precondition
195         assertThat(getServiceExternalRefs()).contains(REF_5).doesNotContain(REF_4);
196
197         //Update REF 5 with REF_4
198         Either<String, ActionStatus> updateResult = externalReferenceOperation.updateExternalReference(this.serviceVertexUuid, COMPONENT_ID,
199             MONITORING_OBJECT_TYPE, REF_5, REF_4);
200
201         assertThat(updateResult.isLeft()).isEqualTo(true);
202
203         //commit changes to janusgraph
204         final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
205         assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
206
207         //Check that ref does not exist anymore
208         assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3, REF_4);
209     }
210
211     private List<String> getServiceExternalRefs() {
212         //Get service vertex
213         final Either<GraphVertex, JanusGraphOperationStatus> externalRefsVertexResult = this.janusGraphDao
214             .getChildVertex(this.serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, JsonParseFlagEnum.ParseJson);
215         assertThat(externalRefsVertexResult.isLeft()).isEqualTo(true);
216
217         GraphVertex externalRefVertex = externalRefsVertexResult.left().value();
218
219         //Get the full map
220         Map<String, MapComponentInstanceExternalRefs> componentInstancesMap = (Map<String, MapComponentInstanceExternalRefs>) externalRefVertex.getJson();
221         assertThat(componentInstancesMap).isNotNull();
222
223         //Get Map of external refs by object type
224         final MapComponentInstanceExternalRefs mapComponentInstanceExternalRefs = componentInstancesMap.get(COMPONENT_ID);
225
226         //Get List of references
227         //final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.externalRefsByObjectType(objectType);
228
229         return mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE);
230     }
231
232     private void initGraphForTest() {
233         //create a service
234         this.serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
235         this.serviceVertexUuid = this.serviceVertex.getUniqueId();
236
237         //monitoring references
238         externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1);
239         externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2);
240         externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3);
241         externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
242
243         //workflow references
244         externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
245
246         //create a service without refs
247         serviceVertex2 = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
248         serviceVertex2Uuid = serviceVertex2.getUniqueId();
249
250         //create a service for adding all references
251         serviceVertex3 = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
252         serviceVertex3Uuid = serviceVertex3.getUniqueId();
253
254         final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
255         assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
256     }
257 }