2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.mr.cambria.service.impl;
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.when;
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;
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;
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;
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
60 public class UIServiceImplTest {
63 UIServiceImpl service;
66 DMaaPContext dmaapContext;
68 ConsumerFactory factory;
71 ConfigurationReader configReader;
74 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
80 public void setUp() throws Exception {
81 MockitoAnnotations.initMocks(this);
82 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
83 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
85 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
86 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
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);
95 public void tearDown() throws Exception {
99 public void testHello() {
102 service.hello(dmaapContext);
103 } catch (IOException e) {
104 // TODO Auto-generated catch block
108 String trueValue = "True";
109 assertTrue(trueValue.equalsIgnoreCase("True"));
114 public void testGetApiKeysTable() {
117 service.getApiKeysTable(dmaapContext);
118 } catch (NullPointerException e) {
119 // TODO Auto-generated catch block
120 // e.printStackTrace();
122 } catch (ConfigDbException e) {
123 // TODO Auto-generated catch block
125 } catch (IOException e) {
126 // TODO Auto-generated catch block
134 public void testGetApiKey() {
137 service.getApiKey(dmaapContext, "admin");
138 } catch (NullPointerException e) {
139 // TODO Auto-generated catch block
140 // e.printStackTrace();
142 } catch (ConfigDbException e) {
143 // TODO Auto-generated catch block
145 } catch (IOException e) {
146 // TODO Auto-generated catch block
148 } catch (Exception e) {
149 // TODO Auto-generated catch block
156 public void testGetApiKey_invalidkey() {
159 service.getApiKey(dmaapContext, "k56HmWT72J");
160 } catch (NullPointerException e) {
161 // TODO Auto-generated catch block
162 // e.printStackTrace();
164 } catch (ConfigDbException e) {
165 // TODO Auto-generated catch block
167 } catch (IOException e) {
168 // TODO Auto-generated catch block
170 } catch (Exception e) {
171 // TODO Auto-generated catch block
178 public void testGetTopicsTable() {
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();
189 } catch (ConfigDbException e) {
190 // TODO Auto-generated catch block
192 } catch (IOException e) {
193 // TODO Auto-generated catch block
195 } catch (Exception e) {
196 // TODO Auto-generated catch block
204 public void testGetTopic() {
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();
213 } catch (ConfigDbException e) {
214 // TODO Auto-generated catch block
216 } catch (IOException e) {
217 // TODO Auto-generated catch block
219 } catch (Exception e) {
220 // TODO Auto-generated catch block
227 public void testGetTopic_nulltopic() {
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
238 } catch (IOException e) {
239 // TODO Auto-generated catch block
241 } catch (Exception e) {
242 // TODO Auto-generated catch block
248 NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
250 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
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);
260 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
261 // TODO Auto-generated method stub
266 public boolean deleteApiKey(String arg0) throws ConfigDbException {
267 // TODO Auto-generated method stub
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"));
281 public Set<String> loadAllKeys() throws ConfigDbException {
282 // TODO Auto-generated method stub
288 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
289 if (!keys.contains(arg0)) {
292 return new NsaSimpleApiKey(arg0, "password");
296 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
297 // TODO Auto-generated method stub