258cad5ae9008d107eb4d7f4896c0fac677dcf01
[sdc.git] /
1 /*
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
6  *
7  *        http://www.apache.org/licenses/LICENSE-2.0
8  *
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===============================================================================================================
12  *
13  */
14 package org.openecomp.sdc.be.components.property;
15
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;
23
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;
29 import java.util.Map;
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;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
60
61
62     private ComponentInstanceInputPropertyDeclarator testInstance;
63
64     @Mock
65     private ToscaOperationFacade toscaOperationFacade;
66
67     @Mock
68     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
69
70     @Mock
71     private PropertyOperation propertyOperation;
72
73     @Captor
74     private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
75
76     private Annotation annotation1;
77
78     private Annotation annotation2;
79
80     @Override
81     @Before
82     public void setUp() throws Exception {
83         super.setUp();
84         testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
85                 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
86         annotation1 =  AnnotationBuilder.create()
87                 .setType("annotationType1")
88                 .setName("annotation1")
89                 .addProperty("prop1")
90                 .addProperty("prop2")
91                 .build();
92
93         annotation2 =  AnnotationBuilder.create()
94                 .setType("annotationType2")
95                 .setName("annotation2")
96                 .addProperty("prop3")
97                 .build();
98     }
99
100     @Test
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();
112     }
113
114     @Test
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));
120     }
121
122     @Test
123     public void testCreateDeclaredProperty() {
124         ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
125         assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
126     }
127
128     @Test
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);
133
134         when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
135                 .thenReturn(Either.left(expectedMap));
136
137         Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
138                 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
139
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);
144     }
145
146     @Test
147     public void testResolvePropertiesOwner() {
148         Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
149
150         assertThat(componentInstance.isPresent());
151         assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
152     }
153
154     @Test
155     public void unDeclarePropertiesAsListInputsTest() {
156         InputDefinition inputToDelete = new InputDefinition();
157         inputToDelete.setUniqueId(INPUT_ID);
158         inputToDelete.setName(INPUT_ID);
159         inputToDelete.setIsDeclaredListInput(true);
160
161         Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
162         PropertyDefinition prop = new PropertyDataDefinitionBuilder()
163                 .setName("propName")
164                 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
165                 .setType("list")
166                 .setUniqueId("propName")
167                 .addGetInputValue(INPUT_ID)
168                 .build();
169         component.setProperties(Collections.singletonList(prop));
170
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);
182
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);
187
188         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
189     }
190
191     @Test
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);
201     }
202
203     private void verifyInputAnnotations(InputDefinition inputDefinition) {
204         List<Annotation> annotations = inputDefinition.getAnnotations();
205         assertThat(annotations)
206                 .containsExactlyInAnyOrder(annotation1, annotation2);
207     }
208
209     private Component createComponentWithInputAndAnnotation(String inputName) {
210         InputDefinition input = InputsBuilder.create()
211                 .setName(inputName)
212                 .addAnnotation(annotation1)
213                 .addAnnotation(annotation2)
214                 .build();
215         return new ResourceBuilder()
216                 .addInput(input)
217                 .build();
218     }
219
220     private Component createComponentWithListInput(String inputName, String propName) {
221         InputDefinition input = InputsBuilder.create()
222                 .setName(inputName)
223                 .build();
224
225         input.setUniqueId(INPUT_ID);
226         input.setName(INPUT_ID);
227         input.setDefaultValue("defaultValue");
228         input.setValue(generateGetInputValueAsListInput(inputName, propName));
229
230         return new ResourceBuilder()
231                 .setUniqueId(RESOURCE_ID)
232                 .addInput(input)
233                 .build();
234     }
235 }