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