Change to enable SDC list type input
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsontitan / 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.jsontitan.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.jsongraph.GraphVertex;
43 import org.openecomp.sdc.be.dao.jsongraph.HealingTitanDao;
44 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
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.dao.titan.TitanOperationStatus;
49 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
50 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
51 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
52 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
53 import org.openecomp.sdc.be.model.Component;
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.jsontitan.datamodel.NodeType;
59 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
60 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
61 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
62 import org.openecomp.sdc.be.model.jsontitan.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.when;
86 import static org.mockito.ArgumentMatchers.any;
87 import static org.mockito.ArgumentMatchers.anyMap;
88 import static org.mockito.ArgumentMatchers.anyInt;
89 import static org.mockito.ArgumentMatchers.anyString;
90 import static org.mockito.ArgumentMatchers.eq;
91
92 @RunWith(MockitoJUnitRunner.class)
93 public class ToscaOperationFacadeTest {
94     @InjectMocks
95     private ToscaOperationFacade testInstance;
96
97     @Mock
98     private HealingTitanDao titanDaoMock;
99
100     @Mock
101     private TopologyTemplateOperation topologyTemplateOperationMock;
102
103     @Mock
104     private NodeTypeOperation nodeTypeOperation;
105
106     @Before
107     public void setUp() throws Exception {
108         testInstance = new ToscaOperationFacade();
109         MockitoAnnotations.initMocks(this);
110     }
111
112     @SuppressWarnings("unchecked")
113     @Test
114     public void fetchMetaDataByResourceType() throws Exception {
115         ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
116         ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
117         ComponentParametersView dataFilter = new ComponentParametersView();
118         List<GraphVertex> mockVertices = getMockVertices(2);
119         Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices);
120
121         when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
122         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
123         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
124         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
125
126         verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
127         verifyCriteriaNotIsDeleted(criteriaNotCapture);
128
129         assertTrue(fetchedComponents.isLeft());
130         List<Component> cmpts = fetchedComponents.left().value();
131         assertEquals(2, cmpts.size());
132         assertEquals("0", cmpts.get(0).getUniqueId());
133         assertEquals("1", cmpts.get(1).getUniqueId());
134     }
135
136     private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
137         Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
138         assertEquals(2, criteria.size());
139         assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
140         assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
141     }
142
143     private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
144         Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
145         assertEquals(1, notCriteria.size());
146         assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
147     }
148
149     @SuppressWarnings("unchecked")
150     @Test
151     public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
152         when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR));
153         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
154         assertTrue(fetchedComponents.isRight());
155         assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
156     }
157
158     @Test
159     public void associatePolicyToComponentSuccessTest(){
160         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
161         assertTrue(result.isLeft());
162     }
163
164     @Test
165     public void associatePolicyToComponentFailureTest(){
166         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
167         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
168     }
169
170     @Test
171     public void updatePolicyOfComponentSuccessTest(){
172         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
173         assertTrue(result.isLeft());
174     }
175
176     @Test
177     public void updatePolicyOfComponentFailureTest(){
178         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
179         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
180     }
181
182     @Test
183     public void removePolicyFromComponentSuccessTest(){
184         removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
185     }
186
187     @Test
188     public void removePolicyFromComponentFailureTest(){
189         removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
190     }
191
192     @Test
193     public void testFindLastCertifiedToscaElementByUUID(){
194         Either<Component, StorageOperationStatus> result;
195         Component component = new Resource();
196         List<GraphVertex> list = new ArrayList<>();
197         GraphVertex graphVertex = getTopologyTemplateVertex();
198         list.add(graphVertex);
199         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
200         props.put(GraphPropertyEnum.UUID, component.getUUID());
201         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
202         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
203         ToscaElement toscaElement = getToscaElementForTest();
204         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
205         when(titanDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
206         result = testInstance.findLastCertifiedToscaElementByUUID(component);
207         Component resultComp = result.left().value();
208         assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
209     }
210
211     @Test
212     public void testLatestComponentByToscaResourceName(){
213         Either<Component, StorageOperationStatus> result;
214         TopologyTemplate toscaElement = new TopologyTemplate();
215         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
216         List<GraphVertex> list = new ArrayList<>();
217         GraphVertex graphVertex = getTopologyTemplateVertex();
218         Map<GraphPropertyEnum, Object> props = new HashMap<>();
219         props.put(GraphPropertyEnum.VERSION, "1.0");
220         graphVertex.setMetadataProperties(props);
221         list.add(graphVertex);
222
223         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
224         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
225         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
226         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
227         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
228
229         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
230         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
231
232         result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
233         assertThat(result.isLeft());
234     }
235
236     @Test
237     public void testValidateCsarUuidUniqueness() {
238         StorageOperationStatus result;
239         String csarUUID = "csarUUID";
240         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
241         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
242         List<GraphVertex> vertexList = new ArrayList<>();
243         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
244         result = testInstance.validateCsarUuidUniqueness(csarUUID);
245         assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
246     }
247
248     @Test
249     public void testValidateCsarUuidUnique_true() {
250         StorageOperationStatus result;
251         String csarUUID = "csarUUID";
252         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
253         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
254         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
255         result = testInstance.validateCsarUuidUniqueness(csarUUID);
256         assertEquals(StorageOperationStatus.OK, result);
257     }
258
259     @Test
260     public void testGetLatestCertiNodeTypeByToscaResourceName() {
261         Either<Resource, StorageOperationStatus> result;
262         String toscaResourceName = "resourceName";
263         String uniqueId = "uniqueId";
264         GraphVertex graphVertex = getTopologyTemplateVertex();
265         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
266         graphVertex.setUniqueId(uniqueId);
267         List<GraphVertex> vertexList = new ArrayList<>();
268         vertexList.add(graphVertex);
269         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
270         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
271         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
272         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
273         ToscaElement topologyTemplate = new TopologyTemplate();
274         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
275         when(titanDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
276         when(titanDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
277         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
278         result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
279         assertThat(result.isLeft());
280     }
281
282     @Test
283     public void testValidateCompExists() {
284         Either<Boolean, StorageOperationStatus> result;
285         String componentId = "componentId";
286         GraphVertex graphVertex = getTopologyTemplateVertex();
287         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
288         result = testInstance.validateComponentExists(componentId);
289         assertEquals(true, result.left().value());
290     }
291
292     @Test
293     public void testValidateCompExists_NotFound() {
294         Either<Boolean, StorageOperationStatus> result;
295         String componentId = "componentId";
296         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
297         result = testInstance.validateComponentExists(componentId);
298         assertEquals(false, result.left().value());
299     }
300
301     @Test
302     public void testValidateToscaResourceNameExists() {
303         Either<Boolean, StorageOperationStatus> result;
304         String templateName = "templateName";
305         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
306         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
307         List<GraphVertex> graphVertexList = new ArrayList<>();
308         GraphVertex graphVertex = getTopologyTemplateVertex();
309         graphVertexList.add(graphVertex);
310         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
311         result = testInstance.validateToscaResourceNameExists(templateName);
312         assertEquals(true, result.left().value());
313     }
314
315     @Test
316     public void testValidateToscaResourceNameExists_false() {
317         Either<Boolean, StorageOperationStatus> result;
318         String templateName = "templateName";
319         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
320         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
321         List<GraphVertex> graphVertexList = new ArrayList<>();
322         GraphVertex graphVertex = getTopologyTemplateVertex();
323         graphVertexList.add(graphVertex);
324         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
325         result = testInstance.validateToscaResourceNameExists(templateName);
326         assertEquals(false, result.left().value());
327     }
328
329     @Test
330     public void testOverrideComponent() {
331         Either<Resource, StorageOperationStatus> result;
332         Resource resource = new Resource();
333         String id = "id";
334         resource.setUniqueId(id);
335         GraphVertex graphVertex = getTopologyTemplateVertex();
336         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
337         NodeType nodeType = new NodeType();
338         nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
339         ToscaElement toscaElement = new TopologyTemplate();
340         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
341         when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
342         when(titanDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
343         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
344         when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
345         when(titanDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
346         when(titanDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(TitanOperationStatus.OK);
347         result = testInstance.overrideComponent(resource, resource);
348         assertTrue(result.isLeft());
349     }
350
351     @Test
352     public void testGetToscaElement() {
353         Either<Component, StorageOperationStatus> result;
354         String id = "id";
355         GraphVertex graphVertex = getTopologyTemplateVertex();
356         ToscaElement toscaElement = getToscaElementForTest();
357         when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
358         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
359         result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
360         assertTrue(result.isLeft());
361     }
362
363     @Test
364     public void testMarkComponentToDelete() {
365         StorageOperationStatus result;
366         Component component = new Resource();
367         String id = "id";
368         component.setUniqueId(id);
369         GraphVertex graphVertex = getTopologyTemplateVertex();
370         when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
371         when(nodeTypeOperation.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
372         result = testInstance.markComponentToDelete(component);
373         assertEquals(result, StorageOperationStatus.OK);
374     }
375
376     @Test
377     public void testDelToscaComponent() {
378         Either<Component, StorageOperationStatus> result;
379         String componentId = "compId";
380         GraphVertex graphVertex = getTopologyTemplateVertex();
381         ToscaElement toscaElement = getToscaElementForTest();
382         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
383         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
384         result = testInstance.deleteToscaComponent(componentId);
385         assertTrue(result.isLeft());
386     }
387
388     @Test
389     public void testGetLatestByToscaResourceName() {
390         Either<Component, StorageOperationStatus> result;
391         String toscaResourceName = "name";
392         ToscaElement toscaElement = getToscaElementForTest();
393
394         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
395         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
396         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
397         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
398         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
399
400         List<GraphVertex> graphVertexList = new ArrayList<>();
401         GraphVertex graphVertex = getTopologyTemplateVertex();
402         graphVertex.setUniqueId(toscaResourceName);
403         Map<GraphPropertyEnum, Object> props = new HashMap<>();
404         props.put(GraphPropertyEnum.VERSION, "1.0");
405         graphVertex.setMetadataProperties(props);
406         graphVertexList.add(graphVertex);
407
408         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
409         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
410         result = testInstance.getLatestByToscaResourceName(toscaResourceName);
411         assertTrue(result.isLeft());
412     }
413
414     @Test
415     public void testGetFollowed() {
416         Either<Set<Component>, StorageOperationStatus> result;
417         String userId = "id";
418         Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
419         Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
420         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
421         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
422         lifecycleStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
423         lifecycleStates.add(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
424         lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
425         lastStateStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
426         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
427         List<ToscaElement> toscaEleList = new ArrayList<>();
428         ToscaElement toscaElement = getToscaElementForTest();
429         toscaEleList.add(toscaElement);
430         when(nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)).thenReturn(Either.left(toscaEleList));
431         result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
432         assertTrue(result.isLeft());
433         assertEquals(1, result.left().value().size());
434     }
435
436     @Test
437     public void testGetBySystemName() {
438         Either<List<Component>, StorageOperationStatus> result;
439         String sysName = "sysName";
440         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
441         ToscaElement toscaElement = getToscaElementForTest();
442         List<GraphVertex> componentVertices = new ArrayList<>();
443         GraphVertex graphVertex = getTopologyTemplateVertex();
444         componentVertices.add(graphVertex);
445         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
446         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
447
448         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
449         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
450
451         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
452
453         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(componentVertices));
454         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
455         result = testInstance.getBySystemName(componentTypeEnum, sysName);
456         assertTrue(result.isLeft());
457         assertEquals(1, result.left().value().size());
458     }
459
460     @Test
461     public void testGetCompByNameAndVersion() {
462         Either<Component, StorageOperationStatus> result;
463         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
464         String name = "name";
465         String version = "1.0";
466         JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
467         List<GraphVertex> graphVertexList = new ArrayList<>();
468         GraphVertex graphVertex = getTopologyTemplateVertex();
469         graphVertexList.add(graphVertex);
470         ToscaElement toscaElement = getToscaElementForTest();
471         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
472         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
473
474         hasProperties.put(GraphPropertyEnum.NAME, name);
475         hasProperties.put(GraphPropertyEnum.VERSION, version);
476         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
477         hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
478         when(titanDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
479         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
480         result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
481         assertTrue(result.isLeft());
482     }
483
484     private ToscaElement getToscaElementForTest() {
485         ToscaElement toscaElement = new TopologyTemplate();
486         toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
487         return toscaElement;
488     }
489
490     @Test
491     public void addDataTypesToComponentSuccessTest(){
492         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK);
493         assertTrue(result.isLeft());
494     }
495
496     @Test
497     public void addDataTypesToComponentFailureTest_BadRequest(){
498         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
499         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
500     }
501
502     private Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) {
503         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
504         String componentId = "componentid";
505         String Id = "id";
506
507         PropertyDefinition noDefaultProp = new PropertyDefinition();
508         noDefaultProp.setName("noDefaultProp");
509         PropertyDefinition prop1 = new PropertyDefinition();
510         prop1.setDefaultValue("def1");
511         prop1.setName("prop1");
512         PropertyDefinition prop2 = new PropertyDefinition();
513         prop2.setType("dataType1");
514         prop2.setName("prop2");
515         PropertyDefinition prop3 = new PropertyDefinition();
516         prop3.setDefaultValue("def3");
517         prop3.setName("prop3");
518
519         DataTypeDefinition noDefaultValue = new DataTypeDefinition();
520         noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
521         noDefaultValue.setDerivedFromName("name0");
522
523         DataTypeDefinition dataType1 = new DataTypeDefinition();
524         dataType1.setProperties(Arrays.asList(prop1, prop3));
525         dataType1.setName("name1");
526         dataType1.setDerivedFromName("derivedfromname1");
527
528         DataTypeDefinition dataType2 = new DataTypeDefinition();
529         dataType2.setDerivedFrom(dataType1);
530         dataType2.setName("name2");
531         dataType2.setDerivedFromName("derivedfromname2");
532
533         DataTypeDefinition dataType3 = new DataTypeDefinition();
534         dataType3.setProperties(Collections.singletonList(prop2));
535         dataType3.setDerivedFrom(noDefaultValue);
536         dataType3.setName("name3");
537         dataType3.setDerivedFromName("derivedfromname3");
538
539         dataTypes.put("noDefault", noDefaultValue);
540         dataTypes.put("dataType1", dataType1);
541         dataTypes.put("dataType2", dataType2);
542         dataTypes.put("dataType3", dataType3);
543
544         GraphVertex vertex;
545         if(status == StorageOperationStatus.OK){
546             vertex = getTopologyTemplateVertex();
547         } else {
548             vertex = getNodeTypeVertex();
549         }
550         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
551         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
552         when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex),
553                 eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status);
554         return testInstance.addDataTypesToComponent(dataTypes, componentId);
555     }
556
557     @Test
558     public void testDataTypesToComponentFailureTest_NotFound() {
559         Either<List<DataTypeDefinition>, StorageOperationStatus> result;
560         String componentId = "componentId";
561         GraphVertex vertex = getNodeTypeVertex();
562         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
563         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
564         result = testInstance.addDataTypesToComponent(dataTypes, componentId);
565         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
566     }
567
568     @Test
569     public void testDeleteDataTypeOfComponent() {
570         StorageOperationStatus result;
571         Component component = new Resource();
572         String id = "id";
573         component.setUniqueId(id);
574         String datatype = null;
575
576         DataTypeDefinition dataType1 = new DataTypeDefinition();
577         dataType1.setName("name1");
578         Map<String, DataTypeDataDefinition> dataTypeDataMap = new HashMap<>();
579         dataTypeDataMap.put("datatype1", dataType1);
580         List<DataTypeDefinition> dataTypeMap = dataTypeDataMap.values().stream().map(e -> { DataTypeDefinition dataType = new DataTypeDefinition(e);return dataType; }).collect(Collectors.toList());
581         component.setDataTypes(dataTypeMap);
582         GraphVertex graphVertex = getTopologyTemplateVertex();
583         result = testInstance.deleteDataTypeOfComponent(component, "datatype1");
584         assertEquals(datatype, result);
585     }
586
587     private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
588         PolicyDefinition policy = new PolicyDefinition();
589         String componentId = "componentId";
590         int counter = 0;
591         GraphVertex vertex;
592         if(status == StorageOperationStatus.OK){
593             vertex = getTopologyTemplateVertex();
594         } else {
595             vertex = getNodeTypeVertex();
596         }
597         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
598         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
599         when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
600         return testInstance.associatePolicyToComponent(componentId, policy, counter);
601     }
602
603     private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
604         PolicyDefinition policy = new PolicyDefinition();
605         String componentId = "componentId";
606         GraphVertex vertex = getTopologyTemplateVertex();
607         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
608         when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
609         return testInstance.updatePolicyOfComponent(componentId, policy);
610     }
611
612     private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
613         String componentId = "componentId";
614         String policyId = "policyId";
615         GraphVertex vertex = getTopologyTemplateVertex();
616         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
617         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
618         when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
619         StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
620         assertSame(result, status);
621     }
622
623     private List<GraphVertex> getMockVertices(int numOfVertices) {
624         return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
625     }
626
627     private ToscaElement getResourceToscaElement(String id) {
628         ToscaElement toscaElement = new TopologyTemplate();
629         toscaElement.setMetadata(new HashMap<>());
630         toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
631         toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
632         return toscaElement;
633     }
634
635     private GraphVertex getTopologyTemplateVertex() {
636         GraphVertex graphVertex = new GraphVertex();
637         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
638         return graphVertex;
639     }
640
641     private GraphVertex getNodeTypeVertex() {
642         GraphVertex graphVertex = new GraphVertex();
643         graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);
644         return graphVertex;
645     }
646 }