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);
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>() {
93 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
94 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
97 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
99 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
100 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
102 }, new HashMap<GraphPropertyEnum, Object>() {
104 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
107 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
111 public void testGetHighestResourcesExclude() {
114 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
115 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
118 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
119 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
121 // exclude CP & VL & VF & VFC
122 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
123 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
127 public void testGetAllResourcesCertifiedExclude() {
129 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
130 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
133 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
134 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
136 // exclude CP & VL & VF & VFC
137 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
138 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
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>() {
149 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
150 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
153 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
164 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
165 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
168 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
170 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
171 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
173 }, new HashMap<GraphPropertyEnum, Object>() {
175 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
178 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
190 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
191 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
194 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
206 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
207 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
210 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
212 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
213 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
215 }, new HashMap<GraphPropertyEnum, Object>() {
217 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
220 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
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>() {
230 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
231 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
233 }, new HashMap<GraphPropertyEnum, Object>() {
235 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
238 return highestResources.stream().count() == (highestResourcesExpectedCount);
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>() {
248 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
249 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
251 }, new HashMap<GraphPropertyEnum, Object>() {
253 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
256 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
258 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
259 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
261 }, new HashMap<GraphPropertyEnum, Object>() {
263 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
264 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
267 return highestResources.stream().count() == highestResourcesExpectedCount;
270 private void initGraphForTest() {
271 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
273 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
277 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
279 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
280 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
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));
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<>()));
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));
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);
316 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){
317 return allVertices.stream().
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())) {
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)) {
340 else if (value != null && value.equals(propValue)){