one more case added in DmaapClientUtilTest.java
[dmaap/messagerouter/dmaapclient.git] / src / test / java / com / att / nsa / mr / tools / ApiKeyCommandTest.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 \r
31 import org.junit.After;\r
32 import org.junit.Before;\r
33 import org.junit.Test;\r
34 import org.junit.runner.RunWith;\r
35 import org.mockito.InjectMocks;\r
36 import org.mockito.Mock;\r
37 import org.mockito.MockitoAnnotations;\r
38 import org.powermock.api.mockito.PowerMockito;\r
39 import org.powermock.core.classloader.annotations.PrepareForTest;\r
40 import org.powermock.modules.junit4.PowerMockRunner;\r
41 \r
42 import com.att.nsa.apiClient.credentials.ApiCredential;\r
43 import com.att.nsa.apiClient.http.HttpException;\r
44 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;\r
45 import com.att.nsa.cmdtool.CommandNotReadyException;\r
46 import com.att.nsa.mr.client.MRClient.MRApiException;\r
47 import com.att.nsa.mr.client.MRClientFactory;\r
48 import com.att.nsa.mr.client.MRIdentityManager;\r
49 import com.att.nsa.mr.client.MRIdentityManager.ApiKey;\r
50 \r
51 @RunWith(PowerMockRunner.class)\r
52 @PrepareForTest({ MRClientFactory.class })\r
53 public class ApiKeyCommandTest {\r
54 \r
55         @InjectMocks\r
56         private ApiKeyCommand command;\r
57         @Mock\r
58         private MRIdentityManager tm;\r
59         @Mock\r
60         private ApiKey ti;\r
61         @Mock\r
62         private ApiKey key;\r
63         @Mock\r
64         private ApiCredential ac;\r
65         @Mock\r
66         private PrintStream printStream;\r
67 \r
68         @Before\r
69         public void setUp() throws Exception {\r
70                 MockitoAnnotations.initMocks(this);\r
71                 PowerMockito.mockStatic(MRClientFactory.class);\r
72                 PowerMockito.when(MRClientFactory.createIdentityManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);\r
73                 PowerMockito.when(tm.getApiKey("testtopic")).thenReturn(key);\r
74                 PowerMockito.when(tm.createApiKey("testtopic", "1")).thenReturn(ac);\r
75 \r
76         }\r
77 \r
78         @After\r
79         public void tearDown() throws Exception {\r
80 \r
81         }\r
82 \r
83         @Test\r
84         public void testGetMatches() {\r
85 \r
86                 command.getMatches();\r
87                 assertTrue(true);\r
88 \r
89         }\r
90 \r
91         @Test\r
92         public void testCheckReady() {\r
93 \r
94                 try {\r
95                         command.checkReady(new MRCommandContext());\r
96                 } catch (CommandNotReadyException e) {\r
97                         // TODO Auto-generated catch block\r
98                         e.printStackTrace();\r
99                 }\r
100                 assertTrue(true);\r
101 \r
102         }\r
103 \r
104          @Test\r
105         public void testExecute() {\r
106 \r
107                 String[] parts1 = { "create", "testtopic", "1" };\r
108                 String[] parts2 = { "list", "testtopic", "1" };\r
109                 String[] parts3 = { "revoke", "write", "read" };\r
110                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);\r
111                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {\r
112                         String[] part = (String[]) iterator.next();\r
113 \r
114                         try {\r
115                                 command.execute(part, new MRCommandContext(), printStream);\r
116                         } catch (CommandNotReadyException e) {\r
117                                 // TODO Auto-generated catch block\r
118                                 e.printStackTrace();\r
119                         }\r
120                         assertTrue(true);\r
121 \r
122                 }\r
123         }\r
124 \r
125          @Test\r
126         public void testExecute_error1() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {\r
127                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new IOException("error"));\r
128                 String[] parts1 = { "create", "testtopic", "1" };\r
129                 String[] parts2 = { "list", "testtopic", "1" };\r
130                 String[] parts3 = { "revoke", "write", "read" };\r
131                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);\r
132                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {\r
133                         String[] part = (String[]) iterator.next();\r
134 \r
135                         try {\r
136                                 command.execute(part, new MRCommandContext(), printStream);\r
137                         } catch (CommandNotReadyException e) {\r
138                                 // TODO Auto-generated catch block\r
139                                 e.printStackTrace();\r
140                         }\r
141                         assertTrue(true);\r
142                 }\r
143 \r
144         }\r
145 \r
146          @Test\r
147         public void testExecute_error2() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {\r
148                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new MRApiException("error"));\r
149                 String[] parts1 = { "create", "testtopic", "1" };\r
150                 String[] parts2 = { "list", "testtopic", "1" };\r
151                 String[] parts3 = { "revoke", "write", "read" };\r
152                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);\r
153                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {\r
154                         String[] part = (String[]) iterator.next();\r
155 \r
156                         try {\r
157                                 command.execute(part, new MRCommandContext(),printStream);\r
158                         } catch (CommandNotReadyException e) {\r
159                                 // TODO Auto-generated catch block\r
160                                 e.printStackTrace();\r
161                         }\r
162                         assertTrue(true);\r
163 \r
164                 }\r
165         }\r
166 \r
167          @Test\r
168         public void testExecute_error3() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {\r
169                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpException(500, "error"));\r
170                 String[] parts1 = { "create", "testtopic", "1" };\r
171                 String[] parts2 = { "list", "testtopic", "1" };\r
172                 String[] parts3 = { "revoke", "write", "read" };\r
173                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);\r
174                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {\r
175                         String[] part = (String[]) iterator.next();\r
176 \r
177                         try {\r
178                                 command.execute(part, new MRCommandContext(), printStream);\r
179                         } catch (CommandNotReadyException e) {\r
180                                 // TODO Auto-generated catch block\r
181                                 e.printStackTrace();\r
182                         }\r
183                         assertTrue(true);\r
184                 }\r
185 \r
186         }\r
187 \r
188          @Test\r
189         public void testExecute_error4() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {\r
190                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));\r
191                 String[] parts1 = { "create", "testtopic", "1" };\r
192                 String[] parts2 = { "list", "testtopic", "1" };\r
193                 String[] parts3 = { "revoke", "write", "read" };\r
194                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);\r
195                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {\r
196                         String[] part = (String[]) iterator.next();\r
197 \r
198                         try {\r
199                                 command.execute(part, new MRCommandContext(), printStream);\r
200                         } catch (CommandNotReadyException e) {\r
201                                 // TODO Auto-generated catch block\r
202                                 e.printStackTrace();\r
203                         }\r
204                         assertTrue(true);\r
205 \r
206                 }\r
207         }\r
208 \r
209          @Test\r
210         public void testDisplayHelp() {\r
211 \r
212                 command.displayHelp(printStream);\r
213                 assertTrue(true);\r
214 \r
215         }\r
216 \r
217 }\r