re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / distribution / engine / CambriaHandlerTest.java
1 package org.openecomp.sdc.be.components.distribution.engine;
2
3 import com.att.nsa.apiClient.credentials.ApiCredential;
4 import com.att.nsa.apiClient.http.HttpException;
5 import com.att.nsa.cambria.client.CambriaClient;
6 import com.att.nsa.cambria.client.CambriaClient.CambriaApiException;
7 import com.att.nsa.cambria.client.CambriaClientBuilders.AbstractAuthenticatedManagerBuilder;
8 import com.att.nsa.cambria.client.CambriaClientBuilders.TopicManagerBuilder;
9 import com.att.nsa.cambria.client.CambriaConsumer;
10 import com.att.nsa.cambria.client.CambriaIdentityManager;
11 import fj.data.Either;
12 import mockit.Deencapsulation;
13 import org.junit.Before;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.Spy;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.openecomp.sdc.be.components.BeConfDependentTest;
22 import org.openecomp.sdc.be.config.ConfigurationManager;
23 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
24 import org.openecomp.sdc.common.api.ConfigurationSource;
25 import org.openecomp.sdc.common.impl.ExternalConfiguration;
26 import org.openecomp.sdc.common.impl.FSConfigurationSource;
27
28 import java.io.IOException;
29 import java.net.MalformedURLException;
30 import java.security.GeneralSecurityException;
31 import java.util.*;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.doReturn;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class CambriaHandlerTest extends BeConfDependentTest {
40
41         private CambriaHandler createTestSubject() {
42                 return new CambriaHandler();
43         }
44
45         @Spy
46         private CambriaHandler handler = new CambriaHandler();
47
48         @Mock
49         private CambriaIdentityManager createIdentityManager;
50
51         private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
52
53         @BeforeClass
54         public static void beforeClass() {
55                 String appConfigDir = "src/test/resources/config/catalog-be";
56                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
57                                 appConfigDir);
58                 new ConfigurationManager(configurationSource);
59         }
60
61         @Before
62         public void startUp() throws MalformedURLException, GeneralSecurityException {
63                 doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
64         }
65
66         @Test
67         public void testMockCreateUebKeys() throws HttpException, CambriaApiException, IOException {
68                 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
69                                 .thenReturn(apiCredential);
70                 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
71                                 .createUebKeys(Arrays.asList("Myhost:1234"));
72
73                 Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
74
75                 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
76
77         }
78
79         @Test
80         public void testMockCreateUebKeys_FAIL() throws HttpException, CambriaApiException, IOException {
81                 Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString()))
82                                 .thenThrow(new CambriaApiException("Error Message"));
83                 Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler
84                                 .createUebKeys(Arrays.asList("Myhost:1234"));
85                 Mockito.verify(createIdentityManager, Mockito.never()).setApiCredentials(Mockito.anyString(),
86                                 Mockito.anyString());
87                 assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isRight());
88                 CambriaErrorResponse response = eitherCreateUebKeys.right().value();
89                 assertEquals("Unexpected Operational Status", CambriaOperationStatus.CONNNECTION_ERROR,
90                                 response.getOperationStatus());
91                 assertEquals("Unexpected HTTP Code", 500, response.getHttpCode().intValue());
92         }
93
94         @Test
95         public void testProcessMessageException() throws Exception {
96                 CambriaHandler testSubject;
97                 String message = "";
98                 Integer result;
99
100                 // default test
101                 testSubject = createTestSubject();
102                 result = Deencapsulation.invoke(testSubject, "processMessageException", new Object[] { message });
103         }
104
105         @Test
106         public void testCheckPattern() throws Exception {
107                 CambriaHandler testSubject;
108                 String patternStr = "";
109                 String message = "";
110                 int groupIndex = 0;
111                 Integer result;
112
113                 // default test
114                 testSubject = createTestSubject();
115                 result = Deencapsulation.invoke(testSubject, "checkPattern", new Object[] { patternStr, message, groupIndex });
116         }
117
118         @Test
119         public void testGetTopics() throws Exception {
120                 CambriaHandler testSubject;
121                 List<String> hostSet = new LinkedList<>();
122                 hostSet.add("mock");
123                 Either<Set<String>, CambriaErrorResponse> result;
124
125                 // default test
126                 testSubject = createTestSubject();
127                 result = testSubject.getTopics(hostSet);
128         }
129
130         @Test
131         public void testProcessError() throws Exception {
132                 CambriaHandler testSubject;
133                 Exception e = null;
134                 CambriaErrorResponse result;
135
136                 // default test
137                 testSubject = createTestSubject();
138
139                 e = new Exception("HTTP Status 999");
140                 result = Deencapsulation.invoke(testSubject, "processError", e);
141
142                 e = new Exception("HTTP Status 401");
143                 result = Deencapsulation.invoke(testSubject, "processError", e);
144
145                 e = new Exception("HTTP Status 409");
146                 result = Deencapsulation.invoke(testSubject, "processError", e);
147
148                 e = new Exception("HTTP Status 500");
149                 result = Deencapsulation.invoke(testSubject, "processError", e);
150
151                 e = new Exception("mock", new Throwable(new Throwable("mock")));
152                 result = Deencapsulation.invoke(testSubject, "processError", e);
153         }
154
155         @Test
156         public void testWriteErrorToLog() throws Exception {
157                 CambriaHandler testSubject;
158                 CambriaErrorResponse cambriaErrorResponse = new CambriaErrorResponse();
159                 cambriaErrorResponse.setOperationStatus(CambriaOperationStatus.AUTHENTICATION_ERROR);
160                 String errorMessage = "mock";
161                 String methodName = "mock";
162                 String operationDesc = "mock";
163
164                 // default test
165                 testSubject = createTestSubject();
166                 Deencapsulation.invoke(testSubject, "writeErrorToLog", cambriaErrorResponse, "mock", "mock");
167         }
168
169         @Test
170         public void testCreateTopic() throws Exception {
171                 CambriaHandler testSubject;
172                 Collection<String> hostSet = new LinkedList<>();
173                 hostSet.add("mock");
174                 String apiKey = "mock";
175                 String secretKey = "mock";
176                 String topicName = "mock";
177                 int partitionCount = 0;
178                 int replicationCount = 0;
179                 CambriaErrorResponse result;
180
181                 // default test
182                 testSubject = createTestSubject();
183                 result = testSubject.createTopic(hostSet, apiKey, secretKey, topicName, partitionCount, replicationCount);
184         }
185
186         @Test
187         public void testUnRegisterFromTopic() throws Exception {
188                 CambriaHandler testSubject;
189                 Collection<String> hostSet = new LinkedList<>();
190                 hostSet.add("mock");
191                 String managerApiKey = "mock";
192                 String managerSecretKey = "mock";
193                 String subscriberApiKey = "mock";
194                 String topicName = "mock";
195                 CambriaErrorResponse unRegisterFromTopic = null;
196
197                 // default test
198                 testSubject = createTestSubject();
199                 unRegisterFromTopic = testSubject.unRegisterFromTopic(hostSet, managerApiKey, managerSecretKey,
200                                 subscriberApiKey, SubscriberTypeEnum.CONSUMER, topicName);
201         }
202
203         @Test
204         public void testRegisterToTopic() throws Exception {
205                 CambriaHandler testSubject;
206                 Collection<String> hostSet = new LinkedList<String>();
207                 hostSet.add("mock");
208                 String managerApiKey = "mock";
209                 String managerSecretKey = "mock";
210                 String subscriberApiKey = "mock";
211                 SubscriberTypeEnum subscriberTypeEnum = null;
212                 String topicName = "mock";
213                 CambriaErrorResponse result;
214
215                 // default test
216                 testSubject = createTestSubject();
217                 result = testSubject.registerToTopic(hostSet, managerApiKey, managerSecretKey, subscriberApiKey,
218                                 SubscriberTypeEnum.CONSUMER, topicName);
219         }
220
221         @Test
222         public void testCreateConsumer() throws Exception {
223                 CambriaHandler testSubject;
224                 Collection<String> hostSet = new LinkedList<>();
225                 hostSet.add("mock");
226                 String topicName = "mock";
227                 String apiKey = "mock";
228                 String secretKey = "mock";
229                 String consumerId = "mock";
230                 String consumerGroup = "mock";
231                 int timeoutMS = 0;
232                 CambriaConsumer result;
233
234                 // default test
235                 testSubject = createTestSubject();
236                 result = testSubject.createConsumer(hostSet, topicName, apiKey, secretKey, consumerId, consumerGroup,
237                                 timeoutMS);
238         }
239
240         @Test
241         public void testCloseConsumer() throws Exception {
242                 CambriaHandler testSubject;
243                 CambriaConsumer consumer = null;
244
245                 // test 1
246                 testSubject = createTestSubject();
247                 consumer = null;
248                 testSubject.closeConsumer(consumer);
249         }
250
251         @Test
252         public void testFetchFromTopic() throws Exception {
253                 CambriaHandler testSubject;
254                 CambriaConsumer topicConsumer = null;
255                 Either<Iterable<String>, CambriaErrorResponse> result;
256
257                 // default test
258                 testSubject = createTestSubject();
259                 result = testSubject.fetchFromTopic(topicConsumer);
260         }
261
262         @Test
263         public void testSendNotification() throws Exception {
264                 CambriaHandler testSubject;
265                 String topicName = "mock";
266                 String uebPublicKey = "mock";
267                 String uebSecretKey = "mock";
268                 List<String> uebServers = new LinkedList<>();
269                 uebServers.add("mock");
270                 INotificationData data = null;
271                 CambriaErrorResponse result;
272
273                 // default test
274                 testSubject = createTestSubject();
275                 result = testSubject.sendNotification(topicName, uebPublicKey, uebSecretKey, uebServers, data);
276         }
277
278         @Test
279         public void testSendNotificationAndClose() throws Exception {
280                 CambriaHandler testSubject;
281                 String topicName = "mock";
282                 String uebPublicKey = "mock";
283                 String uebSecretKey = "mock";
284                 List<String> uebServers = new LinkedList<>();
285                 uebServers.add("mock");
286                 INotificationData data = null;
287                 long waitBeforeCloseTimeout = 1;
288                 CambriaErrorResponse result;
289
290                 // default test
291                 testSubject = createTestSubject();
292                 result = testSubject.sendNotificationAndClose(topicName, uebPublicKey, uebSecretKey, uebServers, data,
293                                 waitBeforeCloseTimeout);
294         }
295
296         @Test
297         public void testGetApiKey() throws Exception {
298                 CambriaHandler testSubject;
299                 String server = "";
300                 String apiKey = "";
301                 CambriaErrorResponse result;
302
303                 // default test
304                 testSubject = createTestSubject();
305                 result = testSubject.getApiKey(server, apiKey);
306         }
307
308         @Test
309         public void testCreateUebKeys() throws Exception {
310                 CambriaHandler testSubject;
311                 List<String> hostSet = null;
312                 Either<ApiCredential, CambriaErrorResponse> result;
313
314                 // default test
315                 testSubject = createTestSubject();
316                 result = testSubject.createUebKeys(hostSet);
317         }
318
319         @Test
320         public void testBuildCambriaClient() throws Exception {
321                 CambriaHandler testSubject;
322                 AbstractAuthenticatedManagerBuilder<? extends CambriaClient> client = new TopicManagerBuilder()
323                                 .usingHosts("mock").authenticatedBy("mock", "mock");
324
325                 // default test
326                 testSubject = createTestSubject();
327                 Deencapsulation.invoke(testSubject, "buildCambriaClient", client);
328         }
329
330 }