[SDC] Add kafka native messaging
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / distribution / engine / CambriaHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.distribution.engine;
22
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;
27
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;
44 import java.util.Set;
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;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class CambriaHandlerTest extends BeConfDependentTest {
63
64         private CambriaHandler createTestSubject() {
65                 return new CambriaHandler();
66         }
67
68         @Spy
69         private CambriaHandler handler = new CambriaHandler();
70
71         @Mock
72         private CambriaIdentityManager createIdentityManager;
73
74         private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
75
76         @BeforeClass
77         public static void beforeClass() {
78                 String appConfigDir = "src/test/resources/config/catalog-be";
79                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
80                                 appConfigDir);
81                 new ConfigurationManager(configurationSource);
82         }
83
84         @Before
85         public void startUp() throws MalformedURLException, GeneralSecurityException {
86                 doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
87         }
88
89         @Test
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"));
95
96                 Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
97
98                 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
99
100         }
101
102         @Test
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());
115         }
116
117         @Test
118         public void testProcessMessageException() throws Exception {
119                 CambriaHandler testSubject;
120                 String message = "";
121                 Integer result;
122
123                 // default test
124                 testSubject = createTestSubject();
125                 result = Deencapsulation.invoke(testSubject, "processMessageException", new Object[] { message });
126         }
127
128         @Test
129         public void testCheckPattern() throws Exception {
130                 CambriaHandler testSubject;
131                 String patternStr = "";
132                 String message = "";
133                 int groupIndex = 0;
134                 Integer result;
135
136                 // default test
137                 testSubject = createTestSubject();
138                 result = Deencapsulation.invoke(testSubject, "checkPattern", new Object[] { patternStr, message, groupIndex });
139         }
140
141         @Test
142         public void testGetTopics() throws Exception {
143
144                 CambriaHandler testSubject;
145                 List<String> hostSet = new LinkedList<>();
146                 hostSet.add("mock");
147                 Either<Set<String>, CambriaErrorResponse> result;
148
149                 // default test
150                 testSubject = createTestSubject();
151                 result = testSubject.getTopics(hostSet);
152         }
153
154         @Test
155         public void testProcessError() throws Exception {
156                 CambriaHandler testSubject;
157                 Exception e = null;
158                 CambriaErrorResponse result;
159
160                 // default test
161                 testSubject = createTestSubject();
162
163                 e = new Exception("HTTP Status 999");
164                 result = Deencapsulation.invoke(testSubject, "processError", e);
165
166                 e = new Exception("HTTP Status 401");
167                 result = Deencapsulation.invoke(testSubject, "processError", e);
168
169                 e = new Exception("HTTP Status 409");
170                 result = Deencapsulation.invoke(testSubject, "processError", e);
171
172                 e = new Exception("HTTP Status 500");
173                 result = Deencapsulation.invoke(testSubject, "processError", e);
174
175                 e = new Exception("mock", new Throwable(new Throwable("mock")));
176                 result = Deencapsulation.invoke(testSubject, "processError", e);
177         }
178
179         @Test
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";
187
188                 // default test
189                 testSubject = createTestSubject();
190                 Deencapsulation.invoke(testSubject, "writeErrorToLog", cambriaErrorResponse, "mock", "mock");
191         }
192
193         @Test
194         public void testCreateTopic() throws Exception {
195                 CambriaHandler testSubject;
196                 Collection<String> hostSet = new LinkedList<>();
197                 hostSet.add("mock");
198                 String apiKey = "mock";
199                 String secretKey = "mock";
200                 String topicName = "mock";
201                 int partitionCount = 0;
202                 int replicationCount = 0;
203                 CambriaErrorResponse result;
204
205                 // default test
206                 testSubject = createTestSubject();
207                 result = testSubject.createTopic(hostSet, apiKey, secretKey, topicName, partitionCount, replicationCount);
208         }
209
210         @Test
211         public void testUnRegisterFromTopic() throws Exception {
212                 CambriaHandler testSubject;
213                 Collection<String> hostSet = new LinkedList<>();
214                 hostSet.add("mock");
215                 String managerApiKey = "mock";
216                 String managerSecretKey = "mock";
217                 String subscriberApiKey = "mock";
218                 String topicName = "mock";
219                 CambriaErrorResponse unRegisterFromTopic = null;
220
221                 // default test
222                 testSubject = createTestSubject();
223                 unRegisterFromTopic = testSubject.unRegisterFromTopic(hostSet, managerApiKey, managerSecretKey,
224                                 subscriberApiKey, SubscriberTypeEnum.CONSUMER, topicName);
225         }
226
227         @Test
228         public void testRegisterToTopic() throws Exception {
229                 CambriaHandler testSubject;
230                 Collection<String> hostSet = new LinkedList<String>();
231                 hostSet.add("mock");
232                 String managerApiKey = "mock";
233                 String managerSecretKey = "mock";
234                 String subscriberApiKey = "mock";
235                 SubscriberTypeEnum subscriberTypeEnum = null;
236                 String topicName = "mock";
237                 CambriaErrorResponse result;
238
239                 // default test
240                 testSubject = createTestSubject();
241                 result = testSubject.registerToTopic(hostSet, managerApiKey, managerSecretKey, subscriberApiKey,
242                                 SubscriberTypeEnum.CONSUMER, topicName);
243         }
244
245         @Test
246         public void testCreateConsumer() throws Exception {
247                 CambriaHandler testSubject;
248                 Collection<String> hostSet = new LinkedList<>();
249                 hostSet.add("mock");
250                 String topicName = "mock";
251                 String apiKey = "mock";
252                 String secretKey = "mock";
253                 String consumerId = "mock";
254                 String consumerGroup = "mock";
255                 int timeoutMS = 0;
256                 CambriaConsumer result;
257
258                 // default test
259                 testSubject = createTestSubject();
260                 result = testSubject.createConsumer(hostSet, topicName, apiKey, secretKey, consumerId, consumerGroup,
261                                 timeoutMS);
262         }
263
264         @Test
265         public void testCloseConsumer() throws Exception {
266                 CambriaHandler testSubject;
267                 CambriaConsumer consumer = null;
268
269                 // test 1
270                 testSubject = createTestSubject();
271                 consumer = null;
272                 testSubject.closeConsumer(consumer);
273         }
274
275         @Test
276         public void testFetchFromTopic() throws Exception {
277                 CambriaHandler testSubject;
278                 CambriaConsumer topicConsumer = null;
279                 Either<Iterable<String>, CambriaErrorResponse> result;
280
281                 // default test
282                 testSubject = createTestSubject();
283                 result = testSubject.fetchFromTopic(topicConsumer);
284         }
285
286         @Test
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;
296
297                 // default test
298                 testSubject = createTestSubject();
299                 result = testSubject.sendNotification(topicName, uebPublicKey, uebSecretKey, uebServers, data);
300         }
301
302         @Test
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;
313
314                 // default test
315                 testSubject = createTestSubject();
316                 result = testSubject.sendNotificationAndClose(topicName, uebPublicKey, uebSecretKey, uebServers, data,
317                                 waitBeforeCloseTimeout);
318         }
319
320         @Test
321         public void testGetApiKey() throws Exception {
322                 CambriaHandler testSubject;
323                 String server = "";
324                 String apiKey = "";
325                 CambriaErrorResponse result;
326
327                 // default test
328                 testSubject = createTestSubject();
329                 result = testSubject.getApiKey(server, apiKey);
330         }
331
332         @Test
333         public void testCreateUebKeys() throws Exception {
334                 CambriaHandler testSubject;
335                 List<String> hostSet = null;
336                 Either<ApiCredential, CambriaErrorResponse> result;
337
338                 // default test
339                 testSubject = createTestSubject();
340                 result = testSubject.createUebKeys(hostSet);
341         }
342
343         @Test
344         public void testBuildCambriaClient() throws Exception {
345                 CambriaHandler testSubject;
346                 AbstractAuthenticatedManagerBuilder<? extends CambriaClient> client = new TopicManagerBuilder()
347                                 .usingHosts("mock").authenticatedBy("mock", "mock");
348
349                 // default test
350                 testSubject = createTestSubject();
351                 Deencapsulation.invoke(testSubject, "buildCambriaClient", client);
352         }
353
354 }