2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.distribution.engine;
23 import com.att.nsa.apiClient.credentials.ApiCredential;
24 import com.att.nsa.apiClient.http.HttpException;
25 import com.att.nsa.cambria.client.CambriaClient;
26 import com.att.nsa.cambria.client.CambriaClient.CambriaApiException;
27 import com.att.nsa.cambria.client.CambriaClientBuilders.AbstractAuthenticatedManagerBuilder;
28 import com.att.nsa.cambria.client.CambriaClientBuilders.TopicManagerBuilder;
29 import com.att.nsa.cambria.client.CambriaConsumer;
30 import com.att.nsa.cambria.client.CambriaIdentityManager;
31 import fj.data.Either;
32 import mockit.Deencapsulation;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.Spy;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.openecomp.sdc.be.components.BeConfDependentTest;
42 import org.openecomp.sdc.be.config.ConfigurationManager;
43 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
44 import org.openecomp.sdc.common.api.ConfigurationSource;
45 import org.openecomp.sdc.common.impl.ExternalConfiguration;
46 import org.openecomp.sdc.common.impl.FSConfigurationSource;
48 import java.io.IOException;
49 import java.net.MalformedURLException;
50 import java.security.GeneralSecurityException;
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertTrue;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.Mockito.doReturn;
58 @RunWith(MockitoJUnitRunner.class)
59 public class CambriaHandlerTest extends BeConfDependentTest {
61 private CambriaHandler createTestSubject() {
62 return new CambriaHandler();
66 private CambriaHandler handler = new CambriaHandler();
69 private CambriaIdentityManager createIdentityManager;
71 private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
74 public static void beforeClass() {
75 String appConfigDir = "src/test/resources/config/catalog-be";
76 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
78 new ConfigurationManager(configurationSource);
82 public void startUp() throws MalformedURLException, GeneralSecurityException {
83 doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
87 public void testMockCreateUebKeys() throws HttpException, CambriaApiException, IOException {
88 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
89 .thenReturn(apiCredential);
90 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
91 .createUebKeys(Arrays.asList("Myhost:1234"));
93 Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
95 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
100 public void testMockCreateUebKeys_FAIL() throws HttpException, CambriaApiException, IOException {
101 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
102 .thenThrow(new CambriaApiException("Error Message"));
103 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
104 .createUebKeys(Arrays.asList("Myhost:1234"));
105 Mockito.verify(createIdentityManager, Mockito.never()).setApiCredentials(Mockito.anyString(),
106 Mockito.anyString());
107 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isRight());
108 CambriaErrorResponse response = eitherCreateUebKeys.right().value();
109 assertEquals("Unexpected Operational Status", CambriaOperationStatus.CONNNECTION_ERROR,
110 response.getOperationStatus());
111 assertEquals("Unexpected HTTP Code", 500, response.getHttpCode().intValue());
115 public void testProcessMessageException() throws Exception {
116 CambriaHandler testSubject;
121 testSubject = createTestSubject();
122 result = Deencapsulation.invoke(testSubject, "processMessageException", new Object[] { message });
126 public void testCheckPattern() throws Exception {
127 CambriaHandler testSubject;
128 String patternStr = "";
134 testSubject = createTestSubject();
135 result = Deencapsulation.invoke(testSubject, "checkPattern", new Object[] { patternStr, message, groupIndex });
139 public void testGetTopics() throws Exception {
140 CambriaHandler testSubject;
141 List<String> hostSet = new LinkedList<>();
143 Either<Set<String>, CambriaErrorResponse> result;
146 testSubject = createTestSubject();
147 result = testSubject.getTopics(hostSet);
151 public void testProcessError() throws Exception {
152 CambriaHandler testSubject;
154 CambriaErrorResponse result;
157 testSubject = createTestSubject();
159 e = new Exception("HTTP Status 999");
160 result = Deencapsulation.invoke(testSubject, "processError", e);
162 e = new Exception("HTTP Status 401");
163 result = Deencapsulation.invoke(testSubject, "processError", e);
165 e = new Exception("HTTP Status 409");
166 result = Deencapsulation.invoke(testSubject, "processError", e);
168 e = new Exception("HTTP Status 500");
169 result = Deencapsulation.invoke(testSubject, "processError", e);
171 e = new Exception("mock", new Throwable(new Throwable("mock")));
172 result = Deencapsulation.invoke(testSubject, "processError", e);
176 public void testWriteErrorToLog() throws Exception {
177 CambriaHandler testSubject;
178 CambriaErrorResponse cambriaErrorResponse = new CambriaErrorResponse();
179 cambriaErrorResponse.setOperationStatus(CambriaOperationStatus.AUTHENTICATION_ERROR);
180 String errorMessage = "mock";
181 String methodName = "mock";
182 String operationDesc = "mock";
185 testSubject = createTestSubject();
186 Deencapsulation.invoke(testSubject, "writeErrorToLog", cambriaErrorResponse, "mock", "mock");
190 public void testCreateTopic() throws Exception {
191 CambriaHandler testSubject;
192 Collection<String> hostSet = new LinkedList<>();
194 String apiKey = "mock";
195 String secretKey = "mock";
196 String topicName = "mock";
197 int partitionCount = 0;
198 int replicationCount = 0;
199 CambriaErrorResponse result;
202 testSubject = createTestSubject();
203 result = testSubject.createTopic(hostSet, apiKey, secretKey, topicName, partitionCount, replicationCount);
207 public void testUnRegisterFromTopic() throws Exception {
208 CambriaHandler testSubject;
209 Collection<String> hostSet = new LinkedList<>();
211 String managerApiKey = "mock";
212 String managerSecretKey = "mock";
213 String subscriberApiKey = "mock";
214 String topicName = "mock";
215 CambriaErrorResponse unRegisterFromTopic = null;
218 testSubject = createTestSubject();
219 unRegisterFromTopic = testSubject.unRegisterFromTopic(hostSet, managerApiKey, managerSecretKey,
220 subscriberApiKey, SubscriberTypeEnum.CONSUMER, topicName);
224 public void testRegisterToTopic() throws Exception {
225 CambriaHandler testSubject;
226 Collection<String> hostSet = new LinkedList<String>();
228 String managerApiKey = "mock";
229 String managerSecretKey = "mock";
230 String subscriberApiKey = "mock";
231 SubscriberTypeEnum subscriberTypeEnum = null;
232 String topicName = "mock";
233 CambriaErrorResponse result;
236 testSubject = createTestSubject();
237 result = testSubject.registerToTopic(hostSet, managerApiKey, managerSecretKey, subscriberApiKey,
238 SubscriberTypeEnum.CONSUMER, topicName);
242 public void testCreateConsumer() throws Exception {
243 CambriaHandler testSubject;
244 Collection<String> hostSet = new LinkedList<>();
246 String topicName = "mock";
247 String apiKey = "mock";
248 String secretKey = "mock";
249 String consumerId = "mock";
250 String consumerGroup = "mock";
252 CambriaConsumer result;
255 testSubject = createTestSubject();
256 result = testSubject.createConsumer(hostSet, topicName, apiKey, secretKey, consumerId, consumerGroup,
261 public void testCloseConsumer() throws Exception {
262 CambriaHandler testSubject;
263 CambriaConsumer consumer = null;
266 testSubject = createTestSubject();
268 testSubject.closeConsumer(consumer);
272 public void testFetchFromTopic() throws Exception {
273 CambriaHandler testSubject;
274 CambriaConsumer topicConsumer = null;
275 Either<Iterable<String>, CambriaErrorResponse> result;
278 testSubject = createTestSubject();
279 result = testSubject.fetchFromTopic(topicConsumer);
283 public void testSendNotification() throws Exception {
284 CambriaHandler testSubject;
285 String topicName = "mock";
286 String uebPublicKey = "mock";
287 String uebSecretKey = "mock";
288 List<String> uebServers = new LinkedList<>();
289 uebServers.add("mock");
290 INotificationData data = null;
291 CambriaErrorResponse result;
294 testSubject = createTestSubject();
295 result = testSubject.sendNotification(topicName, uebPublicKey, uebSecretKey, uebServers, data);
299 public void testSendNotificationAndClose() throws Exception {
300 CambriaHandler testSubject;
301 String topicName = "mock";
302 String uebPublicKey = "mock";
303 String uebSecretKey = "mock";
304 List<String> uebServers = new LinkedList<>();
305 uebServers.add("mock");
306 INotificationData data = null;
307 long waitBeforeCloseTimeout = 1;
308 CambriaErrorResponse result;
311 testSubject = createTestSubject();
312 result = testSubject.sendNotificationAndClose(topicName, uebPublicKey, uebSecretKey, uebServers, data,
313 waitBeforeCloseTimeout);
317 public void testGetApiKey() throws Exception {
318 CambriaHandler testSubject;
321 CambriaErrorResponse result;
324 testSubject = createTestSubject();
325 result = testSubject.getApiKey(server, apiKey);
329 public void testCreateUebKeys() throws Exception {
330 CambriaHandler testSubject;
331 List<String> hostSet = null;
332 Either<ApiCredential, CambriaErrorResponse> result;
335 testSubject = createTestSubject();
336 result = testSubject.createUebKeys(hostSet);
340 public void testBuildCambriaClient() throws Exception {
341 CambriaHandler testSubject;
342 AbstractAuthenticatedManagerBuilder<? extends CambriaClient> client = new TopicManagerBuilder()
343 .usingHosts("mock").authenticatedBy("mock", "mock");
346 testSubject = createTestSubject();
347 Deencapsulation.invoke(testSubject, "buildCambriaClient", client);