1 package org.openecomp.sdc.be.components.distribution.engine;
3 import com.att.nsa.apiClient.credentials.ApiCredential;
4 import com.att.nsa.apiClient.http.HttpException;
5 import com.att.nsa.cambria.client.CambriaClient;
6 import com.att.nsa.cambria.client.CambriaClient.CambriaApiException;
7 import com.att.nsa.cambria.client.CambriaClientBuilders.AbstractAuthenticatedManagerBuilder;
8 import com.att.nsa.cambria.client.CambriaClientBuilders.TopicManagerBuilder;
9 import com.att.nsa.cambria.client.CambriaConsumer;
10 import com.att.nsa.cambria.client.CambriaIdentityManager;
11 import fj.data.Either;
12 import mockit.Deencapsulation;
13 import org.junit.Before;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.Spy;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.openecomp.sdc.be.components.BeConfDependentTest;
22 import org.openecomp.sdc.be.config.ConfigurationManager;
23 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
24 import org.openecomp.sdc.common.api.ConfigurationSource;
25 import org.openecomp.sdc.common.impl.ExternalConfiguration;
26 import org.openecomp.sdc.common.impl.FSConfigurationSource;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
30 import java.security.GeneralSecurityException;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.doReturn;
38 @RunWith(MockitoJUnitRunner.class)
39 public class CambriaHandlerTest extends BeConfDependentTest {
41 private CambriaHandler createTestSubject() {
42 return new CambriaHandler();
46 private CambriaHandler handler = new CambriaHandler();
49 private CambriaIdentityManager createIdentityManager;
51 private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
54 public static void beforeClass() {
55 String appConfigDir = "src/test/resources/config/catalog-be";
56 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
58 new ConfigurationManager(configurationSource);
62 public void startUp() throws MalformedURLException, GeneralSecurityException {
63 doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
67 public void testMockCreateUebKeys() throws HttpException, CambriaApiException, IOException {
68 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
69 .thenReturn(apiCredential);
70 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
71 .createUebKeys(Arrays.asList("Myhost:1234"));
73 Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
75 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
80 public void testMockCreateUebKeys_FAIL() throws HttpException, CambriaApiException, IOException {
81 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
82 .thenThrow(new CambriaApiException("Error Message"));
83 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
84 .createUebKeys(Arrays.asList("Myhost:1234"));
85 Mockito.verify(createIdentityManager, Mockito.never()).setApiCredentials(Mockito.anyString(),
87 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isRight());
88 CambriaErrorResponse response = eitherCreateUebKeys.right().value();
89 assertEquals("Unexpected Operational Status", CambriaOperationStatus.CONNNECTION_ERROR,
90 response.getOperationStatus());
91 assertEquals("Unexpected HTTP Code", 500, response.getHttpCode().intValue());
95 public void testProcessMessageException() throws Exception {
96 CambriaHandler testSubject;
101 testSubject = createTestSubject();
102 result = Deencapsulation.invoke(testSubject, "processMessageException", new Object[] { message });
106 public void testCheckPattern() throws Exception {
107 CambriaHandler testSubject;
108 String patternStr = "";
114 testSubject = createTestSubject();
115 result = Deencapsulation.invoke(testSubject, "checkPattern", new Object[] { patternStr, message, groupIndex });
119 public void testGetTopics() throws Exception {
120 CambriaHandler testSubject;
121 List<String> hostSet = new LinkedList<>();
123 Either<Set<String>, CambriaErrorResponse> result;
126 testSubject = createTestSubject();
127 result = testSubject.getTopics(hostSet);
131 public void testProcessError() throws Exception {
132 CambriaHandler testSubject;
134 CambriaErrorResponse result;
137 testSubject = createTestSubject();
139 e = new Exception("HTTP Status 999");
140 result = Deencapsulation.invoke(testSubject, "processError", e);
142 e = new Exception("HTTP Status 401");
143 result = Deencapsulation.invoke(testSubject, "processError", e);
145 e = new Exception("HTTP Status 409");
146 result = Deencapsulation.invoke(testSubject, "processError", e);
148 e = new Exception("HTTP Status 500");
149 result = Deencapsulation.invoke(testSubject, "processError", e);
151 e = new Exception("mock", new Throwable(new Throwable("mock")));
152 result = Deencapsulation.invoke(testSubject, "processError", e);
156 public void testWriteErrorToLog() throws Exception {
157 CambriaHandler testSubject;
158 CambriaErrorResponse cambriaErrorResponse = new CambriaErrorResponse();
159 cambriaErrorResponse.setOperationStatus(CambriaOperationStatus.AUTHENTICATION_ERROR);
160 String errorMessage = "mock";
161 String methodName = "mock";
162 String operationDesc = "mock";
165 testSubject = createTestSubject();
166 Deencapsulation.invoke(testSubject, "writeErrorToLog", cambriaErrorResponse, "mock", "mock");
170 public void testCreateTopic() throws Exception {
171 CambriaHandler testSubject;
172 Collection<String> hostSet = new LinkedList<>();
174 String apiKey = "mock";
175 String secretKey = "mock";
176 String topicName = "mock";
177 int partitionCount = 0;
178 int replicationCount = 0;
179 CambriaErrorResponse result;
182 testSubject = createTestSubject();
183 result = testSubject.createTopic(hostSet, apiKey, secretKey, topicName, partitionCount, replicationCount);
187 public void testUnRegisterFromTopic() throws Exception {
188 CambriaHandler testSubject;
189 Collection<String> hostSet = new LinkedList<>();
191 String managerApiKey = "mock";
192 String managerSecretKey = "mock";
193 String subscriberApiKey = "mock";
194 String topicName = "mock";
195 CambriaErrorResponse unRegisterFromTopic = null;
198 testSubject = createTestSubject();
199 unRegisterFromTopic = testSubject.unRegisterFromTopic(hostSet, managerApiKey, managerSecretKey,
200 subscriberApiKey, SubscriberTypeEnum.CONSUMER, topicName);
204 public void testRegisterToTopic() throws Exception {
205 CambriaHandler testSubject;
206 Collection<String> hostSet = new LinkedList<String>();
208 String managerApiKey = "mock";
209 String managerSecretKey = "mock";
210 String subscriberApiKey = "mock";
211 SubscriberTypeEnum subscriberTypeEnum = null;
212 String topicName = "mock";
213 CambriaErrorResponse result;
216 testSubject = createTestSubject();
217 result = testSubject.registerToTopic(hostSet, managerApiKey, managerSecretKey, subscriberApiKey,
218 SubscriberTypeEnum.CONSUMER, topicName);
222 public void testCreateConsumer() throws Exception {
223 CambriaHandler testSubject;
224 Collection<String> hostSet = new LinkedList<>();
226 String topicName = "mock";
227 String apiKey = "mock";
228 String secretKey = "mock";
229 String consumerId = "mock";
230 String consumerGroup = "mock";
232 CambriaConsumer result;
235 testSubject = createTestSubject();
236 result = testSubject.createConsumer(hostSet, topicName, apiKey, secretKey, consumerId, consumerGroup,
241 public void testCloseConsumer() throws Exception {
242 CambriaHandler testSubject;
243 CambriaConsumer consumer = null;
246 testSubject = createTestSubject();
248 testSubject.closeConsumer(consumer);
252 public void testFetchFromTopic() throws Exception {
253 CambriaHandler testSubject;
254 CambriaConsumer topicConsumer = null;
255 Either<Iterable<String>, CambriaErrorResponse> result;
258 testSubject = createTestSubject();
259 result = testSubject.fetchFromTopic(topicConsumer);
263 public void testSendNotification() throws Exception {
264 CambriaHandler testSubject;
265 String topicName = "mock";
266 String uebPublicKey = "mock";
267 String uebSecretKey = "mock";
268 List<String> uebServers = new LinkedList<>();
269 uebServers.add("mock");
270 INotificationData data = null;
271 CambriaErrorResponse result;
274 testSubject = createTestSubject();
275 result = testSubject.sendNotification(topicName, uebPublicKey, uebSecretKey, uebServers, data);
279 public void testSendNotificationAndClose() throws Exception {
280 CambriaHandler testSubject;
281 String topicName = "mock";
282 String uebPublicKey = "mock";
283 String uebSecretKey = "mock";
284 List<String> uebServers = new LinkedList<>();
285 uebServers.add("mock");
286 INotificationData data = null;
287 long waitBeforeCloseTimeout = 1;
288 CambriaErrorResponse result;
291 testSubject = createTestSubject();
292 result = testSubject.sendNotificationAndClose(topicName, uebPublicKey, uebSecretKey, uebServers, data,
293 waitBeforeCloseTimeout);
297 public void testGetApiKey() throws Exception {
298 CambriaHandler testSubject;
301 CambriaErrorResponse result;
304 testSubject = createTestSubject();
305 result = testSubject.getApiKey(server, apiKey);
309 public void testCreateUebKeys() throws Exception {
310 CambriaHandler testSubject;
311 List<String> hostSet = null;
312 Either<ApiCredential, CambriaErrorResponse> result;
315 testSubject = createTestSubject();
316 result = testSubject.createUebKeys(hostSet);
320 public void testBuildCambriaClient() throws Exception {
321 CambriaHandler testSubject;
322 AbstractAuthenticatedManagerBuilder<? extends CambriaClient> client = new TopicManagerBuilder()
323 .usingHosts("mock").authenticatedBy("mock", "mock");
326 testSubject = createTestSubject();
327 Deencapsulation.invoke(testSubject, "buildCambriaClient", client);