1 package org.openecomp.sdc.be.model.jsontitan.operations;
4 import org.apache.cassandra.cql3.CQL3Type;
5 import org.junit.Before;
6 import org.junit.BeforeClass;
9 import org.junit.rules.TestName;
10 import org.junit.runner.RunWith;
11 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
12 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
13 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
14 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
15 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
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.ToscaElement;
19 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
20 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
21 import org.springframework.test.context.ContextConfiguration;
22 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
30 * Created by chaya on 6/12/2017.
33 @RunWith(SpringJUnit4ClassRunner.class)
34 @ContextConfiguration("classpath:application-context-test.xml")
35 public class ToscaElementOperationTest extends ModelTestBase{
37 private List<GraphVertex> allVertices = new ArrayList<>();
38 private boolean isInitialized = false;
40 @javax.annotation.Resource
41 ToscaElementOperationTestImpl toscaElementOperation;
43 @javax.annotation.Resource
47 public static void initTest(){
53 public TestName testName = new TestName();
56 public void beforeTest() {
58 GraphTestUtils.clearGraph(titanDao);
59 //exportGraphMl(titanDao.getGraph().left().value(),"");
66 public void testGetAllHighestResourcesNoFilter() {
68 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
69 assertTrue(highestResourcesRes.isLeft());
70 List<ToscaElement> highestResources = highestResourcesRes.left().value();
71 // calculate expected count value
72 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
74 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
75 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
78 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
82 public void testGetAllResourcesCertifiedNoFilter() {
83 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
84 assertTrue(highestResourcesRes.isLeft());
85 List<ToscaElement> highestResources = highestResourcesRes.left().value();
86 // calculate expected count value
87 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
89 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
90 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
93 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
95 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
96 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
98 }, new HashMap<GraphPropertyEnum, Object>() {
100 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
103 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
107 public void testGetHighestResourcesExclude() {
110 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
111 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
114 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
115 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
117 // exclude CP & VL & VF & VFC
118 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
119 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
123 public void testGetAllResourcesCertifiedExclude() {
125 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
126 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
129 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
130 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
132 // exclude CP & VL & VF & VFC
133 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
134 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
138 public void testGetAllHighestServicesNoFilter() {
139 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
140 assertTrue(highestResourcesRes.isLeft());
141 List<ToscaElement> highestResources = highestResourcesRes.left().value();
142 // calculate expected count value
143 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
145 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
146 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
149 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
153 public void testGetAllCertifiedServicesNoFilter() {
154 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
155 assertTrue(highestResourcesRes.isLeft());
156 List<ToscaElement> highestResources = highestResourcesRes.left().value();
157 // calculate expected count value
158 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
160 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
161 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
164 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
166 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
167 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
169 }, new HashMap<GraphPropertyEnum, Object>() {
171 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
174 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
178 public void testGetServicesExcludeList() {
179 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
180 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
181 assertTrue(highestResourcesRes.isLeft());
182 List<ToscaElement> highestResources = highestResourcesRes.left().value();
183 // calculate expected count value
184 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
186 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
187 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
190 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
194 public void testGetCertifiedServicesExcludeList() {
195 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
196 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
197 assertTrue(highestResourcesRes.isLeft());
198 List<ToscaElement> highestResources = highestResourcesRes.left().value();
199 // calculate expected count value
200 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
202 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
203 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
206 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
208 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
209 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
211 }, new HashMap<GraphPropertyEnum, Object>() {
213 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
216 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
219 private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
220 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludeList, true);
221 assertTrue(highestResourcesRes.isLeft());
222 List<ToscaElement> highestResources = highestResourcesRes.left().value();
223 // calculate expected count value
224 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
226 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
227 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
229 }, new HashMap<GraphPropertyEnum, Object>() {
231 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
234 return highestResources.stream().count() == (highestResourcesExpectedCount);
237 private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
238 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
239 assertTrue(highestResourcesRes.isLeft());
240 List<ToscaElement> highestResources = highestResourcesRes.left().value();
241 // calculate expected count value
242 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
244 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
245 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
247 }, new HashMap<GraphPropertyEnum, Object>() {
249 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
252 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
254 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
255 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
257 }, new HashMap<GraphPropertyEnum, Object>() {
259 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
260 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
263 return highestResources.stream().count() == highestResourcesExpectedCount;
266 private void initGraphForTest() {
267 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
269 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
273 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
275 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
276 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
280 // add vertices with higestVersion = true
281 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VF));
282 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VFC));
283 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VFCMT));
284 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VL));
285 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.CP));
286 allVertices.add(GraphTestUtils.createServiceVertex(titanDao, highstVerticesProps));
288 // add vertices with non-additional properties
289 for (int i=0 ; i<2 ; i++) {
290 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VF));
291 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VFC));
292 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VFCMT));
293 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VL));
294 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.CP));
295 allVertices.add(GraphTestUtils.createServiceVertex(titanDao, new HashMap<>()));
298 // add certified vertices
299 for (int i=0; i<3; i++) {
300 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VF));
301 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VFC));
302 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT));
303 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VL));
304 allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.CP));
305 allVertices.add(GraphTestUtils.createServiceVertex(titanDao, certifiedVerticesProps));
307 //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)));
308 //String result = GraphTestUtils.exportGraphMl(titanDao.getGraph().left().value(), "");
309 //System.out.println("graph is: " + result);
312 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){
313 return allVertices.stream().
315 Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties();
316 if (hasProps != null) {
317 for (Map.Entry<GraphPropertyEnum, Object> prop: hasProps.entrySet()){
318 Object value = vertexProps.get(prop.getKey());
319 if ( value == null || !value.equals(prop.getValue())) {
325 if (doesntHaveProps != null) {
326 for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) {
327 Object value = vertexProps.get(prop.getKey());
328 Object propValue = prop.getValue();
329 if ( value != null && propValue != null && propValue instanceof List ) {
330 for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>)propValue) {
331 if (propVal.name().equals(value)) {
336 else if (value != null && value.equals(propValue)){