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.http.HttpException;
44 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
45 import com.att.nsa.cmdtool.CommandNotReadyException;
46 import org.onap.dmaap.mr.client.MRClientFactory;
47 import org.onap.dmaap.mr.client.MRTopicManager.TopicInfo;
48 import org.onap.dmaap.mr.client.MRTopicManager;
50 @RunWith(PowerMockRunner.class)
51 @PowerMockIgnore("jdk.internal.reflect.*")
52 @PrepareForTest({ MRClientFactory.class })
53 public class TopicCommandTest {
55 private TopicCommand command;
57 private MRTopicManager tm;
61 private PrintStream printStream;
64 public void setUp() throws Exception {
66 MockitoAnnotations.initMocks(this);
67 PowerMockito.mockStatic(MRClientFactory.class);
68 PowerMockito.when(MRClientFactory.createTopicManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
69 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenReturn(ti);
74 public void tearDown() throws Exception {
79 public void testGetMatches() {
87 public void testCheckReady() {
90 command.checkReady(new MRCommandContext());
91 } catch (CommandNotReadyException e) {
92 // TODO Auto-generated catch block
100 public void testExecute() {
102 String[] parts1 = { "create", "testtopic", "1", "1" };
103 String[] parts2 = { "grant", "write", "read", "1" };
104 String[] parts3 = { "revoke", "write", "read", "1" };
105 String[] parts4 = { "list", "testtopic", "1", "1" };
106 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
107 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
108 String[] part = (String[]) iterator.next();
111 command.execute(part, new MRCommandContext(), printStream);
112 } catch (CommandNotReadyException e) {
113 // TODO Auto-generated catch block
123 public void testExecute_error() {
125 String[] parts1 = { "create", "testtopic", "1", "1" };
126 String[] parts2 = { "grant", "write", "read", "1" };
127 String[] parts3 = { "revoke", "write", "read", "1" };
128 String[] parts4 = { "list", "testtopic", "1", "1" };
129 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
130 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
131 String[] part = (String[]) iterator.next();
134 command.execute(part, new MRCommandContext(), printStream);
135 } catch (CommandNotReadyException e) {
136 // TODO Auto-generated catch block
146 public void testExecute_error_1() throws com.att.nsa.apiClient.http.HttpException, IOException {
147 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new IOException("error"));
148 PowerMockito.doThrow(new IOException()).when(tm).createTopic("testtopic", "", 1, 1);
149 PowerMockito.doThrow(new IOException()).when(tm).revokeProducer("read", "1");
150 String[] parts1 = { "create", "testtopic", "1", "1" };
151 String[] parts2 = { "grant", "read", "read", "1" };
152 String[] parts3 = { "revoke", "write", "read", "1" };
153 String[] parts4 = { "list", "testtopic", "1", "1" };
154 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
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
171 public void testExecute_error_2() throws com.att.nsa.apiClient.http.HttpException, IOException {
172 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
173 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
174 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).revokeConsumer("read", "1");
175 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowConsumer("read", "1");
176 String[] parts1 = { "create", "testtopic", "1", "1" };
177 String[] parts2 = { "grant", "write", "write", "1" };
178 String[] parts3 = { "revoke", "read", "read", "1" };
179 String[] parts4 = { "list", "testtopic", "1", "1" };
180 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
181 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
182 String[] part = (String[]) iterator.next();
185 command.execute(part, new MRCommandContext(), printStream);
186 } catch (CommandNotReadyException e) {
187 // TODO Auto-generated catch block
197 public void testExecute_error_3() throws com.att.nsa.apiClient.http.HttpException, IOException {
198 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
199 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowProducer("read", "1");
200 String[] parts1 = { "create", "testtopic", "1a", "1a" };
201 String[] parts2 = { "grant", "write", "read", "1" };
202 List<String[]> parts = Arrays.asList(parts1, parts2);
203 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
204 String[] part = (String[]) iterator.next();
207 command.execute(part, new MRCommandContext(), printStream);
208 } catch (CommandNotReadyException e) {
209 // TODO Auto-generated catch block
219 public void testDisplayHelp() {
221 command.displayHelp(printStream);