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