[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / tools / MessageCommandTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.dmaap.mr.tools;
24
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;
41
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;
48
49 import static org.junit.Assert.assertTrue;
50
51 @RunWith(PowerMockRunner.class)
52 @PowerMockIgnore("jdk.internal.reflect.*")
53 @PrepareForTest({MRClientFactory.class, ToolsUtil.class})
54 public class MessageCommandTest {
55     @InjectMocks
56     private MessageCommand command;
57     @Mock
58     private MRConsumer tm;
59     @Mock
60     private TopicInfo ti;
61     @Mock
62     private MRBatchingPublisher pub;
63     @Mock
64     private MRConsumer cc;
65     @Mock
66     private PrintStream printStream;
67
68     @Before
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);
75
76     }
77
78     @After
79     public void tearDown() throws Exception {
80
81     }
82
83     @Test
84     public void testGetMatches() {
85
86         command.getMatches();
87         assertTrue(true);
88
89     }
90
91     @Test
92     public void testCheckReady() {
93
94         try {
95             command.checkReady(new MRCommandContext());
96         } catch (CommandNotReadyException e) {
97             // TODO Auto-generated catch block
98             e.printStackTrace();
99         }
100         assertTrue(true);
101
102     }
103
104     @Test
105     public void testExecute() {
106
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();
112
113             MRCommandContext context = new MRCommandContext();
114             PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
115             try {
116                 command.execute(part, context, printStream);
117             } catch (CommandNotReadyException e) {
118                 assertTrue(true);
119             }
120         }
121         assertTrue(true);
122
123     }
124
125     @Test
126     public void testExecute_error1() {
127         try {
128             PowerMockito.doThrow(new Exception()).when(cc).fetch();
129         } catch (IOException e) {
130             // TODO Auto-generated catch block
131             e.printStackTrace();
132         } catch (Exception e) {
133             // TODO Auto-generated catch block
134             e.printStackTrace();
135         }
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();
141
142             MRCommandContext context = new MRCommandContext();
143             PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
144             try {
145                 command.execute(part, context, printStream);
146             } catch (CommandNotReadyException e) {
147                 // TODO Auto-generated catch block
148                 e.printStackTrace();
149             }
150         }
151         assertTrue(true);
152
153     }
154
155     @Test
156     public void testExecute_error2() {
157         try {
158             PowerMockito.doThrow(new IOException()).when(pub).close(500, TimeUnit.MILLISECONDS);
159             PowerMockito.doThrow(new IOException()).when(pub).send("2", "3");
160
161         } catch (IOException e) {
162             // TODO Auto-generated catch block
163             e.printStackTrace();
164         } catch (InterruptedException e) {
165             // TODO Auto-generated catch block
166             e.printStackTrace();
167         }
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();
173
174             MRCommandContext context = new MRCommandContext();
175             PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);
176             try {
177                 command.execute(part, context, printStream);
178             } catch (CommandNotReadyException e) {
179                 // TODO Auto-generated catch block
180                 e.printStackTrace();
181             }
182         }
183         assertTrue(true);
184
185     }
186
187     /*
188      * @Test public void testExecute_error3() {
189      *
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);
200      *
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);
206      *
207      * }
208      */
209
210     @Test
211     public void testDisplayHelp() {
212
213         command.displayHelp(printStream);
214         assertTrue(true);
215     }
216
217 }