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