2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.core.restful.client;
42 import org.apache.http.HttpEntity;
43 import org.apache.http.StatusLine;
44 import org.apache.http.client.methods.CloseableHttpResponse;
45 import org.apache.http.impl.client.CloseableHttpClient;
46 import org.apache.http.impl.client.HttpClients;
47 import org.junit.Assert;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.onap.portalsdk.core.domain.App;
54 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
55 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
56 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
57 import org.onap.portalsdk.core.service.AppService;
58 import org.powermock.api.mockito.PowerMockito;
59 import org.powermock.core.classloader.annotations.PrepareForTest;
60 import org.powermock.modules.junit4.PowerMockRunner;
62 @RunWith(PowerMockRunner.class)
63 @PrepareForTest({ PortalApiProperties.class, CipherUtil.class, HttpClients.class})
64 public class PortalRestClientBaseTest {
67 private PortalRestClientBase portalRestClientBase;
70 private AppService appService;
72 @Test(expected = IllegalArgumentException.class)
73 public void getRestWithCredentialsExceptionTest() throws Exception {
74 URI uri = PowerMockito.mock(URI.class);
75 portalRestClientBase.getRestWithCredentials(uri);
76 Assert.assertTrue(true);
80 public void getRestWithCredentialsTest() throws Exception {
81 URI uri = PowerMockito.mock(URI.class);
84 app.setUsername("User");
85 String password = "Password";
86 app.setAppPassword(password);
87 Mockito.when(appService.getDefaultApp()).thenReturn(app);
89 PowerMockito.mockStatic(PortalApiProperties.class);
90 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
92 PowerMockito.mockStatic(CipherUtil.class);
93 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
95 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
96 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
98 PowerMockito.mockStatic(HttpClients.class);
99 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
100 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
101 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
102 Mockito.when(response.getEntity()).thenReturn(entity);
103 StatusLine statusLine = Mockito.mock(StatusLine.class);
104 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
106 portalRestClientBase.getRestWithCredentials(uri);
107 Assert.assertTrue(true);
111 public void getRestWithCredentialsEntityNullTest() throws Exception {
112 URI uri = PowerMockito.mock(URI.class);
115 app.setUsername("User");
116 String password = "Password";
117 app.setAppPassword(password);
118 Mockito.when(appService.getDefaultApp()).thenReturn(app);
120 PowerMockito.mockStatic(PortalApiProperties.class);
121 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
123 PowerMockito.mockStatic(CipherUtil.class);
124 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
126 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
127 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
129 PowerMockito.mockStatic(HttpClients.class);
130 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
131 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
132 StatusLine statusLine = Mockito.mock(StatusLine.class);
133 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
135 portalRestClientBase.getRestWithCredentials(uri);
136 Assert.assertTrue(true);
139 @Test(expected = IllegalArgumentException.class)
140 public void postRestWithCredentialsExceptionTest() throws Exception {
141 URI uri = PowerMockito.mock(URI.class);
143 portalRestClientBase.postRestWithCredentials(uri,json);
144 Assert.assertTrue(true);
148 public void postRestWithCredentialsWithEntityTest() throws Exception {
149 URI uri = PowerMockito.mock(URI.class);
152 app.setUsername("User");
153 String password = "Password";
154 app.setAppPassword(password);
155 Mockito.when(appService.getDefaultApp()).thenReturn(app);
157 PowerMockito.mockStatic(PortalApiProperties.class);
158 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
160 PowerMockito.mockStatic(CipherUtil.class);
161 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
163 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
164 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
166 PowerMockito.mockStatic(HttpClients.class);
167 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
168 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
169 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
170 Mockito.when(response.getEntity()).thenReturn(entity);
171 StatusLine statusLine = Mockito.mock(StatusLine.class);
172 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
174 String json = "JSON";
175 portalRestClientBase.postRestWithCredentials(uri, json);
176 Assert.assertTrue(true);
180 public void postRestWithCredentialsNullEntityTest() throws Exception {
181 URI uri = PowerMockito.mock(URI.class);
184 app.setUsername("User");
185 String password = "Password";
186 app.setAppPassword(password);
187 Mockito.when(appService.getDefaultApp()).thenReturn(app);
189 PowerMockito.mockStatic(PortalApiProperties.class);
190 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
192 PowerMockito.mockStatic(CipherUtil.class);
193 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
195 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
196 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
198 PowerMockito.mockStatic(HttpClients.class);
199 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
200 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
201 StatusLine statusLine = Mockito.mock(StatusLine.class);
202 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
204 String json = "JSON";
205 portalRestClientBase.postRestWithCredentials(uri, json);
206 Assert.assertTrue(true);