616e0babfaa643a72e69417245faa820e6dd139e
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / GroupPropertyDeclaratorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. 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.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
33 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
34 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
35 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
36 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
37 import org.openecomp.sdc.be.model.GroupDefinition;
38 import org.openecomp.sdc.be.model.InputDefinition;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.GroupOperation;
42 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
43
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.Optional;
48 import java.util.stream.Collectors;
49 import java.util.stream.Stream;
50
51 import static org.junit.jupiter.api.Assertions.assertEquals;
52 import static org.junit.jupiter.api.Assertions.assertFalse;
53 import static org.junit.jupiter.api.Assertions.assertTrue;
54 import static org.mockito.ArgumentMatchers.eq;
55 import static org.mockito.Mockito.verifyZeroInteractions;
56 import static org.mockito.Mockito.when;
57
58 @ExtendWith(MockitoExtension.class)
59 public class GroupPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
60
61
62     private static final String GROUP_ID = "groupId";
63     @InjectMocks
64     private GroupPropertyDeclarator groupPropertyDeclarator;
65     @Mock
66     private GroupOperation groupOperation;
67     @Mock
68     private PropertyOperation propertyOperation;
69     @Captor
70     private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
71     private Resource resource;
72     private InputDefinition input;
73
74     @Override
75     @BeforeEach
76     public void setUp() throws Exception {
77         super.setUp();
78         resource = createResourceWithGroup();
79         input = new InputDefinition();
80         input.setUniqueId(INPUT_ID);
81         input.setName(INPUT_ID);
82         input.setValue("value");
83     }
84
85     @Test
86     public void testDeclarePropertiesAsInputs_groupNotExist() {
87         Either<List<InputDefinition>, StorageOperationStatus> declareResult = groupPropertyDeclarator.declarePropertiesAsInputs(resource, "nonExistingGroup", Collections.emptyList());
88         assertEquals(StorageOperationStatus.NOT_FOUND, declareResult.right().value());
89         verifyZeroInteractions(groupOperation);
90     }
91
92     @Test
93     public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
94         when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
95         Either<List<InputDefinition>, StorageOperationStatus> declareResult = groupPropertyDeclarator.declarePropertiesAsInputs(resource, GROUP_ID, Collections.emptyList());
96         assertEquals(StorageOperationStatus.GENERAL_ERROR, declareResult.right().value());
97     }
98
99     @Test
100     public void testDeclarePropertiesAsInputs() {
101         List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
102         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
103         when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
104         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = groupPropertyDeclarator.declarePropertiesAsInputs(resource, GROUP_ID, propsToDeclare);
105         List<InputDefinition> inputs = createdInputs.left().value();
106         assertEquals(2, inputs.size());
107         verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
108         //creation of inputs values is part of the DefaultPropertyDeclarator and is tested in the ComponentInstancePropertyDeclaratorTest class
109     }
110
111     @Test
112     public void testUnDeclareProperties_whenComponentHasNoGroups_returnOk() {
113         Resource resource = new Resource();
114         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
115         assertEquals(StorageOperationStatus.OK, storageOperationStatus);
116         verifyZeroInteractions(groupOperation);
117     }
118
119     @Test
120     public void testUnDeclareProperties_whenNoPropertiesFromGroupMatchInputId_returnOk() {
121         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(createResourceWithGroup(), input);
122         assertEquals(StorageOperationStatus.OK, storageOperationStatus);
123         verifyZeroInteractions(groupOperation);
124     }
125
126     @Test
127     public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
128         Resource resource = createResourceWithGroups(GROUP_ID);
129         Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
130         assertTrue(groupDefinition.isPresent());
131         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
132         groupDefinition.get().setProperties(Collections.singletonList(getInputPropForInput));
133         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
134         when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
135         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
136         assertEquals(StorageOperationStatus.GENERAL_ERROR, storageOperationStatus);
137     }
138
139     @Test
140     public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
141         Resource resource = createResourceWithGroups(GROUP_ID, "groupId2");
142         Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
143         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
144         PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
145         groupDefinition.get().setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
146
147         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
148         when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
149         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
150
151         assertEquals(StorageOperationStatus.OK, storageOperationStatus);
152         List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
153         assertEquals(1, updatedProperties.size());
154         PropertyDataDefinition updatedProperty = updatedProperties.get(0);
155         assertFalse(updatedProperty.isGetInputProperty());
156         assertTrue(updatedProperty.getValue().isEmpty());
157         assertEquals(getInputPropForInput.getDefaultValue(), updatedProperty.getDefaultValue());
158         assertEquals(getInputPropForInput.getUniqueId(), updatedProperty.getUniqueId());
159     }
160
161     @Test
162     public void testUnDeclarePropertiesAsListInputs_whenComponentHasNoGroups_returnOk() {
163         Resource resource = new Resource();
164         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
165         assertEquals(StorageOperationStatus.OK, storageOperationStatus);
166         verifyZeroInteractions(groupOperation);
167     }
168
169     @Test
170     public void testUnDeclarePropertiesAsListInputs_whenNoPropertiesFromGroupMatchInputId_returnOk() {
171         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(createResourceWithGroup(), input);
172         assertEquals(StorageOperationStatus.OK, storageOperationStatus);
173         verifyZeroInteractions(groupOperation);
174     }
175
176     @Test
177     public void whenFailingToUpdateDeclaredPropertiesAsListInputs_returnErrorStatus() {
178         Resource resource = createResourceWithGroups(GROUP_ID);
179         Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
180         assertTrue(groupDefinition.isPresent());
181         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
182         groupDefinition.get().setProperties(Collections.singletonList(getInputPropForInput));
183         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
184         when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
185         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
186         assertEquals(StorageOperationStatus.GENERAL_ERROR, storageOperationStatus);
187     }
188
189     @Test
190     public void testUnDeclarePropertiesAsListInputs_propertiesUpdatedCorrectly() {
191         Resource resource = createResourceWithGroups(GROUP_ID, "groupId3");
192         Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
193         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
194         PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
195         groupDefinition.get().setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
196
197         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
198         when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
199         StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
200
201         assertEquals(StorageOperationStatus.OK, storageOperationStatus);
202         List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
203         assertEquals(1, updatedProperties.size());
204         PropertyDataDefinition updatedProperty = updatedProperties.get(0);
205         assertFalse(updatedProperty.isGetInputProperty());
206         assertTrue(updatedProperty.getValue().isEmpty());
207         assertEquals(getInputPropForInput.getDefaultValue(), updatedProperty.getDefaultValue());
208         assertEquals(getInputPropForInput.getUniqueId(), updatedProperty.getUniqueId());
209     }
210
211     private Resource createResourceWithGroup() {
212         return createResourceWithGroups(GROUP_ID);
213     }
214
215     private Resource createResourceWithGroups(String ... groups) {
216         List<GroupDefinition> groupsDef = Stream.of(groups)
217                 .map(this::buildGroup)
218                 .collect(Collectors.toList());
219
220         return new ResourceBuilder()
221                 .setUniqueId(RESOURCE_ID)
222                 .setGroups(groupsDef)
223                 .build();
224     }
225
226     private GroupDefinition buildGroup(String groupId) {
227         return GroupDefinitionBuilder.create()
228                 .setUniqueId(groupId)
229                 .setName(groupId)
230                 .build();
231     }
232
233     private PropertyDataDefinition buildGetInputProperty(String inputId) {
234         return new PropertyDataDefinitionBuilder()
235                 .addGetInputValue(inputId)
236                 .setUniqueId(GROUP_ID + "_" + inputId)
237                 .setDefaultValue("defaultValue")
238                 .setValue(generateGetInputValue(inputId))
239                 .build();
240     }
241
242 }