a19e7c431fce444719a13e2b5039519fcb6c77c7
[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 static org.assertj.core.api.Assertions.assertThat;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertFalse;
35 import static org.junit.Assert.assertNotNull;
36 import static org.junit.Assert.assertNull;
37 import static org.junit.Assert.assertSame;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.ArgumentMatchers.anyInt;
41 import static org.mockito.ArgumentMatchers.anyMap;
42 import static org.mockito.ArgumentMatchers.anyString;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.doReturn;
45 import static org.mockito.Mockito.times;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
48
49 import fj.data.Either;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collections;
53 import java.util.EnumMap;
54 import java.util.HashMap;
55 import java.util.HashSet;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Set;
59 import java.util.stream.Collectors;
60 import java.util.stream.IntStream;
61 import org.apache.commons.collections.map.HashedMap;
62 import org.apache.commons.lang3.tuple.ImmutablePair;
63 import org.junit.Before;
64 import org.junit.Test;
65 import org.junit.runner.RunWith;
66 import org.mockito.ArgumentCaptor;
67 import org.mockito.ArgumentMatchers;
68 import org.mockito.InjectMocks;
69 import org.mockito.Mock;
70 import org.mockito.Mockito;
71 import org.mockito.MockitoAnnotations;
72 import org.mockito.junit.MockitoJUnitRunner;
73 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
74 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
75 import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao;
76 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
77 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
78 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
79 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
80 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
81 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
82 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
83 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
84 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
85 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
86 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
87 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
88 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
89 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
90 import org.openecomp.sdc.be.model.AttributeDefinition;
91 import org.openecomp.sdc.be.model.Component;
92 import org.openecomp.sdc.be.model.ComponentInstance;
93 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
94 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
95 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
96 import org.openecomp.sdc.be.model.ComponentParametersView;
97 import org.openecomp.sdc.be.model.DataTypeDefinition;
98 import org.openecomp.sdc.be.model.LifecycleStateEnum;
99 import org.openecomp.sdc.be.model.OutputDefinition;
100 import org.openecomp.sdc.be.model.PolicyDefinition;
101 import org.openecomp.sdc.be.model.PropertyDefinition;
102 import org.openecomp.sdc.be.model.Resource;
103 import org.openecomp.sdc.be.model.Service;
104 import org.openecomp.sdc.be.model.User;
105 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
106 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
107 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
108 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
109 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
110 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
111
112 @RunWith(MockitoJUnitRunner.class)
113 public class ToscaOperationFacadeTest {
114
115     private static final String COMPONENT_ID = "componentId";
116     private static final String PROPERTY1_NAME = "prop1";
117     private static final String PROPERTY1_TYPE = "string";
118     private static final String PROPERTY2_NAME = "prop2";
119     private static final String PROPERTY2_TYPE = "integer";
120     private static final String ICON_NAME = "icon";
121     private static final String SERVICE_MODEL_NAME = "Test_Service";
122     private static final String SERVICE_PROXY_INSTANCE0_NAME = "testservice_proxy0";
123     private static final String SERVICE_SUBSTITUTION_INSTANCE0_NAME = "testservice0";
124
125     @InjectMocks
126     private ToscaOperationFacade testInstance;
127
128     @Mock
129     private HealingJanusGraphDao janusGraphDaoMock;
130
131     @Mock
132     private TopologyTemplateOperation topologyTemplateOperationMock;
133
134     @Mock
135     private NodeTypeOperation nodeTypeOperationMock;
136
137     @Mock
138     private NodeTemplateOperation nodeTemplateOperationMock;
139
140     @Before
141     public void setUp() throws Exception {
142         testInstance = new ToscaOperationFacade();
143         MockitoAnnotations.initMocks(this);
144     }
145
146     @SuppressWarnings("unchecked")
147     @Test
148     public void fetchMetaDataByResourceType() throws Exception {
149         ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
150         ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
151         ComponentParametersView dataFilter = new ComponentParametersView();
152         List<GraphVertex> mockVertices = getMockVertices(2);
153         Either<List<GraphVertex>, JanusGraphOperationStatus> returnedVertices = Either.left(mockVertices);
154
155         when(janusGraphDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata)))
156             .thenReturn(returnedVertices);
157         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
158         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
159         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance
160             .fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
161
162         verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
163         verifyCriteriaNotIsDeleted(criteriaNotCapture);
164
165         assertTrue(fetchedComponents.isLeft());
166         List<Component> cmpts = fetchedComponents.left().value();
167         assertEquals(2, cmpts.size());
168         assertEquals("0", cmpts.get(0).getUniqueId());
169         assertEquals("1", cmpts.get(1).getUniqueId());
170     }
171
172     private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
173         Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>) criteriaCapture.getValue();
174         assertEquals(2, criteria.size());
175         assertEquals("VF", criteria.get(GraphPropertyEnum.RESOURCE_TYPE));
176         assertEquals(true, criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION));
177     }
178
179     private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
180         Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>) criteriaNotCapture.getValue();
181         assertEquals(1, notCriteria.size());
182         assertEquals(true, notCriteria.get(GraphPropertyEnum.IS_DELETED));
183     }
184
185     @SuppressWarnings("unchecked")
186     @Test
187     public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
188         when(janusGraphDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(
189             JanusGraphOperationStatus.GENERAL_ERROR));
190         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance
191             .fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
192         assertTrue(fetchedComponents.isRight());
193         assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
194     }
195
196     @Test
197     public void associatePolicyToComponentSuccessTest() {
198         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
199         assertTrue(result.isLeft());
200     }
201
202     @Test
203     public void associatePolicyToComponentFailureTest() {
204         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
205         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
206     }
207
208     @Test
209     public void updatePolicyOfComponentSuccessTest() {
210         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
211         assertTrue(result.isLeft());
212     }
213
214     @Test
215     public void updatePolicyOfComponentFailureTest() {
216         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
217         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
218     }
219
220     @Test
221     public void removePolicyFromComponentSuccessTest() {
222         removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
223     }
224
225     @Test
226     public void removePolicyFromComponentFailureTest() {
227         removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
228     }
229
230     @Test
231     public void testFindLastCertifiedToscaElementByUUID() {
232         Either<Component, StorageOperationStatus> result;
233         Component component = new Resource();
234         List<GraphVertex> list = new ArrayList<>();
235         GraphVertex graphVertex = getTopologyTemplateVertex();
236         list.add(graphVertex);
237         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
238         props.put(GraphPropertyEnum.UUID, component.getUUID());
239         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
240         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
241         ToscaElement toscaElement = getToscaElementForTest();
242         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class)))
243             .thenReturn(Either.left(toscaElement));
244         when(janusGraphDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
245         result = testInstance.findLastCertifiedToscaElementByUUID(component);
246         Component resultComp = result.left().value();
247         assertEquals(resultComp.getToscaType(), ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
248     }
249
250     @Test
251     public void testLatestComponentByToscaResourceName() {
252         Either<Component, StorageOperationStatus> result;
253         TopologyTemplate toscaElement = new TopologyTemplate();
254         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
255         List<GraphVertex> list = new ArrayList<>();
256         GraphVertex graphVertex = getTopologyTemplateVertex();
257         Map<GraphPropertyEnum, Object> props = new HashMap<>();
258         props.put(GraphPropertyEnum.VERSION, "1.0");
259         graphVertex.setMetadataProperties(props);
260         list.add(graphVertex);
261
262         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
263         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
264         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
265         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
266         propertiesToMatch.put(GraphPropertyEnum.MODEL, null);
267         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
268
269         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll))
270             .thenReturn(Either.left(list));
271         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class)))
272             .thenReturn(Either.left(toscaElement));
273
274         result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
275         assertTrue(result.isLeft());
276     }
277
278     @Test
279     public void testValidateCsarUuidUniqueness() {
280         StorageOperationStatus result;
281         String csarUUID = "csarUUID";
282         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
283         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
284         List<GraphVertex> vertexList = new ArrayList<>();
285         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
286         result = testInstance.validateCsarUuidUniqueness(csarUUID);
287         assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
288     }
289
290     @Test
291     public void testValidateCsarUuidUnique_true() {
292         StorageOperationStatus result;
293         String csarUUID = "csarUUID";
294         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
295         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
296         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
297             JanusGraphOperationStatus.NOT_FOUND));
298         result = testInstance.validateCsarUuidUniqueness(csarUUID);
299         assertEquals(StorageOperationStatus.OK, result);
300     }
301
302     @Test
303     public void testGetLatestCertiNodeTypeByToscaResourceName() {
304         Either<Resource, StorageOperationStatus> result;
305         String toscaResourceName = "resourceName";
306         String uniqueId = "uniqueId";
307         GraphVertex graphVertex = getTopologyTemplateVertex();
308         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
309         graphVertex.setUniqueId(uniqueId);
310         List<GraphVertex> vertexList = new ArrayList<>();
311         vertexList.add(graphVertex);
312         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
313         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
314         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
315         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
316         ToscaElement topologyTemplate = new TopologyTemplate();
317         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
318         when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
319         when(janusGraphDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
320         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
321             .thenReturn(Either.left(topologyTemplate));
322         result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
323         assertTrue(result.isLeft());
324     }
325
326     @Test
327     public void testValidateCompExists() {
328         Either<Boolean, StorageOperationStatus> result;
329         String componentId = "componentId";
330         GraphVertex graphVertex = getTopologyTemplateVertex();
331         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
332         result = testInstance.validateComponentExists(componentId);
333         assertEquals(true, result.left().value());
334     }
335
336     @Test
337     public void testValidateCompExists_NotFound() {
338         Either<Boolean, StorageOperationStatus> result;
339         String componentId = "componentId";
340         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
341             JanusGraphOperationStatus.NOT_FOUND));
342         result = testInstance.validateComponentExists(componentId);
343         assertEquals(false, result.left().value());
344     }
345
346     @Test
347     public void testValidateToscaResourceNameExists() {
348         Either<Boolean, StorageOperationStatus> result;
349         String templateName = "templateName";
350         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
351         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
352         List<GraphVertex> graphVertexList = new ArrayList<>();
353         GraphVertex graphVertex = getTopologyTemplateVertex();
354         graphVertexList.add(graphVertex);
355         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
356         result = testInstance.validateToscaResourceNameExists(templateName);
357         assertEquals(true, result.left().value());
358     }
359
360     @Test
361     public void testValidateToscaResourceNameExists_false() {
362         Either<Boolean, StorageOperationStatus> result;
363         String templateName = "templateName";
364         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
365         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
366         List<GraphVertex> graphVertexList = new ArrayList<>();
367         GraphVertex graphVertex = getTopologyTemplateVertex();
368         graphVertexList.add(graphVertex);
369         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
370             JanusGraphOperationStatus.NOT_FOUND));
371         result = testInstance.validateToscaResourceNameExists(templateName);
372         assertEquals(false, result.left().value());
373     }
374
375     @Test
376     public void testOverrideComponent() {
377         Either<Resource, StorageOperationStatus> result;
378         Resource resource = new Resource();
379         String id = "id";
380         resource.setUniqueId(id);
381         GraphVertex graphVertex = getTopologyTemplateVertex();
382         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
383         NodeType nodeType = new NodeType();
384         nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
385         ToscaElement toscaElement = new TopologyTemplate();
386         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
387         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
388         when(janusGraphDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
389         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
390         when(nodeTypeOperationMock.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
391         when(janusGraphDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
392         when(janusGraphDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(
393             JanusGraphOperationStatus.OK);
394         result = testInstance.overrideComponent(resource, resource);
395         assertTrue(result.isLeft());
396     }
397
398     @Test
399     public void testGetToscaElement() {
400         Either<Component, StorageOperationStatus> result;
401         String id = "id";
402         GraphVertex graphVertex = getTopologyTemplateVertex();
403         ToscaElement toscaElement = getToscaElementForTest();
404         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
405         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
406             .thenReturn(Either.left(toscaElement));
407         result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
408         assertTrue(result.isLeft());
409     }
410
411     @Test
412     public void testMarkComponentToDelete() {
413         StorageOperationStatus result;
414         Component component = new Resource();
415         String id = "id";
416         component.setUniqueId(id);
417         GraphVertex graphVertex = getTopologyTemplateVertex();
418         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
419         when(nodeTypeOperationMock.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
420         result = testInstance.markComponentToDelete(component);
421         assertEquals(StorageOperationStatus.OK, result);
422     }
423
424     @Test
425     public void testDelToscaComponent() {
426         Either<Component, StorageOperationStatus> result;
427         String componentId = "compId";
428         GraphVertex graphVertex = getTopologyTemplateVertex();
429         ToscaElement toscaElement = getToscaElementForTest();
430         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
431         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
432         result = testInstance.deleteToscaComponent(componentId);
433         assertTrue(result.isLeft());
434     }
435
436     @Test
437     public void testGetLatestByToscaResourceName() {
438         Either<Component, StorageOperationStatus> result;
439         String toscaResourceName = "name";
440         ToscaElement toscaElement = getToscaElementForTest();
441
442         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
443         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
444         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
445         propertiesToMatch.put(GraphPropertyEnum.MODEL, null);
446         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
447         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
448
449         List<GraphVertex> graphVertexList = new ArrayList<>();
450         GraphVertex graphVertex = getTopologyTemplateVertex();
451         graphVertex.setUniqueId(toscaResourceName);
452         Map<GraphPropertyEnum, Object> props = new HashMap<>();
453         props.put(GraphPropertyEnum.VERSION, "1.0");
454         graphVertex.setMetadataProperties(props);
455         graphVertexList.add(graphVertex);
456
457         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata))
458             .thenReturn(Either.left(graphVertexList));
459         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
460             .thenReturn(Either.left(toscaElement));
461         result = testInstance.getLatestByToscaResourceName(toscaResourceName);
462         assertTrue(result.isLeft());
463     }
464
465
466     @Test
467     public void testGetLatestResourceByToscaResourceName() {
468         Either<Resource, StorageOperationStatus> result;
469         String toscaResourceName = "org.openecomp.resource.vf";
470         ToscaElement toscaElement = getToscaElementForTest();
471
472         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
473         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
474         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
475         if (!toscaResourceName.contains("org.openecomp.resource.vf")) {
476             propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
477         }
478
479         List<GraphVertex> graphVertexList = new ArrayList<>();
480         GraphVertex graphVertex = getTopologyTemplateVertex();
481         graphVertex.setUniqueId(toscaResourceName);
482         Map<JsonPresentationFields, Object> props = new HashMap<>();
483         props.put(JsonPresentationFields.VERSION, "1.0");
484         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, props.get(JsonPresentationFields.VERSION));
485         graphVertexList.add(graphVertex);
486
487         when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, JsonParseFlagEnum.ParseMetadata))
488             .thenReturn(Either.left(graphVertexList));
489         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
490             .thenReturn(Either.left(toscaElement));
491
492         when(janusGraphDaoMock.getVertexById(toscaResourceName, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
493
494         result = testInstance.getLatestResourceByToscaResourceName(toscaResourceName);
495         assertTrue(result.isLeft());
496     }
497
498     @Test
499     public void testGetFollowed() {
500         Either<Set<Component>, StorageOperationStatus> result;
501         String userId = "id";
502         Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
503         Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
504         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
505         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
506         lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
507         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
508         List<ToscaElement> toscaEleList = new ArrayList<>();
509         ToscaElement toscaElement = getToscaElementForTest();
510         toscaEleList.add(toscaElement);
511         when(nodeTypeOperationMock.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType))
512             .thenReturn(Either.left(toscaEleList));
513         result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
514         assertTrue(result.isLeft());
515         assertEquals(1, result.left().value().size());
516     }
517
518     @Test
519     public void testGetBySystemName() {
520         Either<List<Component>, StorageOperationStatus> result;
521         String sysName = "sysName";
522         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
523         ToscaElement toscaElement = getToscaElementForTest();
524         List<GraphVertex> componentVertices = new ArrayList<>();
525         GraphVertex graphVertex = getTopologyTemplateVertex();
526         componentVertices.add(graphVertex);
527         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
528         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
529
530         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
531         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
532
533         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
534
535         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll))
536             .thenReturn(Either.left(componentVertices));
537         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
538             .thenReturn(Either.left(toscaElement));
539         result = testInstance.getBySystemName(componentTypeEnum, sysName);
540         assertTrue(result.isLeft());
541         assertEquals(1, result.left().value().size());
542     }
543
544     @Test
545     public void testGetCompByNameAndVersion() {
546         Either<Component, StorageOperationStatus> result;
547         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
548         String name = "name";
549         String version = "1.0";
550         JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
551         List<GraphVertex> graphVertexList = new ArrayList<>();
552         GraphVertex graphVertex = getTopologyTemplateVertex();
553         graphVertexList.add(graphVertex);
554         ToscaElement toscaElement = getToscaElementForTest();
555         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
556         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
557
558         hasProperties.put(GraphPropertyEnum.NAME, name);
559         hasProperties.put(GraphPropertyEnum.VERSION, version);
560         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
561         hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
562         when(janusGraphDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
563         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
564             .thenReturn(Either.left(toscaElement));
565         result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
566         assertTrue(result.isLeft());
567     }
568
569     private ToscaElement getToscaElementForTest() {
570         ToscaElement toscaElement = new TopologyTemplate();
571         toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
572         return toscaElement;
573     }
574
575     @Test
576     public void addDataTypesToComponentSuccessTest() {
577         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK);
578         assertTrue(result.isLeft());
579     }
580
581     @Test
582     public void addDataTypesToComponentFailureTest_BadRequest() {
583         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
584         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
585     }
586
587     private Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) {
588         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
589         String componentId = "componentid";
590         String Id = "id";
591
592         PropertyDefinition noDefaultProp = new PropertyDefinition();
593         noDefaultProp.setName("noDefaultProp");
594         PropertyDefinition prop1 = new PropertyDefinition();
595         prop1.setDefaultValue("def1");
596         prop1.setName("prop1");
597         PropertyDefinition prop2 = new PropertyDefinition();
598         prop2.setType("dataType1");
599         prop2.setName("prop2");
600         PropertyDefinition prop3 = new PropertyDefinition();
601         prop3.setDefaultValue("def3");
602         prop3.setName("prop3");
603
604         DataTypeDefinition noDefaultValue = new DataTypeDefinition();
605         noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
606         noDefaultValue.setDerivedFromName("name0");
607
608         DataTypeDefinition dataType1 = new DataTypeDefinition();
609         dataType1.setProperties(Arrays.asList(prop1, prop3));
610         dataType1.setName("name1");
611         dataType1.setDerivedFromName("derivedfromname1");
612
613         DataTypeDefinition dataType2 = new DataTypeDefinition();
614         dataType2.setDerivedFrom(dataType1);
615         dataType2.setName("name2");
616         dataType2.setDerivedFromName("derivedfromname2");
617
618         DataTypeDefinition dataType3 = new DataTypeDefinition();
619         dataType3.setProperties(Collections.singletonList(prop2));
620         dataType3.setDerivedFrom(noDefaultValue);
621         dataType3.setName("name3");
622         dataType3.setDerivedFromName("derivedfromname3");
623
624         dataTypes.put("noDefault", noDefaultValue);
625         dataTypes.put("dataType1", dataType1);
626         dataTypes.put("dataType2", dataType2);
627         dataTypes.put("dataType3", dataType3);
628
629         GraphVertex vertex;
630         if (status == StorageOperationStatus.OK) {
631             vertex = getTopologyTemplateVertex();
632         } else {
633             vertex = getNodeTypeVertex();
634         }
635         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
636         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
637         when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex),
638             eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status);
639         return testInstance.addDataTypesToComponent(dataTypes, componentId);
640     }
641
642     @Test
643     public void testDataTypesToComponentFailureTest_NotFound() {
644         Either<List<DataTypeDefinition>, StorageOperationStatus> result;
645         String componentId = "componentId";
646         GraphVertex vertex = getNodeTypeVertex();
647         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
648         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
649             JanusGraphOperationStatus.NOT_FOUND));
650         result = testInstance.addDataTypesToComponent(dataTypes, componentId);
651         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
652     }
653
654     @Test
655     public void testDeleteDataTypeOfComponent() {
656         Component component = new Resource();
657         String id = "id";
658         component.setUniqueId(id);
659
660         DataTypeDefinition dataType1 = new DataTypeDefinition();
661         dataType1.setName("name1");
662         Map<String, DataTypeDataDefinition> dataTypeDataMap = new HashMap<>();
663         dataTypeDataMap.put("datatype1", dataType1);
664         List<DataTypeDefinition> dataTypeMap = dataTypeDataMap.values().stream().map(e -> {
665             return new DataTypeDefinition(e);
666         }).collect(Collectors.toList());
667         component.setDataTypes(dataTypeMap);
668         GraphVertex graphVertex = getTopologyTemplateVertex();
669         assertNull(testInstance.deleteDataTypeOfComponent(component, "datatype1"));
670     }
671
672     @Test
673     public void testAddComponentInstancePropertiesToComponent() {
674         // set up component object
675         Component component = new Resource();
676         component.setUniqueId(COMPONENT_ID);
677         List<ComponentInstanceProperty> instanceProps = new ArrayList<>();
678         ComponentInstanceProperty instanceProp = new ComponentInstanceProperty();
679         instanceProp.setName(PROPERTY1_NAME);
680         instanceProp.setType(PROPERTY1_TYPE);
681         instanceProps.add(instanceProp);
682         instanceProp = new ComponentInstanceProperty();
683         instanceProp.setName(PROPERTY2_NAME);
684         instanceProp.setType(PROPERTY2_TYPE);
685         instanceProps.add(instanceProp);
686         Map<String, List<ComponentInstanceProperty>> instancePropsMap =
687             Collections.singletonMap(COMPONENT_ID, instanceProps);
688         component.setComponentInstancesProperties(Collections.singletonMap(COMPONENT_ID, new ArrayList<>()));
689
690         when(nodeTemplateOperationMock.addComponentInstanceProperty(any(), any(), any()))
691             .thenReturn(StorageOperationStatus.OK);
692
693         Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> result =
694             testInstance.addComponentInstancePropertiesToComponent(component, instancePropsMap);
695         assertTrue(result.isLeft());
696         verify(nodeTemplateOperationMock, times(2)).addComponentInstanceProperty(any(), any(), any());
697         List<ComponentInstanceProperty> resultProps = result.left().value().get(COMPONENT_ID);
698         assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY1_NAME)));
699         assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY2_NAME)));
700     }
701
702     @Test
703     public void testAddComponentInstanceToTopologyTemplate_ServiceProxy() {
704         Component containerComponent = new Service();
705         Component originalComponent = new Service();
706         ComponentInstance componentInstance = new ComponentInstance();
707         ComponentInstance existingComponentInstance = new ComponentInstance();
708         User user = new User();
709
710         containerComponent.setComponentType(ComponentTypeEnum.SERVICE);
711
712         originalComponent.setComponentType(ComponentTypeEnum.SERVICE);
713         originalComponent.setIcon(ICON_NAME);
714
715         componentInstance.setOriginType(OriginTypeEnum.ServiceProxy);
716         componentInstance.setSourceModelName(SERVICE_MODEL_NAME);
717
718         List<ComponentInstance> existingInstances = new ArrayList<>();
719         existingComponentInstance.setNormalizedName(SERVICE_PROXY_INSTANCE0_NAME);
720         existingInstances.add(existingComponentInstance);
721         containerComponent.setComponentInstances(existingInstances);
722
723         when(nodeTemplateOperationMock
724             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user)))
725             .thenReturn(Either.left(new ImmutablePair<>(new TopologyTemplate(), COMPONENT_ID)));
726         TopologyTemplate topologyTemplate = new TopologyTemplate();
727         // preset COMPONENT_TYPE field for internal ModelConverter call
728         topologyTemplate.setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
729         when(topologyTemplateOperationMock.getToscaElement(containerComponent.getUniqueId()))
730             .thenReturn(Either.left(topologyTemplate));
731
732         Either<ImmutablePair<Component, String>, StorageOperationStatus> result =
733             testInstance.addComponentInstanceToTopologyTemplate(
734                 containerComponent, originalComponent, componentInstance, false, user);
735
736         assertTrue(result.isLeft());
737         assertEquals(ICON_NAME, componentInstance.getIcon());
738         assertEquals(COMPONENT_ID, result.left().value().getRight());
739         // the instance counter must be 1 because the service proxy instance with suffix 0 already exists.
740         verify(nodeTemplateOperationMock, times(1))
741             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user));
742     }
743
744     @Test
745     public void testAddComponentInstanceToTopologyTemplate_ServiceSubstitution() {
746         Component containerComponent = new Service();
747         Component originalComponent = new Service();
748         ComponentInstance componentInstance = new ComponentInstance();
749         ComponentInstance existingComponentInstance = new ComponentInstance();
750         User user = new User();
751
752         containerComponent.setComponentType(ComponentTypeEnum.SERVICE);
753
754         originalComponent.setComponentType(ComponentTypeEnum.SERVICE);
755         originalComponent.setIcon(ICON_NAME);
756
757         componentInstance.setOriginType(OriginTypeEnum.ServiceSubstitution);
758         componentInstance.setSourceModelName(SERVICE_MODEL_NAME);
759
760         List<ComponentInstance> existingInstances = new ArrayList<>();
761         existingComponentInstance.setNormalizedName(SERVICE_SUBSTITUTION_INSTANCE0_NAME);
762         existingInstances.add(existingComponentInstance);
763         containerComponent.setComponentInstances(existingInstances);
764
765         when(nodeTemplateOperationMock
766             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user)))
767             .thenReturn(Either.left(new ImmutablePair<>(new TopologyTemplate(), COMPONENT_ID)));
768         TopologyTemplate topologyTemplate = new TopologyTemplate();
769         topologyTemplate.setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
770         when(topologyTemplateOperationMock.getToscaElement(containerComponent.getUniqueId()))
771             .thenReturn(Either.left(topologyTemplate));
772
773         Either<ImmutablePair<Component, String>, StorageOperationStatus> result =
774             testInstance.addComponentInstanceToTopologyTemplate(
775                 containerComponent, originalComponent, componentInstance, false, user);
776
777         assertTrue(result.isLeft());
778         assertEquals(ICON_NAME, componentInstance.getIcon());
779         assertEquals(COMPONENT_ID, result.left().value().getRight());
780         verify(nodeTemplateOperationMock, times(1))
781             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user));
782     }
783
784     @Test
785     public void testUpdateComponentInstanceRequirement() {
786         String containerComponentId = "containerComponentId";
787         String componentInstanceUniqueId = "componentInstanceUniqueId";
788         RequirementDataDefinition requirementDataDefinition = Mockito.mock(RequirementDataDefinition.class);
789
790         when(nodeTemplateOperationMock.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition))
791             .thenReturn(StorageOperationStatus.OK);
792         StorageOperationStatus result = testInstance
793             .updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
794         assertEquals(StorageOperationStatus.OK, result);
795         verify(nodeTemplateOperationMock, times(1))
796             .updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
797
798     }
799
800     @Test
801     public void associateCapabilitiesToServiceFailureTest() {
802         StorageOperationStatus result = associateCapabilitiesToServiceWithStatus(StorageOperationStatus.BAD_REQUEST);
803         assertSame(StorageOperationStatus.BAD_REQUEST, result);
804     }
805
806     @Test
807     public void associateCapabilitiesToServiceSuccessTest() {
808         StorageOperationStatus result = associateCapabilitiesToServiceWithStatus(StorageOperationStatus.OK);
809         assertSame(StorageOperationStatus.OK, result);
810     }
811
812     private StorageOperationStatus associateCapabilitiesToServiceWithStatus(StorageOperationStatus status) {
813         Map<String, ListCapabilityDataDefinition> capabilitiesMap = new HashedMap();
814         String componentId = "componentid";
815
816         ListCapabilityDataDefinition listCapabilityDataDefinition1 = new ListCapabilityDataDefinition();
817         capabilitiesMap.put("capabilities1", listCapabilityDataDefinition1);
818
819         GraphVertex vertex;
820         if (status == StorageOperationStatus.OK) {
821             vertex = getTopologyTemplateVertex();
822         } else {
823             vertex = getNodeTypeVertex();
824         }
825
826         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
827         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
828         when(topologyTemplateOperationMock.associateElementToData(eq(vertex),
829             eq(VertexTypeEnum.CAPABILITIES), eq(EdgeLabelEnum.CAPABILITIES), anyMap())).thenReturn(Either.right(status));
830         return testInstance.associateCapabilitiesToService(capabilitiesMap, componentId);
831     }
832
833     @Test
834     public void associateRequirementsToServiceFailureTest() {
835         StorageOperationStatus result = associateRequirementsToServiceWithStatus(StorageOperationStatus.BAD_REQUEST);
836         assertSame(StorageOperationStatus.BAD_REQUEST, result);
837     }
838
839     @Test
840     public void associateRequirementsToServiceSuccessTest() {
841         StorageOperationStatus result = associateRequirementsToServiceWithStatus(StorageOperationStatus.OK);
842         assertSame(StorageOperationStatus.OK, result);
843     }
844
845     @Test
846     public void test_addOutputsToComponent() {
847         final GraphVertex graphVertex = getTopologyTemplateVertex();
848         final String componentId = "componentId";
849
850         doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(componentId, JsonParseFlagEnum.NoParse);
851         doReturn(StorageOperationStatus.OK).when(topologyTemplateOperationMock)
852             .addToscaDataToToscaElement(
853                 any(GraphVertex.class), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyMap(), eq(JsonPresentationFields.NAME));
854
855         final Map<String, OutputDefinition> outputs = new HashMap<>();
856         final OutputDefinition outputDefinition = new OutputDefinition();
857         outputs.put("mock", outputDefinition);
858         final Either<List<OutputDefinition>, StorageOperationStatus> result = testInstance.addOutputsToComponent(outputs, componentId);
859         assertNotNull(result);
860         assertTrue(result.isLeft());
861         assertFalse(result.left().value().isEmpty());
862         assertThat(result.left().value().get(0)).isInstanceOf(OutputDefinition.class);
863         verify(janusGraphDaoMock, times(1)).getVertexById(componentId, JsonParseFlagEnum.NoParse);
864         verify(topologyTemplateOperationMock, times(1)).addToscaDataToToscaElement(
865             any(GraphVertex.class), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyMap(), eq(JsonPresentationFields.NAME));
866     }
867
868     @Test
869     public void test_addComponentInstanceOutputsToComponent_updateComponentInstanceOutput() {
870         final Component component = new Resource();
871         component.setUniqueId(COMPONENT_ID);
872         final Map<String, List<ComponentInstanceOutput>> map = new HashMap<>();
873         final List<ComponentInstanceOutput> componentInstanceOutputList = new ArrayList<>();
874         final ComponentInstanceOutput componentInstanceOutput = new ComponentInstanceOutput();
875         componentInstanceOutput.setComponentInstanceId(COMPONENT_ID);
876         componentInstanceOutput.setComponentInstanceName(COMPONENT_ID);
877         componentInstanceOutput.setName(COMPONENT_ID);
878         componentInstanceOutputList.add(componentInstanceOutput);
879         map.put("mock", componentInstanceOutputList);
880         component.setComponentInstancesOutputs(map);
881
882         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock)
883             .updateComponentInstanceOutput(any(Component.class), anyString(), any(ComponentInstanceOutput.class));
884
885         final Either<Map<String, List<ComponentInstanceOutput>>, StorageOperationStatus> result
886             = testInstance.addComponentInstanceOutputsToComponent(component, map);
887         assertNotNull(result);
888         assertTrue(result.isLeft());
889         assertFalse(result.left().value().isEmpty());
890         assertSame(result.left().value(), map);
891         verify(nodeTemplateOperationMock, times(1))
892             .updateComponentInstanceOutput(any(Component.class), anyString(), any(ComponentInstanceOutput.class));
893
894     }
895
896     @Test
897     public void test_addComponentInstanceOutputsToComponent_addComponentInstanceOutput() {
898         final Component component = new Resource();
899         component.setUniqueId(COMPONENT_ID);
900         Map<String, List<ComponentInstanceOutput>> map = new HashMap<>();
901         List<ComponentInstanceOutput> componentInstanceOutputList = new ArrayList<>();
902         ComponentInstanceOutput componentInstanceOutput = new ComponentInstanceOutput();
903         componentInstanceOutput.setComponentInstanceId(COMPONENT_ID);
904         componentInstanceOutput.setComponentInstanceName(COMPONENT_ID);
905         componentInstanceOutput.setName(COMPONENT_ID);
906         componentInstanceOutputList.add(componentInstanceOutput);
907         map.put("mock", componentInstanceOutputList);
908         component.setComponentInstancesOutputs(map);
909
910         map = new HashMap<>();
911         componentInstanceOutputList = new ArrayList<>();
912         componentInstanceOutput = new ComponentInstanceOutput();
913         componentInstanceOutput.setComponentInstanceId("mock");
914         componentInstanceOutput.setComponentInstanceName("mock");
915         componentInstanceOutput.setName("mock");
916         componentInstanceOutputList.add(componentInstanceOutput);
917         map.put("mock", componentInstanceOutputList);
918
919         final Either<Map<String, List<ComponentInstanceOutput>>, StorageOperationStatus> result = testInstance
920             .addComponentInstanceOutputsToComponent(component, map);
921         assertNotNull(result);
922         assertTrue(result.isRight());
923     }
924
925     @Test
926     public void test_addComponentInstanceAttributesToComponent() {
927         final Component component = new Resource();
928         component.setUniqueId(COMPONENT_ID);
929         Map<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
930         List<ComponentInstanceAttribute> componentInstanceOutputList = new ArrayList<>();
931         ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
932         componentInstanceAttribute.setComponentInstanceId(COMPONENT_ID);
933         componentInstanceAttribute.setUniqueId(COMPONENT_ID);
934         componentInstanceOutputList.add(componentInstanceAttribute);
935         map.put("mock", componentInstanceOutputList);
936         component.setComponentInstancesAttributes(map);
937
938         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock)
939             .updateComponentInstanceAttribute(any(Component.class), anyString(), any(ComponentInstanceAttribute.class));
940
941         final Either<Map<String, List<ComponentInstanceAttribute>>, StorageOperationStatus> result
942             = testInstance.addComponentInstanceAttributesToComponent(component, map);
943
944         assertNotNull(result);
945         assertTrue(result.isLeft());
946         assertFalse(result.left().value().isEmpty());
947         assertSame(result.left().value(), map);
948         verify(nodeTemplateOperationMock, times(1))
949             .updateComponentInstanceAttribute(any(Component.class), anyString(), any(ComponentInstanceAttribute.class));
950     }
951
952     @Test
953     public void test_updateAttributeOfComponent_success() {
954         final GraphVertex graphVertex = getTopologyTemplateVertex();
955         final String componentId = "componentId";
956         final Component component = new Resource();
957         component.setUniqueId(COMPONENT_ID);
958         doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock).updateToscaDataOfToscaElement(
959             anyString(), eq(EdgeLabelEnum.ATTRIBUTES), eq(VertexTypeEnum.ATTRIBUTES), any(AttributeDefinition.class),
960             eq(JsonPresentationFields.NAME));
961         doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(eq(componentId), any(JsonParseFlagEnum.class));
962
963         final ToscaElement toscaElement = getToscaElementForTest();
964         final Map<String, AttributeDataDefinition> attributes = new HashMap<>();
965         final AttributeDefinition attributeDefinition = new AttributeDefinition();
966         attributeDefinition.setName("mock");
967         attributes.put("mock", attributeDefinition);
968         toscaElement.setAttributes(attributes);
969         doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock)
970             .getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class));
971
972         final Either<AttributeDefinition, StorageOperationStatus> result
973             = testInstance.updateAttributeOfComponent(component, attributeDefinition);
974         assertNotNull(result);
975     }
976
977     @Test
978     public void test_updateAttributeOfComponent_isNotPresent() {
979         final GraphVertex graphVertex = getTopologyTemplateVertex();
980         final String componentId = "componentId";
981         final Component component = new Resource();
982         component.setUniqueId(COMPONENT_ID);
983         doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock).updateToscaDataOfToscaElement(
984             anyString(), eq(EdgeLabelEnum.ATTRIBUTES), eq(VertexTypeEnum.ATTRIBUTES), any(AttributeDefinition.class),
985             eq(JsonPresentationFields.NAME));
986         doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(eq(componentId), any(JsonParseFlagEnum.class));
987
988         final ToscaElement toscaElement = getToscaElementForTest();
989         final Map<String, AttributeDataDefinition> attributes = new HashMap<>();
990         final AttributeDefinition attributeDefinition = new AttributeDefinition();
991         attributeDefinition.setName("mock");
992         attributes.put("mock", attributeDefinition);
993         toscaElement.setAttributes(attributes);
994         doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock)
995             .getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class));
996
997         final AttributeDefinition attributeDefinitionOneMore = new AttributeDefinition();
998         attributeDefinitionOneMore.setName("Anothermock");
999
1000         final Either<AttributeDefinition, StorageOperationStatus> result
1001             = testInstance.updateAttributeOfComponent(component, attributeDefinitionOneMore);
1002         assertNotNull(result);
1003     }
1004
1005     @Test
1006     public void test_updateComponentInstanceAttributes() {
1007         final GraphVertex graphVertex = getTopologyTemplateVertex();
1008         final String componentId = "componentId";
1009         final Component component = new Resource();
1010         component.setUniqueId(COMPONENT_ID);
1011
1012         final List<ComponentInstanceAttribute> attributes = new ArrayList<>();
1013         final ComponentInstanceAttribute attributeDefinition = new ComponentInstanceAttribute();
1014         attributeDefinition.setName("mock");
1015         attributes.add(attributeDefinition);
1016
1017         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock).updateComponentInstanceAttributes(component, componentId, attributes);
1018
1019         final StorageOperationStatus result = testInstance.updateComponentInstanceAttributes(component, componentId, attributes);
1020         assertNotNull(result);
1021         assertEquals(StorageOperationStatus.OK, result);
1022         verify(nodeTemplateOperationMock, times(1)).updateComponentInstanceAttributes(component, componentId, attributes);
1023     }
1024
1025     @Test
1026     public void test_updateComponentInstanceOutputs() {
1027         final GraphVertex graphVertex = getTopologyTemplateVertex();
1028         final String componentId = "componentId";
1029         final Component component = new Resource();
1030         component.setUniqueId(COMPONENT_ID);
1031
1032         final List<ComponentInstanceOutput> list = new ArrayList<>();
1033         final ComponentInstanceOutput output = new ComponentInstanceOutput();
1034         output.setName("mock");
1035         list.add(output);
1036
1037         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock).updateComponentInstanceOutputs(component, componentId, list);
1038
1039         final StorageOperationStatus result = testInstance.updateComponentInstanceOutputs(component, componentId, list);
1040         assertNotNull(result);
1041         assertEquals(StorageOperationStatus.OK, result);
1042         verify(nodeTemplateOperationMock, times(1)).updateComponentInstanceOutputs(component, componentId, list);
1043     }
1044
1045     @Test
1046     public void test_deleteOutputOfResource() {
1047         final Component component = new Resource();
1048         component.setUniqueId(COMPONENT_ID);
1049
1050         doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock)
1051             .deleteToscaDataElement(anyString(), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyString(), eq(JsonPresentationFields.NAME));
1052
1053         final StorageOperationStatus result = testInstance.deleteOutputOfResource(component, "mock");
1054         assertNotNull(result);
1055         assertEquals(StorageOperationStatus.OK, result);
1056         verify(nodeTypeOperationMock, times(1))
1057             .deleteToscaDataElement(anyString(), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyString(), eq(JsonPresentationFields.NAME));
1058     }
1059
1060     private StorageOperationStatus associateRequirementsToServiceWithStatus(StorageOperationStatus status) {
1061         Map<String, ListRequirementDataDefinition> requirementsMap = new HashedMap();
1062         String componentId = "componentid";
1063
1064         ListRequirementDataDefinition listRequirementDataDefinition1 = new ListRequirementDataDefinition();
1065         requirementsMap.put("requirements1", listRequirementDataDefinition1);
1066
1067         GraphVertex vertex;
1068         if (status == StorageOperationStatus.OK) {
1069             vertex = getTopologyTemplateVertex();
1070         } else {
1071             vertex = getNodeTypeVertex();
1072         }
1073
1074         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
1075         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
1076         when(topologyTemplateOperationMock.associateElementToData(eq(vertex),
1077             eq(VertexTypeEnum.REQUIREMENTS), eq(EdgeLabelEnum.REQUIREMENTS), anyMap())).thenReturn(Either.right(status));
1078         return testInstance.associateRequirementsToService(requirementsMap, componentId);
1079     }
1080
1081     private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
1082         PolicyDefinition policy = new PolicyDefinition();
1083         String componentId = "componentId";
1084         int counter = 0;
1085         GraphVertex vertex;
1086         if (status == StorageOperationStatus.OK) {
1087             vertex = getTopologyTemplateVertex();
1088         } else {
1089             vertex = getNodeTypeVertex();
1090         }
1091         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
1092         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)).thenReturn(getVertexEither);
1093         when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
1094         return testInstance.associatePolicyToComponent(componentId, policy, counter);
1095     }
1096
1097     private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
1098         PolicyDefinition policy = new PolicyDefinition();
1099         String componentId = "componentId";
1100         GraphVertex vertex = getTopologyTemplateVertex();
1101         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(vertex));
1102         when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
1103         return testInstance.updatePolicyOfComponent(componentId, policy, PromoteVersionEnum.NONE);
1104     }
1105
1106     private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
1107         String componentId = "componentId";
1108         String policyId = "policyId";
1109         GraphVertex vertex = getTopologyTemplateVertex();
1110         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
1111         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
1112         when(topologyTemplateOperationMock.removePolicyFromToscaElement(vertex, policyId)).thenReturn(status);
1113         StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
1114         assertSame(result, status);
1115     }
1116
1117     private List<GraphVertex> getMockVertices(int numOfVertices) {
1118         return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
1119     }
1120
1121     private ToscaElement getResourceToscaElement(String id) {
1122         ToscaElement toscaElement = new TopologyTemplate();
1123         toscaElement.setMetadata(new HashMap<>());
1124         toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
1125         toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
1126         return toscaElement;
1127     }
1128
1129     private GraphVertex getTopologyTemplateVertex() {
1130         GraphVertex graphVertex = new GraphVertex();
1131         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
1132         return graphVertex;
1133     }
1134
1135     private GraphVertex getNodeTypeVertex() {
1136         GraphVertex graphVertex = new GraphVertex();
1137         graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);
1138         return graphVertex;
1139     }
1140 }