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.restful.client.PortalRestClientBase;
58 import org.onap.portalsdk.core.service.AppService;
59 import org.powermock.api.mockito.PowerMockito;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({ PortalApiProperties.class, CipherUtil.class, HttpClients.class})
65 public class PortalRestClientBaseTest {
68 private PortalRestClientBase portalRestClientBase;
71 private AppService appService;
73 @Test(expected = IllegalArgumentException.class)
74 public void getRestWithCredentialsExceptionTest() throws Exception {
75 URI uri = PowerMockito.mock(URI.class);
76 portalRestClientBase.getRestWithCredentials(uri);
77 Assert.assertTrue(true);
81 public void getRestWithCredentialsTest() throws Exception {
82 URI uri = PowerMockito.mock(URI.class);
85 app.setUsername("User");
86 String password = "Password";
87 app.setAppPassword(password);
88 Mockito.when(appService.getDefaultApp()).thenReturn(app);
90 PowerMockito.mockStatic(PortalApiProperties.class);
91 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
93 PowerMockito.mockStatic(CipherUtil.class);
94 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
96 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
97 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
99 PowerMockito.mockStatic(HttpClients.class);
100 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
101 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
102 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
103 Mockito.when(response.getEntity()).thenReturn(entity);
104 StatusLine statusLine = Mockito.mock(StatusLine.class);
105 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
107 portalRestClientBase.getRestWithCredentials(uri);
108 Assert.assertTrue(true);
112 public void getRestWithCredentialsEntityNullTest() throws Exception {
113 URI uri = PowerMockito.mock(URI.class);
116 app.setUsername("User");
117 String password = "Password";
118 app.setAppPassword(password);
119 Mockito.when(appService.getDefaultApp()).thenReturn(app);
121 PowerMockito.mockStatic(PortalApiProperties.class);
122 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
124 PowerMockito.mockStatic(CipherUtil.class);
125 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
127 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
128 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
130 PowerMockito.mockStatic(HttpClients.class);
131 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
132 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
133 StatusLine statusLine = Mockito.mock(StatusLine.class);
134 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
136 portalRestClientBase.getRestWithCredentials(uri);
137 Assert.assertTrue(true);
140 @Test(expected = IllegalArgumentException.class)
141 public void postRestWithCredentialsExceptionTest() throws Exception {
142 URI uri = PowerMockito.mock(URI.class);
144 portalRestClientBase.postRestWithCredentials(uri,json);
145 Assert.assertTrue(true);
149 public void postRestWithCredentialsWithEntityTest() throws Exception {
150 URI uri = PowerMockito.mock(URI.class);
153 app.setUsername("User");
154 String password = "Password";
155 app.setAppPassword(password);
156 Mockito.when(appService.getDefaultApp()).thenReturn(app);
158 PowerMockito.mockStatic(PortalApiProperties.class);
159 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
161 PowerMockito.mockStatic(CipherUtil.class);
162 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
164 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
165 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
167 PowerMockito.mockStatic(HttpClients.class);
168 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
169 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
170 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
171 Mockito.when(response.getEntity()).thenReturn(entity);
172 StatusLine statusLine = Mockito.mock(StatusLine.class);
173 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
175 String json = "JSON";
176 portalRestClientBase.postRestWithCredentials(uri, json);
177 Assert.assertTrue(true);
181 public void postRestWithCredentialsNullEntityTest() throws Exception {
182 URI uri = PowerMockito.mock(URI.class);
185 app.setUsername("User");
186 String password = "Password";
187 app.setAppPassword(password);
188 Mockito.when(appService.getDefaultApp()).thenReturn(app);
190 PowerMockito.mockStatic(PortalApiProperties.class);
191 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
193 PowerMockito.mockStatic(CipherUtil.class);
194 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
196 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
197 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
199 PowerMockito.mockStatic(HttpClients.class);
200 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
201 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
202 StatusLine statusLine = Mockito.mock(StatusLine.class);
203 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
205 String json = "JSON";
206 portalRestClientBase.postRestWithCredentials(uri, json);
207 Assert.assertTrue(true);