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.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
42 import com.att.nsa.apiClient.credentials.ApiCredential;
43 import com.att.nsa.apiClient.http.HttpException;
44 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
45 import com.att.nsa.cmdtool.CommandNotReadyException;
46 import org.onap.dmaap.mr.client.MRClient.MRApiException;
47 import org.onap.dmaap.mr.client.MRClientFactory;
48 import org.onap.dmaap.mr.client.MRIdentityManager;
49 import org.onap.dmaap.mr.client.MRIdentityManager.ApiKey;
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({ MRClientFactory.class })
53 public class ApiKeyCommandTest {
56 private ApiKeyCommand command;
58 private MRIdentityManager tm;
64 private ApiCredential ac;
66 private PrintStream printStream;
69 public void setUp() throws Exception {
70 MockitoAnnotations.initMocks(this);
71 PowerMockito.mockStatic(MRClientFactory.class);
72 PowerMockito.when(MRClientFactory.createIdentityManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
73 PowerMockito.when(tm.getApiKey("testtopic")).thenReturn(key);
74 PowerMockito.when(tm.createApiKey("testtopic", "1")).thenReturn(ac);
79 public void tearDown() throws Exception {
84 public void testGetMatches() {
92 public void testCheckReady() {
95 command.checkReady(new MRCommandContext());
96 } catch (CommandNotReadyException e) {
97 // TODO Auto-generated catch block
105 public void testExecute() {
107 String[] parts1 = { "create", "testtopic", "1" };
108 String[] parts2 = { "list", "testtopic", "1" };
109 String[] parts3 = { "revoke", "write", "read" };
110 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
111 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
112 String[] part = (String[]) iterator.next();
115 command.execute(part, new MRCommandContext(), printStream);
116 } catch (CommandNotReadyException e) {
117 // TODO Auto-generated catch block
126 public void testExecute_error1() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
127 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new IOException("error"));
128 String[] parts1 = { "create", "testtopic", "1" };
129 String[] parts2 = { "list", "testtopic", "1" };
130 String[] parts3 = { "revoke", "write", "read" };
131 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
132 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
133 String[] part = (String[]) iterator.next();
136 command.execute(part, new MRCommandContext(), printStream);
137 } catch (CommandNotReadyException e) {
138 // TODO Auto-generated catch block
147 public void testExecute_error2() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
148 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new MRApiException("error"));
149 String[] parts1 = { "create", "testtopic", "1" };
150 String[] parts2 = { "list", "testtopic", "1" };
151 String[] parts3 = { "revoke", "write", "read" };
152 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
153 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
154 String[] part = (String[]) iterator.next();
157 command.execute(part, new MRCommandContext(),printStream);
158 } catch (CommandNotReadyException e) {
159 // TODO Auto-generated catch block
168 public void testExecute_error3() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
169 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpException(500, "error"));
170 String[] parts1 = { "create", "testtopic", "1" };
171 String[] parts2 = { "list", "testtopic", "1" };
172 String[] parts3 = { "revoke", "write", "read" };
173 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
174 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
175 String[] part = (String[]) iterator.next();
178 command.execute(part, new MRCommandContext(), printStream);
179 } catch (CommandNotReadyException e) {
180 // TODO Auto-generated catch block
189 public void testExecute_error4() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
190 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
191 String[] parts1 = { "create", "testtopic", "1" };
192 String[] parts2 = { "list", "testtopic", "1" };
193 String[] parts3 = { "revoke", "write", "read" };
194 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
195 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
196 String[] part = (String[]) iterator.next();
199 command.execute(part, new MRCommandContext(), printStream);
200 } catch (CommandNotReadyException e) {
201 // TODO Auto-generated catch block
210 public void testDisplayHelp() {
212 command.displayHelp(printStream);