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 java.util.Map.Entry;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.TestName;
30 import org.junit.runner.RunWith;
31 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
32 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
33 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
34 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
35 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
36 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
37 import org.openecomp.sdc.be.model.ComponentParametersView;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.LifecycleStateEnum;
40 import org.openecomp.sdc.be.model.ModelTestBase;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.resources.data.EntryData;
46 import org.openecomp.sdc.be.utils.TypeUtils;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertNotNull;
54 import static org.junit.Assert.assertTrue;
57 * Created by chaya on 6/12/2017.
60 @RunWith(SpringJUnit4ClassRunner.class)
61 @ContextConfiguration("classpath:application-context-test.xml")
62 public class ToscaElementOperationTest extends ModelTestBase {
64 private List<GraphVertex> allVertices = new ArrayList<>();
65 private boolean isInitialized = false;
67 @javax.annotation.Resource
68 private ToscaElementOperationTestImpl toscaElementOperation;
70 @javax.annotation.Resource
71 private JanusGraphDao janusGraphDao;
74 public static void initTest() {
80 public TestName testName = new TestName();
83 public void beforeTest() {
85 GraphTestUtils.clearGraph(janusGraphDao);
86 //exportGraphMl(janusGraphDao.getGraph().left().value(),"");
93 public void testGetAllHighestResourcesNoFilter() {
95 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
96 .getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
97 assertTrue(highestResourcesRes.isLeft());
98 List<ToscaElement> highestResources = highestResourcesRes.left().value();
99 // calculate expected count value
100 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
102 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
103 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
106 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
110 public void testGetAllResourcesCertifiedNoFilter() {
111 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
112 .getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
113 assertTrue(highestResourcesRes.isLeft());
114 List<ToscaElement> highestResources = highestResourcesRes.left().value();
115 // calculate expected count value
116 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
118 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
119 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
122 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
124 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
125 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
127 }, new HashMap<GraphPropertyEnum, Object>() {
129 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
132 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
136 public void testGetHighestResourcesExclude() {
139 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
140 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
143 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
144 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
146 // exclude CP & VL & VF & VFC
148 .asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
149 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
153 public void testGetAllResourcesCertifiedExclude() {
155 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
156 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
159 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
160 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
162 // exclude CP & VL & VF & VFC
164 .asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
165 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
169 public void testGetAllHighestServicesNoFilter() {
170 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
171 .getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
172 assertTrue(highestResourcesRes.isLeft());
173 List<ToscaElement> highestResources = highestResourcesRes.left().value();
174 // calculate expected count value
175 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
177 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
178 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
181 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
185 public void testGetAllCertifiedServicesNoFilter() {
186 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
187 .getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
188 assertTrue(highestResourcesRes.isLeft());
189 List<ToscaElement> highestResources = highestResourcesRes.left().value();
190 // calculate expected count value
191 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
193 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
194 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
197 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
199 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
200 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
202 }, new HashMap<GraphPropertyEnum, Object>() {
204 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
207 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
211 public void testGetServicesExcludeList() {
212 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
213 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
214 .getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
215 assertTrue(highestResourcesRes.isLeft());
216 List<ToscaElement> highestResources = highestResourcesRes.left().value();
217 // calculate expected count value
218 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
220 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
221 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
224 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
228 public void testGetCertifiedServicesExcludeList() {
229 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
230 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
231 .getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
232 assertTrue(highestResourcesRes.isLeft());
233 List<ToscaElement> highestResources = highestResourcesRes.left().value();
234 // calculate expected count value
235 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
237 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
238 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
241 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
243 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
244 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
246 }, new HashMap<GraphPropertyEnum, Object>() {
248 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
251 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
255 public void testUpdateToscaElement_NotFound() {
256 Either<TopologyTemplate, StorageOperationStatus> result;
257 TopologyTemplate topologyTemplate = new TopologyTemplate();
258 String userID = "userID";
259 topologyTemplate.setLastUpdaterUserId(userID);
260 GraphVertex graphVertex = new GraphVertex();
261 ComponentParametersView componentParametersView = new ComponentParametersView();
262 result = toscaElementOperation.updateToscaElement(topologyTemplate, graphVertex, componentParametersView);
263 assertEquals(null, result);
267 public void testCreateDataType() {
268 final String expected = "newDataType";
269 final DataTypeDefinition result = ToscaElementOperation.createDataType(expected);
270 assertNotNull(result);
271 assertEquals(expected, result.getName());
275 public void testCreateDataTypeDefinitionWithName() {
276 final String expected = "newDataType";
277 final String description = "DESCRIPTION";
278 final String derivedFromName = "DERIVED_FROM_NAME";
279 final String uniqueId = "UNIQUE_ID";
281 final Map<String, Object> attributeMap = new HashMap<>();
282 attributeMap.put(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), description);
283 attributeMap.put(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM_NAME.getElementName(), derivedFromName);
285 final Map<String, Object> derivedFromMap = new HashMap<>();
286 derivedFromMap.put(JsonPresentationFields.NAME.getPresentation(), derivedFromName);
287 derivedFromMap.put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
288 final long creationTime = System.currentTimeMillis();
289 derivedFromMap.put(JsonPresentationFields.CREATION_TIME.getPresentation(), creationTime);
290 final long modificationTime = System.currentTimeMillis();
291 derivedFromMap.put(JsonPresentationFields.MODIFICATION_TIME.getPresentation(), modificationTime);
293 attributeMap.put(JsonPresentationFields.DERIVED_FROM.getPresentation(), derivedFromMap);
295 final Entry<String, Object> attributeNameValue = new EntryData<>(expected, attributeMap);
296 final DataTypeDefinition result = ToscaElementOperation.createDataTypeDefinitionWithName(attributeNameValue);
298 assertNotNull(result);
299 assertEquals(derivedFromName, result.getDerivedFromName());
300 assertEquals(description, result.getDescription());
302 final DataTypeDefinition resultDerivedFrom = result.getDerivedFrom();
303 assertNotNull(resultDerivedFrom);
304 assertEquals(derivedFromName, resultDerivedFrom.getName());
305 assertEquals(uniqueId, resultDerivedFrom.getUniqueId());
306 assertEquals(creationTime, resultDerivedFrom.getCreationTime().longValue());
307 assertEquals(modificationTime, resultDerivedFrom.getModificationTime().longValue());
311 private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
312 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
313 .getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, true);
314 assertTrue(highestResourcesRes.isLeft());
315 List<ToscaElement> highestResources = highestResourcesRes.left().value();
316 // calculate expected count value
317 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
319 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
320 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
322 }, new HashMap<GraphPropertyEnum, Object>() {
324 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
327 return highestResources.stream().count() == (highestResourcesExpectedCount);
330 private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
331 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation
332 .getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
333 assertTrue(highestResourcesRes.isLeft());
334 List<ToscaElement> highestResources = highestResourcesRes.left().value();
335 // calculate expected count value
336 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
338 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
339 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
341 }, new HashMap<GraphPropertyEnum, Object>() {
343 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
346 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
348 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
349 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
351 }, new HashMap<GraphPropertyEnum, Object>() {
353 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
354 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
357 return highestResources.stream().count() == highestResourcesExpectedCount;
360 private void initGraphForTest() {
361 GraphTestUtils.createRootCatalogVertex(janusGraphDao);
363 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
365 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
369 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
371 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
372 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
376 // add vertices with higestVersion = true
377 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VF));
378 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFC));
380 .add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFCMT));
381 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VL));
382 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.CP));
383 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, highstVerticesProps));
385 // add vertices with non-additional properties
386 for (int i = 0; i < 2; i++) {
387 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF));
388 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFC));
390 .add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFCMT));
391 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VL));
392 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.CP));
393 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>()));
396 // add certified vertices
397 for (int i = 0; i < 3; i++) {
399 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VF));
401 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFC));
403 GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT));
405 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VL));
407 .add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.CP));
408 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, certifiedVerticesProps));
410 //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)));
411 //String result = GraphTestUtils.exportGraphMl(janusGraphDao.getGraph().left().value(), "");
412 //System.out.println("graph is: " + result);
415 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps,
416 Map<GraphPropertyEnum, Object> doesntHaveProps) {
417 return allVertices.stream().
419 Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties();
420 if (hasProps != null) {
421 for (Map.Entry<GraphPropertyEnum, Object> prop : hasProps.entrySet()) {
422 Object value = vertexProps.get(prop.getKey());
423 if (value == null || !value.equals(prop.getValue())) {
429 if (doesntHaveProps != null) {
430 for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) {
431 Object value = vertexProps.get(prop.getKey());
432 Object propValue = prop.getValue();
433 if (value != null && propValue != null && propValue instanceof List) {
434 for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>) propValue) {
435 if (propVal.name().equals(value)) {
439 } else if (value != null && value.equals(propValue)) {