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