Fix test cases failing incorrectly
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaElementOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import org.junit.Rule;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.rules.TestName;
39 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
40 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
43 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
44 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
45 import org.openecomp.sdc.be.model.ComponentParametersView;
46 import org.openecomp.sdc.be.model.DataTypeDefinition;
47 import org.openecomp.sdc.be.model.LifecycleStateEnum;
48 import org.openecomp.sdc.be.model.ModelTestBase;
49 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
52 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
53 import org.openecomp.sdc.be.resources.data.EntryData;
54 import org.openecomp.sdc.be.utils.TypeUtils;
55 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
56
57 /**
58  * Created by chaya on 6/12/2017.
59  */
60
61 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
62 public class ToscaElementOperationTest extends ModelTestBase {
63
64     @Rule
65     public TestName testName = new TestName();
66     private List<GraphVertex> allVertices = new ArrayList<>();
67     private boolean isInitialized = false;
68     @javax.annotation.Resource
69     private ToscaElementOperationTestImpl toscaElementOperation;
70     @javax.annotation.Resource
71     private JanusGraphDao janusGraphDao;
72
73     @BeforeAll
74     public static void initTest() {
75         ModelTestBase.init();
76
77     }
78
79     @BeforeEach
80     public void beforeTest() {
81         if (!isInitialized) {
82             GraphTestUtils.clearGraph(janusGraphDao);
83             //exportGraphMl(janusGraphDao.getGraph().left().value(),"");
84             initGraphForTest();
85             isInitialized = true;
86         }
87     }
88
89     @Test
90     public void testGetAllHighestResourcesNoFilter() {
91
92         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
93             .getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
94         assertTrue(highestResourcesRes.isLeft());
95         List<ToscaElement> highestResources = highestResourcesRes.left().value();
96         // calculate expected count value
97         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
98             {
99                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
100                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
101             }
102         }, null);
103         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
104     }
105
106     @Test
107     public void testGetAllResourcesCertifiedNoFilter() {
108         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
109             .getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
110         assertTrue(highestResourcesRes.isLeft());
111         List<ToscaElement> highestResources = highestResourcesRes.left().value();
112         // calculate expected count value
113         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
114             {
115                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
116                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
117             }
118         }, null);
119         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
120             {
121                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
122                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
123             }
124         }, new HashMap<GraphPropertyEnum, Object>() {
125             {
126                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
127             }
128         });
129         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
130     }
131
132     @Test
133     public void testGetHighestResourcesExclude() {
134
135         // exclude VFCMT
136         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
137         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
138
139         // exclude CP & VL
140         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
141         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
142
143         // exclude CP & VL & VF & VFC
144         excludeList = Arrays
145             .asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
146         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
147     }
148
149     @Test
150     public void testGetAllResourcesCertifiedExclude() {
151         // exclude VFCMT
152         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
153         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
154
155         // exclude CP & VL
156         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
157         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
158
159         // exclude CP & VL & VF & VFC
160         excludeList = Arrays
161             .asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
162         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
163     }
164
165     @Test
166     public void testGetAllHighestServicesNoFilter() {
167         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
168             .getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
169         assertTrue(highestResourcesRes.isLeft());
170         List<ToscaElement> highestResources = highestResourcesRes.left().value();
171         // calculate expected count value
172         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
173             {
174                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
175                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
176             }
177         }, null);
178         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
179     }
180
181     @Test
182     public void testGetAllCertifiedServicesNoFilter() {
183         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
184             .getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
185         assertTrue(highestResourcesRes.isLeft());
186         List<ToscaElement> highestResources = highestResourcesRes.left().value();
187         // calculate expected count value
188         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
189             {
190                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
191                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
192             }
193         }, null);
194         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
195             {
196                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
197                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
198             }
199         }, new HashMap<GraphPropertyEnum, Object>() {
200             {
201                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
202             }
203         });
204         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
205     }
206
207     @Test
208     public void testGetServicesExcludeList() {
209         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
210         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
211             .getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
212         assertTrue(highestResourcesRes.isLeft());
213         List<ToscaElement> highestResources = highestResourcesRes.left().value();
214         // calculate expected count value
215         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
216             {
217                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
218                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
219             }
220         }, null);
221         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
222     }
223
224     @Test
225     public void testGetCertifiedServicesExcludeList() {
226         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
227         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
228             .getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
229         assertTrue(highestResourcesRes.isLeft());
230         List<ToscaElement> highestResources = highestResourcesRes.left().value();
231         // calculate expected count value
232         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
233             {
234                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
235                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
236             }
237         }, null);
238         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
239             {
240                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
241                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
242             }
243         }, new HashMap<GraphPropertyEnum, Object>() {
244             {
245                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
246             }
247         });
248         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
249     }
250
251     @Test
252     public void testUpdateToscaElement_NotFound() {
253         Either<TopologyTemplate, StorageOperationStatus> result;
254         TopologyTemplate topologyTemplate = new TopologyTemplate();
255         String userID = "userID";
256         topologyTemplate.setLastUpdaterUserId(userID);
257         GraphVertex graphVertex = new GraphVertex();
258         ComponentParametersView componentParametersView = new ComponentParametersView();
259         result = toscaElementOperation.updateToscaElement(topologyTemplate, graphVertex, componentParametersView);
260         assertEquals(null, result);
261     }
262
263     @Test
264     public void testCreateDataType() {
265         final String expected = "newDataType";
266         final DataTypeDefinition result = ToscaElementOperation.createDataType(expected);
267         assertNotNull(result);
268         assertEquals(expected, result.getName());
269     }
270
271     @Test
272     public void testCreateDataTypeDefinitionWithName() {
273         final String expected = "newDataType";
274         final String description = "DESCRIPTION";
275         final String derivedFromName = "DERIVED_FROM_NAME";
276         final String uniqueId = "UNIQUE_ID";
277
278         final Map<String, Object> attributeMap = new HashMap<>();
279         attributeMap.put(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), description);
280         attributeMap.put(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM_NAME.getElementName(), derivedFromName);
281
282         final Map<String, Object> derivedFromMap = new HashMap<>();
283         derivedFromMap.put(JsonPresentationFields.NAME.getPresentation(), derivedFromName);
284         derivedFromMap.put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
285         final long creationTime = System.currentTimeMillis();
286         derivedFromMap.put(JsonPresentationFields.CREATION_TIME.getPresentation(), creationTime);
287         final long modificationTime = System.currentTimeMillis();
288         derivedFromMap.put(JsonPresentationFields.MODIFICATION_TIME.getPresentation(), modificationTime);
289
290         attributeMap.put(JsonPresentationFields.DERIVED_FROM.getPresentation(), derivedFromMap);
291
292         final Entry<String, Object> attributeNameValue = new EntryData<>(expected, attributeMap);
293         final DataTypeDefinition result = ToscaElementOperation.createDataTypeDefinitionWithName(attributeNameValue);
294
295         assertNotNull(result);
296         assertEquals(derivedFromName, result.getDerivedFromName());
297         assertEquals(description, result.getDescription());
298
299         final DataTypeDefinition resultDerivedFrom = result.getDerivedFrom();
300         assertNotNull(resultDerivedFrom);
301         assertEquals(derivedFromName, resultDerivedFrom.getName());
302         assertEquals(uniqueId, resultDerivedFrom.getUniqueId());
303         assertEquals(creationTime, resultDerivedFrom.getCreationTime().longValue());
304         assertEquals(modificationTime, resultDerivedFrom.getModificationTime().longValue());
305
306     }
307
308     private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
309         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
310             .getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, true);
311         assertTrue(highestResourcesRes.isLeft());
312         List<ToscaElement> highestResources = highestResourcesRes.left().value();
313         // calculate expected count value
314         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
315             {
316                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
317                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
318             }
319         }, new HashMap<GraphPropertyEnum, Object>() {
320             {
321                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
322             }
323         });
324         return highestResources.stream().count() == (highestResourcesExpectedCount);
325     }
326
327     private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
328         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
329             .getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
330         assertTrue(highestResourcesRes.isLeft());
331         List<ToscaElement> highestResources = highestResourcesRes.left().value();
332         // calculate expected count value
333         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
334             {
335                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
336                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
337             }
338         }, new HashMap<GraphPropertyEnum, Object>() {
339             {
340                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
341             }
342         });
343         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
344             {
345                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
346                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
347             }
348         }, new HashMap<GraphPropertyEnum, Object>() {
349             {
350                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
351                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
352             }
353         });
354         return highestResources.stream().count() == highestResourcesExpectedCount;
355     }
356
357     private void initGraphForTest() {
358         GraphTestUtils.createRootCatalogVertex(janusGraphDao);
359
360         Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
361             {
362                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
363             }
364         };
365
366         Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
367             {
368                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
369                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
370             }
371         };
372
373         // add vertices with higestVersion = true
374         allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VF));
375         allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFC));
376         allVertices
377             .add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFCMT));
378         allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VL));
379         allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.CP));
380         allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, highstVerticesProps));
381
382         // add vertices with non-additional properties
383         for (int i = 0; i < 2; i++) {
384             allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF));
385             allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFC));
386             allVertices
387                 .add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFCMT));
388             allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VL));
389             allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.CP));
390             allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>()));
391         }
392
393         // add certified vertices
394         for (int i = 0; i < 3; i++) {
395             allVertices
396                 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VF));
397             allVertices
398                 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFC));
399             allVertices.add(
400                 GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT));
401             allVertices
402                 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VL));
403             allVertices
404                 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.CP));
405             allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, certifiedVerticesProps));
406         }
407         //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)));
408         //String result = GraphTestUtils.exportGraphMl(janusGraphDao.getGraph().left().value(), "");
409         //System.out.println("graph is: " + result);
410     }
411
412     private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps,
413                                 Map<GraphPropertyEnum, Object> doesntHaveProps) {
414         return allVertices.stream().
415             filter(v -> {
416                 Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties();
417                 if (hasProps != null) {
418                     for (Map.Entry<GraphPropertyEnum, Object> prop : hasProps.entrySet()) {
419                         Object value = vertexProps.get(prop.getKey());
420                         if (value == null || !value.equals(prop.getValue())) {
421                             return false;
422                         }
423                     }
424                 }
425
426                 if (doesntHaveProps != null) {
427                     for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) {
428                         Object value = vertexProps.get(prop.getKey());
429                         Object propValue = prop.getValue();
430                         if (value != null && propValue != null && propValue instanceof List) {
431                             for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>) propValue) {
432                                 if (propVal.name().equals(value)) {
433                                     return false;
434                                 }
435                             }
436                         } else if (value != null && value.equals(propValue)) {
437                             return false;
438                         }
439                     }
440                 }
441                 return true;
442             }).count();
443     }
444 }