Refactoring Consolidation Service
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / PolicyPropertyDeceleratorTest.java
1 package org.openecomp.sdc.be.components.property;
2
3 import static org.assertj.core.api.Java6Assertions.assertThat;
4 import static org.mockito.ArgumentMatchers.eq;
5 import static org.mockito.Mockito.verifyZeroInteractions;
6 import static org.mockito.Mockito.when;
7
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.stream.Collectors;
12 import java.util.stream.Stream;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Captor;
19 import org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
23 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
24 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
25 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
26 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
27 import org.openecomp.sdc.be.model.InputDefinition;
28 import org.openecomp.sdc.be.model.PolicyDefinition;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.jsontitan.operations.PolicyOperation;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
33
34 import fj.data.Either;
35
36
37 @RunWith(MockitoJUnitRunner.class)
38 //note that testing for most of the common logic is under the ComponentInstancePropertyDeceleratorTest
39 public class PolicyPropertyDeceleratorTest extends PropertyDeceleratorTestBase{
40
41     private static final String POLICY_ID = "policyId";
42     private static final String RESOURCE_ID = "resourceId";
43     private static final String INPUT_ID = "inputId";
44     @InjectMocks
45     private PolicyPropertyDecelerator policyPropertyDecelerator;
46     @Mock
47     private PolicyOperation policyOperation;
48     @Mock
49     private PropertyOperation propertyOperation;
50     @Captor
51     private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
52     private Resource resource;
53     private InputDefinition input;
54
55     @Override
56     @Before
57     public void setUp() throws Exception {
58         super.setUp();
59         resource = createResourceWithPolicy();
60         input = new InputDefinition();
61         input.setUniqueId(INPUT_ID);
62         input.setName(INPUT_ID);
63         input.setValue("value");
64     }
65
66     @Test
67     public void testDeclarePropertiesAsInputs_policyNotExist() {
68         Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDecelerator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
69         assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
70         verifyZeroInteractions(policyOperation);
71     }
72
73     @Test
74     public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
75         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
76         Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDecelerator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
77         assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
78     }
79
80     @Test
81     public void testDeclarePropertiesAsInputs() {
82         List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
83         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
84         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
85         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDecelerator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
86         List<InputDefinition> inputs = createdInputs.left().value();
87         assertThat(inputs).hasSize(2);
88         verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
89         //creation of inputs values is part of the DefaultPropertyDecelerator and is tested in the ComponentInstancePropertyDeceleratorTest class
90     }
91
92     @Test
93     public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
94         Resource resource = new Resource();
95         StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
96         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
97         verifyZeroInteractions(policyOperation);
98     }
99
100     @Test
101     public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
102         StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
103         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
104         verifyZeroInteractions(policyOperation);
105     }
106
107     @Test
108     public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
109         Resource resource = createResourceWithPolicies(POLICY_ID);
110         PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
111         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
112         policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
113         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
114         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
115         StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
116         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
117     }
118
119     @Test
120     public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
121         Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
122         PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
123         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
124         PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
125         policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
126
127         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
128         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
129         StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
130
131         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
132         List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
133         assertThat(updatedProperties).hasSize(1);
134         PropertyDataDefinition updatedProperty = updatedProperties.get(0);
135         assertThat(updatedProperty.isGetInputProperty()).isFalse();
136         assertThat(updatedProperty.getValue()).isEmpty();
137         assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
138         assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
139     }
140
141     private Resource createResourceWithPolicy() {
142         return createResourceWithPolicies(POLICY_ID);
143     }
144
145     private Resource createResourceWithPolicies(String ... policies) {
146         List<PolicyDefinition> policiesDef = Stream.of(policies)
147                 .map(this::buildPolicy)
148                 .collect(Collectors.toList());
149
150         return new ResourceBuilder()
151                 .setUniqueId(RESOURCE_ID)
152                 .setPolicies(policiesDef)
153                 .build();
154     }
155
156     private PolicyDefinition buildPolicy(String policyId) {
157         return PolicyDefinitionBuilder.create()
158                 .setUniqueId(policyId)
159                 .setName(policyId)
160                 .build();
161     }
162
163     private PropertyDataDefinition buildGetInputProperty(String inputId) {
164         return new PropertyDataDefinitionBuilder()
165                 .addGetInputValue(inputId)
166                 .setUniqueId(POLICY_ID + "_" + inputId)
167                 .setDefaultValue("defaultValue")
168                 .setValue(generateGetInputValue(inputId))
169                 .build();
170     }
171
172
173 }