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