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 static org.assertj.core.api.Java6Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.verifyZeroInteractions;
26 import static org.mockito.Mockito.when;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Captor;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
43 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
44 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
45 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
46 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
47 import org.openecomp.sdc.be.model.InputDefinition;
48 import org.openecomp.sdc.be.model.PolicyDefinition;
49 import org.openecomp.sdc.be.model.Resource;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.PolicyOperation;
51 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
52 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
54 import fj.data.Either;
57 @RunWith(MockitoJUnitRunner.class)
58 //note that testing for most of the common logic is under the ComponentInstancePropertyDeceleratorTest
59 public class PolicyPropertyDeceleratorTest extends PropertyDeceleratorTestBase{
61 private static final String POLICY_ID = "policyId";
62 private static final String RESOURCE_ID = "resourceId";
63 private static final String INPUT_ID = "inputId";
65 private PolicyPropertyDeclarator policyPropertyDeclarator;
67 private PolicyOperation policyOperation;
69 private PropertyOperation propertyOperation;
71 private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
72 private Resource resource;
73 private InputDefinition input;
77 public void setUp() throws Exception {
79 resource = createResourceWithPolicy();
80 input = new InputDefinition();
81 input.setUniqueId(INPUT_ID);
82 input.setName(INPUT_ID);
83 input.setValue("value");
87 public void testDeclarePropertiesAsInputs_policyNotExist() {
88 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
89 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
90 verifyZeroInteractions(policyOperation);
94 public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
95 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
96 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
97 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
101 public void testDeclarePropertiesAsInputs() {
102 List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
103 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
104 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
105 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
106 List<InputDefinition> inputs = createdInputs.left().value();
107 assertThat(inputs).hasSize(2);
108 verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
109 //creation of inputs values is part of the DefaultPropertyDecelerator and is tested in the ComponentInstancePropertyDeceleratorTest class
113 public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
114 Resource resource = new Resource();
115 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
116 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
117 verifyZeroInteractions(policyOperation);
121 public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
122 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
123 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
124 verifyZeroInteractions(policyOperation);
128 public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
129 Resource resource = createResourceWithPolicies(POLICY_ID);
130 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
131 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
132 policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
133 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
134 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
135 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
136 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
140 public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
141 Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
142 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
143 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
144 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
145 policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
147 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
148 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
149 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
151 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
152 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
153 assertThat(updatedProperties).hasSize(1);
154 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
155 assertThat(updatedProperty.isGetInputProperty()).isFalse();
156 assertThat(updatedProperty.getValue()).isEmpty();
157 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
158 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
161 private Resource createResourceWithPolicy() {
162 return createResourceWithPolicies(POLICY_ID);
165 private Resource createResourceWithPolicies(String ... policies) {
166 List<PolicyDefinition> policiesDef = Stream.of(policies)
167 .map(this::buildPolicy)
168 .collect(Collectors.toList());
170 return new ResourceBuilder()
171 .setUniqueId(RESOURCE_ID)
172 .setPolicies(policiesDef)
176 private PolicyDefinition buildPolicy(String policyId) {
177 return PolicyDefinitionBuilder.create()
178 .setUniqueId(policyId)
183 private PropertyDataDefinition buildGetInputProperty(String inputId) {
184 return new PropertyDataDefinitionBuilder()
185 .addGetInputValue(inputId)
186 .setUniqueId(POLICY_ID + "_" + inputId)
187 .setDefaultValue("defaultValue")
188 .setValue(generateGetInputValue(inputId))