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.nsa.cambria.backends.ConsumerFactory;
\r
36 import com.att.nsa.cambria.beans.DMaaPContext;
\r
37 import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;
\r
38 import com.att.nsa.cambria.metabroker.Topic;
\r
39 import com.att.nsa.cambria.security.DMaaPAuthenticatorImpl;
\r
40 import com.att.nsa.cambria.utils.ConfigurationReader;
\r
41 import com.att.nsa.cambria.utils.DMaaPResponseBuilder;
\r
42 import com.att.nsa.configs.ConfigDbException;
\r
43 import com.att.nsa.security.db.NsaApiDb;
\r
44 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
\r
46 import org.junit.After;
\r
47 import org.junit.Before;
\r
48 import org.junit.Test;
\r
49 import org.junit.runner.RunWith;
\r
50 import org.mockito.InjectMocks;
\r
51 import org.mockito.Mock;
\r
52 import org.mockito.MockitoAnnotations;
\r
53 import org.powermock.api.mockito.PowerMockito;
\r
54 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
55 import org.powermock.modules.junit4.PowerMockRunner;
\r
57 @RunWith(PowerMockRunner.class)
\r
58 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
\r
59 public class UIServiceImplTest {
\r
62 UIServiceImpl service;
\r
65 DMaaPContext dmaapContext;
\r
67 ConsumerFactory factory;
\r
70 ConfigurationReader configReader;
\r
73 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
\r
79 public void setUp() throws Exception {
\r
80 MockitoAnnotations.initMocks(this);
\r
81 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
\r
82 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
\r
84 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
85 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
\r
87 PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
\r
88 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
\r
89 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
90 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
94 public void tearDown() throws Exception {
\r
98 public void testHello() {
\r
101 service.hello(dmaapContext);
\r
102 } catch (IOException e) {
\r
103 // TODO Auto-generated catch block
\r
104 e.printStackTrace();
\r
107 String trueValue = "True";
\r
108 assertTrue(trueValue.equalsIgnoreCase("True"));
\r
113 public void testGetApiKeysTable() {
\r
116 service.getApiKeysTable(dmaapContext);
\r
117 } catch (NullPointerException e) {
\r
118 // TODO Auto-generated catch block
\r
119 // e.printStackTrace();
\r
121 } catch (ConfigDbException e) {
\r
122 // TODO Auto-generated catch block
\r
123 e.printStackTrace();
\r
124 } catch (IOException e) {
\r
125 // TODO Auto-generated catch block
\r
126 e.printStackTrace();
\r
133 public void testGetApiKey() {
\r
136 service.getApiKey(dmaapContext, "admin");
\r
137 } catch (NullPointerException e) {
\r
138 // TODO Auto-generated catch block
\r
139 // e.printStackTrace();
\r
141 } catch (ConfigDbException e) {
\r
142 // TODO Auto-generated catch block
\r
143 e.printStackTrace();
\r
144 } catch (IOException e) {
\r
145 // TODO Auto-generated catch block
\r
146 e.printStackTrace();
\r
147 } catch (Exception e) {
\r
148 // TODO Auto-generated catch block
\r
155 public void testGetApiKey_invalidkey() {
\r
158 service.getApiKey(dmaapContext, "k56HmWT72J");
\r
159 } catch (NullPointerException e) {
\r
160 // TODO Auto-generated catch block
\r
161 // e.printStackTrace();
\r
163 } catch (ConfigDbException e) {
\r
164 // TODO Auto-generated catch block
\r
165 e.printStackTrace();
\r
166 } catch (IOException e) {
\r
167 // TODO Auto-generated catch block
\r
168 e.printStackTrace();
\r
169 } catch (Exception e) {
\r
170 // TODO Auto-generated catch block
\r
177 public void testGetTopicsTable() {
\r
180 List<Topic> topics = new ArrayList<Topic>();
\r
181 topics.add(metatopic);
\r
182 when(dmaapKafkaMetaBroker.getAllTopics()).thenReturn(topics);
\r
183 service.getTopicsTable(dmaapContext);
\r
184 } catch (NullPointerException e) {
\r
185 // TODO Auto-generated catch block
\r
186 // e.printStackTrace();
\r
188 } catch (ConfigDbException e) {
\r
189 // TODO Auto-generated catch block
\r
190 e.printStackTrace();
\r
191 } catch (IOException e) {
\r
192 // TODO Auto-generated catch block
\r
193 e.printStackTrace();
\r
194 } catch (Exception e) {
\r
195 // TODO Auto-generated catch block
\r
196 e.printStackTrace();
\r
203 public void testGetTopic() {
\r
206 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(metatopic);
\r
207 service.getTopic(dmaapContext, "testTopic");
\r
208 } catch (NullPointerException e) {
\r
209 // TODO Auto-generated catch block
\r
210 // e.printStackTrace();
\r
212 } catch (ConfigDbException e) {
\r
213 // TODO Auto-generated catch block
\r
214 e.printStackTrace();
\r
215 } catch (IOException e) {
\r
216 // TODO Auto-generated catch block
\r
217 e.printStackTrace();
\r
218 } catch (Exception e) {
\r
219 // TODO Auto-generated catch block
\r
220 e.printStackTrace();
\r
226 public void testGetTopic_nulltopic() {
\r
229 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
\r
230 service.getTopic(dmaapContext, "testTopic");
\r
231 } catch (NullPointerException e) {
\r
232 // TODO Auto-generated catch block
\r
233 // e.printStackTrace();
\r
234 } catch (ConfigDbException e) {
\r
235 // TODO Auto-generated catch block
\r
236 e.printStackTrace();
\r
237 } catch (IOException e) {
\r
238 // TODO Auto-generated catch block
\r
239 e.printStackTrace();
\r
240 } catch (Exception e) {
\r
241 // TODO Auto-generated catch block
\r
247 NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
\r
249 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
\r
252 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
\r
253 throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
\r
254 // TODO Auto-generated method stub
\r
255 return new NsaSimpleApiKey(arg0, arg1);
\r
259 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
260 // TODO Auto-generated method stub
\r
265 public boolean deleteApiKey(String arg0) throws ConfigDbException {
\r
266 // TODO Auto-generated method stub
\r
271 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
\r
272 Map<String, NsaSimpleApiKey> map = new HashMap<String, NsaSimpleApiKey>();
\r
273 map.put("testkey", new NsaSimpleApiKey("testkey", "password"));
\r
274 map.put("admin", new NsaSimpleApiKey("admin", "password"));
\r
280 public Set<String> loadAllKeys() throws ConfigDbException {
\r
281 // TODO Auto-generated method stub
\r
287 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
\r
288 if (!keys.contains(arg0)) {
\r
291 return new NsaSimpleApiKey(arg0, "password");
\r
295 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
296 // TODO Auto-generated method stub
\r