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