2 * ============LICENSE_START=============================================================================================================
3 * Copyright (c) 2019 <Company or Individual>.
4 * ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
10 * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
11 * ============LICENSE_END===============================================================================================================
14 package org.openecomp.sdc.be.components.property;
16 import static org.assertj.core.api.Assertions.assertThat;
17 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
18 import static org.mockito.ArgumentMatchers.anyMap;
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Mockito.when;
21 import static org.openecomp.sdc.be.MockGenerator.mockComponentUtils;
22 import static org.openecomp.sdc.be.MockGenerator.mockExceptionUtils;
24 import fj.data.Either;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Optional;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Captor;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
40 import org.openecomp.sdc.be.components.utils.AnnotationBuilder;
41 import org.openecomp.sdc.be.components.utils.InputsBuilder;
42 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
43 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
44 import org.openecomp.sdc.be.datatypes.elements.Annotation;
45 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
46 import org.openecomp.sdc.be.model.Component;
47 import org.openecomp.sdc.be.model.ComponentInstance;
48 import org.openecomp.sdc.be.model.ComponentInstanceInput;
49 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
50 import org.openecomp.sdc.be.model.ComponentParametersView;
51 import org.openecomp.sdc.be.model.InputDefinition;
52 import org.openecomp.sdc.be.model.PropertyDefinition;
53 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.model.operations.StorageException;
55 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
56 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
58 @RunWith(MockitoJUnitRunner.class)
59 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
62 private ComponentInstanceInputPropertyDeclarator testInstance;
65 private ToscaOperationFacade toscaOperationFacade;
68 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
71 private PropertyOperation propertyOperation;
74 private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
76 private Annotation annotation1;
78 private Annotation annotation2;
82 public void setUp() throws Exception {
84 testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
85 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
86 annotation1 = AnnotationBuilder.create()
87 .setType("annotationType1")
88 .setName("annotation1")
93 annotation2 = AnnotationBuilder.create()
94 .setType("annotationType2")
95 .setName("annotation2")
101 public void whenDeclaredPropertyOriginalInputContainsAnnotation_createNewInputWithSameAnnotations() {
102 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
103 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
104 Component originInstanceNodeType = createComponentWithInputAndAnnotation(prop1.getName());
105 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap())).thenReturn(Either.left(Collections.emptyMap()));
106 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.left(originInstanceNodeType));
107 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
108 List<InputDefinition> inputs = createdInputs.left().value();
109 assertThat(inputs).hasSize(1);
110 verifyInputAnnotations(inputs.get(0));
111 assertThat(inputsFilterCaptor.getValue().isIgnoreInputs()).isFalse();
115 public void throwExceptionWhenFailingToGetInstanceOriginType() {
116 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
117 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
118 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
119 assertThatExceptionOfType(StorageException.class).isThrownBy(() -> testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare));
123 public void testCreateDeclaredProperty() {
124 ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
125 assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
129 public void testUpdateDeclaredProperties() {
130 List<ComponentInstanceInput> expectedProperties = Collections.singletonList(new ComponentInstanceInput(prop1));
131 Map<String, List<ComponentInstanceInput>> expectedMap = new HashMap<>();
132 expectedMap.put(prop1.getName(), expectedProperties);
134 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
135 .thenReturn(Either.left(expectedMap));
137 Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
138 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
140 assertThat(updateEither.isLeft());
141 Map<String, List<ComponentInstanceInput>> actualProperties = updateEither.left().value();
142 assertThat(actualProperties.values().size()).isEqualTo(expectedProperties.size());
143 assertThat(actualProperties.values().iterator().next()).isEqualTo(expectedProperties);
147 public void testResolvePropertiesOwner() {
148 Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
150 assertThat(componentInstance.isPresent());
151 assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
155 public void unDeclarePropertiesAsListInputsTest() {
156 InputDefinition inputToDelete = new InputDefinition();
157 inputToDelete.setUniqueId(INPUT_ID);
158 inputToDelete.setName(INPUT_ID);
159 inputToDelete.setIsDeclaredListInput(true);
161 Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
162 PropertyDefinition prop = new PropertyDataDefinitionBuilder()
164 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
166 .setUniqueId("propName")
167 .addGetInputValue(INPUT_ID)
169 component.setProperties(Collections.singletonList(prop));
171 List<ComponentInstanceInput> ciPropList = new ArrayList<>();
172 ComponentInstanceInput ciProp = new ComponentInstanceInput();
173 List<String> pathOfComponentInstances = new ArrayList<>();
174 pathOfComponentInstances.add("pathOfComponentInstances");
175 ciProp.setPath(pathOfComponentInstances);
176 ciProp.setUniqueId("componentInstanceId");
177 ciProp.setDefaultValue("default value");
178 ciProp.setComponentInstanceId("componentInstanceId");
179 ciProp.setComponentInstanceName("componentInstanceName");
180 ciProp.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
181 ciPropList.add(ciProp);
183 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(component), eq(INPUT_ID))).thenReturn(ciPropList);
184 when(propertyOperation.findDefaultValueFromSecondPosition(eq(pathOfComponentInstances), eq(ciProp.getUniqueId()), eq(ciProp.getDefaultValue()))).thenReturn(Either.left(ciProp.getDefaultValue()));
185 when(toscaOperationFacade.updateComponentInstanceInputs(eq(component), eq(ciProp.getComponentInstanceId()), eq(ciPropList))).thenReturn(StorageOperationStatus.OK);
186 StorageOperationStatus storageOperationStatus = testInstance.unDeclarePropertiesAsListInputs(component, inputToDelete);
188 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
192 public void unDeclarePropertiesAsListInputsTest_whenNoListInput_returnOk() {
193 InputDefinition input = new InputDefinition();
194 input.setUniqueId(INPUT_ID);
195 input.setName(INPUT_ID);
196 input.setValue("value");
197 List<ComponentInstanceInput> resList = new ArrayList<>();
198 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(resource), eq(INPUT_ID))).thenReturn(resList);
199 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
200 Assert.assertEquals(status, StorageOperationStatus.OK);
203 private void verifyInputAnnotations(InputDefinition inputDefinition) {
204 List<Annotation> annotations = inputDefinition.getAnnotations();
205 assertThat(annotations)
206 .containsExactlyInAnyOrder(annotation1, annotation2);
209 private Component createComponentWithInputAndAnnotation(String inputName) {
210 InputDefinition input = InputsBuilder.create()
212 .addAnnotation(annotation1)
213 .addAnnotation(annotation2)
215 return new ResourceBuilder()
220 private Component createComponentWithListInput(String inputName, String propName) {
221 InputDefinition input = InputsBuilder.create()
225 input.setUniqueId(INPUT_ID);
226 input.setName(INPUT_ID);
227 input.setDefaultValue("defaultValue");
228 input.setValue(generateGetInputValueAsListInput(inputName, propName));
230 return new ResourceBuilder()
231 .setUniqueId(RESOURCE_ID)