Change to enable SDC list type input
[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
21 package org.openecomp.sdc.be.components.property;
22
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;
37
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.List;
41
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.when;
45
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class ComponentPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
49
50     @InjectMocks
51     private ComponentPropertyDeclarator testInstance;
52     @Mock
53     private PropertyBusinessLogic propertyBusinessLogic;
54     @Mock
55     private PropertyOperation propertyOperation;
56     @Mock
57     private ToscaOperationFacade toscaOperationFacade;
58
59     @Test
60     public void unDeclarePropertiesAsListInputsTest_whenPropertyUsedByOperation() {
61         InputDefinition input = new InputDefinition();
62         input.setUniqueId(INPUT_ID);
63         input.setName(INPUT_ID);
64         input.setValue("value");
65
66         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
67
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);
71     }
72
73     @Test
74     public void unDeclarePropertiesAsListInputsTest_whenNotPresentPropertyToUpdateCandidate() {
75         InputDefinition input = new InputDefinition();
76         input.setUniqueId(INPUT_ID);
77         input.setName(INPUT_ID);
78         input.setValue("value");
79
80         PropertyDefinition propertyDefinition = new PropertyDefinition();
81         resource.setProperties(Collections.singletonList(propertyDefinition));
82
83         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
84         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
85         Assert.assertEquals(status, StorageOperationStatus.OK);
86     }
87
88     @Test
89     public void unDeclarePropertiesAsListInputsTest_whenPropertiesEmpty() {
90         InputDefinition input = new InputDefinition();
91         input.setUniqueId(INPUT_ID);
92         input.setName(INPUT_ID);
93         input.setValue("value");
94
95         resource.setProperties(new ArrayList<>());
96
97         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), any(PropertyDefinition.class))).thenReturn(false);
98         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
99         Assert.assertEquals(status, StorageOperationStatus.OK);
100     }
101
102     @Test
103     public void unDeclarePropertiesAsListInputsTest_whenPropertiesToUpdateIsEmpty() {
104         InputDefinition input = new InputDefinition();
105         input.setUniqueId(INPUT_ID);
106         input.setName(INPUT_ID);
107         input.setValue("value");
108
109         PropertyDefinition propertyDefinition = new PropertyDefinition(input);
110         resource.setProperties(Collections.singletonList(propertyDefinition));
111
112         when(propertyBusinessLogic.isPropertyUsedByOperation(eq(resource), eq(propertyDefinition))).thenReturn(false);
113         StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
114         Assert.assertEquals(status, StorageOperationStatus.OK);
115     }
116
117     @Test
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");
124
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));
134
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);
140     }
141
142     @Test
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");
149
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));
159
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);
165     }
166
167     private GetInputValueDataDefinition buildGetInputValue(String InputId) {
168         GetInputValueDataDefinition getInputValue = new GetInputValueDataDefinition();
169         getInputValue.setInputId(InputId);
170         getInputValue.setInputName(InputId);
171
172         return getInputValue;
173     }
174
175 }