1 package org.openecomp.sdc.be.components.distribution.engine;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.mockito.ArgumentMatchers.any;
6 import static org.mockito.Mockito.doReturn;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.security.GeneralSecurityException;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.LinkedList;
14 import java.util.List;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.Spy;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.openecomp.sdc.be.components.BeConfDependentTest;
26 import org.openecomp.sdc.be.config.ConfigurationManager;
27 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
28 import org.openecomp.sdc.common.api.ConfigurationSource;
29 import org.openecomp.sdc.common.impl.ExternalConfiguration;
30 import org.openecomp.sdc.common.impl.FSConfigurationSource;
32 import com.att.nsa.apiClient.credentials.ApiCredential;
33 import com.att.nsa.apiClient.http.HttpException;
34 import com.att.nsa.cambria.client.CambriaClient;
35 import com.att.nsa.cambria.client.CambriaClient.CambriaApiException;
36 import com.att.nsa.cambria.client.CambriaClientBuilders.AbstractAuthenticatedManagerBuilder;
37 import com.att.nsa.cambria.client.CambriaClientBuilders.TopicManagerBuilder;
38 import com.att.nsa.cambria.client.CambriaConsumer;
39 import com.att.nsa.cambria.client.CambriaIdentityManager;
41 import fj.data.Either;
42 import mockit.Deencapsulation;
44 @RunWith(MockitoJUnitRunner.class)
45 public class CambriaHandlerTest extends BeConfDependentTest {
47 private CambriaHandler createTestSubject() {
48 return new CambriaHandler();
52 private CambriaHandler handler = new CambriaHandler();
55 private CambriaIdentityManager createIdentityManager;
57 private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
60 public static void beforeClass() {
61 String appConfigDir = "src/test/resources/config/catalog-be";
62 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
64 new ConfigurationManager(configurationSource);
68 public void startUp() throws MalformedURLException, GeneralSecurityException {
69 doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
73 public void testMockCreateUebKeys() throws HttpException, CambriaApiException, IOException {
74 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
75 .thenReturn(apiCredential);
76 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
77 .createUebKeys(Arrays.asList("Myhost:1234"));
79 Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
81 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
86 public void testMockCreateUebKeys_FAIL() throws HttpException, CambriaApiException, IOException {
87 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
88 .thenThrow(new CambriaApiException("Error Message"));
89 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
90 .createUebKeys(Arrays.asList("Myhost:1234"));
91 Mockito.verify(createIdentityManager, Mockito.never()).setApiCredentials(Mockito.anyString(),
93 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isRight());
94 CambriaErrorResponse response = eitherCreateUebKeys.right().value();
95 assertEquals("Unexpected Operational Status", CambriaOperationStatus.CONNNECTION_ERROR,
96 response.getOperationStatus());
97 assertEquals("Unexpected HTTP Code", 500, response.getHttpCode().intValue());
101 public void testProcessMessageException() throws Exception {
102 CambriaHandler testSubject;
107 testSubject = createTestSubject();
108 result = Deencapsulation.invoke(testSubject, "processMessageException", new Object[] { message });
112 public void testCheckPattern() throws Exception {
113 CambriaHandler testSubject;
114 String patternStr = "";
120 testSubject = createTestSubject();
121 result = Deencapsulation.invoke(testSubject, "checkPattern", new Object[] { patternStr, message, groupIndex });
125 public void testGetTopics() throws Exception {
126 CambriaHandler testSubject;
127 List<String> hostSet = new LinkedList<>();
129 Either<Set<String>, CambriaErrorResponse> result;
132 testSubject = createTestSubject();
133 result = testSubject.getTopics(hostSet);
137 public void testProcessError() throws Exception {
138 CambriaHandler testSubject;
140 CambriaErrorResponse result;
143 testSubject = createTestSubject();
145 e = new Exception("HTTP Status 999");
146 result = Deencapsulation.invoke(testSubject, "processError", e);
148 e = new Exception("HTTP Status 401");
149 result = Deencapsulation.invoke(testSubject, "processError", e);
151 e = new Exception("HTTP Status 409");
152 result = Deencapsulation.invoke(testSubject, "processError", e);
154 e = new Exception("HTTP Status 500");
155 result = Deencapsulation.invoke(testSubject, "processError", e);
157 e = new Exception("mock", new Throwable(new Throwable("mock")));
158 result = Deencapsulation.invoke(testSubject, "processError", e);
162 public void testWriteErrorToLog() throws Exception {
163 CambriaHandler testSubject;
164 CambriaErrorResponse cambriaErrorResponse = new CambriaErrorResponse();
165 cambriaErrorResponse.setOperationStatus(CambriaOperationStatus.AUTHENTICATION_ERROR);
166 String errorMessage = "mock";
167 String methodName = "mock";
168 String operationDesc = "mock";
171 testSubject = createTestSubject();
172 Deencapsulation.invoke(testSubject, "writeErrorToLog", cambriaErrorResponse, "mock", "mock", "mock");
176 public void testCreateTopic() throws Exception {
177 CambriaHandler testSubject;
178 Collection<String> hostSet = new LinkedList<>();
180 String apiKey = "mock";
181 String secretKey = "mock";
182 String topicName = "mock";
183 int partitionCount = 0;
184 int replicationCount = 0;
185 CambriaErrorResponse result;
188 testSubject = createTestSubject();
189 result = testSubject.createTopic(hostSet, apiKey, secretKey, topicName, partitionCount, replicationCount);
193 public void testUnRegisterFromTopic() throws Exception {
194 CambriaHandler testSubject;
195 Collection<String> hostSet = new LinkedList<>();
197 String managerApiKey = "mock";
198 String managerSecretKey = "mock";
199 String subscriberApiKey = "mock";
200 String topicName = "mock";
201 CambriaErrorResponse unRegisterFromTopic = null;
204 testSubject = createTestSubject();
205 unRegisterFromTopic = testSubject.unRegisterFromTopic(hostSet, managerApiKey, managerSecretKey,
206 subscriberApiKey, SubscriberTypeEnum.CONSUMER, topicName);
210 public void testRegisterToTopic() throws Exception {
211 CambriaHandler testSubject;
212 Collection<String> hostSet = new LinkedList<String>();
214 String managerApiKey = "mock";
215 String managerSecretKey = "mock";
216 String subscriberApiKey = "mock";
217 SubscriberTypeEnum subscriberTypeEnum = null;
218 String topicName = "mock";
219 CambriaErrorResponse result;
222 testSubject = createTestSubject();
223 result = testSubject.registerToTopic(hostSet, managerApiKey, managerSecretKey, subscriberApiKey,
224 SubscriberTypeEnum.CONSUMER, topicName);
228 public void testCreateConsumer() throws Exception {
229 CambriaHandler testSubject;
230 Collection<String> hostSet = new LinkedList<>();
232 String topicName = "mock";
233 String apiKey = "mock";
234 String secretKey = "mock";
235 String consumerId = "mock";
236 String consumerGroup = "mock";
238 CambriaConsumer result;
241 testSubject = createTestSubject();
242 result = testSubject.createConsumer(hostSet, topicName, apiKey, secretKey, consumerId, consumerGroup,
247 public void testCloseConsumer() throws Exception {
248 CambriaHandler testSubject;
249 CambriaConsumer consumer = null;
252 testSubject = createTestSubject();
254 testSubject.closeConsumer(consumer);
258 public void testFetchFromTopic() throws Exception {
259 CambriaHandler testSubject;
260 CambriaConsumer topicConsumer = null;
261 Either<Iterable<String>, CambriaErrorResponse> result;
264 testSubject = createTestSubject();
265 result = testSubject.fetchFromTopic(topicConsumer);
269 public void testSendNotification() throws Exception {
270 CambriaHandler testSubject;
271 String topicName = "mock";
272 String uebPublicKey = "mock";
273 String uebSecretKey = "mock";
274 List<String> uebServers = new LinkedList<>();
275 uebServers.add("mock");
276 INotificationData data = null;
277 CambriaErrorResponse result;
280 testSubject = createTestSubject();
281 result = testSubject.sendNotification(topicName, uebPublicKey, uebSecretKey, uebServers, data);
285 public void testSendNotificationAndClose() throws Exception {
286 CambriaHandler testSubject;
287 String topicName = "mock";
288 String uebPublicKey = "mock";
289 String uebSecretKey = "mock";
290 List<String> uebServers = new LinkedList<>();
291 uebServers.add("mock");
292 INotificationData data = null;
293 long waitBeforeCloseTimeout = 1;
294 CambriaErrorResponse result;
297 testSubject = createTestSubject();
298 result = testSubject.sendNotificationAndClose(topicName, uebPublicKey, uebSecretKey, uebServers, data,
299 waitBeforeCloseTimeout);
303 public void testGetApiKey() throws Exception {
304 CambriaHandler testSubject;
307 CambriaErrorResponse result;
310 testSubject = createTestSubject();
311 result = testSubject.getApiKey(server, apiKey);
315 public void testCreateUebKeys() throws Exception {
316 CambriaHandler testSubject;
317 List<String> hostSet = null;
318 Either<ApiCredential, CambriaErrorResponse> result;
321 testSubject = createTestSubject();
322 result = testSubject.createUebKeys(hostSet);
326 public void testBuildCambriaClient() throws Exception {
327 CambriaHandler testSubject;
328 AbstractAuthenticatedManagerBuilder<? extends CambriaClient> client = new TopicManagerBuilder()
329 .usingHosts("mock").authenticatedBy("mock", "mock");
332 testSubject = createTestSubject();
333 Deencapsulation.invoke(testSubject, "buildCambriaClient", client);