054e71a83e8ad09ec5ce8ffd4ee57aa069d42155
[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    
86
87
88     @Test
89     public void testGetAllResourcesCertifiedNoFilter() {
90         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
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.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
97                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
98             }
99         }, null);
100         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
101             {
102                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
103                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
104             }
105         }, new HashMap<GraphPropertyEnum, Object>() {
106             {
107                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
108             }
109         });
110         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
111     }
112
113     @Test
114     public void testGetHighestResourcesExclude() {
115
116         // exclude VFCMT
117         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
118         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
119
120         // exclude CP & VL
121         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
122         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
123
124         // exclude CP & VL & VF & VFC
125         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
126         assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
127     }
128
129     @Test
130     public void testGetAllResourcesCertifiedExclude() {
131         // exclude VFCMT
132         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
133         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
134
135         // exclude CP & VL
136         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
137         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
138
139         // exclude CP & VL & VF & VFC
140         excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
141         assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
142     }
143
144     @Test
145     public void testGetAllHighestServicesNoFilter() {
146         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
147         assertTrue(highestResourcesRes.isLeft());
148         List<ToscaElement> highestResources = highestResourcesRes.left().value();
149         // calculate expected count value
150         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
151             {
152                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
153                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
154             }
155         }, null);
156         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
157     }
158
159     @Test
160     public void testGetAllCertifiedServicesNoFilter() {
161         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
162         assertTrue(highestResourcesRes.isLeft());
163         List<ToscaElement> highestResources = highestResourcesRes.left().value();
164         // calculate expected count value
165         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
166             {
167                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
168                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
169             }
170         }, null);
171         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
172             {
173                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
174                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
175             }
176         }, new HashMap<GraphPropertyEnum, Object>() {
177             {
178                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
179             }
180         });
181         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
182     }
183
184     @Test
185     public void testGetServicesExcludeList() {
186         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
187         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
188         assertTrue(highestResourcesRes.isLeft());
189         List<ToscaElement> highestResources = highestResourcesRes.left().value();
190         // calculate expected count value
191         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
192             {
193                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
194                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
195             }
196         }, null);
197         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
198     }
199
200     @Test
201     public void testGetCertifiedServicesExcludeList() {
202         List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
203         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
204         assertTrue(highestResourcesRes.isLeft());
205         List<ToscaElement> highestResources = highestResourcesRes.left().value();
206         // calculate expected count value
207         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
208             {
209                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
210                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
211             }
212         }, null);
213         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
214             {
215                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
216                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
217             }
218         }, new HashMap<GraphPropertyEnum, Object>() {
219             {
220                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
221             }
222         });
223         assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
224     }
225
226     private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
227         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludeList, true);
228         assertTrue(highestResourcesRes.isLeft());
229         List<ToscaElement> highestResources = highestResourcesRes.left().value();
230         // calculate expected count value
231         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
232             {
233                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
234                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
235             }
236         }, new HashMap<GraphPropertyEnum, Object>() {
237             {
238                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
239             }
240         });
241         return highestResources.stream().count() == (highestResourcesExpectedCount);
242     }
243
244     private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
245         Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
246         assertTrue(highestResourcesRes.isLeft());
247         List<ToscaElement> highestResources = highestResourcesRes.left().value();
248         // calculate expected count value
249         long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
250             {
251                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
252                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
253             }
254         }, new HashMap<GraphPropertyEnum, Object>() {
255             {
256                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
257             }
258         });
259         highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
260             {
261                 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
262                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
263             }
264         }, new HashMap<GraphPropertyEnum, Object>() {
265             {
266                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
267                 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
268             }
269         });
270         return highestResources.stream().count() == highestResourcesExpectedCount;
271     }
272
273     private void initGraphForTest() {
274                 GraphTestUtils.createRootCatalogVertex(titanDao);
275
276         Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
277             {
278                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
279             }
280         };
281
282         Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
283             {
284                 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
285                 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
286         }
287         };
288
289         // add vertices with higestVersion = true
290         allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VF));
291         allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VFC));
292         allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VFCMT));
293         allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VL));
294         allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.CP));
295         allVertices.add(GraphTestUtils.createServiceVertex(titanDao, highstVerticesProps));
296
297         // add vertices with non-additional properties
298         for (int i=0 ; i<2 ; i++) {
299             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VF));
300             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VFC));
301             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VFCMT));
302             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VL));
303             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.CP));
304             allVertices.add(GraphTestUtils.createServiceVertex(titanDao, new HashMap<>()));
305         }
306
307         // add certified vertices
308         for (int i=0; i<3; i++) {
309             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VF));
310             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VFC));
311             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT));
312             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VL));
313             allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.CP));
314             allVertices.add(GraphTestUtils.createServiceVertex(titanDao, certifiedVerticesProps));
315         }
316         //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)));
317         //String result = GraphTestUtils.exportGraphMl(titanDao.getGraph().left().value(), "");
318         //System.out.println("graph is: " + result);
319     }
320
321     private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){
322         return allVertices.stream().
323                 filter(v -> {
324                     Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties();
325                     if (hasProps != null) {
326                         for (Map.Entry<GraphPropertyEnum, Object> prop: hasProps.entrySet()){
327                             Object value = vertexProps.get(prop.getKey());
328                             if ( value == null || !value.equals(prop.getValue())) {
329                                 return false;
330                             }
331                         }
332                     }
333
334                     if (doesntHaveProps != null) {
335                         for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) {
336                             Object value = vertexProps.get(prop.getKey());
337                             Object propValue = prop.getValue();
338                             if ( value != null && propValue != null && propValue instanceof List ) {
339                                 for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>)propValue) {
340                                     if (propVal.name().equals(value)) {
341                                         return false;
342                                     }
343                                 }
344                             }
345                             else if (value != null && value.equals(propValue)){
346                                 return false;
347                             }
348                         }
349                     }
350                     return true;
351                 }).count();
352     }
353 }