806c22a8fbc63a28cfd6a51a8897db0ebdddf8ae
[sdc.git] /
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.jsontitan.datamodel.ToscaElement;
26 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
28 import org.springframework.test.context.ContextConfiguration;
29 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30
31 import fj.data.Either;
32
33 /**
34  * Created by chaya on 6/12/2017.
35  */
36
37 @RunWith(SpringJUnit4ClassRunner.class)
38 @ContextConfiguration("classpath:application-context-test.xml")
39 public class ToscaElementOperationTest extends ModelTestBase{
40
41     private List<GraphVertex> allVertices = new ArrayList<>();
42     private boolean isInitialized = false;
43
44     @javax.annotation.Resource
45     ToscaElementOperationTestImpl toscaElementOperation;
46
47     @javax.annotation.Resource
48     TitanDao titanDao;
49
50     @BeforeClass
51     public static void initTest(){
52         ModelTestBase.init();
53
54     }
55
56     @Rule
57     public TestName testName = new TestName();
58
59     @Before
60     public void beforeTest() {
61         if (!isInitialized) {
62             GraphTestUtils.clearGraph(titanDao);
63             //exportGraphMl(titanDao.getGraph().left().value(),"");
64             initGraphForTest();
65             isInitialized = true;
66         }
67     }
68
69     @Test
70     public void testGetAllHighestResourcesNoFilter() {
71
72         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
73         assertTrue(highestResourcesRes.isLeft());
74         List<ToscaElement> highestResources = highestResourcesRes.left().value();
75         // calculate expected count value
76         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
77             {
78                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
79                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
80             }
81         }, null);
82         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
83     }
84
85     @Test
86     public void testGetAllResourcesCertifiedNoFilter() {
87         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
88         assertTrue(highestResourcesRes.isLeft());
89         List<ToscaElement> highestResources = highestResourcesRes.left().value();
90         // calculate expected count value
91         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
92             {
93                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
94                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
95             }
96         }, null);
97         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
98             {
99                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
100                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
101             }
102         }, new HashMap<GraphPropertyEnum, Object>() {
103             {
104                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
105             }
106         });
107         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
108     }
109
110     @Test
111     public void testGetHighestResourcesExclude() {
112
113         // exclude VFCMT
114         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
115         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
116
117         // exclude CP & VL
118         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
119         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
120
121         // exclude CP & VL & VF & VFC
122         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
123         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
124     }
125
126     @Test
127     public void testGetAllResourcesCertifiedExclude() {
128         // exclude VFCMT
129         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
130         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
131
132         // exclude CP & VL
133         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
134         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
135
136         // exclude CP & VL & VF & VFC
137         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
138         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
139     }
140
141     @Test
142     public void testGetAllHighestServicesNoFilter() {
143         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
144         assertTrue(highestResourcesRes.isLeft());
145         List<ToscaElement> highestResources = highestResourcesRes.left().value();
146         // calculate expected count value
147         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
148             {
149                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
150                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
151             }
152         }, null);
153         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
154     }
155
156     @Test
157     public void testGetAllCertifiedServicesNoFilter() {
158         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
159         assertTrue(highestResourcesRes.isLeft());
160         List<ToscaElement> highestResources = highestResourcesRes.left().value();
161         // calculate expected count value
162         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
163             {
164                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
165                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
166             }
167         }, null);
168         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
169             {
170                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
171                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
172             }
173         }, new HashMap<GraphPropertyEnum, Object>() {
174             {
175                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
176             }
177         });
178         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
179     }
180
181     @Test
182     public void testGetServicesExcludeList() {
183         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
184         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
185         assertTrue(highestResourcesRes.isLeft());
186         List<ToscaElement> highestResources = highestResourcesRes.left().value();
187         // calculate expected count value
188         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
189             {
190                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
191                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
192             }
193         }, null);
194         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
195     }
196
197     @Test
198     public void testGetCertifiedServicesExcludeList() {
199         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
200         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
201         assertTrue(highestResourcesRes.isLeft());
202         List<ToscaElement> highestResources = highestResourcesRes.left().value();
203         // calculate expected count value
204         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
205             {
206                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
207                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
208             }
209         }, null);
210         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
211             {
212                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
213                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
214             }
215         }, new HashMap<GraphPropertyEnum, Object>() {
216             {
217                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
218             }
219         });
220         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
221     }
222
223     private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
224         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludeList, true);
225         assertTrue(highestResourcesRes.isLeft());
226         List<ToscaElement> highestResources = highestResourcesRes.left().value();
227         // calculate expected count value
228         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
229             {
230                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
231                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
232             }
233         }, new HashMap<GraphPropertyEnum, Object>() {
234             {
235                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
236             }
237         });
238         return highestResources.stream().count() == (highestResourcesExpectedCount);
239     }
240
241     private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
242         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
243         assertTrue(highestResourcesRes.isLeft());
244         List<ToscaElement> highestResources = highestResourcesRes.left().value();
245         // calculate expected count value
246         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
247             {
248                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
249                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
250             }
251         }, new HashMap<GraphPropertyEnum, Object>() {
252             {
253                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
254             }
255         });
256         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
257             {
258                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
259                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
260             }
261         }, new HashMap<GraphPropertyEnum, Object>() {
262             {
263                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
264                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
265             }
266         });
267         return highestResources.stream().count() == highestResourcesExpectedCount;
268     }
269
270     private void initGraphForTest() {
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 }