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