Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaOperationFacadeTest.java
1 /*
2
3  * Copyright (c) 2018 AT&T Intellectual Property.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  *     http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */
30 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
31
32 import fj.data.Either;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.MockitoAnnotations;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
43 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
44 import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao;
45 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
46 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
47 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
49 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
50 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
51 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
52 import org.openecomp.sdc.be.model.Component;
53 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
54 import org.openecomp.sdc.be.model.Resource;
55 import org.openecomp.sdc.be.model.LifecycleStateEnum;
56 import org.openecomp.sdc.be.model.ComponentParametersView;
57 import org.openecomp.sdc.be.model.PolicyDefinition;
58 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
59 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
60 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
61 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
62 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
63 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
64 import org.openecomp.sdc.be.model.DataTypeDefinition;
65 import org.openecomp.sdc.be.model.PropertyDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
67
68 import static org.assertj.core.api.Assertions.assertThat;
69
70 import java.util.HashMap;
71 import java.util.List;
72 import java.util.Map;
73 import java.util.ArrayList;
74 import java.util.EnumMap;
75 import java.util.Set;
76 import java.util.HashSet;
77 import java.util.stream.Collectors;
78 import java.util.stream.IntStream;
79 import java.util.Collections;
80 import java.util.Arrays;
81
82 import static org.junit.Assert.assertEquals;
83 import static org.junit.Assert.assertSame;
84 import static org.junit.Assert.assertTrue;
85 import static org.mockito.Mockito.times;
86 import static org.mockito.Mockito.verify;
87 import static org.mockito.Mockito.when;
88 import static org.mockito.ArgumentMatchers.any;
89 import static org.mockito.ArgumentMatchers.anyMap;
90 import static org.mockito.ArgumentMatchers.anyInt;
91 import static org.mockito.ArgumentMatchers.eq;
92
93 @RunWith(MockitoJUnitRunner.class)
94 public class ToscaOperationFacadeTest {
95     private static final String COMPONENT_ID = "componentId";
96     private static final String PROPERTY1_NAME = "prop1";
97     private static final String PROPERTY1_TYPE = "string";
98     private static final String PROPERTY2_NAME = "prop2";
99     private static final String PROPERTY2_TYPE = "integer";
100
101     @InjectMocks
102     private ToscaOperationFacade testInstance;
103
104     @Mock
105     private HealingJanusGraphDao janusGraphDaoMock;
106
107     @Mock
108     private TopologyTemplateOperation topologyTemplateOperationMock;
109
110     @Mock
111     private NodeTypeOperation nodeTypeOperation;
112
113     @Mock
114     private NodeTemplateOperation nodeTemplateOperationMock;
115
116     @Before
117     public void setUp() throws Exception {
118         testInstance = new ToscaOperationFacade();
119         MockitoAnnotations.initMocks(this);
120     }
121
122     @SuppressWarnings("unchecked")
123     @Test
124     public void fetchMetaDataByResourceType() throws Exception {
125         ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
126         ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
127         ComponentParametersView dataFilter = new ComponentParametersView();
128         List<GraphVertex> mockVertices = getMockVertices(2);
129         Either<List<GraphVertex>, JanusGraphOperationStatus> returnedVertices = Either.left(mockVertices);
130
131         when(janusGraphDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
132         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
133         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
134         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
135
136         verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
137         verifyCriteriaNotIsDeleted(criteriaNotCapture);
138
139         assertTrue(fetchedComponents.isLeft());
140         List<Component> cmpts = fetchedComponents.left().value();
141         assertEquals(2, cmpts.size());
142         assertEquals("0", cmpts.get(0).getUniqueId());
143         assertEquals("1", cmpts.get(1).getUniqueId());
144     }
145
146     private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
147         Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
148         assertEquals(2, criteria.size());
149         assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
150         assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
151     }
152
153     private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
154         Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
155         assertEquals(1, notCriteria.size());
156         assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
157     }
158
159     @SuppressWarnings("unchecked")
160     @Test
161     public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
162         when(janusGraphDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(
163             JanusGraphOperationStatus.GENERAL_ERROR));
164         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
165         assertTrue(fetchedComponents.isRight());
166         assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
167     }
168
169     @Test
170     public void associatePolicyToComponentSuccessTest(){
171         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
172         assertTrue(result.isLeft());
173     }
174
175     @Test
176     public void associatePolicyToComponentFailureTest(){
177         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
178         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
179     }
180
181     @Test
182     public void updatePolicyOfComponentSuccessTest(){
183         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
184         assertTrue(result.isLeft());
185     }
186
187     @Test
188     public void updatePolicyOfComponentFailureTest(){
189         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
190         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
191     }
192
193     @Test
194     public void removePolicyFromComponentSuccessTest(){
195         removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
196     }
197
198     @Test
199     public void removePolicyFromComponentFailureTest(){
200         removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
201     }
202
203     @Test
204     public void testFindLastCertifiedToscaElementByUUID(){
205         Either<Component, StorageOperationStatus> result;
206         Component component = new Resource();
207         List<GraphVertex> list = new ArrayList<>();
208         GraphVertex graphVertex = getTopologyTemplateVertex();
209         list.add(graphVertex);
210         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
211         props.put(GraphPropertyEnum.UUID, component.getUUID());
212         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
213         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
214         ToscaElement toscaElement = getToscaElementForTest();
215         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
216         when(janusGraphDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
217         result = testInstance.findLastCertifiedToscaElementByUUID(component);
218         Component resultComp = result.left().value();
219         assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
220     }
221
222     @Test
223     public void testLatestComponentByToscaResourceName(){
224         Either<Component, StorageOperationStatus> result;
225         TopologyTemplate toscaElement = new TopologyTemplate();
226         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
227         List<GraphVertex> list = new ArrayList<>();
228         GraphVertex graphVertex = getTopologyTemplateVertex();
229         Map<GraphPropertyEnum, Object> props = new HashMap<>();
230         props.put(GraphPropertyEnum.VERSION, "1.0");
231         graphVertex.setMetadataProperties(props);
232         list.add(graphVertex);
233
234         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
235         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
236         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
237         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
238         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
239
240         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
241         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
242
243         result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
244         assertThat(result.isLeft());
245     }
246
247     @Test
248     public void testValidateCsarUuidUniqueness() {
249         StorageOperationStatus result;
250         String csarUUID = "csarUUID";
251         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
252         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
253         List<GraphVertex> vertexList = new ArrayList<>();
254         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
255         result = testInstance.validateCsarUuidUniqueness(csarUUID);
256         assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
257     }
258
259     @Test
260     public void testValidateCsarUuidUnique_true() {
261         StorageOperationStatus result;
262         String csarUUID = "csarUUID";
263         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
264         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
265         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
266             JanusGraphOperationStatus.NOT_FOUND));
267         result = testInstance.validateCsarUuidUniqueness(csarUUID);
268         assertEquals(StorageOperationStatus.OK, result);
269     }
270
271     @Test
272     public void testGetLatestCertiNodeTypeByToscaResourceName() {
273         Either<Resource, StorageOperationStatus> result;
274         String toscaResourceName = "resourceName";
275         String uniqueId = "uniqueId";
276         GraphVertex graphVertex = getTopologyTemplateVertex();
277         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
278         graphVertex.setUniqueId(uniqueId);
279         List<GraphVertex> vertexList = new ArrayList<>();
280         vertexList.add(graphVertex);
281         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
282         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
283         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
284         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
285         ToscaElement topologyTemplate = new TopologyTemplate();
286         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
287         when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
288         when(janusGraphDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
289         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
290         result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
291         assertThat(result.isLeft());
292     }
293
294     @Test
295     public void testValidateCompExists() {
296         Either<Boolean, StorageOperationStatus> result;
297         String componentId = "componentId";
298         GraphVertex graphVertex = getTopologyTemplateVertex();
299         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
300         result = testInstance.validateComponentExists(componentId);
301         assertEquals(true, result.left().value());
302     }
303
304     @Test
305     public void testValidateCompExists_NotFound() {
306         Either<Boolean, StorageOperationStatus> result;
307         String componentId = "componentId";
308         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
309             JanusGraphOperationStatus.NOT_FOUND));
310         result = testInstance.validateComponentExists(componentId);
311         assertEquals(false, result.left().value());
312     }
313
314     @Test
315     public void testValidateToscaResourceNameExists() {
316         Either<Boolean, StorageOperationStatus> result;
317         String templateName = "templateName";
318         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
319         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
320         List<GraphVertex> graphVertexList = new ArrayList<>();
321         GraphVertex graphVertex = getTopologyTemplateVertex();
322         graphVertexList.add(graphVertex);
323         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
324         result = testInstance.validateToscaResourceNameExists(templateName);
325         assertEquals(true, result.left().value());
326     }
327
328     @Test
329     public void testValidateToscaResourceNameExists_false() {
330         Either<Boolean, StorageOperationStatus> result;
331         String templateName = "templateName";
332         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
333         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
334         List<GraphVertex> graphVertexList = new ArrayList<>();
335         GraphVertex graphVertex = getTopologyTemplateVertex();
336         graphVertexList.add(graphVertex);
337         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
338             JanusGraphOperationStatus.NOT_FOUND));
339         result = testInstance.validateToscaResourceNameExists(templateName);
340         assertEquals(false, result.left().value());
341     }
342
343     @Test
344     public void testOverrideComponent() {
345         Either<Resource, StorageOperationStatus> result;
346         Resource resource = new Resource();
347         String id = "id";
348         resource.setUniqueId(id);
349         GraphVertex graphVertex = getTopologyTemplateVertex();
350         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
351         NodeType nodeType = new NodeType();
352         nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
353         ToscaElement toscaElement = new TopologyTemplate();
354         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
355         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
356         when(janusGraphDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
357         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
358         when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
359         when(janusGraphDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
360         when(janusGraphDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(
361             JanusGraphOperationStatus.OK);
362         result = testInstance.overrideComponent(resource, resource);
363         assertTrue(result.isLeft());
364     }
365
366     @Test
367     public void testGetToscaElement() {
368         Either<Component, StorageOperationStatus> result;
369         String id = "id";
370         GraphVertex graphVertex = getTopologyTemplateVertex();
371         ToscaElement toscaElement = getToscaElementForTest();
372         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
373         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
374         result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
375         assertTrue(result.isLeft());
376     }
377
378     @Test
379     public void testMarkComponentToDelete() {
380         StorageOperationStatus result;
381         Component component = new Resource();
382         String id = "id";
383         component.setUniqueId(id);
384         GraphVertex graphVertex = getTopologyTemplateVertex();
385         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
386         when(nodeTypeOperation.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
387         result = testInstance.markComponentToDelete(component);
388         assertEquals(result, StorageOperationStatus.OK);
389     }
390
391     @Test
392     public void testDelToscaComponent() {
393         Either<Component, StorageOperationStatus> result;
394         String componentId = "compId";
395         GraphVertex graphVertex = getTopologyTemplateVertex();
396         ToscaElement toscaElement = getToscaElementForTest();
397         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
398         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
399         result = testInstance.deleteToscaComponent(componentId);
400         assertTrue(result.isLeft());
401     }
402
403     @Test
404     public void testGetLatestByToscaResourceName() {
405         Either<Component, StorageOperationStatus> result;
406         String toscaResourceName = "name";
407         ToscaElement toscaElement = getToscaElementForTest();
408
409         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
410         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
411         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
412         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
413         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
414
415         List<GraphVertex> graphVertexList = new ArrayList<>();
416         GraphVertex graphVertex = getTopologyTemplateVertex();
417         graphVertex.setUniqueId(toscaResourceName);
418         Map<GraphPropertyEnum, Object> props = new HashMap<>();
419         props.put(GraphPropertyEnum.VERSION, "1.0");
420         graphVertex.setMetadataProperties(props);
421         graphVertexList.add(graphVertex);
422
423         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
424         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
425         result = testInstance.getLatestByToscaResourceName(toscaResourceName);
426         assertTrue(result.isLeft());
427     }
428
429     @Test
430     public void testGetFollowed() {
431         Either<Set<Component>, StorageOperationStatus> result;
432         String userId = "id";
433         Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
434         Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
435         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
436         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
437         lifecycleStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
438         lifecycleStates.add(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
439         lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
440         lastStateStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
441         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
442         List<ToscaElement> toscaEleList = new ArrayList<>();
443         ToscaElement toscaElement = getToscaElementForTest();
444         toscaEleList.add(toscaElement);
445         when(nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)).thenReturn(Either.left(toscaEleList));
446         result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
447         assertTrue(result.isLeft());
448         assertEquals(1, result.left().value().size());
449     }
450
451     @Test
452     public void testGetBySystemName() {
453         Either<List<Component>, StorageOperationStatus> result;
454         String sysName = "sysName";
455         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
456         ToscaElement toscaElement = getToscaElementForTest();
457         List<GraphVertex> componentVertices = new ArrayList<>();
458         GraphVertex graphVertex = getTopologyTemplateVertex();
459         componentVertices.add(graphVertex);
460         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
461         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
462
463         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
464         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
465
466         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
467
468         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(componentVertices));
469         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
470         result = testInstance.getBySystemName(componentTypeEnum, sysName);
471         assertTrue(result.isLeft());
472         assertEquals(1, result.left().value().size());
473     }
474
475     @Test
476     public void testGetCompByNameAndVersion() {
477         Either<Component, StorageOperationStatus> result;
478         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
479         String name = "name";
480         String version = "1.0";
481         JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
482         List<GraphVertex> graphVertexList = new ArrayList<>();
483         GraphVertex graphVertex = getTopologyTemplateVertex();
484         graphVertexList.add(graphVertex);
485         ToscaElement toscaElement = getToscaElementForTest();
486         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
487         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
488
489         hasProperties.put(GraphPropertyEnum.NAME, name);
490         hasProperties.put(GraphPropertyEnum.VERSION, version);
491         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
492         hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
493         when(janusGraphDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
494         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
495         result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
496         assertTrue(result.isLeft());
497     }
498
499     private ToscaElement getToscaElementForTest() {
500         ToscaElement toscaElement = new TopologyTemplate();
501         toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
502         return toscaElement;
503     }
504
505     @Test
506     public void addDataTypesToComponentSuccessTest(){
507         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK);
508         assertTrue(result.isLeft());
509     }
510
511     @Test
512     public void addDataTypesToComponentFailureTest_BadRequest(){
513         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
514         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
515     }
516
517     private Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) {
518         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
519         String componentId = "componentid";
520         String Id = "id";
521
522         PropertyDefinition noDefaultProp = new PropertyDefinition();
523         noDefaultProp.setName("noDefaultProp");
524         PropertyDefinition prop1 = new PropertyDefinition();
525         prop1.setDefaultValue("def1");
526         prop1.setName("prop1");
527         PropertyDefinition prop2 = new PropertyDefinition();
528         prop2.setType("dataType1");
529         prop2.setName("prop2");
530         PropertyDefinition prop3 = new PropertyDefinition();
531         prop3.setDefaultValue("def3");
532         prop3.setName("prop3");
533
534         DataTypeDefinition noDefaultValue = new DataTypeDefinition();
535         noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
536         noDefaultValue.setDerivedFromName("name0");
537
538         DataTypeDefinition dataType1 = new DataTypeDefinition();
539         dataType1.setProperties(Arrays.asList(prop1, prop3));
540         dataType1.setName("name1");
541         dataType1.setDerivedFromName("derivedfromname1");
542
543         DataTypeDefinition dataType2 = new DataTypeDefinition();
544         dataType2.setDerivedFrom(dataType1);
545         dataType2.setName("name2");
546         dataType2.setDerivedFromName("derivedfromname2");
547
548         DataTypeDefinition dataType3 = new DataTypeDefinition();
549         dataType3.setProperties(Collections.singletonList(prop2));
550         dataType3.setDerivedFrom(noDefaultValue);
551         dataType3.setName("name3");
552         dataType3.setDerivedFromName("derivedfromname3");
553
554         dataTypes.put("noDefault", noDefaultValue);
555         dataTypes.put("dataType1", dataType1);
556         dataTypes.put("dataType2", dataType2);
557         dataTypes.put("dataType3", dataType3);
558
559         GraphVertex vertex;
560         if(status == StorageOperationStatus.OK){
561             vertex = getTopologyTemplateVertex();
562         } else {
563             vertex = getNodeTypeVertex();
564         }
565         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
566         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
567         when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex),
568                 eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status);
569         return testInstance.addDataTypesToComponent(dataTypes, componentId);
570     }
571
572     @Test
573     public void testDataTypesToComponentFailureTest_NotFound() {
574         Either<List<DataTypeDefinition>, StorageOperationStatus> result;
575         String componentId = "componentId";
576         GraphVertex vertex = getNodeTypeVertex();
577         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
578         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
579             JanusGraphOperationStatus.NOT_FOUND));
580         result = testInstance.addDataTypesToComponent(dataTypes, componentId);
581         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
582     }
583
584     @Test
585     public void testDeleteDataTypeOfComponent() {
586         StorageOperationStatus result;
587         Component component = new Resource();
588         String id = "id";
589         component.setUniqueId(id);
590         String datatype = null;
591
592         DataTypeDefinition dataType1 = new DataTypeDefinition();
593         dataType1.setName("name1");
594         Map<String, DataTypeDataDefinition> dataTypeDataMap = new HashMap<>();
595         dataTypeDataMap.put("datatype1", dataType1);
596         List<DataTypeDefinition> dataTypeMap = dataTypeDataMap.values().stream().map(e -> { DataTypeDefinition dataType = new DataTypeDefinition(e);return dataType; }).collect(Collectors.toList());
597         component.setDataTypes(dataTypeMap);
598         GraphVertex graphVertex = getTopologyTemplateVertex();
599         result = testInstance.deleteDataTypeOfComponent(component, "datatype1");
600         assertEquals(datatype, result);
601     }
602
603     @Test
604     public void testAddComponentInstancePropertiesToComponent() {
605         // set up component object
606         Component component = new Resource();
607         component.setUniqueId(COMPONENT_ID);
608         List<ComponentInstanceProperty> instanceProps = new ArrayList<>();
609         ComponentInstanceProperty instanceProp = new ComponentInstanceProperty();
610         instanceProp.setName(PROPERTY1_NAME);
611         instanceProp.setType(PROPERTY1_TYPE);
612         instanceProps.add(instanceProp);
613         instanceProp = new ComponentInstanceProperty();
614         instanceProp.setName(PROPERTY2_NAME);
615         instanceProp.setType(PROPERTY2_TYPE);
616         instanceProps.add(instanceProp);
617         Map<String, List<ComponentInstanceProperty>> instancePropsMap =
618             Collections.singletonMap(COMPONENT_ID, instanceProps);
619         component.setComponentInstancesProperties(Collections.singletonMap(COMPONENT_ID, new ArrayList<>()));
620
621         when(nodeTemplateOperationMock.addComponentInstanceProperty(any(), any(), any()))
622             .thenReturn(StorageOperationStatus.OK);
623
624         Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> result =
625             testInstance.addComponentInstancePropertiesToComponent(component, instancePropsMap);
626         assertTrue(result.isLeft());
627         verify(nodeTemplateOperationMock, times(2)).addComponentInstanceProperty(any(), any(), any());
628         List<ComponentInstanceProperty> resultProps = result.left().value().get(COMPONENT_ID);
629         assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY1_NAME)));
630         assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY2_NAME)));
631     }
632
633     private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
634         PolicyDefinition policy = new PolicyDefinition();
635         String componentId = "componentId";
636         int counter = 0;
637         GraphVertex vertex;
638         if(status == StorageOperationStatus.OK){
639             vertex = getTopologyTemplateVertex();
640         } else {
641             vertex = getNodeTypeVertex();
642         }
643         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
644         when(janusGraphDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
645         when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
646         return testInstance.associatePolicyToComponent(componentId, policy, counter);
647     }
648
649     private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
650         PolicyDefinition policy = new PolicyDefinition();
651         String componentId = "componentId";
652         GraphVertex vertex = getTopologyTemplateVertex();
653         when(janusGraphDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
654         when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
655         return testInstance.updatePolicyOfComponent(componentId, policy);
656     }
657
658     private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
659         String componentId = "componentId";
660         String policyId = "policyId";
661         GraphVertex vertex = getTopologyTemplateVertex();
662         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
663         when(janusGraphDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
664         when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
665         StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
666         assertSame(result, status);
667     }
668
669     private List<GraphVertex> getMockVertices(int numOfVertices) {
670         return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
671     }
672
673     private ToscaElement getResourceToscaElement(String id) {
674         ToscaElement toscaElement = new TopologyTemplate();
675         toscaElement.setMetadata(new HashMap<>());
676         toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
677         toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
678         return toscaElement;
679     }
680
681     private GraphVertex getTopologyTemplateVertex() {
682         GraphVertex graphVertex = new GraphVertex();
683         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
684         return graphVertex;
685     }
686
687     private GraphVertex getNodeTypeVertex() {
688         GraphVertex graphVertex = new GraphVertex();
689         graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);
690         return graphVertex;
691     }
692 }