4abd1e48d45422c817203f861ec61a7df07e1121
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / tools / TopicCommandTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.mr.tools;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.util.Arrays;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
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;
42
43 import com.att.nsa.apiClient.http.HttpException;
44 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
45 import com.att.nsa.cmdtool.CommandNotReadyException;
46 import org.onap.dmaap.mr.client.MRClientFactory;
47 import org.onap.dmaap.mr.client.MRTopicManager.TopicInfo;
48 import org.onap.dmaap.mr.client.MRTopicManager;
49
50 @RunWith(PowerMockRunner.class)
51 @PowerMockIgnore("jdk.internal.reflect.*")
52 @PrepareForTest({ MRClientFactory.class })
53 public class TopicCommandTest {
54         @InjectMocks
55         private TopicCommand command;
56         @Mock
57         private MRTopicManager tm;
58         @Mock
59         private TopicInfo ti;
60         @Mock
61         private PrintStream printStream;
62
63         @Before
64         public void setUp() throws Exception {
65
66                 MockitoAnnotations.initMocks(this);
67                 PowerMockito.mockStatic(MRClientFactory.class);
68                 PowerMockito.when(MRClientFactory.createTopicManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
69                 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenReturn(ti);
70
71         }
72
73         @After
74         public void tearDown() throws Exception {
75
76         }
77
78         @Test
79         public void testGetMatches() {
80
81                 command.getMatches();
82                 assertTrue(true);
83
84         }
85
86         @Test
87         public void testCheckReady() {
88
89                 try {
90                         command.checkReady(new MRCommandContext());
91                 } catch (CommandNotReadyException e) {
92                         // TODO Auto-generated catch block
93                         e.printStackTrace();
94                 }
95                 assertTrue(true);
96
97         }
98
99         @Test
100         public void testExecute() {
101
102                 String[] parts1 = { "create", "testtopic", "1", "1" };
103                 String[] parts2 = { "grant", "write", "read", "1" };
104                 String[] parts3 = { "revoke", "write", "read", "1" };
105                 String[] parts4 = { "list", "testtopic", "1", "1" };
106                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
107                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
108                         String[] part = (String[]) iterator.next();
109
110                         try {
111                                 command.execute(part, new MRCommandContext(), printStream);
112                         } catch (CommandNotReadyException e) {
113                                 // TODO Auto-generated catch block
114                                 e.printStackTrace();
115                         }
116                         assertTrue(true);
117
118                 }
119
120         }
121
122         @Test
123         public void testExecute_error() {
124
125                 String[] parts1 = { "create", "testtopic", "1", "1" };
126                 String[] parts2 = { "grant", "write", "read", "1" };
127                 String[] parts3 = { "revoke", "write", "read", "1" };
128                 String[] parts4 = { "list", "testtopic", "1", "1" };
129                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
130                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
131                         String[] part = (String[]) iterator.next();
132
133                         try {
134                                 command.execute(part, new MRCommandContext(), printStream);
135                         } catch (CommandNotReadyException e) {
136                                 // TODO Auto-generated catch block
137                                 e.printStackTrace();
138                         }
139                         assertTrue(true);
140
141                 }
142
143         }
144
145         @Test
146         public void testExecute_error_1() throws com.att.nsa.apiClient.http.HttpException, IOException {
147                 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new IOException("error"));
148                 PowerMockito.doThrow(new IOException()).when(tm).createTopic("testtopic", "", 1, 1);
149                 PowerMockito.doThrow(new IOException()).when(tm).revokeProducer("read", "1");
150                 String[] parts1 = { "create", "testtopic", "1", "1" };
151                 String[] parts2 = { "grant", "read", "read", "1" };
152                 String[] parts3 = { "revoke", "write", "read", "1" };
153                 String[] parts4 = { "list", "testtopic", "1", "1" };
154                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
155                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
156                         String[] part = (String[]) iterator.next();
157
158                         try {
159                                 command.execute(part, new MRCommandContext(), printStream);
160                         } catch (CommandNotReadyException e) {
161                                 // TODO Auto-generated catch block
162                                 e.printStackTrace();
163                         }
164                         assertTrue(true);
165
166                 }
167
168         }
169
170         @Test
171         public void testExecute_error_2() throws com.att.nsa.apiClient.http.HttpException, IOException {
172                 PowerMockito.when(tm.getTopicMetadata("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
173                 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
174                 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).revokeConsumer("read", "1");
175                 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowConsumer("read", "1");
176                 String[] parts1 = { "create", "testtopic", "1", "1" };
177                 String[] parts2 = { "grant", "write", "write", "1" };
178                 String[] parts3 = { "revoke", "read", "read", "1" };
179                 String[] parts4 = { "list", "testtopic", "1", "1" };
180                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3, parts4);
181                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
182                         String[] part = (String[]) iterator.next();
183
184                         try {
185                                 command.execute(part, new MRCommandContext(), printStream);
186                         } catch (CommandNotReadyException e) {
187                                 // TODO Auto-generated catch block
188                                 e.printStackTrace();
189                         }
190                         assertTrue(true);
191
192                 }
193
194         }
195
196         @Test
197         public void testExecute_error_3() throws com.att.nsa.apiClient.http.HttpException, IOException {
198                 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).createTopic("testtopic", "", 1, 1);
199                 PowerMockito.doThrow(new HttpException(500, "error")).when(tm).allowProducer("read", "1");
200                 String[] parts1 = { "create", "testtopic", "1a", "1a" };
201                 String[] parts2 = { "grant", "write", "read", "1" };
202                 List<String[]> parts = Arrays.asList(parts1, parts2);
203                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
204                         String[] part = (String[]) iterator.next();
205
206                         try {
207                                 command.execute(part, new MRCommandContext(), printStream);
208                         } catch (CommandNotReadyException e) {
209                                 // TODO Auto-generated catch block
210                                 e.printStackTrace();
211                         }
212                         assertTrue(true);
213
214                 }
215
216         }
217
218         @Test
219         public void testDisplayHelp() {
220
221                 command.displayHelp(printStream);
222                 assertTrue(true);
223
224         }
225
226 }