056a11902a85c499b4a6404e45c01678b6879ad1
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
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  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.core.restful.client;
39
40 import java.net.URI;
41
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;
62
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({ PortalApiProperties.class, CipherUtil.class, HttpClients.class})
65 public class PortalRestClientBaseTest {
66
67         @InjectMocks
68         private PortalRestClientBase portalRestClientBase;
69
70         @Mock
71         private AppService appService;
72
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);
78         }
79
80         @Test
81         public void getRestWithCredentialsTest() throws Exception {
82                 URI uri = PowerMockito.mock(URI.class);
83
84                 App app = new App();
85                 app.setUsername("User");
86                 String password = "Password";
87                 app.setAppPassword(password);
88                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
89                 
90                 PowerMockito.mockStatic(PortalApiProperties.class);
91                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
92                 
93                 PowerMockito.mockStatic(CipherUtil.class);
94                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
95                 
96                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
97                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
98                 
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);
106                 
107                 portalRestClientBase.getRestWithCredentials(uri);
108                 Assert.assertTrue(true);
109         }
110         
111         @Test
112         public void getRestWithCredentialsEntityNullTest() throws Exception {
113                 URI uri = PowerMockito.mock(URI.class);
114
115                 App app = new App();
116                 app.setUsername("User");
117                 String password = "Password";
118                 app.setAppPassword(password);
119                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
120                 
121                 PowerMockito.mockStatic(PortalApiProperties.class);
122                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
123                 
124                 PowerMockito.mockStatic(CipherUtil.class);
125                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
126                 
127                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
128                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
129                 
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);
135                 
136                 portalRestClientBase.getRestWithCredentials(uri);
137                 Assert.assertTrue(true);
138         }
139         
140         @Test(expected = IllegalArgumentException.class)
141         public void postRestWithCredentialsExceptionTest() throws Exception {
142                         URI uri = PowerMockito.mock(URI.class);
143                         String json = "";
144                         portalRestClientBase.postRestWithCredentials(uri,json);
145                         Assert.assertTrue(true);
146         }
147         
148         @Test
149         public void postRestWithCredentialsWithEntityTest() throws Exception {
150                 URI uri = PowerMockito.mock(URI.class);
151
152                 App app = new App();
153                 app.setUsername("User");
154                 String password = "Password";
155                 app.setAppPassword(password);
156                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
157                 
158                 PowerMockito.mockStatic(PortalApiProperties.class);
159                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
160                 
161                 PowerMockito.mockStatic(CipherUtil.class);
162                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
163                 
164                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
165                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
166                 
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);
174                 
175                 String json = "JSON";
176                 portalRestClientBase.postRestWithCredentials(uri, json);
177                 Assert.assertTrue(true);
178         }
179         
180         @Test
181         public void postRestWithCredentialsNullEntityTest() throws Exception {
182                 URI uri = PowerMockito.mock(URI.class);
183
184                 App app = new App();
185                 app.setUsername("User");
186                 String password = "Password";
187                 app.setAppPassword(password);
188                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
189                 
190                 PowerMockito.mockStatic(PortalApiProperties.class);
191                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
192                 
193                 PowerMockito.mockStatic(CipherUtil.class);
194                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
195                 
196                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
197                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
198                 
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);
204                 
205                 String json = "JSON";
206                 portalRestClientBase.postRestWithCredentials(uri, json);
207                 Assert.assertTrue(true);
208         }
209 }