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.tools;
23 import static org.junit.Assert.assertTrue;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.util.Arrays;
28 import java.util.Iterator;
29 import java.util.List;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PowerMockIgnore;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
43 import com.att.nsa.apiClient.credentials.ApiCredential;
44 import com.att.nsa.apiClient.http.HttpException;
45 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
46 import com.att.nsa.cmdtool.CommandNotReadyException;
47 import org.onap.dmaap.mr.client.MRClient.MRApiException;
48 import org.onap.dmaap.mr.client.MRClientFactory;
49 import org.onap.dmaap.mr.client.MRIdentityManager;
50 import org.onap.dmaap.mr.client.MRIdentityManager.ApiKey;
52 @RunWith(PowerMockRunner.class)
53 @PowerMockIgnore("jdk.internal.reflect.*")
54 @PrepareForTest({ MRClientFactory.class })
55 public class ApiKeyCommandTest {
58 private ApiKeyCommand command;
60 private MRIdentityManager tm;
66 private ApiCredential ac;
68 private PrintStream printStream;
71 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
73 PowerMockito.mockStatic(MRClientFactory.class);
74 PowerMockito.when(MRClientFactory.createIdentityManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
75 PowerMockito.when(tm.getApiKey("testtopic")).thenReturn(key);
76 PowerMockito.when(tm.createApiKey("testtopic", "1")).thenReturn(ac);
81 public void tearDown() throws Exception {
86 public void testGetMatches() {
94 public void testCheckReady() {
97 command.checkReady(new MRCommandContext());
98 } catch (CommandNotReadyException e) {
99 // TODO Auto-generated catch block
107 public void testExecute() {
109 String[] parts1 = { "create", "testtopic", "1" };
110 String[] parts2 = { "list", "testtopic", "1" };
111 String[] parts3 = { "revoke", "write", "read" };
112 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
113 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
114 String[] part = (String[]) iterator.next();
117 command.execute(part, new MRCommandContext(), printStream);
118 } catch (CommandNotReadyException e) {
119 // TODO Auto-generated catch block
128 public void testExecute_error1() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
129 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new IOException("error"));
130 String[] parts1 = { "create", "testtopic", "1" };
131 String[] parts2 = { "list", "testtopic", "1" };
132 String[] parts3 = { "revoke", "write", "read" };
133 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
134 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
135 String[] part = (String[]) iterator.next();
138 command.execute(part, new MRCommandContext(), printStream);
139 } catch (CommandNotReadyException e) {
140 // TODO Auto-generated catch block
149 public void testExecute_error2() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
150 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new MRApiException("error"));
151 String[] parts1 = { "create", "testtopic", "1" };
152 String[] parts2 = { "list", "testtopic", "1" };
153 String[] parts3 = { "revoke", "write", "read" };
154 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
155 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
156 String[] part = (String[]) iterator.next();
159 command.execute(part, new MRCommandContext(),printStream);
160 } catch (CommandNotReadyException e) {
161 // TODO Auto-generated catch block
170 public void testExecute_error3() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
171 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpException(500, "error"));
172 String[] parts1 = { "create", "testtopic", "1" };
173 String[] parts2 = { "list", "testtopic", "1" };
174 String[] parts3 = { "revoke", "write", "read" };
175 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
176 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
177 String[] part = (String[]) iterator.next();
180 command.execute(part, new MRCommandContext(), printStream);
181 } catch (CommandNotReadyException e) {
182 // TODO Auto-generated catch block
191 public void testExecute_error4() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
192 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
193 String[] parts1 = { "create", "testtopic", "1" };
194 String[] parts2 = { "list", "testtopic", "1" };
195 String[] parts3 = { "revoke", "write", "read" };
196 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
197 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
198 String[] part = (String[]) iterator.next();
201 command.execute(part, new MRCommandContext(), printStream);
202 } catch (CommandNotReadyException e) {
203 // TODO Auto-generated catch block
212 public void testDisplayHelp() {
214 command.displayHelp(printStream);