MR Java 11 Uplift
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / cambria / service / impl / UIServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.onap.dmaap.mr.cambria.service.impl;
22
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.when;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
36 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
37 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
38 import org.onap.dmaap.dmf.mr.metabroker.Topic;
39 import org.onap.dmaap.dmf.mr.service.impl.UIServiceImpl;
40 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
41 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
42 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
43 import com.att.nsa.configs.ConfigDbException;
44 import com.att.nsa.security.db.NsaApiDb;
45 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
46
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.MockitoAnnotations;
54 import org.powermock.api.mockito.PowerMockito;
55 import org.powermock.core.classloader.annotations.PowerMockIgnore;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
58
59 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
60 @RunWith(PowerMockRunner.class)
61 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
62 public class UIServiceImplTest {
63
64         @InjectMocks
65         UIServiceImpl service;
66
67         @Mock
68         DMaaPContext dmaapContext;
69         @Mock
70         ConsumerFactory factory;
71
72         @Mock
73         ConfigurationReader configReader;
74
75         @Mock
76         DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
77
78         @Mock
79         Topic metatopic;
80
81         @Before
82         public void setUp() throws Exception {
83                 MockitoAnnotations.initMocks(this);
84                 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
85                 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
86
87                 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
88                 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
89
90                 PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
91                 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
92                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
93                 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
94         }
95
96         @After
97         public void tearDown() throws Exception {
98         }
99
100         @Test
101         public void testHello() {
102
103                 try {
104                         service.hello(dmaapContext);
105                 } catch (IOException e) {
106                         // TODO Auto-generated catch block
107                         e.printStackTrace();
108                 }
109
110                 String trueValue = "True";
111                 assertTrue(trueValue.equalsIgnoreCase("True"));
112
113         }
114
115         @Test
116         public void testGetApiKeysTable() {
117
118                 try {
119                         service.getApiKeysTable(dmaapContext);
120                 } catch (NullPointerException e) {
121                         // TODO Auto-generated catch block
122                         // e.printStackTrace();
123                         assertTrue(true);
124                 } catch (ConfigDbException e) {
125                         // TODO Auto-generated catch block
126                         e.printStackTrace();
127                 } catch (IOException e) {
128                         // TODO Auto-generated catch block
129                         e.printStackTrace();
130                 }
131                 assertTrue(true);
132
133         }
134
135         @Test
136         public void testGetApiKey() {
137
138                 try {
139                         service.getApiKey(dmaapContext, "admin");
140                 } catch (NullPointerException e) {
141                         // TODO Auto-generated catch block
142                         // e.printStackTrace();
143                         assertTrue(true);
144                 } catch (ConfigDbException e) {
145                         // TODO Auto-generated catch block
146                         e.printStackTrace();
147                 } catch (IOException e) {
148                         // TODO Auto-generated catch block
149                         e.printStackTrace();
150                 } catch (Exception e) {
151                         // TODO Auto-generated catch block
152                         assertTrue(true);
153                 }
154
155         }
156
157         @Test
158         public void testGetApiKey_invalidkey() {
159
160                 try {
161                         service.getApiKey(dmaapContext, "k56HmWT72J");
162                 } catch (NullPointerException e) {
163                         // TODO Auto-generated catch block
164                         // e.printStackTrace();
165                         assertTrue(true);
166                 } catch (ConfigDbException e) {
167                         // TODO Auto-generated catch block
168                         e.printStackTrace();
169                 } catch (IOException e) {
170                         // TODO Auto-generated catch block
171                         e.printStackTrace();
172                 } catch (Exception e) {
173                         // TODO Auto-generated catch block
174                         assertTrue(true);
175                 }
176
177         }
178
179         @Test
180         public void testGetTopicsTable() {
181
182                 try {
183                         List<Topic> topics = new ArrayList<Topic>();
184                         topics.add(metatopic);
185                         when(dmaapKafkaMetaBroker.getAllTopics()).thenReturn(topics);
186                         service.getTopicsTable(dmaapContext);
187                 } catch (NullPointerException e) {
188                         // TODO Auto-generated catch block
189                         // e.printStackTrace();
190
191                 } catch (ConfigDbException e) {
192                         // TODO Auto-generated catch block
193                         e.printStackTrace();
194                 } catch (IOException e) {
195                         // TODO Auto-generated catch block
196                         e.printStackTrace();
197                 } catch (Exception e) {
198                         // TODO Auto-generated catch block
199                         e.printStackTrace();
200                 }
201                 assertTrue(true);
202
203         }
204
205         @Test
206         public void testGetTopic() {
207
208                 try {
209                         when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(metatopic);
210                         service.getTopic(dmaapContext, "testTopic");
211                 } catch (NullPointerException e) {
212                         // TODO Auto-generated catch block
213                         // e.printStackTrace();
214
215                 } catch (ConfigDbException e) {
216                         // TODO Auto-generated catch block
217                         e.printStackTrace();
218                 } catch (IOException e) {
219                         // TODO Auto-generated catch block
220                         e.printStackTrace();
221                 } catch (Exception e) {
222                         // TODO Auto-generated catch block
223                         e.printStackTrace();
224                 }
225                 assertTrue(true);
226         }
227
228         @Test
229         public void testGetTopic_nulltopic() {
230
231                 try {
232                         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
233                         service.getTopic(dmaapContext, "testTopic");
234                 } catch (NullPointerException e) {
235                         // TODO Auto-generated catch block
236                         // e.printStackTrace();
237                 } catch (ConfigDbException e) {
238                         // TODO Auto-generated catch block
239                         e.printStackTrace();
240                 } catch (IOException e) {
241                         // TODO Auto-generated catch block
242                         e.printStackTrace();
243                 } catch (Exception e) {
244                         // TODO Auto-generated catch block
245                         assertTrue(true);
246                 }
247
248         }
249
250         NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
251
252                 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
253
254                 @Override
255                 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
256                                 throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
257                         // TODO Auto-generated method stub
258                         return new NsaSimpleApiKey(arg0, arg1);
259                 }
260
261                 @Override
262                 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
263                         // TODO Auto-generated method stub
264                         return false;
265                 }
266
267                 @Override
268                 public boolean deleteApiKey(String arg0) throws ConfigDbException {
269                         // TODO Auto-generated method stub
270                         return false;
271                 }
272
273                 @Override
274                 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
275                         Map<String, NsaSimpleApiKey> map = new HashMap<String, NsaSimpleApiKey>();
276                         map.put("testkey", new NsaSimpleApiKey("testkey", "password"));
277                         map.put("admin", new NsaSimpleApiKey("admin", "password"));
278
279                         return map;
280                 }
281
282                 @Override
283                 public Set<String> loadAllKeys() throws ConfigDbException {
284                         // TODO Auto-generated method stub
285
286                         return keys;
287                 }
288
289                 @Override
290                 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
291                         if (!keys.contains(arg0)) {
292                                 return null;
293                         }
294                         return new NsaSimpleApiKey(arg0, "password");
295                 }
296
297                 @Override
298                 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
299                         // TODO Auto-generated method stub
300
301                 }
302         };
303
304 }