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