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