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