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