0dc259e7559f1cd827c34db47de90a04aa5fa941
[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.service.AppService;
58 import org.powermock.api.mockito.PowerMockito;
59 import org.powermock.core.classloader.annotations.PrepareForTest;
60 import org.powermock.modules.junit4.PowerMockRunner;
61
62 @RunWith(PowerMockRunner.class)
63 @PrepareForTest({ PortalApiProperties.class, CipherUtil.class, HttpClients.class})
64 public class PortalRestClientBaseTest {
65
66         @InjectMocks
67         private PortalRestClientBase portalRestClientBase;
68
69         @Mock
70         private AppService appService;
71
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);
77         }
78
79         @Test
80         public void getRestWithCredentialsTest() throws Exception {
81                 URI uri = PowerMockito.mock(URI.class);
82
83                 App app = new App();
84                 app.setUsername("User");
85                 String password = "Password";
86                 app.setAppPassword(password);
87                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
88                 
89                 PowerMockito.mockStatic(PortalApiProperties.class);
90                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
91                 
92                 PowerMockito.mockStatic(CipherUtil.class);
93                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
94                 
95                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
96                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
97                 
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);
105                 
106                 portalRestClientBase.getRestWithCredentials(uri);
107                 Assert.assertTrue(true);
108         }
109         
110         @Test
111         public void getRestWithCredentialsEntityNullTest() throws Exception {
112                 URI uri = PowerMockito.mock(URI.class);
113
114                 App app = new App();
115                 app.setUsername("User");
116                 String password = "Password";
117                 app.setAppPassword(password);
118                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
119                 
120                 PowerMockito.mockStatic(PortalApiProperties.class);
121                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
122                 
123                 PowerMockito.mockStatic(CipherUtil.class);
124                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
125                 
126                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
127                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
128                 
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);
134                 
135                 portalRestClientBase.getRestWithCredentials(uri);
136                 Assert.assertTrue(true);
137         }
138         
139         @Test(expected = IllegalArgumentException.class)
140         public void postRestWithCredentialsExceptionTest() throws Exception {
141                         URI uri = PowerMockito.mock(URI.class);
142                         String json = "";
143                         portalRestClientBase.postRestWithCredentials(uri,json);
144                         Assert.assertTrue(true);
145         }
146         
147         @Test
148         public void postRestWithCredentialsWithEntityTest() throws Exception {
149                 URI uri = PowerMockito.mock(URI.class);
150
151                 App app = new App();
152                 app.setUsername("User");
153                 String password = "Password";
154                 app.setAppPassword(password);
155                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
156                 
157                 PowerMockito.mockStatic(PortalApiProperties.class);
158                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
159                 
160                 PowerMockito.mockStatic(CipherUtil.class);
161                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
162                 
163                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
164                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
165                 
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);
173                 
174                 String json = "JSON";
175                 portalRestClientBase.postRestWithCredentials(uri, json);
176                 Assert.assertTrue(true);
177         }
178         
179         @Test
180         public void postRestWithCredentialsNullEntityTest() throws Exception {
181                 URI uri = PowerMockito.mock(URI.class);
182
183                 App app = new App();
184                 app.setUsername("User");
185                 String password = "Password";
186                 app.setAppPassword(password);
187                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
188                 
189                 PowerMockito.mockStatic(PortalApiProperties.class);
190                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
191                 
192                 PowerMockito.mockStatic(CipherUtil.class);
193                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
194                 
195                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
196                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
197                 
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);
203                 
204                 String json = "JSON";
205                 portalRestClientBase.postRestWithCredentials(uri, json);
206                 Assert.assertTrue(true);
207         }
208 }