2 * ============LICENSE_START=============================================================================================================
3 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
4 * ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
10 * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
11 * ============LICENSE_END===============================================================================================================
14 package org.openecomp.sdc.be.components.property;
16 import fj.data.Either;
17 import mockit.Deencapsulation;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Answers;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentInstancePropertyToPolicyDeclarator;
26 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
27 import org.openecomp.sdc.be.components.utils.ServiceBuilder;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
30 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.PolicyDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.Service;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.LinkedList;
41 import java.util.List;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.anyList;
48 import static org.mockito.ArgumentMatchers.anyString;
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.when;
52 public class PropertyDeclarationOrchestratorTest {
55 PropertyDeclarationOrchestrator testSubject;
57 @Mock(answer = Answers.CALLS_REAL_METHODS)
58 List<PropertyDeclarator> propertyDeceleratorsMock;
61 private ComponentInstancePropertyToPolicyDeclarator componentInstancePropertyToPolicyDeclarator;
64 private ComponentPropertyToPolicyDeclarator componentPropertyToPolicyDeclarator;
67 private ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDecelerator;
70 private ComponentInstancePropertyDeclarator componentInstancePropertyDecelerator;
73 private ComponentPropertyDeclarator servicePropertyDeclarator;
76 private PolicyPropertyDeclarator policyPropertyDecelerator;
79 private GroupPropertyDeclarator groupPropertyDeclarator;;
81 private static final String PROP_UID = "propertyUid";
82 private static final String SERVICE_UID = "serviceUid";
84 private Service service;
87 public void setUp() throws Exception {
89 MockitoAnnotations.openMocks(this);
91 service = new ServiceBuilder().setUniqueId(SERVICE_UID).build();
94 @Test(expected = IllegalStateException.class)
95 public void testDeclarePropertiesToInputs() throws Exception {
96 Component component = new Resource();
97 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
98 Either<List<InputDefinition>, StorageOperationStatus> result;
101 result = testSubject.declarePropertiesToInputs(component, componentInstInputsMap);
105 public void testDeclarePropertiesToListInputs() throws Exception {
106 Component component = new Resource();
107 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
108 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
109 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
110 InputDefinition input = new InputDefinition();
111 input.setUniqueId(PROP_UID);
112 Either<InputDefinition, StorageOperationStatus> result;
114 when(componentInstanceInputPropertyDecelerator.declarePropertiesAsListInput(eq(component), anyString(), anyList(), eq(input))).thenReturn(Either.left(input));
116 result = testSubject.declarePropertiesToListInput(component, componentInstInputsMap, input);
118 assertTrue(result.isLeft());
119 assertEquals(PROP_UID, result.left().value().getUniqueId());
123 public void testUnDeclarePropertiesAsInputs() throws Exception {
124 Component component = new Resource();
125 InputDefinition inputToDelete = new InputDefinition();
126 StorageOperationStatus result;
128 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
129 when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
130 when(mockIter.hasNext()).thenReturn(false);
132 setInputUndeclarationStubbings(component, inputToDelete);
135 result = testSubject.unDeclarePropertiesAsInputs(component, inputToDelete);
137 assertEquals(StorageOperationStatus.OK, result);
141 public void testUnDeclarePropertiesAsListInputs() throws Exception {
142 Component component = new Resource();
143 InputDefinition inputToDelete = new InputDefinition();
144 StorageOperationStatus result;
146 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
147 Mockito.when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
148 Mockito.when(mockIter.hasNext()).thenReturn(false);
151 result = testSubject.unDeclarePropertiesAsListInputs(component, inputToDelete);
155 public void testGetPropOwnerId() throws Exception {
156 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
157 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = new HashMap<>();
158 List<ComponentInstancePropInput> value = new LinkedList<>();
159 componentInstanceInputsMap.put("mock", value);
160 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
164 result = Deencapsulation.invoke(testSubject, "getPropOwnerId", componentInstInputsMap);
167 @Test(expected = IllegalStateException.class)
168 public void testGetPropertyDecelerator() throws Exception {
169 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
170 PropertyDeclarator result;
173 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
177 public void testGetPropertyDeceleratorWithInputsMap() throws Exception {
178 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
179 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
180 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
181 PropertyDeclarator result;
184 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
186 assertTrue(result instanceof ComponentInstanceInputPropertyDeclarator);
190 public void testGetPropertyDeceleratorWithCIProperties() throws Exception {
191 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
192 Map<String, List<ComponentInstancePropInput>> componentInstanceProperties = new HashMap<>();
193 List<ComponentInstancePropInput> value = new LinkedList<>();
194 componentInstanceProperties.put("mock", value);
195 componentInstInputsMap.setComponentInstancePropInput(componentInstanceProperties);
196 PropertyDeclarator result;
199 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
201 assertTrue(result instanceof ComponentInstancePropertyDeclarator);
205 public void testGetPropertyDeceleratorWithCIPolicy() throws Exception {
206 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
207 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
208 componentInstInputsMap.setPolicyProperties(policyProperties);
209 PropertyDeclarator result;
212 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
214 assertTrue(result instanceof PolicyPropertyDeclarator);
218 public void testDeclarePropertiesToPolicies() {
219 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
220 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
221 componentInstInputsMap.setComponentInstancePropertiesToPolicies(policyProperties);
223 PolicyDefinition declaredPolicy = getDeclaredPolicy();
225 when(componentInstancePropertyToPolicyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
226 Either.left(Collections.singletonList(declaredPolicy)));
228 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
229 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
231 validatePolicyDeclaration(declaredPolicy, declareEither);
235 public void testUndeclarePropertiesAsPolicies() {
236 when(componentInstancePropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
237 when(componentPropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
239 StorageOperationStatus undeclareStatus =
240 testSubject.unDeclarePropertiesAsPolicies(service, getDeclaredPolicy());
242 assertEquals(StorageOperationStatus.OK, undeclareStatus);
246 public void testDeclareServiceProperties() {
247 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
248 Map<String, List<ComponentInstancePropInput>> serviceProperties = getPropertiesMapToDeclare();
249 componentInstInputsMap.setServiceProperties(serviceProperties);
251 PolicyDefinition declaredPolicy = getDeclaredPolicy();
253 when(servicePropertyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
254 Either.left(Collections.singletonList(declaredPolicy)));
256 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
257 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
259 validatePolicyDeclaration(declaredPolicy, declareEither);
262 private PolicyDefinition getDeclaredPolicy() {
263 PolicyDefinition declaredPolicy = new PolicyDefinition();
264 declaredPolicy.setUniqueId(PROP_UID);
265 return declaredPolicy;
268 private Map<String, List<ComponentInstancePropInput>> getPropertiesMapToDeclare() {
269 Map<String, List<ComponentInstancePropInput>> policyProperties = new HashMap<>();
271 ComponentInstancePropInput propertyToDeclare = new ComponentInstancePropInput();
272 propertyToDeclare.setUniqueId(PROP_UID);
273 propertyToDeclare.setPropertiesName(PROP_UID);
275 policyProperties.put("mock", Collections.singletonList(propertyToDeclare));
276 return policyProperties;
279 private void validatePolicyDeclaration(PolicyDefinition declaredPolicy,
280 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither) {
281 assertTrue(declareEither.isLeft());
283 List<PolicyDefinition> declaredPolicies = declareEither.left().value();
284 assertEquals(1, declaredPolicies.size());
285 assertEquals(declaredPolicy.getUniqueId(), declaredPolicies.get(0).getUniqueId());
288 private void setInputUndeclarationStubbings(Component component, InputDefinition inputToDelete) {
289 when(policyPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(
290 StorageOperationStatus.OK);
291 when(servicePropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
292 when(componentInstancePropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
293 when(componentInstanceInputPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
294 when(groupPropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);