b0f2fa5010e0cff330728aa320822c1e32895a9c
[sdc.git] /
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         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
267
268         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll))
269             .thenReturn(Either.left(list));
270         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class)))
271             .thenReturn(Either.left(toscaElement));
272
273         result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
274         assertTrue(result.isLeft());
275     }
276
277     @Test
278     public void testValidateCsarUuidUniqueness() {
279         StorageOperationStatus result;
280         String csarUUID = "csarUUID";
281         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
282         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
283         List<GraphVertex> vertexList = new ArrayList<>();
284         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
285         result = testInstance.validateCsarUuidUniqueness(csarUUID);
286         assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
287     }
288
289     @Test
290     public void testValidateCsarUuidUnique_true() {
291         StorageOperationStatus result;
292         String csarUUID = "csarUUID";
293         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
294         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
295         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
296             JanusGraphOperationStatus.NOT_FOUND));
297         result = testInstance.validateCsarUuidUniqueness(csarUUID);
298         assertEquals(StorageOperationStatus.OK, result);
299     }
300
301     @Test
302     public void testGetLatestCertiNodeTypeByToscaResourceName() {
303         Either<Resource, StorageOperationStatus> result;
304         String toscaResourceName = "resourceName";
305         String uniqueId = "uniqueId";
306         GraphVertex graphVertex = getTopologyTemplateVertex();
307         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
308         graphVertex.setUniqueId(uniqueId);
309         List<GraphVertex> vertexList = new ArrayList<>();
310         vertexList.add(graphVertex);
311         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
312         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
313         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
314         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
315         ToscaElement topologyTemplate = new TopologyTemplate();
316         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
317         when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
318         when(janusGraphDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
319         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
320             .thenReturn(Either.left(topologyTemplate));
321         result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
322         assertTrue(result.isLeft());
323     }
324
325     @Test
326     public void testValidateCompExists() {
327         Either<Boolean, StorageOperationStatus> result;
328         String componentId = "componentId";
329         GraphVertex graphVertex = getTopologyTemplateVertex();
330         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
331         result = testInstance.validateComponentExists(componentId);
332         assertEquals(true, result.left().value());
333     }
334
335     @Test
336     public void testValidateCompExists_NotFound() {
337         Either<Boolean, StorageOperationStatus> result;
338         String componentId = "componentId";
339         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
340             JanusGraphOperationStatus.NOT_FOUND));
341         result = testInstance.validateComponentExists(componentId);
342         assertEquals(false, result.left().value());
343     }
344
345     @Test
346     public void testValidateToscaResourceNameExists() {
347         Either<Boolean, StorageOperationStatus> result;
348         String templateName = "templateName";
349         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
350         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
351         List<GraphVertex> graphVertexList = new ArrayList<>();
352         GraphVertex graphVertex = getTopologyTemplateVertex();
353         graphVertexList.add(graphVertex);
354         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
355         result = testInstance.validateToscaResourceNameExists(templateName);
356         assertEquals(true, result.left().value());
357     }
358
359     @Test
360     public void testValidateToscaResourceNameExists_false() {
361         Either<Boolean, StorageOperationStatus> result;
362         String templateName = "templateName";
363         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
364         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
365         List<GraphVertex> graphVertexList = new ArrayList<>();
366         GraphVertex graphVertex = getTopologyTemplateVertex();
367         graphVertexList.add(graphVertex);
368         when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
369             JanusGraphOperationStatus.NOT_FOUND));
370         result = testInstance.validateToscaResourceNameExists(templateName);
371         assertEquals(false, result.left().value());
372     }
373
374     @Test
375     public void testOverrideComponent() {
376         Either<Resource, StorageOperationStatus> result;
377         Resource resource = new Resource();
378         String id = "id";
379         resource.setUniqueId(id);
380         GraphVertex graphVertex = getTopologyTemplateVertex();
381         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
382         NodeType nodeType = new NodeType();
383         nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
384         ToscaElement toscaElement = new TopologyTemplate();
385         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
386         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
387         when(janusGraphDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
388         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
389         when(nodeTypeOperationMock.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
390         when(janusGraphDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
391         when(janusGraphDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(
392             JanusGraphOperationStatus.OK);
393         result = testInstance.overrideComponent(resource, resource);
394         assertTrue(result.isLeft());
395     }
396
397     @Test
398     public void testGetToscaElement() {
399         Either<Component, StorageOperationStatus> result;
400         String id = "id";
401         GraphVertex graphVertex = getTopologyTemplateVertex();
402         ToscaElement toscaElement = getToscaElementForTest();
403         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
404         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
405             .thenReturn(Either.left(toscaElement));
406         result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
407         assertTrue(result.isLeft());
408     }
409
410     @Test
411     public void testMarkComponentToDelete() {
412         StorageOperationStatus result;
413         Component component = new Resource();
414         String id = "id";
415         component.setUniqueId(id);
416         GraphVertex graphVertex = getTopologyTemplateVertex();
417         when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
418         when(nodeTypeOperationMock.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
419         result = testInstance.markComponentToDelete(component);
420         assertEquals(StorageOperationStatus.OK, result);
421     }
422
423     @Test
424     public void testDelToscaComponent() {
425         Either<Component, StorageOperationStatus> result;
426         String componentId = "compId";
427         GraphVertex graphVertex = getTopologyTemplateVertex();
428         ToscaElement toscaElement = getToscaElementForTest();
429         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
430         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
431         result = testInstance.deleteToscaComponent(componentId);
432         assertTrue(result.isLeft());
433     }
434
435     @Test
436     public void testGetLatestByToscaResourceName() {
437         Either<Component, StorageOperationStatus> result;
438         String toscaResourceName = "name";
439         ToscaElement toscaElement = getToscaElementForTest();
440
441         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
442         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
443         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
444         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
445         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
446
447         List<GraphVertex> graphVertexList = new ArrayList<>();
448         GraphVertex graphVertex = getTopologyTemplateVertex();
449         graphVertex.setUniqueId(toscaResourceName);
450         Map<GraphPropertyEnum, Object> props = new HashMap<>();
451         props.put(GraphPropertyEnum.VERSION, "1.0");
452         graphVertex.setMetadataProperties(props);
453         graphVertexList.add(graphVertex);
454
455         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata))
456             .thenReturn(Either.left(graphVertexList));
457         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
458             .thenReturn(Either.left(toscaElement));
459         result = testInstance.getLatestByToscaResourceName(toscaResourceName);
460         assertTrue(result.isLeft());
461     }
462
463
464     @Test
465     public void testGetLatestResourceByToscaResourceName() {
466         Either<Resource, StorageOperationStatus> result;
467         String toscaResourceName = "org.openecomp.resource.vf";
468         ToscaElement toscaElement = getToscaElementForTest();
469
470         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
471         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
472         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
473         if (!toscaResourceName.contains("org.openecomp.resource.vf")) {
474             propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
475         }
476
477         List<GraphVertex> graphVertexList = new ArrayList<>();
478         GraphVertex graphVertex = getTopologyTemplateVertex();
479         graphVertex.setUniqueId(toscaResourceName);
480         Map<JsonPresentationFields, Object> props = new HashMap<>();
481         props.put(JsonPresentationFields.VERSION, "1.0");
482         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, props.get(JsonPresentationFields.VERSION));
483         graphVertexList.add(graphVertex);
484
485         when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, JsonParseFlagEnum.ParseMetadata))
486             .thenReturn(Either.left(graphVertexList));
487         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
488             .thenReturn(Either.left(toscaElement));
489
490         when(janusGraphDaoMock.getVertexById(toscaResourceName, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
491
492         result = testInstance.getLatestResourceByToscaResourceName(toscaResourceName);
493         assertTrue(result.isLeft());
494     }
495
496     @Test
497     public void testGetFollowed() {
498         Either<Set<Component>, StorageOperationStatus> result;
499         String userId = "id";
500         Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
501         Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
502         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
503         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
504         lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
505         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
506         List<ToscaElement> toscaEleList = new ArrayList<>();
507         ToscaElement toscaElement = getToscaElementForTest();
508         toscaEleList.add(toscaElement);
509         when(nodeTypeOperationMock.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType))
510             .thenReturn(Either.left(toscaEleList));
511         result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
512         assertTrue(result.isLeft());
513         assertEquals(1, result.left().value().size());
514     }
515
516     @Test
517     public void testGetBySystemName() {
518         Either<List<Component>, StorageOperationStatus> result;
519         String sysName = "sysName";
520         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
521         ToscaElement toscaElement = getToscaElementForTest();
522         List<GraphVertex> componentVertices = new ArrayList<>();
523         GraphVertex graphVertex = getTopologyTemplateVertex();
524         componentVertices.add(graphVertex);
525         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
526         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
527
528         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
529         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
530
531         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
532
533         when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll))
534             .thenReturn(Either.left(componentVertices));
535         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
536             .thenReturn(Either.left(toscaElement));
537         result = testInstance.getBySystemName(componentTypeEnum, sysName);
538         assertTrue(result.isLeft());
539         assertEquals(1, result.left().value().size());
540     }
541
542     @Test
543     public void testGetCompByNameAndVersion() {
544         Either<Component, StorageOperationStatus> result;
545         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
546         String name = "name";
547         String version = "1.0";
548         JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
549         List<GraphVertex> graphVertexList = new ArrayList<>();
550         GraphVertex graphVertex = getTopologyTemplateVertex();
551         graphVertexList.add(graphVertex);
552         ToscaElement toscaElement = getToscaElementForTest();
553         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
554         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
555
556         hasProperties.put(GraphPropertyEnum.NAME, name);
557         hasProperties.put(GraphPropertyEnum.VERSION, version);
558         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
559         hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
560         when(janusGraphDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
561         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class)))
562             .thenReturn(Either.left(toscaElement));
563         result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
564         assertTrue(result.isLeft());
565     }
566
567     private ToscaElement getToscaElementForTest() {
568         ToscaElement toscaElement = new TopologyTemplate();
569         toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
570         return toscaElement;
571     }
572
573     @Test
574     public void addDataTypesToComponentSuccessTest() {
575         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK);
576         assertTrue(result.isLeft());
577     }
578
579     @Test
580     public void addDataTypesToComponentFailureTest_BadRequest() {
581         Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
582         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
583     }
584
585     private Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) {
586         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
587         String componentId = "componentid";
588         String Id = "id";
589
590         PropertyDefinition noDefaultProp = new PropertyDefinition();
591         noDefaultProp.setName("noDefaultProp");
592         PropertyDefinition prop1 = new PropertyDefinition();
593         prop1.setDefaultValue("def1");
594         prop1.setName("prop1");
595         PropertyDefinition prop2 = new PropertyDefinition();
596         prop2.setType("dataType1");
597         prop2.setName("prop2");
598         PropertyDefinition prop3 = new PropertyDefinition();
599         prop3.setDefaultValue("def3");
600         prop3.setName("prop3");
601
602         DataTypeDefinition noDefaultValue = new DataTypeDefinition();
603         noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
604         noDefaultValue.setDerivedFromName("name0");
605
606         DataTypeDefinition dataType1 = new DataTypeDefinition();
607         dataType1.setProperties(Arrays.asList(prop1, prop3));
608         dataType1.setName("name1");
609         dataType1.setDerivedFromName("derivedfromname1");
610
611         DataTypeDefinition dataType2 = new DataTypeDefinition();
612         dataType2.setDerivedFrom(dataType1);
613         dataType2.setName("name2");
614         dataType2.setDerivedFromName("derivedfromname2");
615
616         DataTypeDefinition dataType3 = new DataTypeDefinition();
617         dataType3.setProperties(Collections.singletonList(prop2));
618         dataType3.setDerivedFrom(noDefaultValue);
619         dataType3.setName("name3");
620         dataType3.setDerivedFromName("derivedfromname3");
621
622         dataTypes.put("noDefault", noDefaultValue);
623         dataTypes.put("dataType1", dataType1);
624         dataTypes.put("dataType2", dataType2);
625         dataTypes.put("dataType3", dataType3);
626
627         GraphVertex vertex;
628         if (status == StorageOperationStatus.OK) {
629             vertex = getTopologyTemplateVertex();
630         } else {
631             vertex = getNodeTypeVertex();
632         }
633         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
634         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
635         when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex),
636             eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status);
637         return testInstance.addDataTypesToComponent(dataTypes, componentId);
638     }
639
640     @Test
641     public void testDataTypesToComponentFailureTest_NotFound() {
642         Either<List<DataTypeDefinition>, StorageOperationStatus> result;
643         String componentId = "componentId";
644         GraphVertex vertex = getNodeTypeVertex();
645         Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
646         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
647             JanusGraphOperationStatus.NOT_FOUND));
648         result = testInstance.addDataTypesToComponent(dataTypes, componentId);
649         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
650     }
651
652     @Test
653     public void testDeleteDataTypeOfComponent() {
654         Component component = new Resource();
655         String id = "id";
656         component.setUniqueId(id);
657
658         DataTypeDefinition dataType1 = new DataTypeDefinition();
659         dataType1.setName("name1");
660         Map<String, DataTypeDataDefinition> dataTypeDataMap = new HashMap<>();
661         dataTypeDataMap.put("datatype1", dataType1);
662         List<DataTypeDefinition> dataTypeMap = dataTypeDataMap.values().stream().map(e -> {
663             return new DataTypeDefinition(e);
664         }).collect(Collectors.toList());
665         component.setDataTypes(dataTypeMap);
666         GraphVertex graphVertex = getTopologyTemplateVertex();
667         assertNull(testInstance.deleteDataTypeOfComponent(component, "datatype1"));
668     }
669
670     @Test
671     public void testAddComponentInstancePropertiesToComponent() {
672         // set up component object
673         Component component = new Resource();
674         component.setUniqueId(COMPONENT_ID);
675         List<ComponentInstanceProperty> instanceProps = new ArrayList<>();
676         ComponentInstanceProperty instanceProp = new ComponentInstanceProperty();
677         instanceProp.setName(PROPERTY1_NAME);
678         instanceProp.setType(PROPERTY1_TYPE);
679         instanceProps.add(instanceProp);
680         instanceProp = new ComponentInstanceProperty();
681         instanceProp.setName(PROPERTY2_NAME);
682         instanceProp.setType(PROPERTY2_TYPE);
683         instanceProps.add(instanceProp);
684         Map<String, List<ComponentInstanceProperty>> instancePropsMap =
685             Collections.singletonMap(COMPONENT_ID, instanceProps);
686         component.setComponentInstancesProperties(Collections.singletonMap(COMPONENT_ID, new ArrayList<>()));
687
688         when(nodeTemplateOperationMock.addComponentInstanceProperty(any(), any(), any()))
689             .thenReturn(StorageOperationStatus.OK);
690
691         Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> result =
692             testInstance.addComponentInstancePropertiesToComponent(component, instancePropsMap);
693         assertTrue(result.isLeft());
694         verify(nodeTemplateOperationMock, times(2)).addComponentInstanceProperty(any(), any(), any());
695         List<ComponentInstanceProperty> resultProps = result.left().value().get(COMPONENT_ID);
696         assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY1_NAME)));
697         assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY2_NAME)));
698     }
699
700     @Test
701     public void testAddComponentInstanceToTopologyTemplate_ServiceProxy() {
702         Component containerComponent = new Service();
703         Component originalComponent = new Service();
704         ComponentInstance componentInstance = new ComponentInstance();
705         ComponentInstance existingComponentInstance = new ComponentInstance();
706         User user = new User();
707
708         containerComponent.setComponentType(ComponentTypeEnum.SERVICE);
709
710         originalComponent.setComponentType(ComponentTypeEnum.SERVICE);
711         originalComponent.setIcon(ICON_NAME);
712
713         componentInstance.setOriginType(OriginTypeEnum.ServiceProxy);
714         componentInstance.setSourceModelName(SERVICE_MODEL_NAME);
715
716         List<ComponentInstance> existingInstances = new ArrayList<>();
717         existingComponentInstance.setNormalizedName(SERVICE_PROXY_INSTANCE0_NAME);
718         existingInstances.add(existingComponentInstance);
719         containerComponent.setComponentInstances(existingInstances);
720
721         when(nodeTemplateOperationMock
722             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user)))
723             .thenReturn(Either.left(new ImmutablePair<>(new TopologyTemplate(), COMPONENT_ID)));
724         TopologyTemplate topologyTemplate = new TopologyTemplate();
725         // preset COMPONENT_TYPE field for internal ModelConverter call
726         topologyTemplate.setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
727         when(topologyTemplateOperationMock.getToscaElement(containerComponent.getUniqueId()))
728             .thenReturn(Either.left(topologyTemplate));
729
730         Either<ImmutablePair<Component, String>, StorageOperationStatus> result =
731             testInstance.addComponentInstanceToTopologyTemplate(
732                 containerComponent, originalComponent, componentInstance, false, user);
733
734         assertTrue(result.isLeft());
735         assertEquals(ICON_NAME, componentInstance.getIcon());
736         assertEquals(COMPONENT_ID, result.left().value().getRight());
737         // the instance counter must be 1 because the service proxy instance with suffix 0 already exists.
738         verify(nodeTemplateOperationMock, times(1))
739             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user));
740     }
741
742     @Test
743     public void testAddComponentInstanceToTopologyTemplate_ServiceSubstitution() {
744         Component containerComponent = new Service();
745         Component originalComponent = new Service();
746         ComponentInstance componentInstance = new ComponentInstance();
747         ComponentInstance existingComponentInstance = new ComponentInstance();
748         User user = new User();
749
750         containerComponent.setComponentType(ComponentTypeEnum.SERVICE);
751
752         originalComponent.setComponentType(ComponentTypeEnum.SERVICE);
753         originalComponent.setIcon(ICON_NAME);
754
755         componentInstance.setOriginType(OriginTypeEnum.ServiceSubstitution);
756         componentInstance.setSourceModelName(SERVICE_MODEL_NAME);
757
758         List<ComponentInstance> existingInstances = new ArrayList<>();
759         existingComponentInstance.setNormalizedName(SERVICE_SUBSTITUTION_INSTANCE0_NAME);
760         existingInstances.add(existingComponentInstance);
761         containerComponent.setComponentInstances(existingInstances);
762
763         when(nodeTemplateOperationMock
764             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user)))
765             .thenReturn(Either.left(new ImmutablePair<>(new TopologyTemplate(), COMPONENT_ID)));
766         TopologyTemplate topologyTemplate = new TopologyTemplate();
767         topologyTemplate.setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
768         when(topologyTemplateOperationMock.getToscaElement(containerComponent.getUniqueId()))
769             .thenReturn(Either.left(topologyTemplate));
770
771         Either<ImmutablePair<Component, String>, StorageOperationStatus> result =
772             testInstance.addComponentInstanceToTopologyTemplate(
773                 containerComponent, originalComponent, componentInstance, false, user);
774
775         assertTrue(result.isLeft());
776         assertEquals(ICON_NAME, componentInstance.getIcon());
777         assertEquals(COMPONENT_ID, result.left().value().getRight());
778         verify(nodeTemplateOperationMock, times(1))
779             .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user));
780     }
781
782     @Test
783     public void testUpdateComponentInstanceRequirement() {
784         String containerComponentId = "containerComponentId";
785         String componentInstanceUniqueId = "componentInstanceUniqueId";
786         RequirementDataDefinition requirementDataDefinition = Mockito.mock(RequirementDataDefinition.class);
787
788         when(nodeTemplateOperationMock.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition))
789             .thenReturn(StorageOperationStatus.OK);
790         StorageOperationStatus result = testInstance
791             .updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
792         assertEquals(StorageOperationStatus.OK, result);
793         verify(nodeTemplateOperationMock, times(1))
794             .updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
795
796     }
797
798     @Test
799     public void associateCapabilitiesToServiceFailureTest() {
800         StorageOperationStatus result = associateCapabilitiesToServiceWithStatus(StorageOperationStatus.BAD_REQUEST);
801         assertSame(StorageOperationStatus.BAD_REQUEST, result);
802     }
803
804     @Test
805     public void associateCapabilitiesToServiceSuccessTest() {
806         StorageOperationStatus result = associateCapabilitiesToServiceWithStatus(StorageOperationStatus.OK);
807         assertSame(StorageOperationStatus.OK, result);
808     }
809
810     private StorageOperationStatus associateCapabilitiesToServiceWithStatus(StorageOperationStatus status) {
811         Map<String, ListCapabilityDataDefinition> capabilitiesMap = new HashedMap();
812         String componentId = "componentid";
813
814         ListCapabilityDataDefinition listCapabilityDataDefinition1 = new ListCapabilityDataDefinition();
815         capabilitiesMap.put("capabilities1", listCapabilityDataDefinition1);
816
817         GraphVertex vertex;
818         if (status == StorageOperationStatus.OK) {
819             vertex = getTopologyTemplateVertex();
820         } else {
821             vertex = getNodeTypeVertex();
822         }
823
824         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
825         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
826         when(topologyTemplateOperationMock.associateElementToData(eq(vertex),
827             eq(VertexTypeEnum.CAPABILITIES), eq(EdgeLabelEnum.CAPABILITIES), anyMap())).thenReturn(Either.right(status));
828         return testInstance.associateCapabilitiesToService(capabilitiesMap, componentId);
829     }
830
831     @Test
832     public void associateRequirementsToServiceFailureTest() {
833         StorageOperationStatus result = associateRequirementsToServiceWithStatus(StorageOperationStatus.BAD_REQUEST);
834         assertSame(StorageOperationStatus.BAD_REQUEST, result);
835     }
836
837     @Test
838     public void associateRequirementsToServiceSuccessTest() {
839         StorageOperationStatus result = associateRequirementsToServiceWithStatus(StorageOperationStatus.OK);
840         assertSame(StorageOperationStatus.OK, result);
841     }
842
843     @Test
844     public void test_addOutputsToComponent() {
845         final GraphVertex graphVertex = getTopologyTemplateVertex();
846         final String componentId = "componentId";
847
848         doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(componentId, JsonParseFlagEnum.NoParse);
849         doReturn(StorageOperationStatus.OK).when(topologyTemplateOperationMock)
850             .addToscaDataToToscaElement(
851                 any(GraphVertex.class), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyMap(), eq(JsonPresentationFields.NAME));
852
853         final Map<String, OutputDefinition> outputs = new HashMap<>();
854         final OutputDefinition outputDefinition = new OutputDefinition();
855         outputs.put("mock", outputDefinition);
856         final Either<List<OutputDefinition>, StorageOperationStatus> result = testInstance.addOutputsToComponent(outputs, componentId);
857         assertNotNull(result);
858         assertTrue(result.isLeft());
859         assertFalse(result.left().value().isEmpty());
860         assertThat(result.left().value().get(0)).isInstanceOf(OutputDefinition.class);
861         verify(janusGraphDaoMock, times(1)).getVertexById(componentId, JsonParseFlagEnum.NoParse);
862         verify(topologyTemplateOperationMock, times(1)).addToscaDataToToscaElement(
863             any(GraphVertex.class), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyMap(), eq(JsonPresentationFields.NAME));
864     }
865
866     @Test
867     public void test_addComponentInstanceOutputsToComponent_updateComponentInstanceOutput() {
868         final Component component = new Resource();
869         component.setUniqueId(COMPONENT_ID);
870         final Map<String, List<ComponentInstanceOutput>> map = new HashMap<>();
871         final List<ComponentInstanceOutput> componentInstanceOutputList = new ArrayList<>();
872         final ComponentInstanceOutput componentInstanceOutput = new ComponentInstanceOutput();
873         componentInstanceOutput.setComponentInstanceId(COMPONENT_ID);
874         componentInstanceOutput.setComponentInstanceName(COMPONENT_ID);
875         componentInstanceOutput.setName(COMPONENT_ID);
876         componentInstanceOutputList.add(componentInstanceOutput);
877         map.put("mock", componentInstanceOutputList);
878         component.setComponentInstancesOutputs(map);
879
880         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock)
881             .updateComponentInstanceOutput(any(Component.class), anyString(), any(ComponentInstanceOutput.class));
882
883         final Either<Map<String, List<ComponentInstanceOutput>>, StorageOperationStatus> result
884             = testInstance.addComponentInstanceOutputsToComponent(component, map);
885         assertNotNull(result);
886         assertTrue(result.isLeft());
887         assertFalse(result.left().value().isEmpty());
888         assertSame(result.left().value(), map);
889         verify(nodeTemplateOperationMock, times(1))
890             .updateComponentInstanceOutput(any(Component.class), anyString(), any(ComponentInstanceOutput.class));
891
892     }
893
894     @Test
895     public void test_addComponentInstanceOutputsToComponent_addComponentInstanceOutput() {
896         final Component component = new Resource();
897         component.setUniqueId(COMPONENT_ID);
898         Map<String, List<ComponentInstanceOutput>> map = new HashMap<>();
899         List<ComponentInstanceOutput> componentInstanceOutputList = new ArrayList<>();
900         ComponentInstanceOutput componentInstanceOutput = new ComponentInstanceOutput();
901         componentInstanceOutput.setComponentInstanceId(COMPONENT_ID);
902         componentInstanceOutput.setComponentInstanceName(COMPONENT_ID);
903         componentInstanceOutput.setName(COMPONENT_ID);
904         componentInstanceOutputList.add(componentInstanceOutput);
905         map.put("mock", componentInstanceOutputList);
906         component.setComponentInstancesOutputs(map);
907
908         map = new HashMap<>();
909         componentInstanceOutputList = new ArrayList<>();
910         componentInstanceOutput = new ComponentInstanceOutput();
911         componentInstanceOutput.setComponentInstanceId("mock");
912         componentInstanceOutput.setComponentInstanceName("mock");
913         componentInstanceOutput.setName("mock");
914         componentInstanceOutputList.add(componentInstanceOutput);
915         map.put("mock", componentInstanceOutputList);
916
917         final Either<Map<String, List<ComponentInstanceOutput>>, StorageOperationStatus> result = testInstance
918             .addComponentInstanceOutputsToComponent(component, map);
919         assertNotNull(result);
920         assertTrue(result.isRight());
921     }
922
923     @Test
924     public void test_addComponentInstanceAttributesToComponent() {
925         final Component component = new Resource();
926         component.setUniqueId(COMPONENT_ID);
927         Map<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
928         List<ComponentInstanceAttribute> componentInstanceOutputList = new ArrayList<>();
929         ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
930         componentInstanceAttribute.setComponentInstanceId(COMPONENT_ID);
931         componentInstanceAttribute.setUniqueId(COMPONENT_ID);
932         componentInstanceOutputList.add(componentInstanceAttribute);
933         map.put("mock", componentInstanceOutputList);
934         component.setComponentInstancesAttributes(map);
935
936         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock)
937             .updateComponentInstanceAttribute(any(Component.class), anyString(), any(ComponentInstanceAttribute.class));
938
939         final Either<Map<String, List<ComponentInstanceAttribute>>, StorageOperationStatus> result
940             = testInstance.addComponentInstanceAttributesToComponent(component, map);
941
942         assertNotNull(result);
943         assertTrue(result.isLeft());
944         assertFalse(result.left().value().isEmpty());
945         assertSame(result.left().value(), map);
946         verify(nodeTemplateOperationMock, times(1))
947             .updateComponentInstanceAttribute(any(Component.class), anyString(), any(ComponentInstanceAttribute.class));
948     }
949
950     @Test
951     public void test_updateAttributeOfComponent_success() {
952         final GraphVertex graphVertex = getTopologyTemplateVertex();
953         final String componentId = "componentId";
954         final Component component = new Resource();
955         component.setUniqueId(COMPONENT_ID);
956         doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock).updateToscaDataOfToscaElement(
957             anyString(), eq(EdgeLabelEnum.ATTRIBUTES), eq(VertexTypeEnum.ATTRIBUTES), any(AttributeDefinition.class),
958             eq(JsonPresentationFields.NAME));
959         doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(eq(componentId), any(JsonParseFlagEnum.class));
960
961         final ToscaElement toscaElement = getToscaElementForTest();
962         final Map<String, AttributeDataDefinition> attributes = new HashMap<>();
963         final AttributeDefinition attributeDefinition = new AttributeDefinition();
964         attributeDefinition.setName("mock");
965         attributes.put("mock", attributeDefinition);
966         toscaElement.setAttributes(attributes);
967         doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock)
968             .getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class));
969
970         final Either<AttributeDefinition, StorageOperationStatus> result
971             = testInstance.updateAttributeOfComponent(component, attributeDefinition);
972         assertNotNull(result);
973     }
974
975     @Test
976     public void test_updateAttributeOfComponent_isNotPresent() {
977         final GraphVertex graphVertex = getTopologyTemplateVertex();
978         final String componentId = "componentId";
979         final Component component = new Resource();
980         component.setUniqueId(COMPONENT_ID);
981         doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock).updateToscaDataOfToscaElement(
982             anyString(), eq(EdgeLabelEnum.ATTRIBUTES), eq(VertexTypeEnum.ATTRIBUTES), any(AttributeDefinition.class),
983             eq(JsonPresentationFields.NAME));
984         doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(eq(componentId), any(JsonParseFlagEnum.class));
985
986         final ToscaElement toscaElement = getToscaElementForTest();
987         final Map<String, AttributeDataDefinition> attributes = new HashMap<>();
988         final AttributeDefinition attributeDefinition = new AttributeDefinition();
989         attributeDefinition.setName("mock");
990         attributes.put("mock", attributeDefinition);
991         toscaElement.setAttributes(attributes);
992         doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock)
993             .getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class));
994
995         final AttributeDefinition attributeDefinitionOneMore = new AttributeDefinition();
996         attributeDefinitionOneMore.setName("Anothermock");
997
998         final Either<AttributeDefinition, StorageOperationStatus> result
999             = testInstance.updateAttributeOfComponent(component, attributeDefinitionOneMore);
1000         assertNotNull(result);
1001     }
1002
1003     @Test
1004     public void test_updateComponentInstanceAttributes() {
1005         final GraphVertex graphVertex = getTopologyTemplateVertex();
1006         final String componentId = "componentId";
1007         final Component component = new Resource();
1008         component.setUniqueId(COMPONENT_ID);
1009
1010         final List<ComponentInstanceAttribute> attributes = new ArrayList<>();
1011         final ComponentInstanceAttribute attributeDefinition = new ComponentInstanceAttribute();
1012         attributeDefinition.setName("mock");
1013         attributes.add(attributeDefinition);
1014
1015         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock).updateComponentInstanceAttributes(component, componentId, attributes);
1016
1017         final StorageOperationStatus result = testInstance.updateComponentInstanceAttributes(component, componentId, attributes);
1018         assertNotNull(result);
1019         assertEquals(StorageOperationStatus.OK, result);
1020         verify(nodeTemplateOperationMock, times(1)).updateComponentInstanceAttributes(component, componentId, attributes);
1021     }
1022
1023     @Test
1024     public void test_updateComponentInstanceOutputs() {
1025         final GraphVertex graphVertex = getTopologyTemplateVertex();
1026         final String componentId = "componentId";
1027         final Component component = new Resource();
1028         component.setUniqueId(COMPONENT_ID);
1029
1030         final List<ComponentInstanceOutput> list = new ArrayList<>();
1031         final ComponentInstanceOutput output = new ComponentInstanceOutput();
1032         output.setName("mock");
1033         list.add(output);
1034
1035         doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock).updateComponentInstanceOutputs(component, componentId, list);
1036
1037         final StorageOperationStatus result = testInstance.updateComponentInstanceOutputs(component, componentId, list);
1038         assertNotNull(result);
1039         assertEquals(StorageOperationStatus.OK, result);
1040         verify(nodeTemplateOperationMock, times(1)).updateComponentInstanceOutputs(component, componentId, list);
1041     }
1042
1043     @Test
1044     public void test_deleteOutputOfResource() {
1045         final Component component = new Resource();
1046         component.setUniqueId(COMPONENT_ID);
1047
1048         doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock)
1049             .deleteToscaDataElement(anyString(), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyString(), eq(JsonPresentationFields.NAME));
1050
1051         final StorageOperationStatus result = testInstance.deleteOutputOfResource(component, "mock");
1052         assertNotNull(result);
1053         assertEquals(StorageOperationStatus.OK, result);
1054         verify(nodeTypeOperationMock, times(1))
1055             .deleteToscaDataElement(anyString(), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyString(), eq(JsonPresentationFields.NAME));
1056     }
1057
1058     private StorageOperationStatus associateRequirementsToServiceWithStatus(StorageOperationStatus status) {
1059         Map<String, ListRequirementDataDefinition> requirementsMap = new HashedMap();
1060         String componentId = "componentid";
1061
1062         ListRequirementDataDefinition listRequirementDataDefinition1 = new ListRequirementDataDefinition();
1063         requirementsMap.put("requirements1", listRequirementDataDefinition1);
1064
1065         GraphVertex vertex;
1066         if (status == StorageOperationStatus.OK) {
1067             vertex = getTopologyTemplateVertex();
1068         } else {
1069             vertex = getNodeTypeVertex();
1070         }
1071
1072         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
1073         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
1074         when(topologyTemplateOperationMock.associateElementToData(eq(vertex),
1075             eq(VertexTypeEnum.REQUIREMENTS), eq(EdgeLabelEnum.REQUIREMENTS), anyMap())).thenReturn(Either.right(status));
1076         return testInstance.associateRequirementsToService(requirementsMap, componentId);
1077     }
1078
1079     private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
1080         PolicyDefinition policy = new PolicyDefinition();
1081         String componentId = "componentId";
1082         int counter = 0;
1083         GraphVertex vertex;
1084         if (status == StorageOperationStatus.OK) {
1085             vertex = getTopologyTemplateVertex();
1086         } else {
1087             vertex = getNodeTypeVertex();
1088         }
1089         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
1090         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)).thenReturn(getVertexEither);
1091         when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
1092         return testInstance.associatePolicyToComponent(componentId, policy, counter);
1093     }
1094
1095     private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
1096         PolicyDefinition policy = new PolicyDefinition();
1097         String componentId = "componentId";
1098         GraphVertex vertex = getTopologyTemplateVertex();
1099         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(vertex));
1100         when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
1101         return testInstance.updatePolicyOfComponent(componentId, policy, PromoteVersionEnum.NONE);
1102     }
1103
1104     private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
1105         String componentId = "componentId";
1106         String policyId = "policyId";
1107         GraphVertex vertex = getTopologyTemplateVertex();
1108         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
1109         when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
1110         when(topologyTemplateOperationMock.removePolicyFromToscaElement(vertex, policyId)).thenReturn(status);
1111         StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
1112         assertSame(result, status);
1113     }
1114
1115     private List<GraphVertex> getMockVertices(int numOfVertices) {
1116         return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
1117     }
1118
1119     private ToscaElement getResourceToscaElement(String id) {
1120         ToscaElement toscaElement = new TopologyTemplate();
1121         toscaElement.setMetadata(new HashMap<>());
1122         toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
1123         toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
1124         return toscaElement;
1125     }
1126
1127     private GraphVertex getTopologyTemplateVertex() {
1128         GraphVertex graphVertex = new GraphVertex();
1129         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
1130         return graphVertex;
1131     }
1132
1133     private GraphVertex getNodeTypeVertex() {
1134         GraphVertex graphVertex = new GraphVertex();
1135         graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);
1136         return graphVertex;
1137     }
1138 }