1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import static org.assertj.core.api.Assertions.assertThat;
4 import static org.mockito.Mockito.when;
6 import java.util.HashMap;
10 import javax.annotation.Resource;
12 import org.junit.Before;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mockito;
17 import org.openecomp.sdc.be.dao.api.ActionStatus;
18 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
19 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
20 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
21 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
22 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
23 import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
24 import org.openecomp.sdc.be.model.ModelTestBase;
25 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
26 import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper;
27 import org.springframework.test.context.ContextConfiguration;
28 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30 import fj.data.Either;
33 * Created by yavivi on 26/01/2018.
35 @RunWith(SpringJUnit4ClassRunner.class)
36 @ContextConfiguration("classpath:application-context-test.xml")
37 public class ExternalReferencesOperationTest extends ModelTestBase {
40 private ExternalReferencesOperation externalReferenceOperation;
43 private TitanDao titanDao;
45 private boolean isInitialized;
47 private GraphVertex serviceVertex;
49 private String serviceVertexUuid;
50 private static final String COMPONENT_ID = "ci-MyComponentName";
52 private static final String MONITORING_OBJECT_TYPE = "monitoring";
53 private static final String WORKFLOW_OBJECT_TYPE = "workflow";
54 private static final String REF_1 = "ref1";
55 private static final String REF_2 = "ref2";
56 private static final String REF_3 = "ref3";
57 private static final String REF_4 = "ref4";
58 private static final String REF_5 = "ref5";
61 private static final String REF_6 = "ref6";
63 private IdMapper idMapper;
66 public static void initTest(){
71 public void beforeTest() {
72 idMapper = Mockito.mock(IdMapper.class);
73 this.externalReferenceOperation.setIdMapper(idMapper);
74 when(idMapper.mapComponentNameToUniqueId(Mockito.anyString(), Mockito.any(GraphVertex.class))).thenReturn(COMPONENT_ID);
76 GraphTestUtils.clearGraph(titanDao);
83 public void testAddComponentInstanceExternalRef(){
84 Either<String, ActionStatus> addResult = externalReferenceOperation.addExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_4);
85 assertThat(addResult.isLeft()).isEqualTo(true);
87 //commit changes to titan
88 final TitanOperationStatus commit = this.titanDao.commit();
89 assertThat(commit).isEqualTo(TitanOperationStatus.OK);
91 assertThat(getServiceExternalRefs()).contains(REF_1, REF_2, REF_3, REF_4);
95 public void testGetComponentInstanceExternalRef(){
96 assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(REF_1, REF_2, REF_3, REF_5);
97 assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE).left().value()).containsExactly(REF_6);
101 public void testGetComponentInstanceExternalRefForNonExistingObjectId(){
102 assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(REF_1, REF_2, REF_3, REF_5);
103 Either<List<String>, ActionStatus> getResult = externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, "FAKE_OBJECT_TYPE");
104 assertThat(getResult.left().value()).isEmpty();
108 public void testDeleteComponentInstanceExternalRef(){
109 //Test the precondition
110 assertThat(getServiceExternalRefs()).contains(REF_5);
113 Either<String, ActionStatus> deleteStatus = externalReferenceOperation.deleteExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
114 assertThat(deleteStatus.isLeft()).isEqualTo(true);
116 //commit changes to titan
117 final TitanOperationStatus commit = this.titanDao.commit();
118 assertThat(commit).isEqualTo(TitanOperationStatus.OK);
120 //Check that ref does not exist anymore
121 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3);
125 public void testUpdateComponentInstanceExternalRef(){
126 //Test the precondition
127 assertThat(getServiceExternalRefs()).contains(REF_5).doesNotContain(REF_4);
129 //Update REF 5 with REF_4
130 Either<String, ActionStatus> updateResult = externalReferenceOperation.updateExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5, REF_4);
132 assertThat(updateResult.isLeft()).isEqualTo(true);
134 //commit changes to titan
135 final TitanOperationStatus commit = this.titanDao.commit();
136 assertThat(commit).isEqualTo(TitanOperationStatus.OK);
138 //Check that ref does not exist anymore
139 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3, REF_4);
142 private List<String> getServiceExternalRefs(){
144 final Either<GraphVertex, TitanOperationStatus> externalRefsVertexResult = this.titanDao.getChildVertex(this.serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, JsonParseFlagEnum.ParseJson);
145 assertThat(externalRefsVertexResult.isLeft()).isEqualTo(true);
147 GraphVertex externalRefVertex = externalRefsVertexResult.left().value();
150 Map<String, MapComponentInstanceExternalRefs> componentInstancesMap = (Map<String, MapComponentInstanceExternalRefs>) externalRefVertex.getJson();
151 assertThat(componentInstancesMap).isNotNull();
153 //Get Map of external refs by object type
154 final MapComponentInstanceExternalRefs mapComponentInstanceExternalRefs = componentInstancesMap.get(COMPONENT_ID);
156 //Get List of references
157 //final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.externalRefsByObjectType(objectType);
158 final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE);
160 return externalRefsByObjectType;
163 private void initGraphForTest() {
164 //create a service and add 1 ref
165 this.serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>());
166 this.serviceVertexUuid = this.serviceVertex.getUniqueId();
168 //monitoring references
169 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1);
170 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2);
171 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3);
172 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
174 //workflow references
175 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
177 final TitanOperationStatus commit = this.titanDao.commit();
178 assertThat(commit).isEqualTo(TitanOperationStatus.OK);