2fabd25549a9bc046a00ddebb86610b99120d801
[sdc.git] /
1 /*
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
6  *
7  *        http://www.apache.org/licenses/LICENSE-2.0
8  *
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===============================================================================================================
12  *
13  */
14 package org.openecomp.sdc.be.components.property;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertTrue;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.ArgumentMatchers.anyList;
20 import static org.mockito.ArgumentMatchers.anyString;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.when;
23
24 import fj.data.Either;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import mockit.Deencapsulation;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Answers;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentInstancePropertyToPolicyDeclarator;
40 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
41 import org.openecomp.sdc.be.components.utils.ServiceBuilder;
42 import org.openecomp.sdc.be.model.Component;
43 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
44 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
45 import org.openecomp.sdc.be.model.InputDefinition;
46 import org.openecomp.sdc.be.model.PolicyDefinition;
47 import org.openecomp.sdc.be.model.Resource;
48 import org.openecomp.sdc.be.model.Service;
49 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
50
51 public class PropertyDeclarationOrchestratorTest {
52
53         @InjectMocks
54         PropertyDeclarationOrchestrator testSubject;
55
56         @Mock(answer = Answers.CALLS_REAL_METHODS)
57         List<PropertyDeclarator> propertyDeceleratorsMock;
58
59         @Mock
60         private ComponentInstancePropertyToPolicyDeclarator componentInstancePropertyToPolicyDeclarator;
61
62         @Mock
63         private ComponentPropertyToPolicyDeclarator componentPropertyToPolicyDeclarator;
64
65         @Mock
66         private ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDecelerator;
67
68         @Mock
69         private ComponentInstancePropertyDeclarator componentInstancePropertyDecelerator;
70
71         @Mock
72         private ComponentPropertyDeclarator servicePropertyDeclarator;
73
74         @Mock
75         private PolicyPropertyDeclarator policyPropertyDecelerator;
76
77         @Mock
78         private GroupPropertyDeclarator groupPropertyDeclarator;;
79
80         private static final String PROP_UID = "propertyUid";
81         private static final String SERVICE_UID = "serviceUid";
82
83         private Service service;
84
85         @Before
86         public void setUp() throws Exception {
87
88                 MockitoAnnotations.initMocks(this);
89
90                 service = new ServiceBuilder().setUniqueId(SERVICE_UID).build();
91         }
92
93         @Test(expected = IllegalStateException.class)
94         public void testDeclarePropertiesToInputs() throws Exception {
95                 Component component = new Resource();
96                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
97                 Either<List<InputDefinition>, StorageOperationStatus> result;
98
99                 // default test
100                 result = testSubject.declarePropertiesToInputs(component, componentInstInputsMap);
101         }
102
103         @Test
104         public void testDeclarePropertiesToListInputs() throws Exception {
105                 Component component = new Resource();
106                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
107                 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
108                 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
109                 InputDefinition input = new InputDefinition();
110                 input.setUniqueId(PROP_UID);
111                 Either<InputDefinition, StorageOperationStatus> result;
112
113                 when(componentInstanceInputPropertyDecelerator.declarePropertiesAsListInput(eq(component), anyString(), anyList(), eq(input))).thenReturn(Either.left(input));
114                 // default test
115                 result = testSubject.declarePropertiesToListInput(component, componentInstInputsMap, input);
116
117                 assertTrue(result.isLeft());
118                 assertEquals(PROP_UID, result.left().value().getUniqueId());
119         }
120
121         @Test
122         public void testUnDeclarePropertiesAsInputs() throws Exception {
123                 Component component = new Resource();
124                 InputDefinition inputToDelete = new InputDefinition();
125                 StorageOperationStatus result;
126
127                 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
128                 when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
129                 when(mockIter.hasNext()).thenReturn(false);
130
131                 setInputUndeclarationStubbings(component, inputToDelete);
132
133                 // default test
134                 result = testSubject.unDeclarePropertiesAsInputs(component, inputToDelete);
135
136                 assertEquals(StorageOperationStatus.OK, result);
137         }
138
139         @Test
140         public void testUnDeclarePropertiesAsListInputs() throws Exception {
141                 Component component = new Resource();
142                 InputDefinition inputToDelete = new InputDefinition();
143                 StorageOperationStatus result;
144
145                 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
146                 Mockito.when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
147                 Mockito.when(mockIter.hasNext()).thenReturn(false);
148
149                 // default test
150                 result = testSubject.unDeclarePropertiesAsListInputs(component, inputToDelete);
151         }
152
153         @Test
154         public void testGetPropOwnerId() throws Exception {
155                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
156                 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = new HashMap<>();
157                 List<ComponentInstancePropInput> value = new LinkedList<>();
158                 componentInstanceInputsMap.put("mock", value);
159                 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
160                 String result;
161
162                 // default test
163                 result = Deencapsulation.invoke(testSubject, "getPropOwnerId", componentInstInputsMap);
164         }
165
166         @Test(expected = IllegalStateException.class)
167         public void testGetPropertyDecelerator() throws Exception {
168                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
169                 PropertyDeclarator result;
170
171                 // default test
172                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
173         }
174
175         @Test
176         public void testGetPropertyDeceleratorWithInputsMap() throws Exception {
177                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
178                 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
179                 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
180                 PropertyDeclarator result;
181
182                 // default test
183                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
184
185                 assertTrue(result instanceof ComponentInstanceInputPropertyDeclarator);
186         }
187
188         @Test
189         public void testGetPropertyDeceleratorWithCIProperties() throws Exception {
190                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
191                 Map<String, List<ComponentInstancePropInput>> componentInstanceProperties = new HashMap<>();
192                 List<ComponentInstancePropInput> value = new LinkedList<>();
193                 componentInstanceProperties.put("mock", value);
194                 componentInstInputsMap.setComponentInstanceProperties(componentInstanceProperties);
195                 PropertyDeclarator result;
196
197                 // default test
198                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
199
200                 assertTrue(result instanceof ComponentInstancePropertyDeclarator);
201         }
202
203         @Test
204         public void testGetPropertyDeceleratorWithCIPolicy() throws Exception {
205                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
206                 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
207                 componentInstInputsMap.setPolicyProperties(policyProperties);
208                 PropertyDeclarator result;
209
210                 // default test
211                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
212
213                 assertTrue(result instanceof PolicyPropertyDeclarator);
214         }
215
216         @Test
217         public void testDeclarePropertiesToPolicies() {
218                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
219                 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
220                 componentInstInputsMap.setComponentInstancePropertiesToPolicies(policyProperties);
221
222                 PolicyDefinition declaredPolicy = getDeclaredPolicy();
223
224                 when(componentInstancePropertyToPolicyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
225                                 Either.left(Collections.singletonList(declaredPolicy)));
226
227                 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
228                                 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
229
230                 validatePolicyDeclaration(declaredPolicy, declareEither);
231         }
232
233         @Test
234         public void testUndeclarePropertiesAsPolicies() {
235                 when(componentInstancePropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
236                 when(componentPropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
237
238                 StorageOperationStatus undeclareStatus =
239                                 testSubject.unDeclarePropertiesAsPolicies(service, getDeclaredPolicy());
240
241                 assertEquals(StorageOperationStatus.OK, undeclareStatus);
242         }
243
244         @Test
245         public void testDeclareServiceProperties() {
246                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
247                 Map<String, List<ComponentInstancePropInput>> serviceProperties = getPropertiesMapToDeclare();
248                 componentInstInputsMap.setServiceProperties(serviceProperties);
249
250                 PolicyDefinition declaredPolicy = getDeclaredPolicy();
251
252                 when(servicePropertyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
253                                 Either.left(Collections.singletonList(declaredPolicy)));
254
255                 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
256                                 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
257
258                 validatePolicyDeclaration(declaredPolicy, declareEither);
259         }
260
261         private PolicyDefinition getDeclaredPolicy() {
262                 PolicyDefinition declaredPolicy = new PolicyDefinition();
263                 declaredPolicy.setUniqueId(PROP_UID);
264                 return declaredPolicy;
265         }
266
267         private Map<String, List<ComponentInstancePropInput>> getPropertiesMapToDeclare() {
268                 Map<String, List<ComponentInstancePropInput>> policyProperties = new HashMap<>();
269
270                 ComponentInstancePropInput propertyToDeclare = new ComponentInstancePropInput();
271                 propertyToDeclare.setUniqueId(PROP_UID);
272                 propertyToDeclare.setPropertiesName(PROP_UID);
273
274                 policyProperties.put("mock", Collections.singletonList(propertyToDeclare));
275                 return policyProperties;
276         }
277
278         private void validatePolicyDeclaration(PolicyDefinition declaredPolicy,
279                         Either<List<PolicyDefinition>, StorageOperationStatus> declareEither) {
280                 assertTrue(declareEither.isLeft());
281
282                 List<PolicyDefinition> declaredPolicies = declareEither.left().value();
283                 assertEquals(1, declaredPolicies.size());
284                 assertEquals(declaredPolicy.getUniqueId(), declaredPolicies.get(0).getUniqueId());
285         }
286
287         private void setInputUndeclarationStubbings(Component component, InputDefinition inputToDelete) {
288                 when(policyPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(
289                                 StorageOperationStatus.OK);
290                 when(servicePropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
291                 when(componentInstancePropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
292                 when(componentInstanceInputPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
293                 when(groupPropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
294         }
295 }