2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package com.att.nsa.mr.tools;
\r
23 import static org.junit.Assert.assertTrue;
\r
25 import java.io.IOException;
\r
26 import java.io.PrintStream;
\r
27 import java.util.Arrays;
\r
28 import java.util.Iterator;
\r
29 import java.util.List;
\r
31 import org.junit.After;
\r
32 import org.junit.Before;
\r
33 import org.junit.Test;
\r
34 import org.junit.runner.RunWith;
\r
35 import org.mockito.InjectMocks;
\r
36 import org.mockito.Mock;
\r
37 import org.mockito.MockitoAnnotations;
\r
38 import org.powermock.api.mockito.PowerMockito;
\r
39 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
40 import org.powermock.modules.junit4.PowerMockRunner;
\r
42 import com.att.nsa.apiClient.credentials.ApiCredential;
\r
43 import com.att.nsa.apiClient.http.HttpException;
\r
44 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
\r
45 import com.att.nsa.cmdtool.CommandNotReadyException;
\r
46 import com.att.nsa.mr.client.MRClient.MRApiException;
\r
47 import com.att.nsa.mr.client.MRClientFactory;
\r
48 import com.att.nsa.mr.client.MRIdentityManager;
\r
49 import com.att.nsa.mr.client.MRIdentityManager.ApiKey;
\r
51 @RunWith(PowerMockRunner.class)
\r
52 @PrepareForTest({ MRClientFactory.class })
\r
53 public class ApiKeyCommandTest {
\r
56 private ApiKeyCommand command;
\r
58 private MRIdentityManager tm;
\r
64 private ApiCredential ac;
\r
66 private PrintStream printStream;
\r
69 public void setUp() throws Exception {
\r
70 MockitoAnnotations.initMocks(this);
\r
71 PowerMockito.mockStatic(MRClientFactory.class);
\r
72 PowerMockito.when(MRClientFactory.createIdentityManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
\r
73 PowerMockito.when(tm.getApiKey("testtopic")).thenReturn(key);
\r
74 PowerMockito.when(tm.createApiKey("testtopic", "1")).thenReturn(ac);
\r
79 public void tearDown() throws Exception {
\r
84 public void testGetMatches() {
\r
86 command.getMatches();
\r
92 public void testCheckReady() {
\r
95 command.checkReady(new MRCommandContext());
\r
96 } catch (CommandNotReadyException e) {
\r
97 // TODO Auto-generated catch block
\r
98 e.printStackTrace();
\r
105 public void testExecute() {
\r
107 String[] parts1 = { "create", "testtopic", "1" };
\r
108 String[] parts2 = { "list", "testtopic", "1" };
\r
109 String[] parts3 = { "revoke", "write", "read" };
\r
110 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
\r
111 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
112 String[] part = (String[]) iterator.next();
\r
115 command.execute(part, new MRCommandContext(), printStream);
\r
116 } catch (CommandNotReadyException e) {
\r
117 // TODO Auto-generated catch block
\r
118 e.printStackTrace();
\r
126 public void testExecute_error1() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
\r
127 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new IOException("error"));
\r
128 String[] parts1 = { "create", "testtopic", "1" };
\r
129 String[] parts2 = { "list", "testtopic", "1" };
\r
130 String[] parts3 = { "revoke", "write", "read" };
\r
131 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
\r
132 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
133 String[] part = (String[]) iterator.next();
\r
136 command.execute(part, new MRCommandContext(), printStream);
\r
137 } catch (CommandNotReadyException e) {
\r
138 // TODO Auto-generated catch block
\r
139 e.printStackTrace();
\r
147 public void testExecute_error2() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
\r
148 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new MRApiException("error"));
\r
149 String[] parts1 = { "create", "testtopic", "1" };
\r
150 String[] parts2 = { "list", "testtopic", "1" };
\r
151 String[] parts3 = { "revoke", "write", "read" };
\r
152 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
\r
153 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
154 String[] part = (String[]) iterator.next();
\r
157 command.execute(part, new MRCommandContext(),printStream);
\r
158 } catch (CommandNotReadyException e) {
\r
159 // TODO Auto-generated catch block
\r
160 e.printStackTrace();
\r
168 public void testExecute_error3() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
\r
169 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpException(500, "error"));
\r
170 String[] parts1 = { "create", "testtopic", "1" };
\r
171 String[] parts2 = { "list", "testtopic", "1" };
\r
172 String[] parts3 = { "revoke", "write", "read" };
\r
173 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
\r
174 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
175 String[] part = (String[]) iterator.next();
\r
178 command.execute(part, new MRCommandContext(), printStream);
\r
179 } catch (CommandNotReadyException e) {
\r
180 // TODO Auto-generated catch block
\r
181 e.printStackTrace();
\r
189 public void testExecute_error4() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
\r
190 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
\r
191 String[] parts1 = { "create", "testtopic", "1" };
\r
192 String[] parts2 = { "list", "testtopic", "1" };
\r
193 String[] parts3 = { "revoke", "write", "read" };
\r
194 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
\r
195 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
196 String[] part = (String[]) iterator.next();
\r
199 command.execute(part, new MRCommandContext(), printStream);
\r
200 } catch (CommandNotReadyException e) {
\r
201 // TODO Auto-generated catch block
\r
202 e.printStackTrace();
\r
210 public void testDisplayHelp() {
\r
212 command.displayHelp(printStream);
\r