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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doReturn;
28 import com.att.nsa.apiClient.credentials.ApiCredential;
29 import com.att.nsa.apiClient.http.HttpException;
30 import com.att.nsa.cambria.client.CambriaClient;
31 import com.att.nsa.cambria.client.CambriaClient.CambriaApiException;
32 import com.att.nsa.cambria.client.CambriaClientBuilders.AbstractAuthenticatedManagerBuilder;
33 import com.att.nsa.cambria.client.CambriaClientBuilders.TopicManagerBuilder;
34 import com.att.nsa.cambria.client.CambriaConsumer;
35 import com.att.nsa.cambria.client.CambriaIdentityManager;
36 import fj.data.Either;
37 import java.io.IOException;
38 import java.net.MalformedURLException;
39 import java.security.GeneralSecurityException;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.LinkedList;
43 import java.util.List;
45 import mockit.Deencapsulation;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.Spy;
53 import org.mockito.junit.MockitoJUnitRunner;
54 import org.openecomp.sdc.be.components.BeConfDependentTest;
55 import org.openecomp.sdc.be.config.ConfigurationManager;
56 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
57 import org.openecomp.sdc.common.api.ConfigurationSource;
58 import org.openecomp.sdc.common.impl.ExternalConfiguration;
59 import org.openecomp.sdc.common.impl.FSConfigurationSource;
61 @RunWith(MockitoJUnitRunner.class)
62 public class CambriaHandlerTest extends BeConfDependentTest {
64 private CambriaHandler createTestSubject() {
65 return new CambriaHandler();
69 private CambriaHandler handler = new CambriaHandler();
72 private CambriaIdentityManager createIdentityManager;
74 private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
77 public static void beforeClass() {
78 String appConfigDir = "src/test/resources/config/catalog-be";
79 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
81 new ConfigurationManager(configurationSource);
85 public void startUp() throws MalformedURLException, GeneralSecurityException {
86 doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
90 public void testMockCreateUebKeys() throws HttpException, CambriaApiException, IOException {
91 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
92 .thenReturn(apiCredential);
93 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
94 .createUebKeys(Arrays.asList("Myhost:1234"));
96 Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
98 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
103 public void testMockCreateUebKeys_FAIL() throws HttpException, CambriaApiException, IOException {
104 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
105 .thenThrow(new CambriaApiException("Error Message"));
106 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
107 .createUebKeys(Arrays.asList("Myhost:1234"));
108 Mockito.verify(createIdentityManager, Mockito.never()).setApiCredentials(Mockito.anyString(),
109 Mockito.anyString());
110 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isRight());
111 CambriaErrorResponse response = eitherCreateUebKeys.right().value();
112 assertEquals("Unexpected Operational Status", CambriaOperationStatus.CONNNECTION_ERROR,
113 response.getOperationStatus());
114 assertEquals("Unexpected HTTP Code", 500, response.getHttpCode().intValue());
118 public void testProcessMessageException() throws Exception {
119 CambriaHandler testSubject;
124 testSubject = createTestSubject();
125 result = Deencapsulation.invoke(testSubject, "processMessageException", new Object[] { message });
129 public void testCheckPattern() throws Exception {
130 CambriaHandler testSubject;
131 String patternStr = "";
137 testSubject = createTestSubject();
138 result = Deencapsulation.invoke(testSubject, "checkPattern", new Object[] { patternStr, message, groupIndex });
142 public void testGetTopics() throws Exception {
144 CambriaHandler testSubject;
145 List<String> hostSet = new LinkedList<>();
147 Either<Set<String>, CambriaErrorResponse> result;
150 testSubject = createTestSubject();
151 result = testSubject.getTopics(hostSet);
155 public void testProcessError() throws Exception {
156 CambriaHandler testSubject;
158 CambriaErrorResponse result;
161 testSubject = createTestSubject();
163 e = new Exception("HTTP Status 999");
164 result = Deencapsulation.invoke(testSubject, "processError", e);
166 e = new Exception("HTTP Status 401");
167 result = Deencapsulation.invoke(testSubject, "processError", e);
169 e = new Exception("HTTP Status 409");
170 result = Deencapsulation.invoke(testSubject, "processError", e);
172 e = new Exception("HTTP Status 500");
173 result = Deencapsulation.invoke(testSubject, "processError", e);
175 e = new Exception("mock", new Throwable(new Throwable("mock")));
176 result = Deencapsulation.invoke(testSubject, "processError", e);
180 public void testWriteErrorToLog() throws Exception {
181 CambriaHandler testSubject;
182 CambriaErrorResponse cambriaErrorResponse = new CambriaErrorResponse();
183 cambriaErrorResponse.setOperationStatus(CambriaOperationStatus.AUTHENTICATION_ERROR);
184 String errorMessage = "mock";
185 String methodName = "mock";
186 String operationDesc = "mock";
189 testSubject = createTestSubject();
190 Deencapsulation.invoke(testSubject, "writeErrorToLog", cambriaErrorResponse, "mock", "mock");
194 public void testCreateTopic() throws Exception {
195 CambriaHandler testSubject;
196 Collection<String> hostSet = new LinkedList<>();
198 String apiKey = "mock";
199 String secretKey = "mock";
200 String topicName = "mock";
201 int partitionCount = 0;
202 int replicationCount = 0;
203 CambriaErrorResponse result;
206 testSubject = createTestSubject();
207 result = testSubject.createTopic(hostSet, apiKey, secretKey, topicName, partitionCount, replicationCount);
211 public void testUnRegisterFromTopic() throws Exception {
212 CambriaHandler testSubject;
213 Collection<String> hostSet = new LinkedList<>();
215 String managerApiKey = "mock";
216 String managerSecretKey = "mock";
217 String subscriberApiKey = "mock";
218 String topicName = "mock";
219 CambriaErrorResponse unRegisterFromTopic = null;
222 testSubject = createTestSubject();
223 unRegisterFromTopic = testSubject.unRegisterFromTopic(hostSet, managerApiKey, managerSecretKey,
224 subscriberApiKey, SubscriberTypeEnum.CONSUMER, topicName);
228 public void testRegisterToTopic() throws Exception {
229 CambriaHandler testSubject;
230 Collection<String> hostSet = new LinkedList<String>();
232 String managerApiKey = "mock";
233 String managerSecretKey = "mock";
234 String subscriberApiKey = "mock";
235 SubscriberTypeEnum subscriberTypeEnum = null;
236 String topicName = "mock";
237 CambriaErrorResponse result;
240 testSubject = createTestSubject();
241 result = testSubject.registerToTopic(hostSet, managerApiKey, managerSecretKey, subscriberApiKey,
242 SubscriberTypeEnum.CONSUMER, topicName);
246 public void testCreateConsumer() throws Exception {
247 CambriaHandler testSubject;
248 Collection<String> hostSet = new LinkedList<>();
250 String topicName = "mock";
251 String apiKey = "mock";
252 String secretKey = "mock";
253 String consumerId = "mock";
254 String consumerGroup = "mock";
256 CambriaConsumer result;
259 testSubject = createTestSubject();
260 result = testSubject.createConsumer(hostSet, topicName, apiKey, secretKey, consumerId, consumerGroup,
265 public void testCloseConsumer() throws Exception {
266 CambriaHandler testSubject;
267 CambriaConsumer consumer = null;
270 testSubject = createTestSubject();
272 testSubject.closeConsumer(consumer);
276 public void testFetchFromTopic() throws Exception {
277 CambriaHandler testSubject;
278 CambriaConsumer topicConsumer = null;
279 Either<Iterable<String>, CambriaErrorResponse> result;
282 testSubject = createTestSubject();
283 result = testSubject.fetchFromTopic(topicConsumer);
287 public void testSendNotification() throws Exception {
288 CambriaHandler testSubject;
289 String topicName = "mock";
290 String uebPublicKey = "mock";
291 String uebSecretKey = "mock";
292 List<String> uebServers = new LinkedList<>();
293 uebServers.add("mock");
294 INotificationData data = null;
295 CambriaErrorResponse result;
298 testSubject = createTestSubject();
299 result = testSubject.sendNotification(topicName, uebPublicKey, uebSecretKey, uebServers, data);
303 public void testSendNotificationAndClose() throws Exception {
304 CambriaHandler testSubject;
305 String topicName = "mock";
306 String uebPublicKey = "mock";
307 String uebSecretKey = "mock";
308 List<String> uebServers = new LinkedList<>();
309 uebServers.add("mock");
310 INotificationData data = null;
311 long waitBeforeCloseTimeout = 1;
312 CambriaErrorResponse result;
315 testSubject = createTestSubject();
316 result = testSubject.sendNotificationAndClose(topicName, uebPublicKey, uebSecretKey, uebServers, data,
317 waitBeforeCloseTimeout);
321 public void testGetApiKey() throws Exception {
322 CambriaHandler testSubject;
325 CambriaErrorResponse result;
328 testSubject = createTestSubject();
329 result = testSubject.getApiKey(server, apiKey);
333 public void testCreateUebKeys() throws Exception {
334 CambriaHandler testSubject;
335 List<String> hostSet = null;
336 Either<ApiCredential, CambriaErrorResponse> result;
339 testSubject = createTestSubject();
340 result = testSubject.createUebKeys(hostSet);
344 public void testBuildCambriaClient() throws Exception {
345 CambriaHandler testSubject;
346 AbstractAuthenticatedManagerBuilder<? extends CambriaClient> client = new TopicManagerBuilder()
347 .usingHosts("mock").authenticatedBy("mock", "mock");
350 testSubject = createTestSubject();
351 Deencapsulation.invoke(testSubject, "buildCambriaClient", client);