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.http.HttpException;
43 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
44 import com.att.nsa.cmdtool.CommandNotReadyException;
45 import org.onap.dmaap.mr.client.MRClientFactory;
46 import org.onap.dmaap.mr.client.MRTopicManager.TopicInfo;
47 import org.onap.dmaap.mr.client.MRTopicManager;
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({ MRClientFactory.class })
51 public class TopicCommandTest {
53 private TopicCommand command;
55 private MRTopicManager tm;
59 private PrintStream printStream;
62 public void setUp() throws Exception {
64 MockitoAnnotations.initMocks(this);
65 PowerMockito.mockStatic(MRClientFactory.class);
66 PowerMockito.when(MRClientFactory.createTopicManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
67 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenReturn(ti);
72 public void tearDown() throws Exception {
77 public void testGetMatches() {
85 public void testCheckReady() {
88 command.checkReady(new MRCommandContext());
89 } catch (CommandNotReadyException e) {
90 // TODO Auto-generated catch block
98 public void testExecute() {
100 String[] parts1 = { "create", "testtopic", "1", "1" };
101 String[] parts2 = { "grant", "write", "read", "1" };
102 String[] parts3 = { "revoke", "write", "read", "1" };
103 String[] parts4 = { "list", "testtopic", "1", "1" };
104 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
105 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
106 String[] part = (String[]) iterator.next();
109 command.execute(part, new MRCommandContext(), printStream);
110 } catch (CommandNotReadyException e) {
111 // TODO Auto-generated catch block
121 public void testExecute_error() {
123 String[] parts1 = { "create", "testtopic", "1", "1" };
124 String[] parts2 = { "grant", "write", "read", "1" };
125 String[] parts3 = { "revoke", "write", "read", "1" };
126 String[] parts4 = { "list", "testtopic", "1", "1" };
127 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
128 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
129 String[] part = (String[]) iterator.next();
132 command.execute(part, new MRCommandContext(), printStream);
133 } catch (CommandNotReadyException e) {
134 // TODO Auto-generated catch block
144 public void testExecute_error_1() throws com.att.nsa.apiClient.http.HttpException, IOException {
145 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new IOException("error"));
146 PowerMockito.doThrow(new IOException()).when(tm).createTopic("testtopic", "", 1, 1);
147 PowerMockito.doThrow(new IOException()).when(tm).revokeProducer("read", "1");
148 String[] parts1 = { "create", "testtopic", "1", "1" };
149 String[] parts2 = { "grant", "read", "read", "1" };
150 String[] parts3 = { "revoke", "write", "read", "1" };
151 String[] parts4 = { "list", "testtopic", "1", "1" };
152 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
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
169 public void testExecute_error_2() throws com.att.nsa.apiClient.http.HttpException, IOException {
170 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
171 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
172 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).revokeConsumer("read", "1");
173 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowConsumer("read", "1");
174 String[] parts1 = { "create", "testtopic", "1", "1" };
175 String[] parts2 = { "grant", "write", "write", "1" };
176 String[] parts3 = { "revoke", "read", "read", "1" };
177 String[] parts4 = { "list", "testtopic", "1", "1" };
178 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
179 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
180 String[] part = (String[]) iterator.next();
183 command.execute(part, new MRCommandContext(), printStream);
184 } catch (CommandNotReadyException e) {
185 // TODO Auto-generated catch block
195 public void testExecute_error_3() throws com.att.nsa.apiClient.http.HttpException, IOException {
196 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
197 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowProducer("read", "1");
198 String[] parts1 = { "create", "testtopic", "1a", "1a" };
199 String[] parts2 = { "grant", "write", "read", "1" };
200 List<String[]> parts = Arrays.asList(parts1, parts2);
201 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
202 String[] part = (String[]) iterator.next();
205 command.execute(part, new MRCommandContext(), printStream);
206 } catch (CommandNotReadyException e) {
207 // TODO Auto-generated catch block
217 public void testDisplayHelp() {
219 command.displayHelp(printStream);