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