Upgrade SDC from Titan to Janus Graph
[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 static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.when;
27
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Objects;
33 import java.util.Optional;
34 import org.apache.commons.collections.CollectionUtils;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
43 import org.openecomp.sdc.be.components.utils.InputsBuilder;
44 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
45 import org.openecomp.sdc.be.components.utils.ServiceBuilder;
46 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
48 import org.openecomp.sdc.be.model.Component;
49 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
50 import org.openecomp.sdc.be.model.InputDefinition;
51 import org.openecomp.sdc.be.model.PropertyDefinition;
52 import org.openecomp.sdc.be.model.Service;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
55 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class ComponentPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
59
60     @InjectMocks
61     private ComponentPropertyDeclarator testInstance;
62     @Mock
63     private PropertyBusinessLogic propertyBusinessLogic;
64     @Mock
65     private PropertyOperation propertyOperation;
66     @Mock
67     private ToscaOperationFacade toscaOperationFacade;
68
69     private static final String PROPERTY_UID = "propertyUid";
70     private static final String SERVICE_UID = "serviceUid";
71     private Service service;
72
73     @Before
74     public void init() {
75         service = new ServiceBuilder().setUniqueId(SERVICE_UID).build();
76     }
77
78     @Test
79     public void unDeclarePropertiesAsListInputsTest_whenPropertyUsedByOperation() {
80         InputDefinition input = new InputDefinition();
81         input.setUniqueId(INPUT_ID);
82         input.setName(INPUT_ID);
83         input.setValue("value");
84
85         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
86
87         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(true);
88         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
89         Assert.assertEquals(status, StorageOperationStatus.DECLARED_INPUT_USED_BY_OPERATION);
90     }
91
92     @Test
93     public void unDeclarePropertiesAsListInputsTest_whenNotPresentPropertyToUpdateCandidate() {
94         InputDefinition input = new InputDefinition();
95         input.setUniqueId(INPUT_ID);
96         input.setName(INPUT_ID);
97         input.setValue("value");
98
99         PropertyDefinition propertyDefinition = new PropertyDefinition();
100         resource.setProperties(Collections.singletonList(propertyDefinition));
101
102         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
103         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
104         Assert.assertEquals(status, StorageOperationStatus.OK);
105     }
106
107     @Test
108     public void unDeclarePropertiesAsListInputsTest_whenPropertiesEmpty() {
109         InputDefinition input = new InputDefinition();
110         input.setUniqueId(INPUT_ID);
111         input.setName(INPUT_ID);
112         input.setValue("value");
113
114         resource.setProperties(new ArrayList<>());
115
116         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
117         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
118         Assert.assertEquals(status, StorageOperationStatus.OK);
119     }
120
121     @Test
122     public void unDeclarePropertiesAsListInputsTest_whenPropertiesToUpdateIsEmpty() {
123         InputDefinition input = new InputDefinition();
124         input.setUniqueId(INPUT_ID);
125         input.setName(INPUT_ID);
126         input.setValue("value");
127
128         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
129         resource.setProperties(Collections.singletonList(propertyDefinition));
130
131         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(false);
132         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
133         Assert.assertEquals(status, StorageOperationStatus.OK);
134     }
135
136     @Test
137     public void unDeclarePropertiesAsListInputsTest_singleProperty() {
138         InputDefinition input = new InputDefinition();
139         input.setUniqueId(INPUT_ID);
140         input.setName(INPUT_ID);
141         input.setValue("value");
142         input.setDefaultValue("default value");
143
144         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
145         List<GetInputValueDataDefinition> getInputValueList = new ArrayList<>();
146         getInputValueList.add(buildGetInputValue(INPUT_ID));
147         getInputValueList.add(buildGetInputValue("otherInputId"));
148         propertyDefinition.setGetInputValues(getInputValueList);
149         propertyDefinition.setUniqueId("propertyId");
150         propertyDefinition.setDefaultValue("default value");
151         propertyDefinition.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
152         resource.setProperties(Collections.singletonList(propertyDefinition));
153
154         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any())).thenReturn(false);
155         when(propertyOperation.findDefaultValueFromSecondPosition(eq(Collections.emptyList()), eq(propertyDefinition.getUniqueId()), eq(propertyDefinition.getDefaultValue()))).thenReturn(Either.left(propertyDefinition.getDefaultValue()));
156         when(toscaOperationFacade.updatePropertyOfComponent(eq(resource), any())).thenReturn(Either.left(propertyDefinition));
157         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
158         Assert.assertEquals(status, StorageOperationStatus.OK);
159     }
160
161     @Test
162     public void unDeclarePropertiesAsListInputsTest_UnDeclareInputFail() {
163         InputDefinition input = new InputDefinition();
164         input.setUniqueId(INPUT_ID);
165         input.setName(INPUT_ID);
166         input.setValue("value");
167         input.setDefaultValue("default value");
168
169         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
170         List<GetInputValueDataDefinition> getInputValueList = new ArrayList<>();
171         getInputValueList.add(buildGetInputValue(INPUT_ID));
172         getInputValueList.add(buildGetInputValue("otherInputId"));
173         propertyDefinition.setGetInputValues(getInputValueList);
174         propertyDefinition.setUniqueId("propertyId");
175         propertyDefinition.setDefaultValue("default value");
176         propertyDefinition.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
177         resource.setProperties(Collections.singletonList(propertyDefinition));
178
179         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any())).thenReturn(false);
180         when(propertyOperation.findDefaultValueFromSecondPosition(eq(Collections.emptyList()), eq(propertyDefinition.getUniqueId()), eq(propertyDefinition.getDefaultValue()))).thenReturn(Either.left(propertyDefinition.getDefaultValue()));
181         when(toscaOperationFacade.updatePropertyOfComponent(eq(resource), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
182         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
183         Assert.assertEquals(status, StorageOperationStatus.NOT_FOUND);
184     }
185
186
187     @Test
188     public void createDeclaredProperty_success() {
189         PropertyDataDefinition propertyDataDefinition = getPropertyForDeclaration();
190         PropertyDataDefinition declaredProperty = testInstance.createDeclaredProperty(propertyDataDefinition);
191
192         assertTrue(Objects.nonNull(declaredProperty));
193         assertEquals(propertyDataDefinition.getUniqueId(), declaredProperty.getUniqueId());
194     }
195
196     @Test
197     public void updatePropertiesValues_success() {
198         PropertyDataDefinition propertyForDeclaration = getPropertyForDeclaration();
199         when(toscaOperationFacade.updatePropertyOfComponent(any(Component.class), any(PropertyDefinition.class)))
200                 .thenReturn(Either.left(new PropertyDefinition(propertyForDeclaration)));
201
202         Either<List<PropertyDataDefinition>, StorageOperationStatus> updateEither =
203                 (Either<List<PropertyDataDefinition>, StorageOperationStatus>) testInstance
204                                                                                        .updatePropertiesValues(service,
205                                                                                                SERVICE_UID, Collections
206                                                                                                                     .singletonList(
207                                                                                                                             propertyForDeclaration));
208
209         assertTrue(updateEither.isLeft());
210
211         List<PropertyDataDefinition> properties = updateEither.left().value();
212         assertTrue(CollectionUtils.isNotEmpty(properties));
213         assertEquals(1, properties.size());
214         assertEquals(propertyForDeclaration, properties.get(0));
215     }
216
217     @Test
218     public void resolvePropertiesOwner_success() {
219         Optional<Component> ownerCandidate = testInstance.resolvePropertiesOwner(service, SERVICE_UID);
220
221         assertTrue(ownerCandidate.isPresent());
222         assertEquals(service, ownerCandidate.get());
223     }
224
225     @Test
226     public void addPropertiesListToInput_success() {
227         InputDefinition input = InputsBuilder.create().setPropertyId(PROPERTY_UID).build();
228         PropertyDataDefinition propertyForDeclaration = getPropertyForDeclaration();
229
230         testInstance.addPropertiesListToInput(propertyForDeclaration, input);
231
232         List<ComponentInstanceProperty> inputProperties = input.getProperties();
233         assertTrue(CollectionUtils.isNotEmpty(inputProperties));
234         assertEquals(1, inputProperties.size());
235         assertEquals(propertyForDeclaration.getUniqueId(), inputProperties.get(0).getUniqueId());
236     }
237
238     private PropertyDataDefinition getPropertyForDeclaration() {
239         return new PropertyDataDefinitionBuilder().setUniqueId(PROPERTY_UID).build();
240     }
241
242     private GetInputValueDataDefinition buildGetInputValue(String InputId) {
243         GetInputValueDataDefinition getInputValue = new GetInputValueDataDefinition();
244         getInputValue.setInputId(InputId);
245         getInputValue.setInputName(InputId);
246
247         return getInputValue;
248     }
249 }