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=========================================================
21 package org.openecomp.sdc.be.components.property;
23 import fj.data.Either;
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
31 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
32 import org.openecomp.sdc.be.model.InputDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.List;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.when;
47 @RunWith(MockitoJUnitRunner.class)
48 public class ComponentPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
51 private ComponentPropertyDeclarator testInstance;
53 private PropertyBusinessLogic propertyBusinessLogic;
55 private PropertyOperation propertyOperation;
57 private ToscaOperationFacade toscaOperationFacade;
60 public void unDeclarePropertiesAsListInputsTest_whenPropertyUsedByOperation() {
61 InputDefinition input = new InputDefinition();
62 input.setUniqueId(INPUT_ID);
63 input.setName(INPUT_ID);
64 input.setValue("value");
66 PropertyDefinition propertyDefinition = new PropertyDefinition(input);
68 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(true);
69 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
70 Assert.assertEquals(status, StorageOperationStatus.DECLARED_INPUT_USED_BY_OPERATION);
74 public void unDeclarePropertiesAsListInputsTest_whenNotPresentPropertyToUpdateCandidate() {
75 InputDefinition input = new InputDefinition();
76 input.setUniqueId(INPUT_ID);
77 input.setName(INPUT_ID);
78 input.setValue("value");
80 PropertyDefinition propertyDefinition = new PropertyDefinition();
81 resource.setProperties(Collections.singletonList(propertyDefinition));
83 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
84 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
85 Assert.assertEquals(status, StorageOperationStatus.OK);
89 public void unDeclarePropertiesAsListInputsTest_whenPropertiesEmpty() {
90 InputDefinition input = new InputDefinition();
91 input.setUniqueId(INPUT_ID);
92 input.setName(INPUT_ID);
93 input.setValue("value");
95 resource.setProperties(new ArrayList<>());
97 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
98 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
99 Assert.assertEquals(status, StorageOperationStatus.OK);
103 public void unDeclarePropertiesAsListInputsTest_whenPropertiesToUpdateIsEmpty() {
104 InputDefinition input = new InputDefinition();
105 input.setUniqueId(INPUT_ID);
106 input.setName(INPUT_ID);
107 input.setValue("value");
109 PropertyDefinition propertyDefinition = new PropertyDefinition(input);
110 resource.setProperties(Collections.singletonList(propertyDefinition));
112 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(false);
113 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
114 Assert.assertEquals(status, StorageOperationStatus.OK);
118 public void unDeclarePropertiesAsListInputsTest_singleProperty() {
119 InputDefinition input = new InputDefinition();
120 input.setUniqueId(INPUT_ID);
121 input.setName(INPUT_ID);
122 input.setValue("value");
123 input.setDefaultValue("default value");
125 PropertyDefinition propertyDefinition = new PropertyDefinition(input);
126 List<GetInputValueDataDefinition> getInputValueList = new ArrayList<>();
127 getInputValueList.add(buildGetInputValue(INPUT_ID));
128 getInputValueList.add(buildGetInputValue("otherInputId"));
129 propertyDefinition.setGetInputValues(getInputValueList);
130 propertyDefinition.setUniqueId("propertyId");
131 propertyDefinition.setDefaultValue("default value");
132 propertyDefinition.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
133 resource.setProperties(Collections.singletonList(propertyDefinition));
135 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any())).thenReturn(false);
136 when(propertyOperation.findDefaultValueFromSecondPosition(eq(Collections.emptyList()), eq(propertyDefinition.getUniqueId()), eq(propertyDefinition.getDefaultValue()))).thenReturn(Either.left(propertyDefinition.getDefaultValue()));
137 when(toscaOperationFacade.updatePropertyOfComponent(eq(resource), any())).thenReturn(Either.left(propertyDefinition));
138 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
139 Assert.assertEquals(status, StorageOperationStatus.OK);
143 public void unDeclarePropertiesAsListInputsTest_UnDeclareInputFail() {
144 InputDefinition input = new InputDefinition();
145 input.setUniqueId(INPUT_ID);
146 input.setName(INPUT_ID);
147 input.setValue("value");
148 input.setDefaultValue("default value");
150 PropertyDefinition propertyDefinition = new PropertyDefinition(input);
151 List<GetInputValueDataDefinition> getInputValueList = new ArrayList<>();
152 getInputValueList.add(buildGetInputValue(INPUT_ID));
153 getInputValueList.add(buildGetInputValue("otherInputId"));
154 propertyDefinition.setGetInputValues(getInputValueList);
155 propertyDefinition.setUniqueId("propertyId");
156 propertyDefinition.setDefaultValue("default value");
157 propertyDefinition.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
158 resource.setProperties(Collections.singletonList(propertyDefinition));
160 when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any())).thenReturn(false);
161 when(propertyOperation.findDefaultValueFromSecondPosition(eq(Collections.emptyList()), eq(propertyDefinition.getUniqueId()), eq(propertyDefinition.getDefaultValue()))).thenReturn(Either.left(propertyDefinition.getDefaultValue()));
162 when(toscaOperationFacade.updatePropertyOfComponent(eq(resource), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
163 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
164 Assert.assertEquals(status, StorageOperationStatus.NOT_FOUND);
167 private GetInputValueDataDefinition buildGetInputValue(String InputId) {
168 GetInputValueDataDefinition getInputValue = new GetInputValueDataDefinition();
169 getInputValue.setInputId(InputId);
170 getInputValue.setInputName(InputId);
172 return getInputValue;