2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package com.att.nsa.cambria.service.impl;
\r
23 import static org.junit.Assert.*;
\r
24 import static org.mockito.Mockito.when;
\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
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
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
58 @RunWith(PowerMockRunner.class)
\r
59 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
\r
60 public class UIServiceImplTest {
\r
63 UIServiceImpl service;
\r
66 DMaaPContext dmaapContext;
\r
68 ConsumerFactory factory;
\r
71 ConfigurationReader configReader;
\r
74 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
\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
85 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
86 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
\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
95 public void tearDown() throws Exception {
\r
99 public void testHello() {
\r
102 service.hello(dmaapContext);
\r
103 } catch (IOException e) {
\r
104 // TODO Auto-generated catch block
\r
105 e.printStackTrace();
\r
108 String trueValue = "True";
\r
109 assertTrue(trueValue.equalsIgnoreCase("True"));
\r
114 public void testGetApiKeysTable() {
\r
117 service.getApiKeysTable(dmaapContext);
\r
118 } catch (NullPointerException e) {
\r
119 // TODO Auto-generated catch block
\r
120 // e.printStackTrace();
\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
134 public void testGetApiKey() {
\r
137 service.getApiKey(dmaapContext, "admin");
\r
138 } catch (NullPointerException e) {
\r
139 // TODO Auto-generated catch block
\r
140 // e.printStackTrace();
\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
156 public void testGetApiKey_invalidkey() {
\r
159 service.getApiKey(dmaapContext, "k56HmWT72J");
\r
160 } catch (NullPointerException e) {
\r
161 // TODO Auto-generated catch block
\r
162 // e.printStackTrace();
\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
178 public void testGetTopicsTable() {
\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
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
204 public void testGetTopic() {
\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
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
227 public void testGetTopic_nulltopic() {
\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
248 NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
\r
250 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
\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
260 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
261 // TODO Auto-generated method stub
\r
266 public boolean deleteApiKey(String arg0) throws ConfigDbException {
\r
267 // TODO Auto-generated method stub
\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
281 public Set<String> loadAllKeys() throws ConfigDbException {
\r
282 // TODO Auto-generated method stub
\r
288 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
\r
289 if (!keys.contains(arg0)) {
\r
292 return new NsaSimpleApiKey(arg0, "password");
\r
296 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
297 // TODO Auto-generated method stub
\r