1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashMap;
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;
32 import fj.data.Either;
35 * Created by chaya on 6/12/2017.
38 @RunWith(SpringJUnit4ClassRunner.class)
39 @ContextConfiguration("classpath:application-context-test.xml")
40 public class ToscaElementOperationTest extends ModelTestBase{
42 private List<GraphVertex> allVertices = new ArrayList<>();
43 private boolean isInitialized = false;
45 @javax.annotation.Resource
46 ToscaElementOperationTestImpl toscaElementOperation;
48 @javax.annotation.Resource
52 public static void initTest(){
58 public TestName testName = new TestName();
61 public void beforeTest() {
63 GraphTestUtils.clearGraph(titanDao);
64 //exportGraphMl(titanDao.getGraph().left().value(),"");
71 public void testGetAllHighestResourcesNoFilter() {
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>() {
79 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
80 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
83 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
97 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
98 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
101 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
103 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
104 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
106 }, new HashMap<GraphPropertyEnum, Object>() {
108 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
111 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
115 public void testGetHighestResourcesExclude() {
118 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
119 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
122 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
123 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
125 // exclude CP & VL & VF & VFC
126 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
127 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
131 public void testGetAllResourcesCertifiedExclude() {
133 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
134 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
137 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
138 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
140 // exclude CP & VL & VF & VFC
141 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
142 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
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>() {
153 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
154 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
157 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
168 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
169 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
172 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
174 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
175 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
177 }, new HashMap<GraphPropertyEnum, Object>() {
179 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
182 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
194 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
195 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
198 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
210 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
211 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
214 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
216 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
217 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
219 }, new HashMap<GraphPropertyEnum, Object>() {
221 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
224 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
234 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
235 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
237 }, new HashMap<GraphPropertyEnum, Object>() {
239 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
242 return highestResources.stream().count() == (highestResourcesExpectedCount);
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>() {
252 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
253 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
255 }, new HashMap<GraphPropertyEnum, Object>() {
257 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
260 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
262 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
263 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
265 }, new HashMap<GraphPropertyEnum, Object>() {
267 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
268 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
271 return highestResources.stream().count() == highestResourcesExpectedCount;
274 private void initGraphForTest() {
275 GraphTestUtils.createRootCatalogVertex(titanDao);
277 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
279 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
283 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
285 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
286 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
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));
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<>()));
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));
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);
322 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){
323 return allVertices.stream().
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())) {
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)) {
346 else if (value != null && value.equals(propValue)){