b40f5d1cb8a8898610798a918783561ed52842e5
[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 import static org.assertj.core.api.Assertions.assertThat;
64
65 import java.util.HashMap;
66 import java.util.List;
67 import java.util.Map;
68 import java.util.ArrayList;
69 import java.util.EnumMap;
70 import java.util.stream.Collectors;
71 import java.util.stream.IntStream;
72
73 import static org.junit.Assert.assertEquals;
74 import static org.junit.Assert.assertSame;
75 import static org.junit.Assert.assertTrue;
76 import static org.mockito.Mockito.when;
77 import static org.mockito.ArgumentMatchers.any;
78 import static org.mockito.ArgumentMatchers.anyMap;
79 import static org.mockito.ArgumentMatchers.anyInt;
80 import static org.mockito.ArgumentMatchers.eq;
81
82 @RunWith(MockitoJUnitRunner.class)
83 public class ToscaOperationFacadeTest {
84
85     @InjectMocks
86     private ToscaOperationFacade testInstance;
87
88     @Mock
89     private TitanDao titanDaoMock;
90
91     @Mock
92     private TopologyTemplateOperation topologyTemplateOperationMock;
93
94     @Mock
95     private NodeTypeOperation nodeTypeOperation;
96
97     @Before
98     public void setUp() throws Exception {
99         testInstance = new ToscaOperationFacade();
100         MockitoAnnotations.initMocks(this);
101     }
102
103     @SuppressWarnings("unchecked")
104     @Test
105     public void fetchMetaDataByResourceType() throws Exception {
106         ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
107         ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
108         ComponentParametersView dataFilter = new ComponentParametersView();
109         List<GraphVertex> mockVertices = getMockVertices(2);
110         Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices);
111
112         when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
113         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
114         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
115         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
116
117         verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
118         verifyCriteriaNotIsDeleted(criteriaNotCapture);
119
120         assertTrue(fetchedComponents.isLeft());
121         List<Component> cmpts = fetchedComponents.left().value();
122         assertEquals(2, cmpts.size());
123         assertEquals("0", cmpts.get(0).getUniqueId());
124         assertEquals("1", cmpts.get(1).getUniqueId());
125     }
126
127     private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
128         Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
129         assertEquals(2, criteria.size());
130         assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
131         assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
132     }
133
134     private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
135         Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
136         assertEquals(1, notCriteria.size());
137         assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
138     }
139
140     @SuppressWarnings("unchecked")
141     @Test
142     public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
143         when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR));
144         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
145         assertTrue(fetchedComponents.isRight());
146         assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
147     }
148
149     @Test
150     public void associatePolicyToComponentSuccessTest(){
151         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
152         assertTrue(result.isLeft());
153     }
154
155     @Test
156     public void associatePolicyToComponentFailureTest(){
157         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
158         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
159     }
160
161     @Test
162     public void updatePolicyOfComponentSuccessTest(){
163         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
164         assertTrue(result.isLeft());
165     }
166
167     @Test
168     public void updatePolicyOfComponentFailureTest(){
169         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
170         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
171     }
172
173     @Test
174     public void removePolicyFromComponentSuccessTest(){
175         removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
176     }
177
178     @Test
179     public void removePolicyFromComponentFailureTest(){
180         removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
181     }
182
183     @Test
184     public void testFindLastCertifiedToscaElementByUUID(){
185         Either<Component, StorageOperationStatus> result;
186         Component component = new Resource();
187         List<GraphVertex> list = new ArrayList<>();
188         GraphVertex graphVertex = getTopologyTemplateVertex();
189         list.add(graphVertex);
190         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
191         props.put(GraphPropertyEnum.UUID, component.getUUID());
192         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
193         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
194         TopologyTemplate toscaElement = new TopologyTemplate();
195         toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
196         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
197         when(titanDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
198         result = testInstance.findLastCertifiedToscaElementByUUID(component);
199         Component resultComp = result.left().value();
200         assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
201     }
202
203     @Test
204     public void testLatestComponentByToscaResourceName(){
205         Either<Component, StorageOperationStatus> result;
206         TopologyTemplate toscaElement = new TopologyTemplate();
207         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
208         List<GraphVertex> list = new ArrayList<>();
209         GraphVertex graphVertex = getTopologyTemplateVertex();
210         Map<GraphPropertyEnum, Object> props = new HashMap<>();
211         props.put(GraphPropertyEnum.VERSION, "1.0");
212         graphVertex.setMetadataProperties(props);
213         list.add(graphVertex);
214
215         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
216         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
217         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
218         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
219         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
220
221         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
222         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
223
224         result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
225         assertThat(result.isLeft());
226     }
227
228     @Test
229     public void testValidateCsarUuidUniqueness() {
230         StorageOperationStatus result;
231         String csarUUID = "csarUUID";
232         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
233         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
234         List<GraphVertex> vertexList = new ArrayList<>();
235         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
236         result = testInstance.validateCsarUuidUniqueness(csarUUID);
237         assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
238     }
239
240     @Test
241     public void testValidateCsarUuidUnique_true() {
242         StorageOperationStatus result;
243         String csarUUID = "csarUUID";
244         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
245         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
246         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
247         result = testInstance.validateCsarUuidUniqueness(csarUUID);
248         assertEquals(StorageOperationStatus.OK, result);
249     }
250
251     @Test
252     public void testGetLatestCertiNodeTypeByToscaResourceName() {
253         Either<Resource, StorageOperationStatus> result;
254         String toscaResourceName = "resourceName";
255         String uniqueId = "uniqueId";
256         GraphVertex graphVertex = getTopologyTemplateVertex();
257         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
258         graphVertex.setUniqueId(uniqueId);
259         List<GraphVertex> vertexList = new ArrayList<>();
260         vertexList.add(graphVertex);
261         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
262         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
263         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
264         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
265         ToscaElement topologyTemplate = new TopologyTemplate();
266         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
267         when(titanDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
268         when(titanDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
269         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
270         result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
271         assertThat(result.isLeft());
272     }
273
274     @Test
275     public void testValidateCompExists() {
276         Either<Boolean, StorageOperationStatus> result;
277         String componentId = "componentId";
278         GraphVertex graphVertex = getTopologyTemplateVertex();
279         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
280         result = testInstance.validateComponentExists(componentId);
281         assertEquals(true, result.left().value());
282     }
283
284     @Test
285     public void testValidateCompExists_NotFound() {
286         Either<Boolean, StorageOperationStatus> result;
287         String componentId = "componentId";
288         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
289         result = testInstance.validateComponentExists(componentId);
290         assertEquals(false, result.left().value());
291     }
292
293     @Test
294     public void testValidateToscaResourceNameExists() {
295         Either<Boolean, StorageOperationStatus> result;
296         String templateName = "templateName";
297         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
298         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
299         List<GraphVertex> graphVertexList = new ArrayList<>();
300         GraphVertex graphVertex = getTopologyTemplateVertex();
301         graphVertexList.add(graphVertex);
302         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
303         result = testInstance.validateToscaResourceNameExists(templateName);
304         assertEquals(true, result.left().value());
305     }
306
307     @Test
308     public void testValidateToscaResourceNameExists_false() {
309         Either<Boolean, StorageOperationStatus> result;
310         String templateName = "templateName";
311         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
312         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
313         List<GraphVertex> graphVertexList = new ArrayList<>();
314         GraphVertex graphVertex = getTopologyTemplateVertex();
315         graphVertexList.add(graphVertex);
316         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
317         result = testInstance.validateToscaResourceNameExists(templateName);
318         assertEquals(false, result.left().value());
319     }
320
321     @Test
322     public void testOverrideComponent() {
323         Either<Resource, StorageOperationStatus> result;
324         Resource resource = new Resource();
325         String id = "id";
326         resource.setUniqueId(id);
327         GraphVertex graphVertex = getTopologyTemplateVertex();
328         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
329         NodeType nodeType = new NodeType();
330         nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
331         ToscaElement toscaElement = new TopologyTemplate();
332         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
333         when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
334         when(titanDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
335         when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
336         when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
337         when(titanDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
338         when(titanDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(TitanOperationStatus.OK);
339         result = testInstance.overrideComponent(resource, resource);
340         assertTrue(result.isLeft());
341     }
342
343     private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
344         PolicyDefinition policy = new PolicyDefinition();
345         String componentId = "componentId";
346         int counter = 0;
347         GraphVertex vertex;
348         if(status == StorageOperationStatus.OK){
349             vertex = getTopologyTemplateVertex();
350         } else {
351             vertex = getNodeTypeVertex();
352         }
353         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
354         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
355         when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
356         return testInstance.associatePolicyToComponent(componentId, policy, counter);
357     }
358
359     private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
360         PolicyDefinition policy = new PolicyDefinition();
361         String componentId = "componentId";
362         GraphVertex vertex = getTopologyTemplateVertex();
363         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
364         when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
365         return testInstance.updatePolicyOfComponent(componentId, policy);
366     }
367
368     private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
369         String componentId = "componentId";
370         String policyId = "policyId";
371         GraphVertex vertex = getTopologyTemplateVertex();
372         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
373         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
374         when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
375         StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
376         assertSame(result, status);
377     }
378
379     private List<GraphVertex> getMockVertices(int numOfVertices) {
380         return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
381     }
382
383     private ToscaElement getResourceToscaElement(String id) {
384         ToscaElement toscaElement = new TopologyTemplate();
385         toscaElement.setMetadata(new HashMap<>());
386         toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
387         toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
388         return toscaElement;
389     }
390
391     private GraphVertex getTopologyTemplateVertex() {
392         GraphVertex graphVertex = new GraphVertex();
393         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
394         return graphVertex;
395     }
396
397     private GraphVertex getNodeTypeVertex() {
398         GraphVertex graphVertex = new GraphVertex();
399         graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);
400         return graphVertex;
401     }
402 }