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