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.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;
31 import fj.data.Either;
34 * Created by chaya on 6/12/2017.
37 @RunWith(SpringJUnit4ClassRunner.class)
38 @ContextConfiguration("classpath:application-context-test.xml")
39 public class ToscaElementOperationTest extends ModelTestBase{
41 private List<GraphVertex> allVertices = new ArrayList<>();
42 private boolean isInitialized = false;
44 @javax.annotation.Resource
45 ToscaElementOperationTestImpl toscaElementOperation;
47 @javax.annotation.Resource
51 public static void initTest(){
57 public TestName testName = new TestName();
60 public void beforeTest() {
62 GraphTestUtils.clearGraph(titanDao);
63 //exportGraphMl(titanDao.getGraph().left().value(),"");
70 public void testGetAllHighestResourcesNoFilter() {
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>() {
78 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
79 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
82 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
96 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
97 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
100 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
102 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
103 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
105 }, new HashMap<GraphPropertyEnum, Object>() {
107 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
110 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
114 public void testGetHighestResourcesExclude() {
117 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
118 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
121 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
122 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
124 // exclude CP & VL & VF & VFC
125 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
126 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
130 public void testGetAllResourcesCertifiedExclude() {
132 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
133 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
136 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
137 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
139 // exclude CP & VL & VF & VFC
140 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
141 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
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>() {
152 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
153 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
156 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
167 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
168 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
171 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
173 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
174 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
176 }, new HashMap<GraphPropertyEnum, Object>() {
178 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
181 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
193 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
194 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
197 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
209 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
210 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
213 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
215 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
216 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
218 }, new HashMap<GraphPropertyEnum, Object>() {
220 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
223 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
233 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
234 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
236 }, new HashMap<GraphPropertyEnum, Object>() {
238 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
241 return highestResources.stream().count() == (highestResourcesExpectedCount);
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>() {
251 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
252 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
254 }, new HashMap<GraphPropertyEnum, Object>() {
256 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
259 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
261 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
262 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
264 }, new HashMap<GraphPropertyEnum, Object>() {
266 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
267 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
270 return highestResources.stream().count() == highestResourcesExpectedCount;
273 private void initGraphForTest() {
274 GraphTestUtils.createRootCatalogVertex(titanDao);
276 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
278 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
282 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
284 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
285 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
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));
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<>()));
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));
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);
321 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){
322 return allVertices.stream().
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())) {
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)) {
345 else if (value != null && value.equals(propValue)){