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