update the package name
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / tools / ApiKeyCommandTest.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.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41
42 import com.att.nsa.apiClient.credentials.ApiCredential;
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.MRClient.MRApiException;
47 import org.onap.dmaap.mr.client.MRClientFactory;
48 import org.onap.dmaap.mr.client.MRIdentityManager;
49 import org.onap.dmaap.mr.client.MRIdentityManager.ApiKey;
50
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({ MRClientFactory.class })
53 public class ApiKeyCommandTest {
54
55         @InjectMocks
56         private ApiKeyCommand command;
57         @Mock
58         private MRIdentityManager tm;
59         @Mock
60         private ApiKey ti;
61         @Mock
62         private ApiKey key;
63         @Mock
64         private ApiCredential ac;
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.when(MRClientFactory.createIdentityManager(Arrays.asList("localhost"), null, null)).thenReturn(tm);
73                 PowerMockito.when(tm.getApiKey("testtopic")).thenReturn(key);
74                 PowerMockito.when(tm.createApiKey("testtopic", "1")).thenReturn(ac);
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 = { "create", "testtopic", "1" };
108                 String[] parts2 = { "list", "testtopic", "1" };
109                 String[] parts3 = { "revoke", "write", "read" };
110                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
111                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
112                         String[] part = (String[]) iterator.next();
113
114                         try {
115                                 command.execute(part, new MRCommandContext(), printStream);
116                         } catch (CommandNotReadyException e) {
117                                 // TODO Auto-generated catch block
118                                 e.printStackTrace();
119                         }
120                         assertTrue(true);
121
122                 }
123         }
124
125          @Test
126         public void testExecute_error1() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
127                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new IOException("error"));
128                 String[] parts1 = { "create", "testtopic", "1" };
129                 String[] parts2 = { "list", "testtopic", "1" };
130                 String[] parts3 = { "revoke", "write", "read" };
131                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
132                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
133                         String[] part = (String[]) iterator.next();
134
135                         try {
136                                 command.execute(part, new MRCommandContext(), printStream);
137                         } catch (CommandNotReadyException e) {
138                                 // TODO Auto-generated catch block
139                                 e.printStackTrace();
140                         }
141                         assertTrue(true);
142                 }
143
144         }
145
146          @Test
147         public void testExecute_error2() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
148                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new MRApiException("error"));
149                 String[] parts1 = { "create", "testtopic", "1" };
150                 String[] parts2 = { "list", "testtopic", "1" };
151                 String[] parts3 = { "revoke", "write", "read" };
152                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
153                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
154                         String[] part = (String[]) iterator.next();
155
156                         try {
157                                 command.execute(part, new MRCommandContext(),printStream);
158                         } catch (CommandNotReadyException e) {
159                                 // TODO Auto-generated catch block
160                                 e.printStackTrace();
161                         }
162                         assertTrue(true);
163
164                 }
165         }
166
167          @Test
168         public void testExecute_error3() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
169                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpException(500, "error"));
170                 String[] parts1 = { "create", "testtopic", "1" };
171                 String[] parts2 = { "list", "testtopic", "1" };
172                 String[] parts3 = { "revoke", "write", "read" };
173                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
174                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
175                         String[] part = (String[]) iterator.next();
176
177                         try {
178                                 command.execute(part, new MRCommandContext(), printStream);
179                         } catch (CommandNotReadyException e) {
180                                 // TODO Auto-generated catch block
181                                 e.printStackTrace();
182                         }
183                         assertTrue(true);
184                 }
185
186         }
187
188          @Test
189         public void testExecute_error4() throws HttpObjectNotFoundException, HttpException, MRApiException, IOException {
190                 PowerMockito.when(tm.getApiKey("testtopic")).thenThrow(new HttpObjectNotFoundException("error"));
191                 String[] parts1 = { "create", "testtopic", "1" };
192                 String[] parts2 = { "list", "testtopic", "1" };
193                 String[] parts3 = { "revoke", "write", "read" };
194                 List<String[]> parts = Arrays.asList(parts1, parts2, parts3);
195                 for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
196                         String[] part = (String[]) iterator.next();
197
198                         try {
199                                 command.execute(part, new MRCommandContext(), printStream);
200                         } catch (CommandNotReadyException e) {
201                                 // TODO Auto-generated catch block
202                                 e.printStackTrace();
203                         }
204                         assertTrue(true);
205
206                 }
207         }
208
209          @Test
210         public void testDisplayHelp() {
211
212                 command.displayHelp(printStream);
213                 assertTrue(true);
214
215         }
216
217 }