2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
22 package org.openecomp.sdc.be.components.impl.generic;
24 import fj.data.Either;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.InputDefinition;
34 import org.openecomp.sdc.be.model.PropertyDefinition;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.exception.ResponseFormat;
39 import java.util.Arrays;
40 import java.util.List;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertTrue;
44 import static org.mockito.Mockito.when;
46 public class GenericTypeBusinessLogicTest {
49 private GenericTypeBusinessLogic testInstance;
52 private ToscaOperationFacade toscaOperationFacadeMock;
55 private ComponentsUtils componentsUtils;
58 public void setUp() throws Exception {
59 MockitoAnnotations.initMocks(this);
60 testInstance = new GenericTypeBusinessLogic(componentsUtils, toscaOperationFacadeMock);
64 public void fetchDerivedFromGenericType_cvfv_getGenericResourceTypeFromDerivedFrom() throws Exception {
65 Resource cvfc = new Resource();
66 cvfc.setResourceType(ResourceTypeEnum.CVFC);
67 cvfc.setDerivedFrom(Arrays.asList("genericType", "someOtherType"));
68 cvfc.setDerivedFromGenericType("genericType");
69 Resource genericResource = new Resource();
70 when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
71 Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(cvfc);
72 assertEquals(genericResource, fetchedGenericType.left().value());
76 public void fetchDerivedFromGenericType_getGenericResourceTypeFromConfiguration() throws Exception {
77 Resource resource = Mockito.mock(Resource.class);
78 when(resource.getResourceType()).thenReturn(ResourceTypeEnum.VF);
79 when(resource.fetchGenericTypeToscaNameFromConfig()).thenReturn("genericType");
80 Resource genericResource = new Resource();
81 when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
82 Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(resource);
83 assertEquals(genericResource, fetchedGenericType.left().value());
87 public void generateInputsFromGenericTypeProperties() throws Exception {
88 Resource genericNodeType = new Resource();
89 genericNodeType.setUniqueId("genericUid");
90 PropertyDefinition propertyDefinition = generatePropDefinition("prop1");
91 PropertyDefinition propertyDefinition2 = generatePropDefinition("prop2");
93 genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));
95 List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
96 assertEquals(2, genericInputs.size());
97 assertInput(genericInputs.get(0), propertyDefinition);
98 assertInput(genericInputs.get(1), propertyDefinition2);
102 public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
103 Resource genericNodeType = new Resource();
104 assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
107 private void assertInput(InputDefinition inputDefinition, PropertyDefinition propertyDefinition) {
108 assertEquals(inputDefinition.getOwnerId(), "genericUid");
109 assertEquals(inputDefinition.getValue(), propertyDefinition.getValue());
110 assertEquals(inputDefinition.getName(), propertyDefinition.getName());
113 private PropertyDefinition generatePropDefinition(String name) {
114 PropertyDefinition propertyDefinition = new PropertyDefinition();
115 propertyDefinition.setName(name);
116 propertyDefinition.setValue(name + "value");
117 return propertyDefinition;