2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package com.att.nsa.mr.tools;
\r
23 import static org.junit.Assert.assertTrue;
\r
25 import java.io.IOException;
\r
26 import java.io.PrintStream;
\r
27 import java.util.Arrays;
\r
28 import java.util.Iterator;
\r
29 import java.util.List;
\r
30 import java.util.concurrent.TimeUnit;
\r
32 import org.junit.After;
\r
33 import org.junit.Before;
\r
34 import org.junit.Test;
\r
35 import org.junit.runner.RunWith;
\r
36 import org.mockito.InjectMocks;
\r
37 import org.mockito.Mock;
\r
38 import org.mockito.MockitoAnnotations;
\r
39 import org.powermock.api.mockito.PowerMockito;
\r
40 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
41 import org.powermock.modules.junit4.PowerMockRunner;
\r
43 import com.att.nsa.cmdtool.CommandNotReadyException;
\r
44 import com.att.nsa.mr.client.MRBatchingPublisher;
\r
45 import com.att.nsa.mr.client.MRClientFactory;
\r
46 import com.att.nsa.mr.client.MRConsumer;
\r
47 import com.att.nsa.mr.client.MRTopicManager.TopicInfo;
\r
49 @RunWith(PowerMockRunner.class)
\r
50 @PrepareForTest({ MRClientFactory.class, ToolsUtil.class })
\r
51 public class MessageCommandTest {
\r
53 private MessageCommand command;
\r
55 private MRConsumer tm;
\r
57 private TopicInfo ti;
\r
59 private MRBatchingPublisher pub;
\r
61 private MRConsumer cc;
\r
63 private PrintStream printStream;
\r
66 public void setUp() throws Exception {
\r
67 MockitoAnnotations.initMocks(this);
\r
68 PowerMockito.mockStatic(MRClientFactory.class);
\r
69 PowerMockito.mockStatic(ToolsUtil.class);
\r
70 PowerMockito.when(MRClientFactory.createConsumer(Arrays.asList("localhost"), "testtopic", "2", "3", -1, -1,
\r
71 null, null, null)).thenReturn(cc);
\r
76 public void tearDown() throws Exception {
\r
81 public void testGetMatches() {
\r
83 command.getMatches();
\r
89 public void testCheckReady() {
\r
92 command.checkReady(new MRCommandContext());
\r
93 } catch (CommandNotReadyException e) {
\r
94 // TODO Auto-generated catch block
\r
95 e.printStackTrace();
\r
102 public void testExecute() {
\r
104 String[] parts1 = { "read", "testtopic", "2", "3" };
\r
105 String[] parts2 = { "write", "testtopic", "2", "3" };
\r
106 List<String[]> parts = Arrays.asList(parts1, parts2);
\r
107 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
108 String[] part = (String[]) iterator.next();
\r
110 MRCommandContext context = new MRCommandContext();
\r
111 PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
\r
113 command.execute(part, context, printStream);
\r
114 } catch (CommandNotReadyException e) {
\r
123 public void testExecute_error1() {
\r
125 PowerMockito.doThrow(new Exception()).when(cc).fetch();
\r
126 } catch (IOException e) {
\r
127 // TODO Auto-generated catch block
\r
128 e.printStackTrace();
\r
129 } catch (Exception e) {
\r
130 // TODO Auto-generated catch block
\r
131 e.printStackTrace();
\r
133 String[] parts1 = { "read", "testtopic", "2", "3" };
\r
134 String[] parts2 = { "write", "testtopic", "2", "3" };
\r
135 List<String[]> parts = Arrays.asList(parts1, parts2);
\r
136 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
137 String[] part = (String[]) iterator.next();
\r
139 MRCommandContext context = new MRCommandContext();
\r
140 PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
\r
142 command.execute(part, context, printStream);
\r
143 } catch (CommandNotReadyException e) {
\r
144 // TODO Auto-generated catch block
\r
145 e.printStackTrace();
\r
153 public void testExecute_error2() {
\r
155 PowerMockito.doThrow(new IOException()).when(pub).close(500, TimeUnit.MILLISECONDS);
\r
156 PowerMockito.doThrow(new IOException()).when(pub).send("2", "3");
\r
158 } catch (IOException e) {
\r
159 // TODO Auto-generated catch block
\r
160 e.printStackTrace();
\r
161 } catch (InterruptedException e) {
\r
162 // TODO Auto-generated catch block
\r
163 e.printStackTrace();
\r
165 String[] parts1 = { "read", "testtopic", "2", "3" };
\r
166 String[] parts2 = { "write", "testtopic", "2", "3" };
\r
167 List<String[]> parts = Arrays.asList(parts1, parts2);
\r
168 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
\r
169 String[] part = (String[]) iterator.next();
\r
171 MRCommandContext context = new MRCommandContext();
\r
172 PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
\r
174 command.execute(part, context, printStream);
\r
175 } catch (CommandNotReadyException e) {
\r
176 // TODO Auto-generated catch block
\r
177 e.printStackTrace();
\r
185 * @Test public void testExecute_error3() {
\r
187 * try { PowerMockito.doThrow(new IOException()).when(pub).send("2", "3");
\r
188 * PowerMockito.doThrow(new InterruptedException()).when(pub).close(500,
\r
189 * TimeUnit.MILLISECONDS); } catch (IOException e) { // TODO Auto-generated
\r
190 * catch block e.printStackTrace(); } catch (InterruptedException e) { //
\r
191 * TODO Auto-generated catch block e.printStackTrace(); } String[] parts1 =
\r
192 * { "read", "testtopic", "2", "3" }; String[] parts2 = { "write",
\r
193 * "testtopic", "2", "3" }; List<String[]> parts = Arrays.asList(parts1,
\r
194 * parts2); for (Iterator iterator = parts.iterator(); iterator.hasNext();)
\r
195 * { String[] part = (String[]) iterator.next(); PrintStream printStream =
\r
196 * new PrintStream(System.out);
\r
198 * MRCommandContext context = new MRCommandContext();
\r
199 * PowerMockito.when(ToolsUtil.createBatchPublisher(context,
\r
200 * "testtopic")).thenReturn(pub); try { command.execute(part, context,
\r
201 * printStream); } catch (CommandNotReadyException e) { // TODO
\r
202 * Auto-generated catch block e.printStackTrace(); } } assertTrue(true);
\r
208 public void testDisplayHelp() {
\r
210 command.displayHelp(printStream);
\r