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 com.att.nsa.configs.ConfigDbException;
24 import com.att.nsa.security.db.NsaApiDb;
25 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
34 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
35 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
36 import org.onap.dmaap.dmf.mr.metabroker.Topic;
37 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
38 import org.onap.dmaap.dmf.mr.service.impl.UIServiceImpl;
39 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
40 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PowerMockIgnore;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
46 import java.io.IOException;
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.Mockito.when;
52 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
55 public class UIServiceImplTest {
58 UIServiceImpl service;
61 DMaaPContext dmaapContext;
63 ConsumerFactory factory;
66 ConfigurationReader configReader;
69 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
75 public void setUp() throws Exception {
76 MockitoAnnotations.initMocks(this);
77 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
78 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
80 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
81 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
83 PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
84 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
85 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
86 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
90 public void tearDown() throws Exception {
94 public void testHello() {
97 service.hello(dmaapContext);
98 } catch (IOException e) {
99 // TODO Auto-generated catch block
103 String trueValue = "True";
104 assertTrue(trueValue.equalsIgnoreCase("True"));
109 public void testGetApiKeysTable() {
112 service.getApiKeysTable(dmaapContext);
113 } catch (NullPointerException e) {
114 // TODO Auto-generated catch block
115 // e.printStackTrace();
117 } catch (ConfigDbException e) {
118 // TODO Auto-generated catch block
120 } catch (IOException e) {
121 // TODO Auto-generated catch block
129 public void testGetApiKey() {
132 service.getApiKey(dmaapContext, "admin");
133 } catch (NullPointerException e) {
134 // TODO Auto-generated catch block
135 // e.printStackTrace();
137 } catch (ConfigDbException e) {
138 // TODO Auto-generated catch block
140 } catch (IOException e) {
141 // TODO Auto-generated catch block
143 } catch (Exception e) {
144 // TODO Auto-generated catch block
151 public void testGetApiKey_invalidkey() {
154 service.getApiKey(dmaapContext, "k56HmWT72J");
155 } catch (NullPointerException e) {
156 // TODO Auto-generated catch block
157 // e.printStackTrace();
159 } catch (ConfigDbException e) {
160 // TODO Auto-generated catch block
162 } catch (IOException e) {
163 // TODO Auto-generated catch block
165 } catch (Exception e) {
166 // TODO Auto-generated catch block
173 public void testGetTopicsTable() {
176 List<Topic> topics = new ArrayList<Topic>();
177 topics.add(metatopic);
178 when(dmaapKafkaMetaBroker.getAllTopics()).thenReturn(topics);
179 service.getTopicsTable(dmaapContext);
180 } catch (NullPointerException e) {
181 // TODO Auto-generated catch block
182 // e.printStackTrace();
184 } catch (ConfigDbException e) {
185 // TODO Auto-generated catch block
187 } catch (IOException e) {
188 // TODO Auto-generated catch block
190 } catch (Exception e) {
191 // TODO Auto-generated catch block
199 public void testGetTopic() {
202 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(metatopic);
203 service.getTopic(dmaapContext, "testTopic");
204 } catch (NullPointerException e) {
205 // TODO Auto-generated catch block
206 // e.printStackTrace();
208 } catch (ConfigDbException e) {
209 // TODO Auto-generated catch block
211 } catch (IOException e) {
212 // TODO Auto-generated catch block
214 } catch (Exception e) {
215 // TODO Auto-generated catch block
222 public void testGetTopic_nulltopic() {
225 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
226 service.getTopic(dmaapContext, "testTopic");
227 } catch (NullPointerException e) {
228 // TODO Auto-generated catch block
229 // e.printStackTrace();
230 } catch (ConfigDbException e) {
231 // TODO Auto-generated catch block
233 } catch (IOException e) {
234 // TODO Auto-generated catch block
236 } catch (Exception e) {
237 // TODO Auto-generated catch block
243 NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
245 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
248 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
249 throws KeyExistsException, ConfigDbException {
250 // TODO Auto-generated method stub
251 return new NsaSimpleApiKey(arg0, arg1);
255 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
256 // TODO Auto-generated method stub
261 public boolean deleteApiKey(String arg0) throws ConfigDbException {
262 // TODO Auto-generated method stub
267 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
268 Map<String, NsaSimpleApiKey> map = new HashMap<String, NsaSimpleApiKey>();
269 map.put("testkey", new NsaSimpleApiKey("testkey", "password"));
270 map.put("admin", new NsaSimpleApiKey("admin", "password"));
276 public Set<String> loadAllKeys() throws ConfigDbException {
277 // TODO Auto-generated method stub
283 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
284 if (!keys.contains(arg0)) {
287 return new NsaSimpleApiKey(arg0, "password");
291 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
292 // TODO Auto-generated method stub