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 static org.assertj.core.api.Assertions.assertThat;
20 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21 import static org.mockito.ArgumentMatchers.anyMap;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.when;
24 import static org.openecomp.sdc.be.MockGenerator.mockComponentUtils;
25 import static org.openecomp.sdc.be.MockGenerator.mockExceptionUtils;
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Optional;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Captor;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
43 import org.openecomp.sdc.be.components.utils.AnnotationBuilder;
44 import org.openecomp.sdc.be.components.utils.InputsBuilder;
45 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
46 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
47 import org.openecomp.sdc.be.datatypes.elements.Annotation;
48 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
49 import org.openecomp.sdc.be.model.Component;
50 import org.openecomp.sdc.be.model.ComponentInstance;
51 import org.openecomp.sdc.be.model.ComponentInstanceInput;
52 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
53 import org.openecomp.sdc.be.model.ComponentParametersView;
54 import org.openecomp.sdc.be.model.InputDefinition;
55 import org.openecomp.sdc.be.model.PropertyDefinition;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
57 import org.openecomp.sdc.be.model.operations.StorageException;
58 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
59 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
61 @RunWith(MockitoJUnitRunner.class)
62 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
65 private ComponentInstanceInputPropertyDeclarator testInstance;
68 private ToscaOperationFacade toscaOperationFacade;
71 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
74 private PropertyOperation propertyOperation;
77 private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
79 private Annotation annotation1;
81 private Annotation annotation2;
85 public void setUp() throws Exception {
87 testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
88 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
89 annotation1 = AnnotationBuilder.create()
90 .setType("annotationType1")
91 .setName("annotation1")
96 annotation2 = AnnotationBuilder.create()
97 .setType("annotationType2")
98 .setName("annotation2")
104 public void whenDeclaredPropertyOriginalInputContainsAnnotation_createNewInputWithSameAnnotations() {
105 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
106 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
107 Component originInstanceNodeType = createComponentWithInputAndAnnotation(prop1.getName());
108 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap())).thenReturn(Either.left(Collections.emptyMap()));
109 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.left(originInstanceNodeType));
110 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
111 List<InputDefinition> inputs = createdInputs.left().value();
112 assertThat(inputs).hasSize(1);
113 verifyInputAnnotations(inputs.get(0));
114 assertThat(inputsFilterCaptor.getValue().isIgnoreInputs()).isFalse();
118 public void throwExceptionWhenFailingToGetInstanceOriginType() {
119 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
120 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
121 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
122 assertThatExceptionOfType(StorageException.class).isThrownBy(() -> testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare));
126 public void testCreateDeclaredProperty() {
127 ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
128 assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
132 public void testUpdateDeclaredProperties() {
133 List<ComponentInstanceInput> expectedProperties = Collections.singletonList(new ComponentInstanceInput(prop1));
134 Map<String, List<ComponentInstanceInput>> expectedMap = new HashMap<>();
135 expectedMap.put(prop1.getName(), expectedProperties);
137 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
138 .thenReturn(Either.left(expectedMap));
140 Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
141 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
143 assertThat(updateEither.isLeft());
144 Map<String, List<ComponentInstanceInput>> actualProperties = updateEither.left().value();
145 assertThat(actualProperties.values().size()).isEqualTo(expectedProperties.size());
146 assertThat(actualProperties.values().iterator().next()).isEqualTo(expectedProperties);
150 public void testResolvePropertiesOwner() {
151 Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
153 assertThat(componentInstance.isPresent());
154 assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
158 public void unDeclarePropertiesAsListInputsTest() {
159 InputDefinition inputToDelete = new InputDefinition();
160 inputToDelete.setUniqueId(INPUT_ID);
161 inputToDelete.setName(INPUT_ID);
162 inputToDelete.setIsDeclaredListInput(true);
164 Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
165 PropertyDefinition prop = new PropertyDataDefinitionBuilder()
167 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
169 .setUniqueId("propName")
170 .addGetInputValue(INPUT_ID)
172 component.setProperties(Collections.singletonList(prop));
174 List<ComponentInstanceInput> ciPropList = new ArrayList<>();
175 ComponentInstanceInput ciProp = new ComponentInstanceInput();
176 List<String> pathOfComponentInstances = new ArrayList<>();
177 pathOfComponentInstances.add("pathOfComponentInstances");
178 ciProp.setPath(pathOfComponentInstances);
179 ciProp.setUniqueId("componentInstanceId");
180 ciProp.setDefaultValue("default value");
181 ciProp.setComponentInstanceId("componentInstanceId");
182 ciProp.setComponentInstanceName("componentInstanceName");
183 ciProp.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
184 ciPropList.add(ciProp);
186 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(component), eq(INPUT_ID))).thenReturn(ciPropList);
187 when(propertyOperation.findDefaultValueFromSecondPosition(eq(pathOfComponentInstances), eq(ciProp.getUniqueId()), eq(ciProp.getDefaultValue()))).thenReturn(Either.left(ciProp.getDefaultValue()));
188 when(toscaOperationFacade.updateComponentInstanceInputs(eq(component), eq(ciProp.getComponentInstanceId()), eq(ciPropList))).thenReturn(StorageOperationStatus.OK);
189 StorageOperationStatus storageOperationStatus = testInstance.unDeclarePropertiesAsListInputs(component, inputToDelete);
191 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
195 public void unDeclarePropertiesAsListInputsTest_whenNoListInput_returnOk() {
196 InputDefinition input = new InputDefinition();
197 input.setUniqueId(INPUT_ID);
198 input.setName(INPUT_ID);
199 input.setValue("value");
200 List<ComponentInstanceInput> resList = new ArrayList<>();
201 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(resource), eq(INPUT_ID))).thenReturn(resList);
202 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
203 Assert.assertEquals(status, StorageOperationStatus.OK);
206 private void verifyInputAnnotations(InputDefinition inputDefinition) {
207 List<Annotation> annotations = inputDefinition.getAnnotations();
208 assertThat(annotations)
209 .containsExactlyInAnyOrder(annotation1, annotation2);
212 private Component createComponentWithInputAndAnnotation(String inputName) {
213 InputDefinition input = InputsBuilder.create()
215 .addAnnotation(annotation1)
216 .addAnnotation(annotation2)
218 return new ResourceBuilder()
223 private Component createComponentWithListInput(String inputName, String propName) {
224 InputDefinition input = InputsBuilder.create()
228 input.setUniqueId(INPUT_ID);
229 input.setName(INPUT_ID);
230 input.setDefaultValue("defaultValue");
231 input.setValue(generateGetInputValueAsListInput(inputName, propName));
233 return new ResourceBuilder()
234 .setUniqueId(RESOURCE_ID)