2 * Copyright © 2016-2019 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.be.components.property;
19 import fj.data.Either;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Captor;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
29 import org.openecomp.sdc.be.components.utils.AnnotationBuilder;
30 import org.openecomp.sdc.be.components.utils.InputsBuilder;
31 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
32 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
33 import org.openecomp.sdc.be.datatypes.elements.Annotation;
34 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.ComponentInstance;
37 import org.openecomp.sdc.be.model.ComponentInstanceInput;
38 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
39 import org.openecomp.sdc.be.model.ComponentParametersView;
40 import org.openecomp.sdc.be.model.InputDefinition;
41 import org.openecomp.sdc.be.model.PropertyDefinition;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
43 import org.openecomp.sdc.be.model.operations.StorageException;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.HashMap;
50 import java.util.List;
52 import java.util.Optional;
54 import static org.assertj.core.api.Assertions.assertThat;
55 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
56 import static org.mockito.ArgumentMatchers.anyMap;
57 import static org.mockito.ArgumentMatchers.eq;
58 import static org.mockito.Mockito.when;
59 import static org.openecomp.sdc.be.MockGenerator.mockComponentUtils;
60 import static org.openecomp.sdc.be.MockGenerator.mockExceptionUtils;
62 @RunWith(MockitoJUnitRunner.class)
63 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
66 private ComponentInstanceInputPropertyDeclarator testInstance;
69 private ToscaOperationFacade toscaOperationFacade;
72 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
75 private PropertyOperation propertyOperation;
78 private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
80 private Annotation annotation1;
82 private Annotation annotation2;
86 public void setUp() throws Exception {
88 testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
89 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
90 annotation1 = AnnotationBuilder.create()
91 .setType("annotationType1")
92 .setName("annotation1")
97 annotation2 = AnnotationBuilder.create()
98 .setType("annotationType2")
99 .setName("annotation2")
100 .addProperty("prop3")
105 public void whenDeclaredPropertyOriginalInputContainsAnnotation_createNewInputWithSameAnnotations() {
106 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
107 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
108 Component originInstanceNodeType = createComponentWithInputAndAnnotation(prop1.getName());
109 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap())).thenReturn(Either.left(Collections.emptyMap()));
110 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.left(originInstanceNodeType));
111 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
112 List<InputDefinition> inputs = createdInputs.left().value();
113 assertThat(inputs).hasSize(1);
114 verifyInputAnnotations(inputs.get(0));
115 assertThat(inputsFilterCaptor.getValue().isIgnoreInputs()).isFalse();
119 public void throwExceptionWhenFailingToGetInstanceOriginType() {
120 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
121 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
122 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
123 assertThatExceptionOfType(StorageException.class).isThrownBy(() -> testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare));
127 public void testCreateDeclaredProperty() {
128 ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
129 assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
133 public void testUpdateDeclaredProperties() {
134 List<ComponentInstanceInput> expectedProperties = Collections.singletonList(new ComponentInstanceInput(prop1));
135 Map<String, List<ComponentInstanceInput>> expectedMap = new HashMap<>();
136 expectedMap.put(prop1.getName(), expectedProperties);
138 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
139 .thenReturn(Either.left(expectedMap));
141 Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
142 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
144 assertThat(updateEither.isLeft());
145 Map<String, List<ComponentInstanceInput>> actualProperties = updateEither.left().value();
146 assertThat(actualProperties.values().size()).isEqualTo(expectedProperties.size());
147 assertThat(actualProperties.values().iterator().next()).isEqualTo(expectedProperties);
151 public void testResolvePropertiesOwner() {
152 Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
154 assertThat(componentInstance.isPresent());
155 assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
159 public void unDeclarePropertiesAsListInputsTest() {
160 InputDefinition inputToDelete = new InputDefinition();
161 inputToDelete.setUniqueId(INPUT_ID);
162 inputToDelete.setName(INPUT_ID);
163 inputToDelete.setIsDeclaredListInput(true);
165 Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
166 PropertyDefinition prop = new PropertyDataDefinitionBuilder()
168 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
170 .setUniqueId("propName")
171 .addGetInputValue(INPUT_ID)
173 component.setProperties(Collections.singletonList(prop));
175 List<ComponentInstanceInput> ciPropList = new ArrayList<>();
176 ComponentInstanceInput ciProp = new ComponentInstanceInput();
177 List<String> pathOfComponentInstances = new ArrayList<>();
178 pathOfComponentInstances.add("pathOfComponentInstances");
179 ciProp.setPath(pathOfComponentInstances);
180 ciProp.setUniqueId("componentInstanceId");
181 ciProp.setDefaultValue("default value");
182 ciProp.setComponentInstanceId("componentInstanceId");
183 ciProp.setComponentInstanceName("componentInstanceName");
184 ciProp.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
185 ciPropList.add(ciProp);
187 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(component), eq(INPUT_ID))).thenReturn(ciPropList);
188 when(propertyOperation.findDefaultValueFromSecondPosition(eq(pathOfComponentInstances), eq(ciProp.getUniqueId()), eq(ciProp.getDefaultValue()))).thenReturn(Either.left(ciProp.getDefaultValue()));
189 when(toscaOperationFacade.updateComponentInstanceInputs(eq(component), eq(ciProp.getComponentInstanceId()), eq(ciPropList))).thenReturn(StorageOperationStatus.OK);
190 StorageOperationStatus storageOperationStatus = testInstance.unDeclarePropertiesAsListInputs(component, inputToDelete);
192 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
196 public void unDeclarePropertiesAsListInputsTest_whenNoListInput_returnOk() {
197 InputDefinition input = new InputDefinition();
198 input.setUniqueId(INPUT_ID);
199 input.setName(INPUT_ID);
200 input.setValue("value");
201 List<ComponentInstanceInput> resList = new ArrayList<>();
202 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(resource), eq(INPUT_ID))).thenReturn(resList);
203 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
204 Assert.assertEquals(status, StorageOperationStatus.OK);
207 private void verifyInputAnnotations(InputDefinition inputDefinition) {
208 List<Annotation> annotations = inputDefinition.getAnnotations();
209 assertThat(annotations)
210 .containsExactlyInAnyOrder(annotation1, annotation2);
213 private Component createComponentWithInputAndAnnotation(String inputName) {
214 InputDefinition input = InputsBuilder.create()
216 .addAnnotation(annotation1)
217 .addAnnotation(annotation2)
219 return new ResourceBuilder()
224 private Component createComponentWithListInput(String inputName, String propName) {
225 InputDefinition input = InputsBuilder.create()
229 input.setUniqueId(INPUT_ID);
230 input.setName(INPUT_ID);
231 input.setDefaultValue("defaultValue");
232 input.setValue(generateGetInputValueAsListInput(inputName, propName));
234 return new ResourceBuilder()
235 .setUniqueId(RESOURCE_ID)