2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TestName;
29 import org.junit.runner.RunWith;
30 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
31 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
32 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
33 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
34 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
35 import org.openecomp.sdc.be.model.ComponentParametersView;
36 import org.openecomp.sdc.be.model.LifecycleStateEnum;
37 import org.openecomp.sdc.be.model.ModelTestBase;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertTrue;
51 * Created by chaya on 6/12/2017.
54 @RunWith(SpringJUnit4ClassRunner.class)
55 @ContextConfiguration("classpath:application-context-test.xml")
56 public class ToscaElementOperationTest extends ModelTestBase{
58 private List<GraphVertex> allVertices = new ArrayList<>();
59 private boolean isInitialized = false;
61 @javax.annotation.Resource
62 private ToscaElementOperationTestImpl toscaElementOperation;
64 @javax.annotation.Resource
65 private JanusGraphDao janusGraphDao;
68 public static void initTest(){
74 public TestName testName = new TestName();
77 public void beforeTest() {
79 GraphTestUtils.clearGraph(janusGraphDao);
80 //exportGraphMl(janusGraphDao.getGraph().left().value(),"");
88 public void testGetAllHighestResourcesNoFilter() {
90 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
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.IS_HIGHEST_VERSION, true);
97 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
100 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
107 public void testGetAllResourcesCertifiedNoFilter() {
108 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
109 assertTrue(highestResourcesRes.isLeft());
110 List<ToscaElement> highestResources = highestResourcesRes.left().value();
111 // calculate expected count value
112 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
114 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
115 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
118 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
120 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
121 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
123 }, new HashMap<GraphPropertyEnum, Object>() {
125 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
128 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
132 public void testGetHighestResourcesExclude() {
135 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
136 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
139 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
140 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
142 // exclude CP & VL & VF & VFC
143 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
144 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
148 public void testGetAllResourcesCertifiedExclude() {
150 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
151 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
154 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
155 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
157 // exclude CP & VL & VF & VFC
158 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
159 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
163 public void testGetAllHighestServicesNoFilter() {
164 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
165 assertTrue(highestResourcesRes.isLeft());
166 List<ToscaElement> highestResources = highestResourcesRes.left().value();
167 // calculate expected count value
168 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
170 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
171 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
174 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
178 public void testGetAllCertifiedServicesNoFilter() {
179 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
180 assertTrue(highestResourcesRes.isLeft());
181 List<ToscaElement> highestResources = highestResourcesRes.left().value();
182 // calculate expected count value
183 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
185 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
186 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
189 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
191 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
192 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
194 }, new HashMap<GraphPropertyEnum, Object>() {
196 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
199 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
203 public void testGetServicesExcludeList() {
204 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
205 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
206 assertTrue(highestResourcesRes.isLeft());
207 List<ToscaElement> highestResources = highestResourcesRes.left().value();
208 // calculate expected count value
209 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
211 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
212 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
215 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
219 public void testGetCertifiedServicesExcludeList() {
220 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
221 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
222 assertTrue(highestResourcesRes.isLeft());
223 List<ToscaElement> highestResources = highestResourcesRes.left().value();
224 // calculate expected count value
225 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
227 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
228 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
231 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
233 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
234 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
236 }, new HashMap<GraphPropertyEnum, Object>() {
238 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
241 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
245 public void testUpdateToscaElement_NotFound() {
246 Either<TopologyTemplate, StorageOperationStatus> result;
247 TopologyTemplate topologyTemplate = new TopologyTemplate();
248 String userID = "userID";
249 topologyTemplate.setLastUpdaterUserId(userID);
250 GraphVertex graphVertex = new GraphVertex();
251 ComponentParametersView componentParametersView = new ComponentParametersView();
252 result = toscaElementOperation.updateToscaElement(topologyTemplate, graphVertex, componentParametersView);
253 assertEquals(null, result);
256 private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
257 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludeList, true);
258 assertTrue(highestResourcesRes.isLeft());
259 List<ToscaElement> highestResources = highestResourcesRes.left().value();
260 // calculate expected count value
261 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
263 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
264 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
266 }, new HashMap<GraphPropertyEnum, Object>() {
268 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
271 return highestResources.stream().count() == (highestResourcesExpectedCount);
274 private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
275 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
276 assertTrue(highestResourcesRes.isLeft());
277 List<ToscaElement> highestResources = highestResourcesRes.left().value();
278 // calculate expected count value
279 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
281 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
282 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
284 }, new HashMap<GraphPropertyEnum, Object>() {
286 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
289 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
291 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
292 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
294 }, new HashMap<GraphPropertyEnum, Object>() {
296 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
297 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
300 return highestResources.stream().count() == highestResourcesExpectedCount;
303 private void initGraphForTest() {
304 GraphTestUtils.createRootCatalogVertex(janusGraphDao);
306 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
308 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
312 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
314 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
315 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
319 // add vertices with higestVersion = true
320 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VF));
321 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFC));
322 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFCMT));
323 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VL));
324 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.CP));
325 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, highstVerticesProps));
327 // add vertices with non-additional properties
328 for (int i=0 ; i<2 ; i++) {
329 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF));
330 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFC));
331 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFCMT));
332 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VL));
333 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.CP));
334 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>()));
337 // add certified vertices
338 for (int i=0; i<3; i++) {
339 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VF));
340 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFC));
341 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT));
342 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VL));
343 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.CP));
344 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, certifiedVerticesProps));
346 //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)));
347 //String result = GraphTestUtils.exportGraphMl(janusGraphDao.getGraph().left().value(), "");
348 //System.out.println("graph is: " + result);
351 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){
352 return allVertices.stream().
354 Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties();
355 if (hasProps != null) {
356 for (Map.Entry<GraphPropertyEnum, Object> prop: hasProps.entrySet()){
357 Object value = vertexProps.get(prop.getKey());
358 if ( value == null || !value.equals(prop.getValue())) {
364 if (doesntHaveProps != null) {
365 for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) {
366 Object value = vertexProps.get(prop.getKey());
367 Object propValue = prop.getValue();
368 if ( value != null && propValue != null && propValue instanceof List ) {
369 for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>)propValue) {
370 if (propVal.name().equals(value)) {
375 else if (value != null && value.equals(propValue)){