Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / ComponentPropertyDeclaratorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Fujitsu Limited. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.property;
21
22 import fj.data.Either;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
32 import org.openecomp.sdc.be.components.utils.InputsBuilder;
33 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
34 import org.openecomp.sdc.be.components.utils.ServiceBuilder;
35 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
39 import org.openecomp.sdc.be.model.InputDefinition;
40 import org.openecomp.sdc.be.model.PropertyDefinition;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
43 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
44 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
45
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.List;
49 import java.util.Objects;
50 import java.util.Optional;
51
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertTrue;
54 import static org.mockito.ArgumentMatchers.any;
55 import static org.mockito.ArgumentMatchers.eq;
56 import static org.mockito.Mockito.when;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class ComponentPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
60
61     @InjectMocks
62     private ComponentPropertyDeclarator testInstance;
63     @Mock
64     private PropertyBusinessLogic propertyBusinessLogic;
65     @Mock
66     private PropertyOperation propertyOperation;
67     @Mock
68     private ToscaOperationFacade toscaOperationFacade;
69
70     private static final String PROPERTY_UID = "propertyUid";
71     private static final String SERVICE_UID = "serviceUid";
72     private Service service;
73
74     @Before
75     public void init() {
76         service = new ServiceBuilder().setUniqueId(SERVICE_UID).build();
77     }
78
79     @Test
80     public void unDeclarePropertiesAsListInputsTest_whenPropertyUsedByOperation() {
81         InputDefinition input = new InputDefinition();
82         input.setUniqueId(INPUT_ID);
83         input.setName(INPUT_ID);
84         input.setValue("value");
85
86         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
87
88         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(true);
89         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
90         Assert.assertEquals(status, StorageOperationStatus.DECLARED_INPUT_USED_BY_OPERATION);
91     }
92
93     @Test
94     public void unDeclarePropertiesAsListInputsTest_whenNotPresentPropertyToUpdateCandidate() {
95         InputDefinition input = new InputDefinition();
96         input.setUniqueId(INPUT_ID);
97         input.setName(INPUT_ID);
98         input.setValue("value");
99
100         PropertyDefinition propertyDefinition = new PropertyDefinition();
101         resource.setProperties(Collections.singletonList(propertyDefinition));
102
103         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
104         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
105         Assert.assertEquals(status, StorageOperationStatus.OK);
106     }
107
108     @Test
109     public void unDeclarePropertiesAsListInputsTest_whenPropertiesEmpty() {
110         InputDefinition input = new InputDefinition();
111         input.setUniqueId(INPUT_ID);
112         input.setName(INPUT_ID);
113         input.setValue("value");
114
115         resource.setProperties(new ArrayList<>());
116
117         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
118         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
119         Assert.assertEquals(status, StorageOperationStatus.OK);
120     }
121
122     @Test
123     public void unDeclarePropertiesAsListInputsTest_whenPropertiesToUpdateIsEmpty() {
124         InputDefinition input = new InputDefinition();
125         input.setUniqueId(INPUT_ID);
126         input.setName(INPUT_ID);
127         input.setValue("value");
128
129         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
130         resource.setProperties(Collections.singletonList(propertyDefinition));
131
132         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(false);
133         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
134         Assert.assertEquals(status, StorageOperationStatus.OK);
135     }
136
137     @Test
138     public void unDeclarePropertiesAsListInputsTest_singleProperty() {
139         InputDefinition input = new InputDefinition();
140         input.setUniqueId(INPUT_ID);
141         input.setName(INPUT_ID);
142         input.setValue("value");
143         input.setDefaultValue("default value");
144
145         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
146         List<GetInputValueDataDefinition> getInputValueList = new ArrayList<>();
147         getInputValueList.add(buildGetInputValue(INPUT_ID));
148         getInputValueList.add(buildGetInputValue("otherInputId"));
149         propertyDefinition.setGetInputValues(getInputValueList);
150         propertyDefinition.setUniqueId("propertyId");
151         propertyDefinition.setDefaultValue("default value");
152         propertyDefinition.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
153         resource.setProperties(Collections.singletonList(propertyDefinition));
154
155         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any())).thenReturn(false);
156         when(propertyOperation.findDefaultValueFromSecondPosition(eq(Collections.emptyList()), eq(propertyDefinition.getUniqueId()), eq(propertyDefinition.getDefaultValue()))).thenReturn(Either.left(propertyDefinition.getDefaultValue()));
157         when(toscaOperationFacade.updatePropertyOfComponent(eq(resource), any())).thenReturn(Either.left(propertyDefinition));
158         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
159         Assert.assertEquals(status, StorageOperationStatus.OK);
160     }
161
162     @Test
163     public void unDeclarePropertiesAsListInputsTest_UnDeclareInputFail() {
164         InputDefinition input = new InputDefinition();
165         input.setUniqueId(INPUT_ID);
166         input.setName(INPUT_ID);
167         input.setValue("value");
168         input.setDefaultValue("default value");
169
170         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
171         List<GetInputValueDataDefinition> getInputValueList = new ArrayList<>();
172         getInputValueList.add(buildGetInputValue(INPUT_ID));
173         getInputValueList.add(buildGetInputValue("otherInputId"));
174         propertyDefinition.setGetInputValues(getInputValueList);
175         propertyDefinition.setUniqueId("propertyId");
176         propertyDefinition.setDefaultValue("default value");
177         propertyDefinition.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
178         resource.setProperties(Collections.singletonList(propertyDefinition));
179
180         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any())).thenReturn(false);
181         when(propertyOperation.findDefaultValueFromSecondPosition(eq(Collections.emptyList()), eq(propertyDefinition.getUniqueId()), eq(propertyDefinition.getDefaultValue()))).thenReturn(Either.left(propertyDefinition.getDefaultValue()));
182         when(toscaOperationFacade.updatePropertyOfComponent(eq(resource), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
183         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
184         Assert.assertEquals(status, StorageOperationStatus.NOT_FOUND);
185     }
186
187
188     @Test
189     public void createDeclaredProperty_success() {
190         PropertyDataDefinition propertyDataDefinition = getPropertyForDeclaration();
191         PropertyDataDefinition declaredProperty = testInstance.createDeclaredProperty(propertyDataDefinition);
192
193         assertTrue(Objects.nonNull(declaredProperty));
194         assertEquals(propertyDataDefinition.getUniqueId(), declaredProperty.getUniqueId());
195     }
196
197     @Test
198     public void updatePropertiesValues_success() {
199         PropertyDataDefinition propertyForDeclaration = getPropertyForDeclaration();
200         when(toscaOperationFacade.updatePropertyOfComponent(any(Component.class), any(PropertyDefinition.class)))
201                 .thenReturn(Either.left(new PropertyDefinition(propertyForDeclaration)));
202
203         Either<List<PropertyDataDefinition>, StorageOperationStatus> updateEither =
204                 (Either<List<PropertyDataDefinition>, StorageOperationStatus>) testInstance
205                                                                                        .updatePropertiesValues(service,
206                                                                                                SERVICE_UID, Collections
207                                                                                                                     .singletonList(
208                                                                                                                             propertyForDeclaration));
209
210         assertTrue(updateEither.isLeft());
211
212         List<PropertyDataDefinition> properties = updateEither.left().value();
213         assertTrue(CollectionUtils.isNotEmpty(properties));
214         assertEquals(1, properties.size());
215         assertEquals(propertyForDeclaration, properties.get(0));
216     }
217
218     @Test
219     public void resolvePropertiesOwner_success() {
220         Optional<Component> ownerCandidate = testInstance.resolvePropertiesOwner(service, SERVICE_UID);
221
222         assertTrue(ownerCandidate.isPresent());
223         assertEquals(service, ownerCandidate.get());
224     }
225
226     @Test
227     public void addPropertiesListToInput_success() {
228         InputDefinition input = InputsBuilder.create().setPropertyId(PROPERTY_UID).build();
229         PropertyDataDefinition propertyForDeclaration = getPropertyForDeclaration();
230
231         testInstance.addPropertiesListToInput(propertyForDeclaration, input);
232
233         List<ComponentInstanceProperty> inputProperties = input.getProperties();
234         assertTrue(CollectionUtils.isNotEmpty(inputProperties));
235         assertEquals(1, inputProperties.size());
236         assertEquals(propertyForDeclaration.getUniqueId(), inputProperties.get(0).getUniqueId());
237     }
238
239     private PropertyDataDefinition getPropertyForDeclaration() {
240         return new PropertyDataDefinitionBuilder().setUniqueId(PROPERTY_UID).build();
241     }
242
243     private GetInputValueDataDefinition buildGetInputValue(String InputId) {
244         GetInputValueDataDefinition getInputValue = new GetInputValueDataDefinition();
245         getInputValue.setInputId(InputId);
246         getInputValue.setInputName(InputId);
247
248         return getInputValue;
249     }
250 }