Catalog alignment
[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 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;
47
48 import java.io.IOException;
49 import java.net.MalformedURLException;
50 import java.security.GeneralSecurityException;
51 import java.util.Arrays;
52 import java.util.Collection;
53 import java.util.LinkedList;
54 import java.util.List;
55 import java.util.Set;
56
57 import static org.junit.Assert.assertEquals;
58 import static org.junit.Assert.assertTrue;
59 import static org.mockito.ArgumentMatchers.any;
60 import static org.mockito.Mockito.doReturn;
61
62 @RunWith(MockitoJUnitRunner.class)
63 public class CambriaHandlerTest extends BeConfDependentTest {
64
65         private CambriaHandler createTestSubject() {
66                 return new CambriaHandler();
67         }
68
69         @Spy
70         private CambriaHandler handler = new CambriaHandler();
71
72         @Mock
73         private CambriaIdentityManager createIdentityManager;
74
75         private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
76
77         @BeforeClass
78         public static void beforeClass() {
79                 String appConfigDir = "src/test/resources/config/catalog-be";
80                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
81                                 appConfigDir);
82                 new ConfigurationManager(configurationSource);
83         }
84
85         @Before
86         public void startUp() throws MalformedURLException, GeneralSecurityException {
87                 doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
88         }
89
90         @Test
91         public void testMockCreateUebKeys() throws HttpException, CambriaApiException, IOException {
92                 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
93                                 .thenReturn(apiCredential);
94                 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
95                                 .createUebKeys(Arrays.asList("Myhost:1234"));
96
97                 Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
98
99                 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
100
101         }
102
103         @Test
104         public void testMockCreateUebKeys_FAIL() throws HttpException, CambriaApiException, IOException {
105                 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
106                                 .thenThrow(new CambriaApiException("Error Message"));
107                 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
108                                 .createUebKeys(Arrays.asList("Myhost:1234"));
109                 Mockito.verify(createIdentityManager, Mockito.never()).setApiCredentials(Mockito.anyString(),
110                                 Mockito.anyString());
111                 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isRight());
112                 CambriaErrorResponse response = eitherCreateUebKeys.right().value();
113                 assertEquals("Unexpected Operational Status", CambriaOperationStatus.CONNNECTION_ERROR,
114                                 response.getOperationStatus());
115                 assertEquals("Unexpected HTTP Code", 500, response.getHttpCode().intValue());
116         }
117
118         @Test
119         public void testProcessMessageException() throws Exception {
120                 CambriaHandler testSubject;
121                 String message = "";
122                 Integer result;
123
124                 // default test
125                 testSubject = createTestSubject();
126                 result = Deencapsulation.invoke(testSubject, "processMessageException", new Object[] { message });
127         }
128
129         @Test
130         public void testCheckPattern() throws Exception {
131                 CambriaHandler testSubject;
132                 String patternStr = "";
133                 String message = "";
134                 int groupIndex = 0;
135                 Integer result;
136
137                 // default test
138                 testSubject = createTestSubject();
139                 result = Deencapsulation.invoke(testSubject, "checkPattern", new Object[] { patternStr, message, groupIndex });
140         }
141
142         @Test
143         public void testGetTopics() throws Exception {
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 }