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.http.HttpException;
26 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
27 import com.att.nsa.cmdtool.CommandNotReadyException;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.dmaap.mr.client.MRClientFactory;
36 import org.onap.dmaap.mr.client.MRTopicManager;
37 import org.onap.dmaap.mr.client.MRTopicManager.TopicInfo;
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 java.io.IOException;
44 import java.io.PrintStream;
45 import java.util.Arrays;
46 import java.util.Iterator;
47 import java.util.List;
49 import static org.junit.Assert.assertTrue;
51 @RunWith(PowerMockRunner.class)
52 @PowerMockIgnore("jdk.internal.reflect.*")
53 @PrepareForTest({MRClientFactory.class})
54 public class TopicCommandTest {
56 private TopicCommand command;
58 private MRTopicManager tm;
62 private PrintStream printStream;
65 public void setUp() throws Exception {
67 MockitoAnnotations.initMocks(this);
68 PowerMockito.mockStatic(MRClientFactory.class);
69 PowerMockito.when(MRClientFactory.createTopicManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
70 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenReturn(ti);
75 public void tearDown() throws Exception {
80 public void testGetMatches() {
88 public void testCheckReady() {
91 command.checkReady(new MRCommandContext());
92 } catch (CommandNotReadyException e) {
93 // TODO Auto-generated catch block
101 public void testExecute() {
103 String[] parts1 = {"create", "testtopic", "1", "1"};
104 String[] parts2 = {"grant", "write", "read", "1"};
105 String[] parts3 = {"revoke", "write", "read", "1"};
106 String[] parts4 = {"list", "testtopic", "1", "1"};
107 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
108 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
109 String[] part = (String[]) iterator.next();
112 command.execute(part, new MRCommandContext(), printStream);
113 } catch (CommandNotReadyException e) {
114 // TODO Auto-generated catch block
124 public void testExecute_error() {
126 String[] parts1 = {"create", "testtopic", "1", "1"};
127 String[] parts2 = {"grant", "write", "read", "1"};
128 String[] parts3 = {"revoke", "write", "read", "1"};
129 String[] parts4 = {"list", "testtopic", "1", "1"};
130 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
131 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
132 String[] part = (String[]) iterator.next();
135 command.execute(part, new MRCommandContext(), printStream);
136 } catch (CommandNotReadyException e) {
137 // TODO Auto-generated catch block
147 public void testExecute_error_1() throws com.att.nsa.apiClient.http.HttpException, IOException {
148 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new IOException("error"));
149 PowerMockito.doThrow(new IOException()).when(tm).createTopic("testtopic", "", 1, 1);
150 PowerMockito.doThrow(new IOException()).when(tm).revokeProducer("read", "1");
151 String[] parts1 = {"create", "testtopic", "1", "1"};
152 String[] parts2 = {"grant", "read", "read", "1"};
153 String[] parts3 = {"revoke", "write", "read", "1"};
154 String[] parts4 = {"list", "testtopic", "1", "1"};
155 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
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
172 public void testExecute_error_2() throws com.att.nsa.apiClient.http.HttpException, IOException {
173 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
174 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
175 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).revokeConsumer("read", "1");
176 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowConsumer("read", "1");
177 String[] parts1 = {"create", "testtopic", "1", "1"};
178 String[] parts2 = {"grant", "write", "write", "1"};
179 String[] parts3 = {"revoke", "read", "read", "1"};
180 String[] parts4 = {"list", "testtopic", "1", "1"};
181 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
182 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
183 String[] part = (String[]) iterator.next();
186 command.execute(part, new MRCommandContext(), printStream);
187 } catch (CommandNotReadyException e) {
188 // TODO Auto-generated catch block
198 public void testExecute_error_3() throws com.att.nsa.apiClient.http.HttpException, IOException {
199 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
200 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowProducer("read", "1");
201 String[] parts1 = {"create", "testtopic", "1a", "1a"};
202 String[] parts2 = {"grant", "write", "read", "1"};
203 List<String[]> parts = Arrays.asList(parts1, parts2);
204 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
205 String[] part = (String[]) iterator.next();
208 command.execute(part, new MRCommandContext(), printStream);
209 } catch (CommandNotReadyException e) {
210 // TODO Auto-generated catch block
220 public void testDisplayHelp() {
222 command.displayHelp(printStream);