Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / ComponentInstanceInputPropertyDeclaratorTest.java
1 /*
2  * Copyright © 2016-2019 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.be.components.property;
18
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;
46
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Optional;
53
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;
61
62 @RunWith(MockitoJUnitRunner.class)
63 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
64
65
66     private ComponentInstanceInputPropertyDeclarator testInstance;
67
68     @Mock
69     private ToscaOperationFacade toscaOperationFacade;
70
71     @Mock
72     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
73
74     @Mock
75     private PropertyOperation propertyOperation;
76
77     @Captor
78     private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
79
80     private Annotation annotation1;
81
82     private Annotation annotation2;
83
84     @Override
85     @Before
86     public void setUp() throws Exception {
87         super.setUp();
88         testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
89                 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
90         annotation1 =  AnnotationBuilder.create()
91                 .setType("annotationType1")
92                 .setName("annotation1")
93                 .addProperty("prop1")
94                 .addProperty("prop2")
95                 .build();
96
97         annotation2 =  AnnotationBuilder.create()
98                 .setType("annotationType2")
99                 .setName("annotation2")
100                 .addProperty("prop3")
101                 .build();
102     }
103
104     @Test
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();
116     }
117
118     @Test
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));
124     }
125
126     @Test
127     public void testCreateDeclaredProperty() {
128         ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
129         assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
130     }
131
132     @Test
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);
137
138         when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
139                 .thenReturn(Either.left(expectedMap));
140
141         Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
142                 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
143
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);
148     }
149
150     @Test
151     public void testResolvePropertiesOwner() {
152         Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
153
154         assertThat(componentInstance.isPresent());
155         assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
156     }
157
158     @Test
159     public void unDeclarePropertiesAsListInputsTest() {
160         InputDefinition inputToDelete = new InputDefinition();
161         inputToDelete.setUniqueId(INPUT_ID);
162         inputToDelete.setName(INPUT_ID);
163         inputToDelete.setIsDeclaredListInput(true);
164
165         Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
166         PropertyDefinition prop = new PropertyDataDefinitionBuilder()
167                 .setName("propName")
168                 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
169                 .setType("list")
170                 .setUniqueId("propName")
171                 .addGetInputValue(INPUT_ID)
172                 .build();
173         component.setProperties(Collections.singletonList(prop));
174
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);
186
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);
191
192         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
193     }
194
195     @Test
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);
205     }
206
207     private void verifyInputAnnotations(InputDefinition inputDefinition) {
208         List<Annotation> annotations = inputDefinition.getAnnotations();
209         assertThat(annotations)
210                 .containsExactlyInAnyOrder(annotation1, annotation2);
211     }
212
213     private Component createComponentWithInputAndAnnotation(String inputName) {
214         InputDefinition input = InputsBuilder.create()
215                 .setName(inputName)
216                 .addAnnotation(annotation1)
217                 .addAnnotation(annotation2)
218                 .build();
219         return new ResourceBuilder()
220                 .addInput(input)
221                 .build();
222     }
223
224     private Component createComponentWithListInput(String inputName, String propName) {
225         InputDefinition input = InputsBuilder.create()
226                 .setName(inputName)
227                 .build();
228
229         input.setUniqueId(INPUT_ID);
230         input.setName(INPUT_ID);
231         input.setDefaultValue("defaultValue");
232         input.setValue(generateGetInputValueAsListInput(inputName, propName));
233
234         return new ResourceBuilder()
235                 .setUniqueId(RESOURCE_ID)
236                 .addInput(input)
237                 .build();
238     }
239 }