Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / distribution / engine / EnvironmentsEngineTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.distribution.engine;
22
23 import com.att.nsa.apiClient.credentials.ApiCredential;
24 import fj.data.Either;
25 import mockit.Deencapsulation;
26 import org.apache.http.HttpStatus;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.openecomp.sdc.be.config.Configuration;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
36 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
37 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
38 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
39 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
40 import org.openecomp.sdc.be.info.OperationalEnvInfo;
41 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
42 import org.openecomp.sdc.common.datastructure.Wrapper;
43 import org.openecomp.sdc.common.http.client.api.HttpResponse;
44
45 import java.io.IOException;
46 import java.util.*;
47 import java.util.concurrent.atomic.AtomicBoolean;
48
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertTrue;
51 import static org.mockito.Mockito.when;
52
53 public class EnvironmentsEngineTest {
54
55     @InjectMocks
56     private EnvironmentsEngine envEngine;
57     @Mock
58     private OperationalEnvironmentDao operationalEnvironmentDao;
59     @Mock
60     private ConfigurationManager configurationManager;
61     @Mock
62     private DistributionEngineConfiguration distributionEngineConfiguration;
63     @Mock
64     private AaiRequestHandler aaiRequestHandler;
65
66     @Before
67     public void preStart() {
68         MockitoAnnotations.initMocks(this);
69     }
70
71     @Test
72     public void testInit() {
73         envEngine.setConfigurationManager(configurationManager);
74         Configuration config = Mockito.mock(Configuration.class);
75         DmaapConsumerConfiguration dmaapConf = Mockito.mock(DmaapConsumerConfiguration.class);
76         List<OperationalEnvironmentEntry> entryList = Arrays.asList(createOpEnvEntry("Env1"), createOpEnvEntry("Env2"));
77         Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> successEither = Either.left(entryList);
78         when(operationalEnvironmentDao.getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED)).thenReturn(successEither);
79         when(configurationManager.getDistributionEngineConfiguration()).thenReturn(distributionEngineConfiguration);
80         when(distributionEngineConfiguration.getEnvironments()).thenReturn(Arrays.asList("Env Loaded From Configuration"));
81         when(distributionEngineConfiguration.getUebPublicKey()).thenReturn("Dummy Public Key");
82         when(distributionEngineConfiguration.getUebSecretKey()).thenReturn("Dummy Private Key");
83         when(distributionEngineConfiguration.getUebServers()).thenReturn(
84                 Arrays.asList("uebsb91kcdc.it.com:3904", "uebsb92kcdc.it.com:3904", "uebsb91kcdc.it.com:3904"));
85         when(configurationManager.getConfiguration()).thenReturn(config);
86         when(config.getDmaapConsumerConfiguration()).thenReturn(dmaapConf);
87         when(dmaapConf.isActive()).thenReturn(false);
88         envEngine.init();
89
90         Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
91         assertEquals("unexpected size of map",3, mapEnvs.size());
92     }
93
94
95     @Test
96     public void testGetFullOperationalEnvByIdSuccess() {
97         String json = getFullOperationalEnvJson();
98         
99         HttpResponse restResponse = new HttpResponse(json, HttpStatus.SC_OK, "Successfully completed");
100         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
101         
102         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
103         assertTrue("The operational environment request ran as not expected", response.isLeft());
104         
105         OperationalEnvInfo operationalEnvInfo = response.left().value();
106
107         assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
108     }
109
110     @Test
111     public void testGetPartialOperationalEnvByIdSuccess() {
112         String json = getPartialOperationalEnvJson();
113         
114         HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
115         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
116         
117         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
118         assertTrue("The operational environment request ran as not expected", response.isLeft());
119         
120         OperationalEnvInfo operationalEnvInfo = response.left().value();
121
122         assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
123     }
124
125     
126     @Test
127     public void testGetOperationalEnvByIdFailedByJsonConvert() {
128         String jsonCorrupted = getCorruptedOperationalEnvJson();
129         
130         HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
131         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
132         
133         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
134         assertTrue("The operational environment request ran as not expected", response.isRight());
135         assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_INTERNAL_SERVER_ERROR, response.right().value());
136     }
137
138     @Test
139     public void testGetOperationalEnvByIdFailed404() {
140         String json = getFullOperationalEnvJson();
141         HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_NOT_FOUND, "Not Found");
142         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
143         
144         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
145         assertTrue("The operational environment request ran as not expected", response.isRight());
146         assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_NOT_FOUND, response.right().value());
147     }
148
149
150     @Test(expected = IOException.class)
151     public void testCorruptedOperationalEnvJson() throws IOException {
152         String jsonCorrupted = getCorruptedOperationalEnvJson();
153         OperationalEnvInfo.createFromJson(jsonCorrupted);
154     }
155
156     @Test
157     public void getEnvironmentById() {
158         OperationalEnvironmentEntry oe = new OperationalEnvironmentEntry();
159         oe.setEnvironmentId("mock");
160         envEngine.addToMap(oe);
161         assertTrue(envEngine.isInMap("mock"));
162         assertTrue(envEngine.isInMap(oe));
163         OperationalEnvironmentEntry returnedOe = envEngine.getEnvironmentById("mock");
164         assertTrue(oe == returnedOe);
165     }
166
167     private String getCorruptedOperationalEnvJson() {
168         return "{\"OPERATIONAL-environment-name\":\"Op Env Name\","
169                 + "\"OPERATIONAL-environment-type\":\"VNF\","
170                 + "\"OPERATIONAL-environment-status\":\"Activate\","
171                 + "\"tenant-context\":\"Test\"}";
172     }
173
174     private String getPartialOperationalEnvJson() {
175         return "{" +
176                 "\"operational-environment-id\":\"UUID of Operational Environment\"," +
177                 "\"operational-environment-name\":\"Op Env Name\"," +
178                 "\"operational-environment-type\":\"VNF\"," +
179                 "\"operational-environment-status\":\"Activate\"," +
180                 "\"tenant-context\":\"Test\"," +
181                 "\"workload-context\":\"VNF_Development\"," +
182                 "\"resource-version\":\"1505228226913\"," +
183                 "\"relationship-list\":{" +
184                 "\"relationship\":[]" +
185                 "}" +
186              "}";
187     }
188
189     private String getFullOperationalEnvJson() {
190         return  "{" +
191                 "\"operational-environment-id\":\"OEid1\"," +
192                 "\"operational-environment-name\":\"OEname1\"," +
193                 "\"operational-environment-type\":\"OEtype1\"," +
194                 "\"operational-environment-status\":\"OEstatus1\"," +
195                 "\"tenant-context\":\"OEtenantcontext1\"," +
196                 "\"workload-context\":\"OEworkloadcontext1\"," +
197                 "\"resource-version\":\"1511363173278\"," +
198                 "\"relationship-list\":{" +
199                 "\"relationship\":[" +
200                 "{" +
201                 "\"related-to\":\"operational-environment\"," +
202                 "\"relationship-label\":\"managedBy\"," +
203                 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
204                 "\"relationship-data\":[" +
205                 "{" +
206                 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
207                 "\"relationship-value\":\"OEid3\"" +
208                 "}" +
209                 "]," +
210                 "\"related-to-property\":[" +
211                 "{" +
212                 "\"property-key\":\"operational-environment.operational-environment-name\"," +
213                 "\"property-value\":\"OEname3\"" +
214                 "}]}]}}";
215     }
216     
217     private OperationalEnvironmentEntry createOpEnvEntry(String name) {
218         OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
219         entry.setEnvironmentId(name);
220         return entry;
221     }
222
223     public void testHandleMessageLogic() throws Exception {
224         String notification = "";
225         boolean result;
226
227         // default test
228         result = envEngine.handleMessageLogic(notification);
229     }
230
231     @Test
232     public void testValidateNotification() throws Exception {
233         Wrapper<Boolean> errorWrapper = new Wrapper<>();
234         errorWrapper.setInnerElement(true);
235         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
236         IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
237
238         // default test
239         Deencapsulation.invoke(envEngine, "validateNotification", errorWrapper, notificationData,
240                 auditNotificationData);
241     }
242
243     @Test
244     public void testSaveEntryWithFailedStatus() throws Exception {
245         Wrapper<Boolean> errorWrapper = new Wrapper<>();
246         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
247
248         // default test
249         Deencapsulation.invoke(envEngine, "saveEntryWithFailedStatus", errorWrapper, opEnvEntry);
250     }
251
252     @Test
253     public void testRetrieveUebAddressesFromAftDme() throws Exception {
254         Wrapper<Boolean> errorWrapper = new Wrapper<>();
255         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
256
257         // default test
258         Deencapsulation.invoke(envEngine, "retrieveUebAddressesFromAftDme", errorWrapper, opEnvEntry);
259     }
260
261     @Test
262     public void testRetrieveOpEnvInfoFromAAI() throws Exception {
263         Wrapper<Boolean> errorWrapper = new Wrapper<>();
264         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
265         opEnvEntry.setEnvironmentId("mock");
266         Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 200));
267         // default test
268         Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
269     }
270
271     @Test
272     public void testRetrieveOpEnvInfoFromAAIError() throws Exception {
273         Wrapper<Boolean> errorWrapper = new Wrapper<>();
274         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
275         opEnvEntry.setEnvironmentId("mock");
276         Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 500));
277         // default test
278         Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
279     }
280
281     @Test
282     public void testSaveEntryWithInProgressStatus() throws Exception {
283         Wrapper<Boolean> errorWrapper = new Wrapper<>();
284         Wrapper<OperationalEnvironmentEntry> opEnvEntryWrapper = new Wrapper<>();
285         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
286
287         Deencapsulation.invoke(envEngine, "saveEntryWithInProgressStatus", errorWrapper, opEnvEntryWrapper,
288                 notificationData);
289     }
290
291     @Test
292     public void testValidateStateGeneralError() throws Exception {
293         Wrapper<Boolean> errorWrapper = null;
294         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
295
296         Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class)))
297                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
298
299         Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
300     }
301
302     @Test
303     public void testValidateState() throws Exception {
304         Wrapper<Boolean> errorWrapper = null;
305         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
306
307         OperationalEnvironmentEntry a = new OperationalEnvironmentEntry();
308         a.setStatus(EnvironmentStatusEnum.IN_PROGRESS.getName());
309         Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class))).thenReturn(Either.left(a));
310
311         Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
312     }
313
314     @Test
315     public void testValidateActionType() throws Exception {
316         Wrapper<Boolean> errorWrapper = new Wrapper<>();
317         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
318         Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.DELETE);
319
320         Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
321     }
322
323     @Test
324     public void testValidateActionType2() throws Exception {
325         Wrapper<Boolean> errorWrapper = new Wrapper<>();
326         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
327         Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.CREATE);
328
329         Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
330     }
331
332     @Test
333     public void testValidateEnvironmentType() throws Exception {
334         Wrapper<Boolean> errorWrapper = new Wrapper<>();
335         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
336         IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
337         Mockito.when(auditNotificationData.getOperationalEnvironmentName()).thenReturn("mock");
338         Mockito.when(notificationData.getOperationalEnvironmentType()).thenReturn(IDmaapNotificationData.OperationaEnvironmentTypeEnum.ECOMP);
339
340         // default test
341         Deencapsulation.invoke(envEngine, "validateEnvironmentType", errorWrapper, notificationData,
342                 auditNotificationData);
343     }
344
345     @Test
346     public void testMap2OpEnvKey() throws Exception {
347         OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
348         String result;
349
350         // default test
351         result = Deencapsulation.invoke(envEngine, "map2OpEnvKey", entry);
352     }
353 }