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