Added oparent to sdc main
[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 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;
26
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;
32 import java.util.Map;
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;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
63
64
65     private ComponentInstanceInputPropertyDeclarator testInstance;
66
67     @Mock
68     private ToscaOperationFacade toscaOperationFacade;
69
70     @Mock
71     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
72
73     @Mock
74     private PropertyOperation propertyOperation;
75
76     @Captor
77     private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
78
79     private Annotation annotation1;
80
81     private Annotation annotation2;
82
83     @Override
84     @Before
85     public void setUp() throws Exception {
86         super.setUp();
87         testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
88                 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
89         annotation1 =  AnnotationBuilder.create()
90                 .setType("annotationType1")
91                 .setName("annotation1")
92                 .addProperty("prop1")
93                 .addProperty("prop2")
94                 .build();
95
96         annotation2 =  AnnotationBuilder.create()
97                 .setType("annotationType2")
98                 .setName("annotation2")
99                 .addProperty("prop3")
100                 .build();
101     }
102
103     @Test
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();
115     }
116
117     @Test
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));
123     }
124
125     @Test
126     public void testCreateDeclaredProperty() {
127         ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
128         assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
129     }
130
131     @Test
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);
136
137         when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
138                 .thenReturn(Either.left(expectedMap));
139
140         Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
141                 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
142
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);
147     }
148
149     @Test
150     public void testResolvePropertiesOwner() {
151         Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
152
153         assertThat(componentInstance.isPresent());
154         assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
155     }
156
157     @Test
158     public void unDeclarePropertiesAsListInputsTest() {
159         InputDefinition inputToDelete = new InputDefinition();
160         inputToDelete.setUniqueId(INPUT_ID);
161         inputToDelete.setName(INPUT_ID);
162         inputToDelete.setIsDeclaredListInput(true);
163
164         Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
165         PropertyDefinition prop = new PropertyDataDefinitionBuilder()
166                 .setName("propName")
167                 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
168                 .setType("list")
169                 .setUniqueId("propName")
170                 .addGetInputValue(INPUT_ID)
171                 .build();
172         component.setProperties(Collections.singletonList(prop));
173
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);
185
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);
190
191         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
192     }
193
194     @Test
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);
204     }
205
206     private void verifyInputAnnotations(InputDefinition inputDefinition) {
207         List<Annotation> annotations = inputDefinition.getAnnotations();
208         assertThat(annotations)
209                 .containsExactlyInAnyOrder(annotation1, annotation2);
210     }
211
212     private Component createComponentWithInputAndAnnotation(String inputName) {
213         InputDefinition input = InputsBuilder.create()
214                 .setName(inputName)
215                 .addAnnotation(annotation1)
216                 .addAnnotation(annotation2)
217                 .build();
218         return new ResourceBuilder()
219                 .addInput(input)
220                 .build();
221     }
222
223     private Component createComponentWithListInput(String inputName, String propName) {
224         InputDefinition input = InputsBuilder.create()
225                 .setName(inputName)
226                 .build();
227
228         input.setUniqueId(INPUT_ID);
229         input.setName(INPUT_ID);
230         input.setDefaultValue("defaultValue");
231         input.setValue(generateGetInputValueAsListInput(inputName, propName));
232
233         return new ResourceBuilder()
234                 .setUniqueId(RESOURCE_ID)
235                 .addInput(input)
236                 .build();
237     }
238 }