2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.components.property;
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;
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;
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;
58 @RunWith(MockitoJUnitRunner.class)
59 public class ComponentPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
62 private ComponentPropertyDeclarator testInstance;
64 private PropertyBusinessLogic propertyBusinessLogic;
66 private PropertyOperation propertyOperation;
68 private ToscaOperationFacade toscaOperationFacade;
70 private static final String PROPERTY_UID = "propertyUid";
71 private static final String SERVICE_UID = "serviceUid";
72 private Service service;
76 service = new ServiceBuilder().setUniqueId(SERVICE_UID).build();
80 public void unDeclarePropertiesAsListInputsTest_whenPropertyUsedByOperation() {
81 InputDefinition input = new InputDefinition();
82 input.setUniqueId(INPUT_ID);
83 input.setName(INPUT_ID);
84 input.setValue("value");
86 PropertyDefinition propertyDefinition = new PropertyDefinition(input);
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);
94 public void unDeclarePropertiesAsListInputsTest_whenNotPresentPropertyToUpdateCandidate() {
95 InputDefinition input = new InputDefinition();
96 input.setUniqueId(INPUT_ID);
97 input.setName(INPUT_ID);
98 input.setValue("value");
100 PropertyDefinition propertyDefinition = new PropertyDefinition();
101 resource.setProperties(Collections.singletonList(propertyDefinition));
103 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
104 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
105 Assert.assertEquals(status, StorageOperationStatus.OK);
109 public void unDeclarePropertiesAsListInputsTest_whenPropertiesEmpty() {
110 InputDefinition input = new InputDefinition();
111 input.setUniqueId(INPUT_ID);
112 input.setName(INPUT_ID);
113 input.setValue("value");
115 resource.setProperties(new ArrayList<>());
117 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
118 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
119 Assert.assertEquals(status, StorageOperationStatus.OK);
123 public void unDeclarePropertiesAsListInputsTest_whenPropertiesToUpdateIsEmpty() {
124 InputDefinition input = new InputDefinition();
125 input.setUniqueId(INPUT_ID);
126 input.setName(INPUT_ID);
127 input.setValue("value");
129 PropertyDefinition propertyDefinition = new PropertyDefinition(input);
130 resource.setProperties(Collections.singletonList(propertyDefinition));
132 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(false);
133 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
134 Assert.assertEquals(status, StorageOperationStatus.OK);
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");
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));
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);
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");
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));
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);
189 public void createDeclaredProperty_success() {
190 PropertyDataDefinition propertyDataDefinition = getPropertyForDeclaration();
191 PropertyDataDefinition declaredProperty = testInstance.createDeclaredProperty(propertyDataDefinition);
193 assertTrue(Objects.nonNull(declaredProperty));
194 assertEquals(propertyDataDefinition.getUniqueId(), declaredProperty.getUniqueId());
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)));
203 Either<List<PropertyDataDefinition>, StorageOperationStatus> updateEither =
204 (Either<List<PropertyDataDefinition>, StorageOperationStatus>) testInstance
205 .updatePropertiesValues(service,
206 SERVICE_UID, Collections
208 propertyForDeclaration));
210 assertTrue(updateEither.isLeft());
212 List<PropertyDataDefinition> properties = updateEither.left().value();
213 assertTrue(CollectionUtils.isNotEmpty(properties));
214 assertEquals(1, properties.size());
215 assertEquals(propertyForDeclaration, properties.get(0));
219 public void resolvePropertiesOwner_success() {
220 Optional<Component> ownerCandidate = testInstance.resolvePropertiesOwner(service, SERVICE_UID);
222 assertTrue(ownerCandidate.isPresent());
223 assertEquals(service, ownerCandidate.get());
227 public void addPropertiesListToInput_success() {
228 InputDefinition input = InputsBuilder.create().setPropertyId(PROPERTY_UID).build();
229 PropertyDataDefinition propertyForDeclaration = getPropertyForDeclaration();
231 testInstance.addPropertiesListToInput(propertyForDeclaration, input);
233 List<ComponentInstanceProperty> inputProperties = input.getProperties();
234 assertTrue(CollectionUtils.isNotEmpty(inputProperties));
235 assertEquals(1, inputProperties.size());
236 assertEquals(propertyForDeclaration.getUniqueId(), inputProperties.get(0).getUniqueId());
239 private PropertyDataDefinition getPropertyForDeclaration() {
240 return new PropertyDataDefinitionBuilder().setUniqueId(PROPERTY_UID).build();
243 private GetInputValueDataDefinition buildGetInputValue(String InputId) {
244 GetInputValueDataDefinition getInputValue = new GetInputValueDataDefinition();
245 getInputValue.setInputId(InputId);
246 getInputValue.setInputName(InputId);
248 return getInputValue;