1 package org.openecomp.sdc.be.components.distribution.engine;
3 import java.util.HashMap;
4 import java.util.HashSet;
7 import java.util.concurrent.atomic.AtomicBoolean;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.mockito.InjectMocks;
12 import org.mockito.Mock;
13 import org.mockito.Mockito;
14 import org.mockito.MockitoAnnotations;
15 import org.openecomp.sdc.be.components.BeConfDependentTest;
16 import org.openecomp.sdc.be.components.distribution.engine.IDmaapNotificationData.DmaapActionEnum;
17 import org.openecomp.sdc.be.components.distribution.engine.IDmaapNotificationData.OperationaEnvironmentTypeEnum;
18 import org.openecomp.sdc.be.config.ConfigurationManager;
19 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
20 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
21 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
22 import org.openecomp.sdc.be.impl.ComponentsUtils;
23 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
24 import org.openecomp.sdc.common.datastructure.Wrapper;
25 import org.openecomp.sdc.common.http.client.api.HttpResponse;
26 import org.springframework.test.util.ReflectionTestUtils;
28 import com.att.nsa.apiClient.credentials.ApiCredential;
30 import fj.data.Either;
31 import groovyjarjarantlr.collections.List;
32 import mockit.Deencapsulation;
34 public class EnvironmentsEngineTest extends BeConfDependentTest {
37 EnvironmentsEngine testSubject;
40 ComponentsUtils componentUtils;
43 OperationalEnvironmentDao operationalEnvironmentDao;
46 CambriaHandler cambriaHandler;
49 AaiRequestHandler aaiRequestHandler;
52 public void setUpMocks() throws Exception {
53 MockitoAnnotations.initMocks(this);
57 public void testInit() throws Exception {
60 Deencapsulation.invoke(testSubject, "init");
64 public void testConnectUebTopicTenantIsolation() throws Exception {
65 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
66 opEnvEntry.setEnvironmentId("mock");
67 AtomicBoolean status = null;
68 Map<String, DistributionEngineInitTask> envNamePerInitTask = new HashMap<>();
69 Map<String, DistributionEnginePollingTask> envNamePerPollingTask = new HashMap<>();
72 testSubject.connectUebTopicTenantIsolation(opEnvEntry, status, envNamePerInitTask, envNamePerPollingTask);
76 public void testConnectUebTopic() throws Exception {
77 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
78 AtomicBoolean status = new AtomicBoolean(true);
79 Map<String, DistributionEngineInitTask> envNamePerInitTask = new HashMap<>();
80 Map<String, DistributionEnginePollingTask> envNamePerPollingTask = new HashMap<>();
83 Deencapsulation.invoke(testSubject, "connectUebTopic", opEnvEntry, status, envNamePerInitTask,
84 envNamePerPollingTask);
88 public void testHandleMessage() throws Exception {
89 String notification = "";
93 result = testSubject.handleMessage(notification);
97 public void testHandleMessageLogic() throws Exception {
98 String notification = "";
102 result = testSubject.handleMessageLogic(notification);
106 public void testValidateNotification() throws Exception {
107 Wrapper<Boolean> errorWrapper = new Wrapper<>();
108 errorWrapper.setInnerElement(true);
109 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
110 IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
113 Deencapsulation.invoke(testSubject, "validateNotification", errorWrapper, notificationData,
114 auditNotificationData);
118 public void testSaveEntryWithFailedStatus() throws Exception {
119 Wrapper<Boolean> errorWrapper = new Wrapper<>();
120 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
123 Deencapsulation.invoke(testSubject, "saveEntryWithFailedStatus", errorWrapper, opEnvEntry);
127 public void testRetrieveUebAddressesFromAftDme() throws Exception {
128 Wrapper<Boolean> errorWrapper = new Wrapper<>();
129 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
132 Deencapsulation.invoke(testSubject, "retrieveUebAddressesFromAftDme", errorWrapper, opEnvEntry);
136 public void testCreateUebKeys() throws Exception {
137 Wrapper<Boolean> errorWrapper = new Wrapper<>();
138 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
140 Set<String> dmaapUebAddress = new HashSet<>();
141 dmaapUebAddress.add("mock");
142 opEnvEntry.setDmaapUebAddress(dmaapUebAddress);
144 Mockito.when(cambriaHandler.createUebKeys(Mockito.any())).thenReturn(Either.left(new ApiCredential("mock", "mock")));
147 Deencapsulation.invoke(testSubject, "createUebKeys", errorWrapper, opEnvEntry);
151 public void testCreateUebKeysError() throws Exception {
152 Wrapper<Boolean> errorWrapper = new Wrapper<>();
153 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
155 Set<String> dmaapUebAddress = new HashSet<>();
156 dmaapUebAddress.add("mock");
157 opEnvEntry.setDmaapUebAddress(dmaapUebAddress);
159 Mockito.when(cambriaHandler.createUebKeys(Mockito.any())).thenReturn(Either.right(new CambriaErrorResponse()));
162 Deencapsulation.invoke(testSubject, "createUebKeys", errorWrapper, opEnvEntry);
166 public void testRetrieveOpEnvInfoFromAAI() throws Exception {
167 Wrapper<Boolean> errorWrapper = new Wrapper<>();
168 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
169 opEnvEntry.setEnvironmentId("mock");
170 Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 200));
172 Deencapsulation.invoke(testSubject, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
176 public void testRetrieveOpEnvInfoFromAAIError() throws Exception {
177 Wrapper<Boolean> errorWrapper = new Wrapper<>();
178 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
179 opEnvEntry.setEnvironmentId("mock");
180 Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 500));
182 Deencapsulation.invoke(testSubject, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
186 public void testSaveEntryWithInProgressStatus() throws Exception {
187 Wrapper<Boolean> errorWrapper = new Wrapper<>();
188 Wrapper<OperationalEnvironmentEntry> opEnvEntryWrapper = new Wrapper<>();
189 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
191 Deencapsulation.invoke(testSubject, "saveEntryWithInProgressStatus", errorWrapper, opEnvEntryWrapper,
196 public void testValidateStateGeneralError() throws Exception {
197 Wrapper<Boolean> errorWrapper = null;
198 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
200 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class)))
201 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
203 Deencapsulation.invoke(testSubject, "validateState", new Wrapper<>(), notificationData);
207 public void testValidateState() throws Exception {
208 Wrapper<Boolean> errorWrapper = null;
209 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
211 OperationalEnvironmentEntry a = new OperationalEnvironmentEntry();
212 a.setStatus(EnvironmentStatusEnum.IN_PROGRESS.getName());
213 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class))).thenReturn(Either.left(a));
215 Deencapsulation.invoke(testSubject, "validateState", new Wrapper<>(), notificationData);
219 public void testValidateActionType() throws Exception {
220 Wrapper<Boolean> errorWrapper = new Wrapper<>();
221 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
222 Mockito.when(notificationData.getAction()).thenReturn(DmaapActionEnum.DELETE);
224 Deencapsulation.invoke(testSubject, "validateActionType", errorWrapper, notificationData);
228 public void testValidateActionType2() throws Exception {
229 Wrapper<Boolean> errorWrapper = new Wrapper<>();
230 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
231 Mockito.when(notificationData.getAction()).thenReturn(DmaapActionEnum.CREATE);
233 Deencapsulation.invoke(testSubject, "validateActionType", errorWrapper, notificationData);
237 public void testValidateEnvironmentType() throws Exception {
238 Wrapper<Boolean> errorWrapper = new Wrapper<>();
239 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
240 IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
241 Mockito.when(auditNotificationData.getOperationalEnvironmentName()).thenReturn("mock");
242 Mockito.when(notificationData.getOperationalEnvironmentType()).thenReturn(OperationaEnvironmentTypeEnum.ECOMP);
245 Deencapsulation.invoke(testSubject, "validateEnvironmentType", errorWrapper, notificationData,
246 auditNotificationData);
250 public void testValidateEnvironmentType1() throws Exception {
251 Wrapper<Boolean> errorWrapper = new Wrapper<>();
252 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
253 IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
254 Mockito.when(auditNotificationData.getOperationalEnvironmentName()).thenReturn("mock");
255 Mockito.when(notificationData.getOperationalEnvironmentType()).thenReturn(OperationaEnvironmentTypeEnum.UNKONW);
256 Mockito.when(notificationData.getAction()).thenReturn(DmaapActionEnum.CREATE);
258 Deencapsulation.invoke(testSubject, "validateEnvironmentType", errorWrapper, notificationData,
259 auditNotificationData);
263 public void testMap2OpEnvKey() throws Exception {
264 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
268 result = Deencapsulation.invoke(testSubject, "map2OpEnvKey", entry);
272 public void testReadEnvFromConfig() throws Exception {
273 OperationalEnvironmentEntry result;
276 result = Deencapsulation.invoke(testSubject, "readEnvFromConfig");
280 public void testCreateUebTopicsForEnvironment() throws Exception {
281 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
284 testSubject.createUebTopicsForEnvironment(opEnvEntry);
288 public void testSetConfigurationManager() throws Exception {
289 ConfigurationManager configurationManager = null;
292 Deencapsulation.invoke(testSubject, "setConfigurationManager", new Object[] { ConfigurationManager.class });
296 public void testGetEnvironments() throws Exception {
297 Map<String, OperationalEnvironmentEntry> result;
300 result = testSubject.getEnvironments();
304 public void testIsInMap() throws Exception {
305 OperationalEnvironmentEntry env = new OperationalEnvironmentEntry();
306 env.setEnvironmentId("mock");
307 Map<String, OperationalEnvironmentEntry> mockEnvironments = new HashMap<>();
308 mockEnvironments.put("mock", new OperationalEnvironmentEntry());
312 ReflectionTestUtils.setField(testSubject, "environments", mockEnvironments);
313 result = testSubject.isInMap(env);