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