one more case added in DmaapClientUtilTest.java
[dmaap/messagerouter/dmaapclient.git] / src / test / java / com / att / nsa / mr / tools / MessageCommandTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy Engine\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
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \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
19  */\r
20 \r
21 package com.att.nsa.mr.tools;\r
22 \r
23 import static org.junit.Assert.assertTrue;\r
24 \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
31 \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
42 \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
48 \r
49 @RunWith(PowerMockRunner.class)\r
50 @PrepareForTest({ MRClientFactory.class, ToolsUtil.class })\r
51 public class MessageCommandTest {\r
52         @InjectMocks\r
53         private MessageCommand command;\r
54         @Mock\r
55         private MRConsumer tm;\r
56         @Mock\r
57         private TopicInfo ti;\r
58         @Mock\r
59         private MRBatchingPublisher pub;\r
60         @Mock\r
61         private MRConsumer cc;\r
62         @Mock\r
63         private PrintStream printStream;\r
64 \r
65         @Before\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
72 \r
73         }\r
74 \r
75         @After\r
76         public void tearDown() throws Exception {\r
77 \r
78         }\r
79 \r
80         @Test\r
81         public void testGetMatches() {\r
82 \r
83                 command.getMatches();\r
84                 assertTrue(true);\r
85 \r
86         }\r
87 \r
88         @Test\r
89         public void testCheckReady() {\r
90 \r
91                 try {\r
92                         command.checkReady(new MRCommandContext());\r
93                 } catch (CommandNotReadyException e) {\r
94                         // TODO Auto-generated catch block\r
95                         e.printStackTrace();\r
96                 }\r
97                 assertTrue(true);\r
98 \r
99         }\r
100 \r
101         @Test\r
102         public void testExecute() {\r
103 \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
109 \r
110                         MRCommandContext context = new MRCommandContext();\r
111                         PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);\r
112                         try {\r
113                                 command.execute(part, context, printStream);\r
114                         } catch (CommandNotReadyException e) {\r
115                                 assertTrue(true);\r
116                         }\r
117                 }\r
118                 assertTrue(true);\r
119 \r
120         }\r
121 \r
122         @Test\r
123         public void testExecute_error1() {\r
124                 try {\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
132                 }\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
138 \r
139                         MRCommandContext context = new MRCommandContext();\r
140                         PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);\r
141                         try {\r
142                                 command.execute(part, context, printStream);\r
143                         } catch (CommandNotReadyException e) {\r
144                                 // TODO Auto-generated catch block\r
145                                 e.printStackTrace();\r
146                         }\r
147                 }\r
148                 assertTrue(true);\r
149 \r
150         }\r
151 \r
152         @Test\r
153         public void testExecute_error2() {\r
154                 try {\r
155                         PowerMockito.doThrow(new IOException()).when(pub).close(500, TimeUnit.MILLISECONDS);\r
156                         PowerMockito.doThrow(new IOException()).when(pub).send("2", "3");\r
157 \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
164                 }\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
170 \r
171                         MRCommandContext context = new MRCommandContext();\r
172                         PowerMockito.when(ToolsUtil.createBatchPublisher(context, "testtopic")).thenReturn(pub);\r
173                         try {\r
174                                 command.execute(part, context, printStream);\r
175                         } catch (CommandNotReadyException e) {\r
176                                 // TODO Auto-generated catch block\r
177                                 e.printStackTrace();\r
178                         }\r
179                 }\r
180                 assertTrue(true);\r
181 \r
182         }\r
183 \r
184         /*\r
185          * @Test public void testExecute_error3() {\r
186          * \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
197          * \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
203          * \r
204          * }\r
205          */\r
206 \r
207         @Test\r
208         public void testDisplayHelp() {\r
209 \r
210                 command.displayHelp(printStream);\r
211 \r
212         }\r
213 \r
214 }\r