1 package org.openecomp.sdc.be.model.jsontitan.operations;
4 import org.junit.Before;
5 import org.junit.BeforeClass;
7 import org.junit.runner.RunWith;
8 import org.mockito.Mockito;
9 import org.openecomp.sdc.be.dao.api.ActionStatus;
10 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
11 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
12 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
13 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
14 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
15 import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
16 import org.openecomp.sdc.be.model.ModelTestBase;
17 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
18 import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper;
19 import org.springframework.test.context.ContextConfiguration;
20 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
22 import javax.annotation.Resource;
23 import java.util.HashMap;
24 import java.util.List;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.mockito.Mockito.when;
31 * Created by yavivi on 26/01/2018.
33 @RunWith(SpringJUnit4ClassRunner.class)
34 @ContextConfiguration("classpath:application-context-test.xml")
35 public class ExternalReferencesOperationTest extends ModelTestBase {
38 private ExternalReferencesOperation externalReferenceOperation;
41 private TitanDao titanDao;
43 private boolean isInitialized;
45 private GraphVertex serviceVertex;
47 private String serviceVertexUuid;
48 private static final String COMPONENT_ID = "ci-MyComponentName";
50 private static final String MONITORING_OBJECT_TYPE = "monitoring";
51 private static final String WORKFLOW_OBJECT_TYPE = "workflow";
52 private static final String REF_1 = "ref1";
53 private static final String REF_2 = "ref2";
54 private static final String REF_3 = "ref3";
55 private static final String REF_4 = "ref4";
56 private static final String REF_5 = "ref5";
59 private static final String REF_6 = "ref6";
61 private IdMapper idMapper;
64 public static void initTest(){
69 public void beforeTest() {
70 idMapper = Mockito.mock(IdMapper.class);
71 this.externalReferenceOperation.setIdMapper(idMapper);
72 when(idMapper.mapComponentNameToUniqueId(Mockito.anyString(), Mockito.any(GraphVertex.class))).thenReturn(COMPONENT_ID);
74 GraphTestUtils.clearGraph(titanDao);
81 public void testAddComponentInstanceExternalRef(){
82 Either<String, ActionStatus> addResult = externalReferenceOperation.addExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_4);
83 assertThat(addResult.isLeft()).isEqualTo(true);
85 //commit changes to titan
86 final TitanOperationStatus commit = this.titanDao.commit();
87 assertThat(commit).isEqualTo(TitanOperationStatus.OK);
89 assertThat(getServiceExternalRefs()).contains(REF_1, REF_2, REF_3, REF_4);
93 public void testGetComponentInstanceExternalRef(){
94 assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(REF_1, REF_2, REF_3, REF_5);
95 assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE).left().value()).containsExactly(REF_6);
99 public void testGetComponentInstanceExternalRefForNonExistingObjectId(){
100 assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(REF_1, REF_2, REF_3, REF_5);
101 Either<List<String>, ActionStatus> getResult = externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, "FAKE_OBJECT_TYPE");
102 assertThat(getResult.left().value()).isEmpty();
106 public void testDeleteComponentInstanceExternalRef(){
107 //Test the precondition
108 assertThat(getServiceExternalRefs()).contains(REF_5);
111 Either<String, ActionStatus> deleteStatus = externalReferenceOperation.deleteExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
112 assertThat(deleteStatus.isLeft()).isEqualTo(true);
114 //commit changes to titan
115 final TitanOperationStatus commit = this.titanDao.commit();
116 assertThat(commit).isEqualTo(TitanOperationStatus.OK);
118 //Check that ref does not exist anymore
119 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3);
123 public void testUpdateComponentInstanceExternalRef(){
124 //Test the precondition
125 assertThat(getServiceExternalRefs()).contains(REF_5).doesNotContain(REF_4);
127 //Update REF 5 with REF_4
128 Either<String, ActionStatus> updateResult = externalReferenceOperation.updateExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5, REF_4);
130 assertThat(updateResult.isLeft()).isEqualTo(true);
132 //commit changes to titan
133 final TitanOperationStatus commit = this.titanDao.commit();
134 assertThat(commit).isEqualTo(TitanOperationStatus.OK);
136 //Check that ref does not exist anymore
137 assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3, REF_4);
140 private List<String> getServiceExternalRefs(){
142 final Either<GraphVertex, TitanOperationStatus> externalRefsVertexResult = this.titanDao.getChildVertex(this.serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, JsonParseFlagEnum.ParseJson);
143 assertThat(externalRefsVertexResult.isLeft()).isEqualTo(true);
145 GraphVertex externalRefVertex = externalRefsVertexResult.left().value();
148 Map<String, MapComponentInstanceExternalRefs> componentInstancesMap = (Map<String, MapComponentInstanceExternalRefs>) externalRefVertex.getJson();
149 assertThat(componentInstancesMap).isNotNull();
151 //Get Map of external refs by object type
152 final MapComponentInstanceExternalRefs mapComponentInstanceExternalRefs = componentInstancesMap.get(COMPONENT_ID);
154 //Get List of references
155 //final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.externalRefsByObjectType(objectType);
156 final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE);
158 return externalRefsByObjectType;
161 private void initGraphForTest() {
162 //create a service and add 1 ref
163 this.serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>());
164 this.serviceVertexUuid = this.serviceVertex.getUniqueId();
166 //monitoring references
167 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1);
168 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2);
169 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3);
170 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
172 //workflow references
173 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
175 final TitanOperationStatus commit = this.titanDao.commit();
176 assertThat(commit).isEqualTo(TitanOperationStatus.OK);