aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PolicyPropertyDeclaratorTest.java
blob: 0f065b8ffb76d186500e5cbd33a83a0a96ade693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package org.openecomp.sdc.be.components.property;

import fj.data.Either;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.model.ComponentInstancePropInput;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.PolicyDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.PolicyOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
//note that testing for most of the common logic is under the ComponentInstancePropertyDeclaratorTest
public class PolicyPropertyDeclaratorTest extends PropertyDeclaratorTestBase {

    private static final String POLICY_ID = "policyId";
    @InjectMocks
    private PolicyPropertyDeclarator policyPropertyDeclarator;
    @Mock
    private PolicyOperation policyOperation;
    @Mock
    private PropertyOperation propertyOperation;
    @Captor
    private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
    private Resource resource;
    private InputDefinition input;

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        resource = createResourceWithPolicy();
        input = new InputDefinition();
        input.setUniqueId(INPUT_ID);
        input.setName(INPUT_ID);
        input.setValue("value");
    }

    @Test
    public void testDeclarePropertiesAsInputs_policyNotExist() {
        Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
        assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
        verifyZeroInteractions(policyOperation);
    }

    @Test
    public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
        when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
        Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
        assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
    }

    @Test
    public void testDeclarePropertiesAsInputs() {
        List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
        List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
        when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
        Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
        List<InputDefinition> inputs = createdInputs.left().value();
        assertThat(inputs).hasSize(2);
        verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
        //creation of inputs values is part of the DefaultPropertyDeclarator and is tested in the ComponentInstancePropertyDeclaratorTest class
    }

    @Test
    public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
        Resource resource = new Resource();
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
        verifyZeroInteractions(policyOperation);
    }

    @Test
    public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
        verifyZeroInteractions(policyOperation);
    }

    @Test
    public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
        Resource resource = createResourceWithPolicies(POLICY_ID);
        PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
        PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
        policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
        when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
        when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
    }

    @Test
    public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
        Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
        PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
        PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
        PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
        policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));

        when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
        when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);

        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
        List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
        assertThat(updatedProperties).hasSize(1);
        PropertyDataDefinition updatedProperty = updatedProperties.get(0);
        assertThat(updatedProperty.isGetInputProperty()).isFalse();
        assertThat(updatedProperty.getValue()).isEmpty();
        assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
        assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
    }

    @Test
    public void testUnDeclarePropertiesAsListInputs_whenComponentHasNoPolicies_returnOk() {
        Resource resource = new Resource();
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
        verifyZeroInteractions(policyOperation);
    }

    @Test
    public void testUnDeclarePropertiesAsListInputs_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(createResourceWithPolicy(), input);
        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
        verifyZeroInteractions(policyOperation);
    }

    @Test
    public void whenFailingToUpdateDeclaredPropertiesAsListInputs_returnErrorStatus() {
        Resource resource = createResourceWithPolicies(POLICY_ID);
        PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
        PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
        policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
        when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
        when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
    }

    @Test
    public void testUnDeclarePropertiesAsListInputs_propertiesUpdatedCorrectly() {
        Resource resource = createResourceWithPolicies(POLICY_ID, "policyId3");
        PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
        PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
        PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
        policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));

        when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
        when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
        StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);

        assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
        List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
        assertThat(updatedProperties).hasSize(1);
        PropertyDataDefinition updatedProperty = updatedProperties.get(0);
        assertThat(updatedProperty.isGetInputProperty()).isFalse();
        assertThat(updatedProperty.getValue()).isEmpty();
        assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
        assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
    }

    private Resource createResourceWithPolicy() {
        return createResourceWithPolicies(POLICY_ID);
    }

    private Resource createResourceWithPolicies(String ... policies) {
        List<PolicyDefinition> policiesDef = Stream.of(policies)
                .map(this::buildPolicy)
                .collect(Collectors.toList());

        return new ResourceBuilder()
                .setUniqueId(RESOURCE_ID)
                .setPolicies(policiesDef)
                .build();
    }

    private PolicyDefinition buildPolicy(String policyId) {
        return PolicyDefinitionBuilder.create()
                .setUniqueId(policyId)
                .setName(policyId)
                .build();
    }

    private PropertyDataDefinition buildGetInputProperty(String inputId) {
        return new PropertyDataDefinitionBuilder()
                .addGetInputValue(inputId)
                .setUniqueId(POLICY_ID + "_" + inputId)
                .setDefaultValue("defaultValue")
                .setValue(generateGetInputValue(inputId))
                .build();
    }


}