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