708611014ae2fea66a8569710d480fd4fd2f3678
[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.jsontitan.operations;
31
32 import fj.data.Either;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.MockitoAnnotations;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
43 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
44 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
45 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
46 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
47 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
48 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
49 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
50 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
51 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
52 import org.openecomp.sdc.be.model.Component;
53 import org.openecomp.sdc.be.model.Resource;
54 import org.openecomp.sdc.be.model.LifecycleStateEnum;
55 import org.openecomp.sdc.be.model.ComponentParametersView;
56 import org.openecomp.sdc.be.model.PolicyDefinition;
57 import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType;
58 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
59 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
60 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
61 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
62 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
63
64 import static org.assertj.core.api.Assertions.assertThat;
65
66 import java.util.HashMap;
67 import java.util.List;
68 import java.util.Map;
69 import java.util.ArrayList;
70 import java.util.EnumMap;
71 import java.util.Set;
72 import java.util.HashSet;
73 import java.util.stream.Collectors;
74 import java.util.stream.IntStream;
75
76 import static org.junit.Assert.assertEquals;
77 import static org.junit.Assert.assertSame;
78 import static org.junit.Assert.assertTrue;
79 import static org.mockito.Mockito.when;
80 import static org.mockito.ArgumentMatchers.any;
81 import static org.mockito.ArgumentMatchers.anyMap;
82 import static org.mockito.ArgumentMatchers.anyInt;
83 import static org.mockito.ArgumentMatchers.eq;
84
85 @RunWith(MockitoJUnitRunner.class)
86 public class ToscaOperationFacadeTest {
87
88     @InjectMocks
89     private ToscaOperationFacade testInstance;
90
91     @Mock
92     private TitanDao titanDaoMock;
93
94     @Mock
95     private TopologyTemplateOperation topologyTemplateOperationMock;
96
97     @Mock
98     private NodeTypeOperation nodeTypeOperation;
99
100     @Before
101     public void setUp() throws Exception {
102         testInstance = new ToscaOperationFacade();
103         MockitoAnnotations.initMocks(this);
104     }
105
106     @SuppressWarnings("unchecked")
107     @Test
108     public void fetchMetaDataByResourceType() throws Exception {
109         ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
110         ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
111         ComponentParametersView dataFilter = new ComponentParametersView();
112         List<GraphVertex> mockVertices = getMockVertices(2);
113         Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices);
114
115         when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
116         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
117         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
118         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
119
120         verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
121         verifyCriteriaNotIsDeleted(criteriaNotCapture);
122
123         assertTrue(fetchedComponents.isLeft());
124         List<Component> cmpts = fetchedComponents.left().value();
125         assertEquals(2, cmpts.size());
126         assertEquals("0", cmpts.get(0).getUniqueId());
127         assertEquals("1", cmpts.get(1).getUniqueId());
128     }
129
130     private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
131         Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
132         assertEquals(2, criteria.size());
133         assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
134         assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
135     }
136
137     private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
138         Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
139         assertEquals(1, notCriteria.size());
140         assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
141     }
142
143     @SuppressWarnings("unchecked")
144     @Test
145     public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
146         when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR));
147         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
148         assertTrue(fetchedComponents.isRight());
149         assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
150     }
151
152     @Test
153     public void associatePolicyToComponentSuccessTest(){
154         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
155         assertTrue(result.isLeft());
156     }
157
158     @Test
159     public void associatePolicyToComponentFailureTest(){
160         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
161         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
162     }
163
164     @Test
165     public void updatePolicyOfComponentSuccessTest(){
166         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
167         assertTrue(result.isLeft());
168     }
169
170     @Test
171     public void updatePolicyOfComponentFailureTest(){
172         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
173         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
174     }
175
176     @Test
177     public void removePolicyFromComponentSuccessTest(){
178         removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
179     }
180
181     @Test
182     public void removePolicyFromComponentFailureTest(){
183         removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
184     }
185
186     @Test
187     public void testFindLastCertifiedToscaElementByUUID(){
188         Either<Component, StorageOperationStatus> result;
189         Component component = new Resource();
190         List<GraphVertex> list = new ArrayList<>();
191         GraphVertex graphVertex = getTopologyTemplateVertex();
192         list.add(graphVertex);
193         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
194         props.put(GraphPropertyEnum.UUID, component.getUUID());
195         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
196         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
197         ToscaElement toscaElement = getToscaElementForTest();
198         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
199         when(titanDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
200         result = testInstance.findLastCertifiedToscaElementByUUID(component);
201         Component resultComp = result.left().value();
202         assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
203     }
204
205     @Test
206     public void testLatestComponentByToscaResourceName(){
207         Either<Component, StorageOperationStatus> result;
208         TopologyTemplate toscaElement = new TopologyTemplate();
209         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
210         List<GraphVertex> list = new ArrayList<>();
211         GraphVertex graphVertex = getTopologyTemplateVertex();
212         Map<GraphPropertyEnum, Object> props = new HashMap<>();
213         props.put(GraphPropertyEnum.VERSION, "1.0");
214         graphVertex.setMetadataProperties(props);
215         list.add(graphVertex);
216
217         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
218         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
219         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
220         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
221         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
222
223         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
224         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
225
226         result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
227         assertThat(result.isLeft());
228     }
229
230     @Test
231     public void testValidateCsarUuidUniqueness() {
232         StorageOperationStatus result;
233         String csarUUID = "csarUUID";
234         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
235         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
236         List<GraphVertex> vertexList = new ArrayList<>();
237         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
238         result = testInstance.validateCsarUuidUniqueness(csarUUID);
239         assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
240     }
241
242     @Test
243     public void testValidateCsarUuidUnique_true() {
244         StorageOperationStatus result;
245         String csarUUID = "csarUUID";
246         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
247         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
248         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
249         result = testInstance.validateCsarUuidUniqueness(csarUUID);
250         assertEquals(StorageOperationStatus.OK, result);
251     }
252
253     @Test
254     public void testGetLatestCertiNodeTypeByToscaResourceName() {
255         Either<Resource, StorageOperationStatus> result;
256         String toscaResourceName = "resourceName";
257         String uniqueId = "uniqueId";
258         GraphVertex graphVertex = getTopologyTemplateVertex();
259         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
260         graphVertex.setUniqueId(uniqueId);
261         List<GraphVertex> vertexList = new ArrayList<>();
262         vertexList.add(graphVertex);
263         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
264         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
265         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
266         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
267         ToscaElement topologyTemplate = new TopologyTemplate();
268         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
269         when(titanDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
270         when(titanDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
271         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
272         result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
273         assertThat(result.isLeft());
274     }
275
276     @Test
277     public void testValidateCompExists() {
278         Either<Boolean, StorageOperationStatus> result;
279         String componentId = "componentId";
280         GraphVertex graphVertex = getTopologyTemplateVertex();
281         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
282         result = testInstance.validateComponentExists(componentId);
283         assertEquals(true, result.left().value());
284     }
285
286     @Test
287     public void testValidateCompExists_NotFound() {
288         Either<Boolean, StorageOperationStatus> result;
289         String componentId = "componentId";
290         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
291         result = testInstance.validateComponentExists(componentId);
292         assertEquals(false, result.left().value());
293     }
294
295     @Test
296     public void testValidateToscaResourceNameExists() {
297         Either<Boolean, StorageOperationStatus> result;
298         String templateName = "templateName";
299         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
300         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
301         List<GraphVertex> graphVertexList = new ArrayList<>();
302         GraphVertex graphVertex = getTopologyTemplateVertex();
303         graphVertexList.add(graphVertex);
304         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
305         result = testInstance.validateToscaResourceNameExists(templateName);
306         assertEquals(true, result.left().value());
307     }
308
309     @Test
310     public void testValidateToscaResourceNameExists_false() {
311         Either<Boolean, StorageOperationStatus> result;
312         String templateName = "templateName";
313         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
314         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
315         List<GraphVertex> graphVertexList = new ArrayList<>();
316         GraphVertex graphVertex = getTopologyTemplateVertex();
317         graphVertexList.add(graphVertex);
318         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
319         result = testInstance.validateToscaResourceNameExists(templateName);
320         assertEquals(false, result.left().value());
321     }
322
323     @Test
324     public void testOverrideComponent() {
325         Either<Resource, StorageOperationStatus> result;
326         Resource resource = new Resource();
327         String id = "id";
328         resource.setUniqueId(id);
329         GraphVertex graphVertex = getTopologyTemplateVertex();
330         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
331         NodeType nodeType = new NodeType();
332         nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
333         ToscaElement toscaElement = new TopologyTemplate();
334         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
335         when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
336         when(titanDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
337         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
338         when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
339         when(titanDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
340         when(titanDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(TitanOperationStatus.OK);
341         result = testInstance.overrideComponent(resource, resource);
342         assertTrue(result.isLeft());
343     }
344
345     @Test
346     public void testGetToscaElement() {
347         Either<Component, StorageOperationStatus> result;
348         String id = "id";
349         GraphVertex graphVertex = getTopologyTemplateVertex();
350         ToscaElement toscaElement = getToscaElementForTest();
351         when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
352         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
353         result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
354         assertTrue(result.isLeft());
355     }
356
357     @Test
358     public void testMarkComponentToDelete() {
359         StorageOperationStatus result;
360         Component component = new Resource();
361         String id = "id";
362         component.setUniqueId(id);
363         GraphVertex graphVertex = getTopologyTemplateVertex();
364         when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
365         when(nodeTypeOperation.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
366         result = testInstance.markComponentToDelete(component);
367         assertEquals(result, StorageOperationStatus.OK);
368     }
369
370     @Test
371     public void testDelToscaComponent() {
372         Either<Component, StorageOperationStatus> result;
373         String componentId = "compId";
374         GraphVertex graphVertex = getTopologyTemplateVertex();
375         ToscaElement toscaElement = getToscaElementForTest();
376         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
377         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
378         result = testInstance.deleteToscaComponent(componentId);
379         assertTrue(result.isLeft());
380     }
381
382     @Test
383     public void testGetLatestByToscaResourceName() {
384         Either<Component, StorageOperationStatus> result;
385         String toscaResourceName = "name";
386         ToscaElement toscaElement = getToscaElementForTest();
387
388         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
389         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
390         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
391         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
392         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
393
394         List<GraphVertex> graphVertexList = new ArrayList<>();
395         GraphVertex graphVertex = getTopologyTemplateVertex();
396         graphVertex.setUniqueId(toscaResourceName);
397         Map<GraphPropertyEnum, Object> props = new HashMap<>();
398         props.put(GraphPropertyEnum.VERSION, "1.0");
399         graphVertex.setMetadataProperties(props);
400         graphVertexList.add(graphVertex);
401
402         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
403         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
404         result = testInstance.getLatestByToscaResourceName(toscaResourceName);
405         assertTrue(result.isLeft());
406     }
407
408     @Test
409     public void testGetFollowed() {
410         Either<Set<Component>, StorageOperationStatus> result;
411         String userId = "id";
412         Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
413         Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
414         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
415         lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
416         lifecycleStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
417         lifecycleStates.add(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
418         lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
419         lastStateStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
420         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
421         List<ToscaElement> toscaEleList = new ArrayList<>();
422         ToscaElement toscaElement = getToscaElementForTest();
423         toscaEleList.add(toscaElement);
424         when(nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)).thenReturn(Either.left(toscaEleList));
425         result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
426         assertTrue(result.isLeft());
427         assertEquals(1, result.left().value().size());
428     }
429
430     @Test
431     public void testGetBySystemName() {
432         Either<List<Component>, StorageOperationStatus> result;
433         String sysName = "sysName";
434         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
435         ToscaElement toscaElement = getToscaElementForTest();
436         List<GraphVertex> componentVertices = new ArrayList<>();
437         GraphVertex graphVertex = getTopologyTemplateVertex();
438         componentVertices.add(graphVertex);
439         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
440         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
441
442         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
443         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
444
445         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
446
447         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(componentVertices));
448         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
449         result = testInstance.getBySystemName(componentTypeEnum, sysName);
450         assertTrue(result.isLeft());
451         assertEquals(1, result.left().value().size());
452     }
453
454     @Test
455     public void testGetCompByNameAndVersion() {
456         Either<Component, StorageOperationStatus> result;
457         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
458         String name = "name";
459         String version = "1.0";
460         JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
461         List<GraphVertex> graphVertexList = new ArrayList<>();
462         GraphVertex graphVertex = getTopologyTemplateVertex();
463         graphVertexList.add(graphVertex);
464         ToscaElement toscaElement = getToscaElementForTest();
465         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
466         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
467
468         hasProperties.put(GraphPropertyEnum.NAME, name);
469         hasProperties.put(GraphPropertyEnum.VERSION, version);
470         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
471         hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
472         when(titanDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
473         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
474         result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
475         assertTrue(result.isLeft());
476     }
477
478     private ToscaElement getToscaElementForTest() {
479         ToscaElement toscaElement = new TopologyTemplate();
480         toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
481         return toscaElement;
482     }
483
484     private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
485         PolicyDefinition policy = new PolicyDefinition();
486         String componentId = "componentId";
487         int counter = 0;
488         GraphVertex vertex;
489         if(status == StorageOperationStatus.OK){
490             vertex = getTopologyTemplateVertex();
491         } else {
492             vertex = getNodeTypeVertex();
493         }
494         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
495         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
496         when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
497         return testInstance.associatePolicyToComponent(componentId, policy, counter);
498     }
499
500     private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
501         PolicyDefinition policy = new PolicyDefinition();
502         String componentId = "componentId";
503         GraphVertex vertex = getTopologyTemplateVertex();
504         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
505         when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
506         return testInstance.updatePolicyOfComponent(componentId, policy);
507     }
508
509     private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
510         String componentId = "componentId";
511         String policyId = "policyId";
512         GraphVertex vertex = getTopologyTemplateVertex();
513         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
514         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
515         when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
516         StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
517         assertSame(result, status);
518     }
519
520     private List<GraphVertex> getMockVertices(int numOfVertices) {
521         return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
522     }
523
524     private ToscaElement getResourceToscaElement(String id) {
525         ToscaElement toscaElement = new TopologyTemplate();
526         toscaElement.setMetadata(new HashMap<>());
527         toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
528         toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
529         return toscaElement;
530     }
531
532     private GraphVertex getTopologyTemplateVertex() {
533         GraphVertex graphVertex = new GraphVertex();
534         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
535         return graphVertex;
536     }
537
538     private GraphVertex getNodeTypeVertex() {
539         GraphVertex graphVertex = new GraphVertex();
540         graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);
541         return graphVertex;
542     }
543 }