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.model.jsonjanusgraph.operations;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.when;
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;
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;
52 * Created by yavivi on 26/01/2018.
54 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
55 public class ExternalReferencesOperationTest extends ModelTestBase {
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";
67 private static final String REF_6 = "ref6";
70 private ExternalReferencesOperation externalReferenceOperation;
73 private JanusGraphDao janusGraphDao;
75 private boolean isInitialized;
77 private GraphVertex serviceVertex;
78 private GraphVertex serviceVertex2;
79 private GraphVertex serviceVertex3;
81 private String serviceVertexUuid;
82 private String serviceVertex2Uuid;
83 private String serviceVertex3Uuid;
85 private IdMapper idMapper;
88 public static void initTest() {
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);
98 GraphTestUtils.clearGraph(janusGraphDao);
100 isInitialized = true;
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);
110 //commit changes to janusgraph
111 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
112 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
114 assertThat(getServiceExternalRefs()).contains(REF_1, REF_2, REF_3, REF_4);
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);
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);
139 public void testGetAllCIExternalRefs_noRefsExist() {
140 Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, COMPONENT_ID);
141 assertThat(allExternalReferences.size()).isZero();
145 public void testGetAllCIExternalRefs_noSuchComponentInstance() {
146 Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, "FAKE");
147 assertThat(allExternalReferences.size()).isZero();
151 public void testGetAllCIExternalRefs_nonExitingService_throwsException() {
152 Assertions.assertThrows(StorageException.class, () -> externalReferenceOperation.getAllExternalReferences("FAKE", COMPONENT_ID));
156 public void testGetComponentInstanceExternalRef() {
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);
165 public void testGetComponentInstanceExternalRefForNonExistingObjectId() {
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,
171 assertThat(getResult.left().value()).isEmpty();
175 public void testDeleteComponentInstanceExternalRef() {
176 //Test the precondition
177 assertThat(getServiceExternalRefs()).contains(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);
184 //commit changes to janusgraph
185 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
186 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
188 //Check that ref does not exist anymore
189 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3);
193 public void testUpdateComponentInstanceExternalRef() {
194 //Test the precondition
195 assertThat(getServiceExternalRefs()).contains(REF_5).doesNotContain(REF_4);
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);
201 assertThat(updateResult.isLeft()).isEqualTo(true);
203 //commit changes to janusgraph
204 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
205 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
207 //Check that ref does not exist anymore
208 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3, REF_4);
211 private List<String> getServiceExternalRefs() {
213 final Either<GraphVertex, JanusGraphOperationStatus> externalRefsVertexResult = this.janusGraphDao
214 .getChildVertex(this.serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, JsonParseFlagEnum.ParseJson);
215 assertThat(externalRefsVertexResult.isLeft()).isEqualTo(true);
217 GraphVertex externalRefVertex = externalRefsVertexResult.left().value();
220 Map<String, MapComponentInstanceExternalRefs> componentInstancesMap = (Map<String, MapComponentInstanceExternalRefs>) externalRefVertex.getJson();
221 assertThat(componentInstancesMap).isNotNull();
223 //Get Map of external refs by object type
224 final MapComponentInstanceExternalRefs mapComponentInstanceExternalRefs = componentInstancesMap.get(COMPONENT_ID);
226 //Get List of references
227 //final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.externalRefsByObjectType(objectType);
229 return mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE);
232 private void initGraphForTest() {
234 this.serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
235 this.serviceVertexUuid = this.serviceVertex.getUniqueId();
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);
243 //workflow references
244 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
246 //create a service without refs
247 serviceVertex2 = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
248 serviceVertex2Uuid = serviceVertex2.getUniqueId();
250 //create a service for adding all references
251 serviceVertex3 = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
252 serviceVertex3Uuid = serviceVertex3.getUniqueId();
254 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
255 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);