2 * ============LICENSE_START=======================================================
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
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.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
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.MockitoJUnitRunner;
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;
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;
51 import static org.assertj.core.api.Java6Assertions.assertThat;
52 import static org.mockito.ArgumentMatchers.eq;
53 import static org.mockito.Mockito.verifyZeroInteractions;
54 import static org.mockito.Mockito.when;
56 @RunWith(MockitoJUnitRunner.class)
57 public class GroupPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
60 private static final String GROUP_ID = "groupId";
62 private GroupPropertyDeclarator groupPropertyDeclarator;
64 private GroupOperation groupOperation;
66 private PropertyOperation propertyOperation;
68 private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
69 private Resource resource;
70 private InputDefinition input;
74 public void setUp() throws Exception {
76 resource = createResourceWithGroup();
77 input = new InputDefinition();
78 input.setUniqueId(INPUT_ID);
79 input.setName(INPUT_ID);
80 input.setValue("value");
84 public void testDeclarePropertiesAsInputs_groupNotExist() {
85 Either<List<InputDefinition>, StorageOperationStatus> declareResult = groupPropertyDeclarator.declarePropertiesAsInputs(resource, "nonExistingGroup", Collections.emptyList());
86 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
87 verifyZeroInteractions(groupOperation);
91 public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
92 when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
93 Either<List<InputDefinition>, StorageOperationStatus> declareResult = groupPropertyDeclarator.declarePropertiesAsInputs(resource, GROUP_ID, Collections.emptyList());
94 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
98 public void testDeclarePropertiesAsInputs() {
99 List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
100 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
101 when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
102 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = groupPropertyDeclarator.declarePropertiesAsInputs(resource, GROUP_ID, propsToDeclare);
103 List<InputDefinition> inputs = createdInputs.left().value();
104 assertThat(inputs).hasSize(2);
105 verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
106 //creation of inputs values is part of the DefaultPropertyDeclarator and is tested in the ComponentInstancePropertyDeclaratorTest class
110 public void testUnDeclareProperties_whenComponentHasNoGroups_returnOk() {
111 Resource resource = new Resource();
112 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
113 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
114 verifyZeroInteractions(groupOperation);
118 public void testUnDeclareProperties_whenNoPropertiesFromGroupMatchInputId_returnOk() {
119 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(createResourceWithGroup(), input);
120 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
121 verifyZeroInteractions(groupOperation);
125 public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
126 Resource resource = createResourceWithGroups(GROUP_ID);
127 Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
128 assertThat(groupDefinition.isPresent()).isTrue();
129 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
130 groupDefinition.get().setProperties(Collections.singletonList(getInputPropForInput));
131 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
132 when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
133 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
134 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
138 public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
139 Resource resource = createResourceWithGroups(GROUP_ID, "groupId2");
140 Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
141 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
142 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
143 groupDefinition.get().setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
145 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
146 when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
147 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
149 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
150 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
151 assertThat(updatedProperties).hasSize(1);
152 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
153 assertThat(updatedProperty.isGetInputProperty()).isFalse();
154 assertThat(updatedProperty.getValue()).isEmpty();
155 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
156 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
160 public void testUnDeclarePropertiesAsListInputs_whenComponentHasNoGroups_returnOk() {
161 Resource resource = new Resource();
162 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
163 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
164 verifyZeroInteractions(groupOperation);
168 public void testUnDeclarePropertiesAsListInputs_whenNoPropertiesFromGroupMatchInputId_returnOk() {
169 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(createResourceWithGroup(), input);
170 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
171 verifyZeroInteractions(groupOperation);
175 public void whenFailingToUpdateDeclaredPropertiesAsListInputs_returnErrorStatus() {
176 Resource resource = createResourceWithGroups(GROUP_ID);
177 Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
178 assertThat(groupDefinition.isPresent()).isTrue();
179 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
180 groupDefinition.get().setProperties(Collections.singletonList(getInputPropForInput));
181 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
182 when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
183 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
184 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
188 public void testUnDeclarePropertiesAsListInputs_propertiesUpdatedCorrectly() {
189 Resource resource = createResourceWithGroups(GROUP_ID, "groupId3");
190 Optional<GroupDefinition> groupDefinition = resource.getGroupById(GROUP_ID);
191 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
192 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
193 groupDefinition.get().setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
195 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
196 when(groupOperation.updateGroupProperties(eq(resource), eq(GROUP_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
197 StorageOperationStatus storageOperationStatus = groupPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
199 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
200 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
201 assertThat(updatedProperties).hasSize(1);
202 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
203 assertThat(updatedProperty.isGetInputProperty()).isFalse();
204 assertThat(updatedProperty.getValue()).isEmpty();
205 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
206 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
209 private Resource createResourceWithGroup() {
210 return createResourceWithGroups(GROUP_ID);
213 private Resource createResourceWithGroups(String ... groups) {
214 List<GroupDefinition> groupsDef = Stream.of(groups)
215 .map(this::buildGroup)
216 .collect(Collectors.toList());
218 return new ResourceBuilder()
219 .setUniqueId(RESOURCE_ID)
220 .setGroups(groupsDef)
224 private GroupDefinition buildGroup(String groupId) {
225 return GroupDefinitionBuilder.create()
226 .setUniqueId(groupId)
231 private PropertyDataDefinition buildGetInputProperty(String inputId) {
232 return new PropertyDataDefinitionBuilder()
233 .addGetInputValue(inputId)
234 .setUniqueId(GROUP_ID + "_" + inputId)
235 .setDefaultValue("defaultValue")
236 .setValue(generateGetInputValue(inputId))