2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2021 Orange.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.mr.tools;
25 import com.att.nsa.apiClient.credentials.ApiCredential;
26 import com.att.nsa.apiClient.http.HttpException;
27 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
28 import com.att.nsa.cmdtool.CommandNotReadyException;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.dmaap.mr.client.MRClient.MRApiException;
37 import org.onap.dmaap.mr.client.MRClientFactory;
38 import org.onap.dmaap.mr.client.MRIdentityManager;
39 import org.onap.dmaap.mr.client.MRIdentityManager.ApiKey;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PowerMockIgnore;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
45 import java.io.IOException;
46 import java.io.PrintStream;
47 import java.util.Arrays;
48 import java.util.Iterator;
49 import java.util.List;
51 import static org.junit.Assert.assertTrue;
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore("jdk.internal.reflect.*")
55 @PrepareForTest({MRClientFactory.class})
56 public class ApiKeyCommandTest {
59 private ApiKeyCommand command;
61 private MRIdentityManager tm;
67 private ApiCredential ac;
69 private PrintStream printStream;
72 public void setUp() throws Exception {
73 MockitoAnnotations.initMocks(this);
74 PowerMockito.mockStatic(MRClientFactory.class);
75 PowerMockito.when(MRClientFactory.createIdentityManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
76 PowerMockito.when(tm.getApiKey("testtopic")).thenReturn(key);
77 PowerMockito.when(tm.createApiKey("testtopic", "1")).thenReturn(ac);
82 public void tearDown() throws Exception {
87 public void testGetMatches() {
95 public void testCheckReady() {
98 command.checkReady(new MRCommandContext());
99 } catch (CommandNotReadyException e) {
100 // TODO Auto-generated catch block
108 public void testExecute() {
110 String[] parts1 = {"create", "testtopic", "1"};
111 String[] parts2 = {"list", "testtopic", "1"};
112 String[] parts3 = {"revoke", "write", "read"};
113 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
114 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
115 String[] part = (String[]) iterator.next();
118 command.execute(part, new MRCommandContext(), printStream);
119 } catch (CommandNotReadyException e) {
120 // TODO Auto-generated catch block
129 public void testExecute_error1() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
130 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new IOException("error"));
131 String[] parts1 = {"create", "testtopic", "1"};
132 String[] parts2 = {"list", "testtopic", "1"};
133 String[] parts3 = {"revoke", "write", "read"};
134 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
135 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
136 String[] part = (String[]) iterator.next();
139 command.execute(part, new MRCommandContext(), printStream);
140 } catch (CommandNotReadyException e) {
141 // TODO Auto-generated catch block
150 public void testExecute_error2() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
151 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new MRApiException("error"));
152 String[] parts1 = {"create", "testtopic", "1"};
153 String[] parts2 = {"list", "testtopic", "1"};
154 String[] parts3 = {"revoke", "write", "read"};
155 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
156 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
157 String[] part = (String[]) iterator.next();
160 command.execute(part, new MRCommandContext(), printStream);
161 } catch (CommandNotReadyException e) {
162 // TODO Auto-generated catch block
171 public void testExecute_error3() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
172 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpException(500, "error"));
173 String[] parts1 = {"create", "testtopic", "1"};
174 String[] parts2 = {"list", "testtopic", "1"};
175 String[] parts3 = {"revoke", "write", "read"};
176 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
177 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
178 String[] part = (String[]) iterator.next();
181 command.execute(part, new MRCommandContext(), printStream);
182 } catch (CommandNotReadyException e) {
183 // TODO Auto-generated catch block
192 public void testExecute_error4() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
193 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
194 String[] parts1 = {"create", "testtopic", "1"};
195 String[] parts2 = {"list", "testtopic", "1"};
196 String[] parts3 = {"revoke", "write", "read"};
197 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
198 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
199 String[] part = (String[]) iterator.next();
202 command.execute(part, new MRCommandContext(), printStream);
203 } catch (CommandNotReadyException e) {
204 // TODO Auto-generated catch block
213 public void testDisplayHelp() {
215 command.displayHelp(printStream);