UT-ToscaOperationFacade 3
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsontitan / operations / ToscaOperationFacadeTest.java
1 /*
2
3  * Copyright (c) 2018 AT&T Intellectual Property.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  *     http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */
30 package org.openecomp.sdc.be.model.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.JsonParseFlagEnum;
45 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
46 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
47 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
49 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
50 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
51 import org.openecomp.sdc.be.model.Component;
52 import org.openecomp.sdc.be.model.Resource;
53 import org.openecomp.sdc.be.model.LifecycleStateEnum;
54 import org.openecomp.sdc.be.model.ComponentParametersView;
55 import org.openecomp.sdc.be.model.PolicyDefinition;
56 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
57 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
58 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
59 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
60 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
61 import static org.assertj.core.api.Assertions.assertThat;
62
63 import java.util.HashMap;
64 import java.util.List;
65 import java.util.Map;
66 import java.util.ArrayList;
67 import java.util.EnumMap;
68 import java.util.stream.Collectors;
69 import java.util.stream.IntStream;
70
71 import static org.junit.Assert.assertEquals;
72 import static org.junit.Assert.assertSame;
73 import static org.junit.Assert.assertTrue;
74 import static org.mockito.Mockito.when;
75 import static org.mockito.ArgumentMatchers.any;
76 import static org.mockito.ArgumentMatchers.anyMap;
77 import static org.mockito.ArgumentMatchers.anyInt;
78 import static org.mockito.ArgumentMatchers.eq;
79
80 @RunWith(MockitoJUnitRunner.class)
81 public class ToscaOperationFacadeTest {
82
83     @InjectMocks
84     private ToscaOperationFacade testInstance;
85
86     @Mock
87     private TitanDao titanDaoMock;
88
89     @Mock
90     private TopologyTemplateOperation topologyTemplateOperationMock;
91
92     @Before
93     public void setUp() throws Exception {
94         testInstance = new ToscaOperationFacade();
95         MockitoAnnotations.initMocks(this);
96     }
97
98     @SuppressWarnings("unchecked")
99     @Test
100     public void fetchMetaDataByResourceType() throws Exception {
101         ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
102         ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
103         ComponentParametersView dataFilter = new ComponentParametersView();
104         List<GraphVertex> mockVertices = getMockVertices(2);
105         Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices);
106
107         when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
108         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
109         when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
110         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
111
112         verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
113         verifyCriteriaNotIsDeleted(criteriaNotCapture);
114
115         assertTrue(fetchedComponents.isLeft());
116         List<Component> cmpts = fetchedComponents.left().value();
117         assertEquals(2, cmpts.size());
118         assertEquals("0", cmpts.get(0).getUniqueId());
119         assertEquals("1", cmpts.get(1).getUniqueId());
120     }
121
122     private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
123         Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
124         assertEquals(2, criteria.size());
125         assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
126         assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
127     }
128
129     private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
130         Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
131         assertEquals(1, notCriteria.size());
132         assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
133     }
134
135     @SuppressWarnings("unchecked")
136     @Test
137     public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
138         when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR));
139         Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
140         assertTrue(fetchedComponents.isRight());
141         assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
142     }
143
144     @Test
145     public void associatePolicyToComponentSuccessTest(){
146         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
147         assertTrue(result.isLeft());
148     }
149
150     @Test
151     public void associatePolicyToComponentFailureTest(){
152         Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
153         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
154     }
155
156     @Test
157     public void updatePolicyOfComponentSuccessTest(){
158         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
159         assertTrue(result.isLeft());
160     }
161
162     @Test
163     public void updatePolicyOfComponentFailureTest(){
164         Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
165         assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
166     }
167
168     @Test
169     public void removePolicyFromComponentSuccessTest(){
170         removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
171     }
172
173     @Test
174     public void removePolicyFromComponentFailureTest(){
175         removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
176     }
177
178     @Test
179     public void testFindLastCertifiedToscaElementByUUID(){
180         Either<Component, StorageOperationStatus> result;
181         Component component = new Resource();
182         List<GraphVertex> list = new ArrayList<>();
183         GraphVertex graphVertex = getTopologyTemplateVertex();
184         list.add(graphVertex);
185         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
186         props.put(GraphPropertyEnum.UUID, component.getUUID());
187         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
188         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
189         TopologyTemplate toscaElement = new TopologyTemplate();
190         toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
191         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
192         when(titanDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
193         result = testInstance.findLastCertifiedToscaElementByUUID(component);
194         Component resultComp = result.left().value();
195         assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
196     }
197
198     @Test
199     public void testLatestComponentByToscaResourceName(){
200         Either<Component, StorageOperationStatus> result;
201         TopologyTemplate toscaElement = new TopologyTemplate();
202         toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
203         List<GraphVertex> list = new ArrayList<>();
204         GraphVertex graphVertex = getTopologyTemplateVertex();
205         Map<GraphPropertyEnum, Object> props = new HashMap<>();
206         props.put(GraphPropertyEnum.VERSION, "1.0");
207         graphVertex.setMetadataProperties(props);
208         list.add(graphVertex);
209
210         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
211         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
212         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
213         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
214         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
215
216         when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
217         when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
218
219         result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
220         assertThat(result.isLeft());
221     }
222
223     @Test
224     public void testValidateCsarUuidUniqueness() {
225         StorageOperationStatus result;
226         String csarUUID = "csarUUID";
227         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
228         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
229         List<GraphVertex> vertexList = new ArrayList<>();
230         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
231         result = testInstance.validateCsarUuidUniqueness(csarUUID);
232         assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
233     }
234
235     @Test
236     public void testValidateCsarUuidUnique_true() {
237         StorageOperationStatus result;
238         String csarUUID = "csarUUID";
239         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
240         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
241         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
242         result = testInstance.validateCsarUuidUniqueness(csarUUID);
243         assertEquals(StorageOperationStatus.OK, result);
244     }
245
246     @Test
247     public void testGetLatestCertiNodeTypeByToscaResourceName() {
248         Either<Resource, StorageOperationStatus> result;
249         String toscaResourceName = "resourceName";
250         String uniqueId = "uniqueId";
251         GraphVertex graphVertex = getTopologyTemplateVertex();
252         graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
253         graphVertex.setUniqueId(uniqueId);
254         List<GraphVertex> vertexList = new ArrayList<>();
255         vertexList.add(graphVertex);
256         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
257         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
258         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
259         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
260         ToscaElement topologyTemplate = new TopologyTemplate();
261         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
262         when(titanDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
263         when(titanDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
264         when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
265         result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
266         assertThat(result.isLeft());
267     }
268
269     @Test
270     public void testValidateCompExists() {
271         Either<Boolean, StorageOperationStatus> result;
272         String componentId = "componentId";
273         GraphVertex graphVertex = getTopologyTemplateVertex();
274         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
275         result = testInstance.validateComponentExists(componentId);
276         assertEquals(true, result.left().value());
277     }
278
279     @Test
280     public void testValidateCompExists_NotFound() {
281         Either<Boolean, StorageOperationStatus> result;
282         String componentId = "componentId";
283         when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
284         result = testInstance.validateComponentExists(componentId);
285         assertEquals(false, result.left().value());
286     }
287
288     @Test
289     public void testValidateToscaResourceNameExists() {
290         Either<Boolean, StorageOperationStatus> result;
291         String templateName = "templateName";
292         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
293         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
294         List<GraphVertex> graphVertexList = new ArrayList<>();
295         GraphVertex graphVertex = getTopologyTemplateVertex();
296         graphVertexList.add(graphVertex);
297         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
298         result = testInstance.validateToscaResourceNameExists(templateName);
299         assertEquals(true, result.left().value());
300     }
301
302     @Test
303     public void testValidateToscaResourceNameExists_false() {
304         Either<Boolean, StorageOperationStatus> result;
305         String templateName = "templateName";
306         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
307         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
308         List<GraphVertex> graphVertexList = new ArrayList<>();
309         GraphVertex graphVertex = getTopologyTemplateVertex();
310         graphVertexList.add(graphVertex);
311         when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
312         result = testInstance.validateToscaResourceNameExists(templateName);
313         assertEquals(false, result.left().value());
314     }
315
316     private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
317         PolicyDefinition policy = new PolicyDefinition();
318         String componentId = "componentId";
319         int counter = 0;
320         GraphVertex vertex;
321         if(status == StorageOperationStatus.OK){
322             vertex = getTopologyTemplateVertex();
323         } else {
324             vertex = getNodeTypeVertex();
325         }
326         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
327         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
328         when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
329         return testInstance.associatePolicyToComponent(componentId, policy, counter);
330     }
331
332     private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
333         PolicyDefinition policy = new PolicyDefinition();
334         String componentId = "componentId";
335         GraphVertex vertex = getTopologyTemplateVertex();
336         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
337         when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
338         return testInstance.updatePolicyOfComponent(componentId, policy);
339     }
340
341     private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
342         String componentId = "componentId";
343         String policyId = "policyId";
344         GraphVertex vertex = getTopologyTemplateVertex();
345         Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
346         when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
347         when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
348         StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
349         assertSame(result, status);
350     }
351
352     private List<GraphVertex> getMockVertices(int numOfVertices) {
353         return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
354     }
355
356     private ToscaElement getResourceToscaElement(String id) {
357         ToscaElement toscaElement = new TopologyTemplate();
358         toscaElement.setMetadata(new HashMap<>());
359         toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
360         toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
361         return toscaElement;
362     }
363
364     private GraphVertex getTopologyTemplateVertex() {
365         GraphVertex graphVertex = new GraphVertex();
366         graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
367         return graphVertex;
368     }
369
370     private GraphVertex getNodeTypeVertex() {
371         GraphVertex graphVertex = new GraphVertex();
372         graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);
373         return graphVertex;
374     }
375 }