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