1 package org.openecomp.sdc.be.model.jsontitan.operations;
4 import org.junit.Before;
5 import org.junit.BeforeClass;
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.LifecycleStateEnum;
16 import org.openecomp.sdc.be.model.ModelTestBase;
17 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
18 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
19 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
20 import org.springframework.test.context.ContextConfiguration;
21 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
29 * Created by chaya on 6/12/2017.
32 @RunWith(SpringJUnit4ClassRunner.class)
33 @ContextConfiguration("classpath:application-context-test.xml")
34 public class ToscaElementOperationTest extends ModelTestBase{
36 private List<GraphVertex> allVertices = new ArrayList<>();
37 private boolean isInitialized = false;
39 @javax.annotation.Resource
40 private ToscaElementOperationTestImpl toscaElementOperation;
42 @javax.annotation.Resource
43 private TitanDao titanDao;
46 public static void initTest(){
52 public TestName testName = new TestName();
55 public void beforeTest() {
57 GraphTestUtils.clearGraph(titanDao);
58 //exportGraphMl(titanDao.getGraph().left().value(),"");
65 public void testGetAllHighestResourcesNoFilter() {
67 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
68 assertTrue(highestResourcesRes.isLeft());
69 List<ToscaElement> highestResources = highestResourcesRes.left().value();
70 // calculate expected count value
71 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
73 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
74 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
77 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
84 public void testGetAllResourcesCertifiedNoFilter() {
85 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
86 assertTrue(highestResourcesRes.isLeft());
87 List<ToscaElement> highestResources = highestResourcesRes.left().value();
88 // calculate expected count value
89 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
91 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
92 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
95 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
97 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
98 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
100 }, new HashMap<GraphPropertyEnum, Object>() {
102 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
105 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
109 public void testGetHighestResourcesExclude() {
112 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
113 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
116 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
117 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
119 // exclude CP & VL & VF & VFC
120 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
121 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
125 public void testGetAllResourcesCertifiedExclude() {
127 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
128 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
131 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
132 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
134 // exclude CP & VL & VF & VFC
135 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
136 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
140 public void testGetAllHighestServicesNoFilter() {
141 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
142 assertTrue(highestResourcesRes.isLeft());
143 List<ToscaElement> highestResources = highestResourcesRes.left().value();
144 // calculate expected count value
145 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
147 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
148 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
151 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
155 public void testGetAllCertifiedServicesNoFilter() {
156 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
157 assertTrue(highestResourcesRes.isLeft());
158 List<ToscaElement> highestResources = highestResourcesRes.left().value();
159 // calculate expected count value
160 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
162 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
163 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
166 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
168 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
169 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
171 }, new HashMap<GraphPropertyEnum, Object>() {
173 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
176 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
180 public void testGetServicesExcludeList() {
181 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
182 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
183 assertTrue(highestResourcesRes.isLeft());
184 List<ToscaElement> highestResources = highestResourcesRes.left().value();
185 // calculate expected count value
186 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
188 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
189 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
192 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
196 public void testGetCertifiedServicesExcludeList() {
197 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
198 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
199 assertTrue(highestResourcesRes.isLeft());
200 List<ToscaElement> highestResources = highestResourcesRes.left().value();
201 // calculate expected count value
202 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
204 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
205 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
208 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
210 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
211 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
213 }, new HashMap<GraphPropertyEnum, Object>() {
215 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
218 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
221 private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
222 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludeList, true);
223 assertTrue(highestResourcesRes.isLeft());
224 List<ToscaElement> highestResources = highestResourcesRes.left().value();
225 // calculate expected count value
226 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
228 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
229 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
231 }, new HashMap<GraphPropertyEnum, Object>() {
233 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
236 return highestResources.stream().count() == (highestResourcesExpectedCount);
239 private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
240 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
241 assertTrue(highestResourcesRes.isLeft());
242 List<ToscaElement> highestResources = highestResourcesRes.left().value();
243 // calculate expected count value
244 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
246 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
247 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
249 }, new HashMap<GraphPropertyEnum, Object>() {
251 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
254 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
256 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
257 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
259 }, new HashMap<GraphPropertyEnum, Object>() {
261 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
262 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
265 return highestResources.stream().count() == highestResourcesExpectedCount;
268 private void initGraphForTest() {
269 GraphTestUtils.createRootCatalogVertex(titanDao);
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)){