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.cmdtool.CommandNotReadyException;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.dmaap.mr.client.MRBatchingPublisher;
34 import org.onap.dmaap.mr.client.MRClientFactory;
35 import org.onap.dmaap.mr.client.MRConsumer;
36 import org.onap.dmaap.mr.client.MRTopicManager.TopicInfo;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PowerMockIgnore;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
42 import java.io.IOException;
43 import java.io.PrintStream;
44 import java.util.Arrays;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.concurrent.TimeUnit;
49 import static org.junit.Assert.assertTrue;
51 @RunWith(PowerMockRunner.class)
52 @PowerMockIgnore("jdk.internal.reflect.*")
53 @PrepareForTest({MRClientFactory.class, ToolsUtil.class})
54 public class MessageCommandTest {
56 private MessageCommand command;
58 private MRConsumer tm;
62 private MRBatchingPublisher pub;
64 private MRConsumer cc;
66 private PrintStream printStream;
69 public void setUp() throws Exception {
70 MockitoAnnotations.initMocks(this);
71 PowerMockito.mockStatic(MRClientFactory.class);
72 PowerMockito.mockStatic(ToolsUtil.class);
73 PowerMockito.when(MRClientFactory.createConsumer(Arrays.asList("localhost"), "testtopic", "2", "3", -1, -1,
74 null, null, null)).thenReturn(cc);
79 public void tearDown() throws Exception {
84 public void testGetMatches() {
92 public void testCheckReady() {
95 command.checkReady(new MRCommandContext());
96 } catch (CommandNotReadyException e) {
97 // TODO Auto-generated catch block
105 public void testExecute() {
107 String[] parts1 = {"read", "testtopic", "2", "3"};
108 String[] parts2 = {"write", "testtopic", "2", "3"};
109 List<String[]> parts = Arrays.asList(parts1, parts2);
110 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
111 String[] part = (String[]) iterator.next();
113 MRCommandContext context = new MRCommandContext();
114 PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
116 command.execute(part, context, printStream);
117 } catch (CommandNotReadyException e) {
126 public void testExecute_error1() {
128 PowerMockito.doThrow(new Exception()).when(cc).fetch();
129 } catch (IOException e) {
130 // TODO Auto-generated catch block
132 } catch (Exception e) {
133 // TODO Auto-generated catch block
136 String[] parts1 = {"read", "testtopic", "2", "3"};
137 String[] parts2 = {"write", "testtopic", "2", "3"};
138 List<String[]> parts = Arrays.asList(parts1, parts2);
139 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
140 String[] part = (String[]) iterator.next();
142 MRCommandContext context = new MRCommandContext();
143 PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
145 command.execute(part, context, printStream);
146 } catch (CommandNotReadyException e) {
147 // TODO Auto-generated catch block
156 public void testExecute_error2() {
158 PowerMockito.doThrow(new IOException()).when(pub).close(500, TimeUnit.MILLISECONDS);
159 PowerMockito.doThrow(new IOException()).when(pub).send("2", "3");
161 } catch (IOException e) {
162 // TODO Auto-generated catch block
164 } catch (InterruptedException e) {
165 // TODO Auto-generated catch block
168 String[] parts1 = {"read", "testtopic", "2", "3"};
169 String[] parts2 = {"write", "testtopic", "2", "3"};
170 List<String[]> parts = Arrays.asList(parts1, parts2);
171 for (Iterator iterator = parts.iterator(); iterator.hasNext(); ) {
172 String[] part = (String[]) iterator.next();
174 MRCommandContext context = new MRCommandContext();
175 PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
177 command.execute(part, context, printStream);
178 } catch (CommandNotReadyException e) {
179 // TODO Auto-generated catch block
188 * @Test public void testExecute_error3() {
190 * try { PowerMockito.doThrow(new IOException()).when(pub).send("2", "3");
191 * PowerMockito.doThrow(new InterruptedException()).when(pub).close(500,
192 * TimeUnit.MILLISECONDS); } catch (IOException e) { // TODO Auto-generated
193 * catch block e.printStackTrace(); } catch (InterruptedException e) { //
194 * TODO Auto-generated catch block e.printStackTrace(); } String[] parts1 =
195 * { "read", "testtopic", "2", "3" }; String[] parts2 = { "write",
196 * "testtopic", "2", "3" }; List<String[]> parts = Arrays.asList(parts1,
197 * parts2); for (Iterator iterator = parts.iterator(); iterator.hasNext();)
198 * { String[] part = (String[]) iterator.next(); PrintStream printStream =
199 * new PrintStream(System.out);
201 * MRCommandContext context = new MRCommandContext();
202 * PowerMockito.when(ToolsUtil.createBatchPublisher(context,
203 * "testtopic")).thenReturn(pub); try { command.execute(part, context,
204 * printStream); } catch (CommandNotReadyException e) { // TODO
205 * Auto-generated catch block e.printStackTrace(); } } assertTrue(true);
211 public void testDisplayHelp() {
213 command.displayHelp(printStream);