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 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;
43 import javax.annotation.Resource;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.mockito.Mockito.when;
50 * Created by yavivi on 26/01/2018.
52 @RunWith(SpringJUnit4ClassRunner.class)
53 @ContextConfiguration("classpath:application-context-test.xml")
54 public class ExternalReferencesOperationTest extends ModelTestBase {
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";
66 private static final String REF_6 = "ref6";
69 private ExternalReferencesOperation externalReferenceOperation;
72 private JanusGraphDao janusGraphDao;
74 private boolean isInitialized;
76 private GraphVertex serviceVertex;
77 private GraphVertex serviceVertex2;
78 private GraphVertex serviceVertex3;
80 private String serviceVertexUuid;
81 private String serviceVertex2Uuid;
82 private String serviceVertex3Uuid;
84 private IdMapper idMapper;
87 public static void initTest(){
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);
97 GraphTestUtils.clearGraph(janusGraphDao);
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);
108 //commit changes to janusgraph
109 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
110 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
112 assertThat(getServiceExternalRefs()).contains(REF_1, REF_2, REF_3, REF_4);
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);
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);
137 public void testGetAllCIExternalRefs_noRefsExist() {
138 Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, COMPONENT_ID);
139 assertThat(allExternalReferences.size()).isZero();
143 public void testGetAllCIExternalRefs_noSuchComponentInstance() {
144 Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, "FAKE");
145 assertThat(allExternalReferences.size()).isZero();
148 @Test(expected=StorageException.class)
149 public void testGetAllCIExternalRefs_nonExitingService_throwsException() {
150 externalReferenceOperation.getAllExternalReferences("FAKE", COMPONENT_ID);
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);
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();
167 public void testDeleteComponentInstanceExternalRef(){
168 //Test the precondition
169 assertThat(getServiceExternalRefs()).contains(REF_5);
172 Either<String, ActionStatus> deleteStatus = externalReferenceOperation.deleteExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
173 assertThat(deleteStatus.isLeft()).isEqualTo(true);
175 //commit changes to janusgraph
176 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
177 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
179 //Check that ref does not exist anymore
180 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3);
184 public void testUpdateComponentInstanceExternalRef(){
185 //Test the precondition
186 assertThat(getServiceExternalRefs()).contains(REF_5).doesNotContain(REF_4);
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);
191 assertThat(updateResult.isLeft()).isEqualTo(true);
193 //commit changes to janusgraph
194 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
195 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
197 //Check that ref does not exist anymore
198 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3, REF_4);
201 private List<String> getServiceExternalRefs(){
203 final Either<GraphVertex, JanusGraphOperationStatus> externalRefsVertexResult = this.janusGraphDao
204 .getChildVertex(this.serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, JsonParseFlagEnum.ParseJson);
205 assertThat(externalRefsVertexResult.isLeft()).isEqualTo(true);
207 GraphVertex externalRefVertex = externalRefsVertexResult.left().value();
210 Map<String, MapComponentInstanceExternalRefs> componentInstancesMap = (Map<String, MapComponentInstanceExternalRefs>) externalRefVertex.getJson();
211 assertThat(componentInstancesMap).isNotNull();
213 //Get Map of external refs by object type
214 final MapComponentInstanceExternalRefs mapComponentInstanceExternalRefs = componentInstancesMap.get(COMPONENT_ID);
216 //Get List of references
217 //final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.externalRefsByObjectType(objectType);
219 return mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE);
222 private void initGraphForTest() {
224 this.serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
225 this.serviceVertexUuid = this.serviceVertex.getUniqueId();
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);
233 //workflow references
234 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
236 //create a service without refs
237 serviceVertex2 = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
238 serviceVertex2Uuid = serviceVertex2.getUniqueId();
240 //create a service for adding all references
241 serviceVertex3 = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
242 serviceVertex3Uuid = serviceVertex3.getUniqueId();
244 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
245 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);