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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import org.junit.Rule;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.rules.TestName;
39 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
40 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
43 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
44 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
45 import org.openecomp.sdc.be.model.ComponentParametersView;
46 import org.openecomp.sdc.be.model.DataTypeDefinition;
47 import org.openecomp.sdc.be.model.LifecycleStateEnum;
48 import org.openecomp.sdc.be.model.ModelTestBase;
49 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
52 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
53 import org.openecomp.sdc.be.resources.data.EntryData;
54 import org.openecomp.sdc.be.utils.TypeUtils;
55 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
58 * Created by chaya on 6/12/2017.
61 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
62 public class ToscaElementOperationTest extends ModelTestBase {
65 public TestName testName = new TestName();
66 private List<GraphVertex> allVertices = new ArrayList<>();
67 private boolean isInitialized = false;
68 @javax.annotation.Resource
69 private ToscaElementOperationTestImpl toscaElementOperation;
70 @javax.annotation.Resource
71 private JanusGraphDao janusGraphDao;
74 public static void initTest() {
80 public void beforeTest() {
82 GraphTestUtils.clearGraph(janusGraphDao);
83 //exportGraphMl(janusGraphDao.getGraph().left().value(),"");
90 public void testGetAllHighestResourcesNoFilter() {
92 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
93 .getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
94 assertTrue(highestResourcesRes.isLeft());
95 List<ToscaElement> highestResources = highestResourcesRes.left().value();
96 // calculate expected count value
97 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
99 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
100 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
103 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
107 public void testGetAllResourcesCertifiedNoFilter() {
108 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
109 .getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
110 assertTrue(highestResourcesRes.isLeft());
111 List<ToscaElement> highestResources = highestResourcesRes.left().value();
112 // calculate expected count value
113 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
115 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
116 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
119 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
121 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
122 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
124 }, new HashMap<GraphPropertyEnum, Object>() {
126 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
129 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
133 public void testGetHighestResourcesExclude() {
136 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
137 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
140 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
141 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
143 // exclude CP & VL & VF & VFC
145 .asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
146 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
150 public void testGetAllResourcesCertifiedExclude() {
152 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
153 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
156 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
157 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
159 // exclude CP & VL & VF & VFC
161 .asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
162 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
166 public void testGetAllHighestServicesNoFilter() {
167 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
168 .getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
169 assertTrue(highestResourcesRes.isLeft());
170 List<ToscaElement> highestResources = highestResourcesRes.left().value();
171 // calculate expected count value
172 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
174 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
175 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
178 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
182 public void testGetAllCertifiedServicesNoFilter() {
183 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
184 .getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
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.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
191 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
194 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
196 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
197 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
199 }, new HashMap<GraphPropertyEnum, Object>() {
201 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
204 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
208 public void testGetServicesExcludeList() {
209 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
210 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
211 .getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
212 assertTrue(highestResourcesRes.isLeft());
213 List<ToscaElement> highestResources = highestResourcesRes.left().value();
214 // calculate expected count value
215 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
217 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
218 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
221 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
225 public void testGetCertifiedServicesExcludeList() {
226 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
227 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
228 .getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
229 assertTrue(highestResourcesRes.isLeft());
230 List<ToscaElement> highestResources = highestResourcesRes.left().value();
231 // calculate expected count value
232 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
234 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
235 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
238 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
240 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
241 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
243 }, new HashMap<GraphPropertyEnum, Object>() {
245 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
248 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
252 public void testUpdateToscaElement_NotFound() {
253 Either<TopologyTemplate, StorageOperationStatus> result;
254 TopologyTemplate topologyTemplate = new TopologyTemplate();
255 String userID = "userID";
256 topologyTemplate.setLastUpdaterUserId(userID);
257 GraphVertex graphVertex = new GraphVertex();
258 ComponentParametersView componentParametersView = new ComponentParametersView();
259 result = toscaElementOperation.updateToscaElement(topologyTemplate, graphVertex, componentParametersView);
260 assertEquals(null, result);
264 public void testCreateDataType() {
265 final String expected = "newDataType";
266 final DataTypeDefinition result = ToscaElementOperation.createDataType(expected);
267 assertNotNull(result);
268 assertEquals(expected, result.getName());
272 public void testCreateDataTypeDefinitionWithName() {
273 final String expected = "newDataType";
274 final String description = "DESCRIPTION";
275 final String derivedFromName = "DERIVED_FROM_NAME";
276 final String uniqueId = "UNIQUE_ID";
278 final Map<String, Object> attributeMap = new HashMap<>();
279 attributeMap.put(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), description);
280 attributeMap.put(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM_NAME.getElementName(), derivedFromName);
282 final Map<String, Object> derivedFromMap = new HashMap<>();
283 derivedFromMap.put(JsonPresentationFields.NAME.getPresentation(), derivedFromName);
284 derivedFromMap.put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
285 final long creationTime = System.currentTimeMillis();
286 derivedFromMap.put(JsonPresentationFields.CREATION_TIME.getPresentation(), creationTime);
287 final long modificationTime = System.currentTimeMillis();
288 derivedFromMap.put(JsonPresentationFields.MODIFICATION_TIME.getPresentation(), modificationTime);
290 attributeMap.put(JsonPresentationFields.DERIVED_FROM.getPresentation(), derivedFromMap);
292 final Entry<String, Object> attributeNameValue = new EntryData<>(expected, attributeMap);
293 final DataTypeDefinition result = ToscaElementOperation.createDataTypeDefinitionWithName(attributeNameValue);
295 assertNotNull(result);
296 assertEquals(derivedFromName, result.getDerivedFromName());
297 assertEquals(description, result.getDescription());
299 final DataTypeDefinition resultDerivedFrom = result.getDerivedFrom();
300 assertNotNull(resultDerivedFrom);
301 assertEquals(derivedFromName, resultDerivedFrom.getName());
302 assertEquals(uniqueId, resultDerivedFrom.getUniqueId());
303 assertEquals(creationTime, resultDerivedFrom.getCreationTime().longValue());
304 assertEquals(modificationTime, resultDerivedFrom.getModificationTime().longValue());
308 private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
309 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
310 .getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, true);
311 assertTrue(highestResourcesRes.isLeft());
312 List<ToscaElement> highestResources = highestResourcesRes.left().value();
313 // calculate expected count value
314 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
316 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
317 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
319 }, new HashMap<GraphPropertyEnum, Object>() {
321 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
324 return highestResources.stream().count() == (highestResourcesExpectedCount);
327 private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
328 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
329 .getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
330 assertTrue(highestResourcesRes.isLeft());
331 List<ToscaElement> highestResources = highestResourcesRes.left().value();
332 // calculate expected count value
333 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
335 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
336 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
338 }, new HashMap<GraphPropertyEnum, Object>() {
340 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
343 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
345 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
346 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
348 }, new HashMap<GraphPropertyEnum, Object>() {
350 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
351 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
354 return highestResources.stream().count() == highestResourcesExpectedCount;
357 private void initGraphForTest() {
358 GraphTestUtils.createRootCatalogVertex(janusGraphDao);
360 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
362 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
366 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
368 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
369 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
373 // add vertices with higestVersion = true
374 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VF));
375 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFC));
377 .add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFCMT));
378 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VL));
379 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.CP));
380 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, highstVerticesProps));
382 // add vertices with non-additional properties
383 for (int i = 0; i < 2; i++) {
384 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF));
385 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFC));
387 .add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFCMT));
388 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VL));
389 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.CP));
390 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>()));
393 // add certified vertices
394 for (int i = 0; i < 3; i++) {
396 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VF));
398 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFC));
400 GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT));
402 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VL));
404 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.CP));
405 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, certifiedVerticesProps));
407 //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)));
408 //String result = GraphTestUtils.exportGraphMl(janusGraphDao.getGraph().left().value(), "");
409 //System.out.println("graph is: " + result);
412 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps,
413 Map<GraphPropertyEnum, Object> doesntHaveProps) {
414 return allVertices.stream().
416 Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties();
417 if (hasProps != null) {
418 for (Map.Entry<GraphPropertyEnum, Object> prop : hasProps.entrySet()) {
419 Object value = vertexProps.get(prop.getKey());
420 if (value == null || !value.equals(prop.getValue())) {
426 if (doesntHaveProps != null) {
427 for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) {
428 Object value = vertexProps.get(prop.getKey());
429 Object propValue = prop.getValue();
430 if (value != null && propValue != null && propValue instanceof List) {
431 for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>) propValue) {
432 if (propVal.name().equals(value)) {
436 } else if (value != null && value.equals(propValue)) {