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.PowerMockIgnore;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
59 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
60 @RunWith(PowerMockRunner.class)
61 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
62 public class UIServiceImplTest {
65 UIServiceImpl service;
68 DMaaPContext dmaapContext;
70 ConsumerFactory factory;
73 ConfigurationReader configReader;
76 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
82 public void setUp() throws Exception {
83 MockitoAnnotations.initMocks(this);
84 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
85 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
87 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
88 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
90 PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
91 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
92 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
93 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
97 public void tearDown() throws Exception {
101 public void testHello() {
104 service.hello(dmaapContext);
105 } catch (IOException e) {
106 // TODO Auto-generated catch block
110 String trueValue = "True";
111 assertTrue(trueValue.equalsIgnoreCase("True"));
116 public void testGetApiKeysTable() {
119 service.getApiKeysTable(dmaapContext);
120 } catch (NullPointerException e) {
121 // TODO Auto-generated catch block
122 // e.printStackTrace();
124 } catch (ConfigDbException e) {
125 // TODO Auto-generated catch block
127 } catch (IOException e) {
128 // TODO Auto-generated catch block
136 public void testGetApiKey() {
139 service.getApiKey(dmaapContext, "admin");
140 } catch (NullPointerException e) {
141 // TODO Auto-generated catch block
142 // e.printStackTrace();
144 } catch (ConfigDbException e) {
145 // TODO Auto-generated catch block
147 } catch (IOException e) {
148 // TODO Auto-generated catch block
150 } catch (Exception e) {
151 // TODO Auto-generated catch block
158 public void testGetApiKey_invalidkey() {
161 service.getApiKey(dmaapContext, "k56HmWT72J");
162 } catch (NullPointerException e) {
163 // TODO Auto-generated catch block
164 // e.printStackTrace();
166 } catch (ConfigDbException e) {
167 // TODO Auto-generated catch block
169 } catch (IOException e) {
170 // TODO Auto-generated catch block
172 } catch (Exception e) {
173 // TODO Auto-generated catch block
180 public void testGetTopicsTable() {
183 List<Topic> topics = new ArrayList<Topic>();
184 topics.add(metatopic);
185 when(dmaapKafkaMetaBroker.getAllTopics()).thenReturn(topics);
186 service.getTopicsTable(dmaapContext);
187 } catch (NullPointerException e) {
188 // TODO Auto-generated catch block
189 // e.printStackTrace();
191 } catch (ConfigDbException e) {
192 // TODO Auto-generated catch block
194 } catch (IOException e) {
195 // TODO Auto-generated catch block
197 } catch (Exception e) {
198 // TODO Auto-generated catch block
206 public void testGetTopic() {
209 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(metatopic);
210 service.getTopic(dmaapContext, "testTopic");
211 } catch (NullPointerException e) {
212 // TODO Auto-generated catch block
213 // e.printStackTrace();
215 } catch (ConfigDbException e) {
216 // TODO Auto-generated catch block
218 } catch (IOException e) {
219 // TODO Auto-generated catch block
221 } catch (Exception e) {
222 // TODO Auto-generated catch block
229 public void testGetTopic_nulltopic() {
232 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
233 service.getTopic(dmaapContext, "testTopic");
234 } catch (NullPointerException e) {
235 // TODO Auto-generated catch block
236 // e.printStackTrace();
237 } catch (ConfigDbException e) {
238 // TODO Auto-generated catch block
240 } catch (IOException e) {
241 // TODO Auto-generated catch block
243 } catch (Exception e) {
244 // TODO Auto-generated catch block
250 NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
252 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
255 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
256 throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
257 // TODO Auto-generated method stub
258 return new NsaSimpleApiKey(arg0, arg1);
262 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
263 // TODO Auto-generated method stub
268 public boolean deleteApiKey(String arg0) throws ConfigDbException {
269 // TODO Auto-generated method stub
274 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
275 Map<String, NsaSimpleApiKey> map = new HashMap<String, NsaSimpleApiKey>();
276 map.put("testkey", new NsaSimpleApiKey("testkey", "password"));
277 map.put("admin", new NsaSimpleApiKey("admin", "password"));
283 public Set<String> loadAllKeys() throws ConfigDbException {
284 // TODO Auto-generated method stub
290 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
291 if (!keys.contains(arg0)) {
294 return new NsaSimpleApiKey(arg0, "password");
298 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
299 // TODO Auto-generated method stub