2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
48 import org.apache.cxf.jaxrs.client.WebClient;
49 import org.apache.cxf.transport.http.HTTPException;
50 import org.apache.http.HttpStatus;
51 import org.junit.Assert;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 /*import org.junit.Assert;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;*/
59 import org.mockito.InjectMocks;
60 import org.mockito.Matchers;
61 import org.mockito.Mock;
62 import org.mockito.Mockito;
63 import org.mockito.MockitoAnnotations;
64 import org.onap.portalapp.portal.domain.EPApp;
65 import org.onap.portalapp.portal.service.ApplicationsRestClientServiceImpl;
66 import org.onap.portalapp.portal.service.AppsCacheService;
67 import org.onap.portalapp.util.SystemType;
68 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
69 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
70 import org.powermock.api.mockito.PowerMockito;
71 import org.powermock.core.classloader.annotations.PrepareForTest;
72 import org.powermock.modules.junit4.PowerMockRunner;
73 import org.apache.cxf.jaxrs.impl.ResponseImpl;
75 @RunWith(PowerMockRunner.class)
76 @PrepareForTest({WebClient.class,Object.class,CipherUtil.class})
77 public class ApplicationsRestClientServiceImplTest {
80 private AppsCacheService appsCacheService;
83 private ApplicationsRestClientServiceImpl serviceImpl= new ApplicationsRestClientServiceImpl();
87 MockitoAnnotations.initMocks(this);
91 public void unt_get_failure() throws HTTPException{
92 EPApp appTest=new EPApp();
93 appTest.setAppRestEndpoint("https");
94 appTest.setAppBasicAuthPassword("testPassword");
95 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
96 ApplicationsRestClientServiceImpl appservice=serviceImpl.get(ApplicationsRestClientServiceImpl.class, 12L,"/path" );
97 Assert.assertNull(appservice);
101 @Test(expected=ClassCastException.class)
102 public void unt_get_successwithException() throws HTTPException{
103 PowerMockito.mockStatic(WebClient.class);
104 WebClient client=Mockito.mock(WebClient.class);
105 PowerMockito.when(WebClient.create(Matchers.anyString())).thenReturn(client);
106 Response response=Mockito.mock(Response.class);
107 Mockito.when(client.get()).thenReturn(response);
108 Mockito.when(client.type(MediaType.APPLICATION_JSON)).thenReturn(client);
109 Mockito.when(response.getStatus()).thenReturn(HttpStatus.SC_OK);
110 EPApp appTest=new EPApp();
111 appTest.setAppRestEndpoint("https");
112 appTest.setAppBasicAuthPassword("testPassword");
113 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
114 ApplicationsRestClientServiceImpl appservice=serviceImpl.get(ApplicationsRestClientServiceImpl.class, 12L,"/path" );
115 Assert.assertNull(appservice);
119 @Test(expected=ClassCastException.class)
120 public void unt_get_successwithException1() throws HTTPException{
121 PowerMockito.mockStatic(WebClient.class);
122 WebClient client=Mockito.mock(WebClient.class);
123 PowerMockito.when(WebClient.create(Matchers.anyString())).thenReturn(client);
124 Response response=Mockito.mock(Response.class);
125 Mockito.when(client.get()).thenReturn(response);
126 Mockito.when(client.type(MediaType.APPLICATION_JSON)).thenReturn(client);
127 Mockito.when(response.getStatus()).thenReturn(1);
128 EPApp appTest=new EPApp();
129 appTest.setAppRestEndpoint("https");
130 appTest.setAppBasicAuthPassword("testPassword");
131 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
132 ApplicationsRestClientServiceImpl appservice=serviceImpl.get(ApplicationsRestClientServiceImpl.class, 12L,"/path" );
133 Assert.assertNull(appservice);
138 public void unt_post_failure() throws HTTPException{
139 PowerMockito.mockStatic(Object.class);
140 EPApp appTest=new EPApp();
141 appTest.setAppRestEndpoint("https");
142 appTest.setAppBasicAuthPassword("testPassword");
143 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
144 ApplicationsRestClientServiceImpl appservice=serviceImpl.post(ApplicationsRestClientServiceImpl.class, 12L, Matchers.any() , "/path",Matchers.any());
145 Assert.assertNull(appservice);
150 public void unt_post_successwithException() throws HTTPException, CipherUtilException{
151 PowerMockito.mockStatic(CipherUtil.class);
152 PowerMockito.mockStatic(WebClient.class);
153 WebClient client=Mockito.mock(WebClient.class);
154 PowerMockito.when(WebClient.create(Matchers.anyString())).thenReturn(client);
155 PowerMockito.when(CipherUtil.decryptPKC(Matchers.anyString())).thenReturn("password1234");
156 /*Map<String, String> payload = new HashMap<String, String>();
157 payload.put("payload1", Long.toString(1));*/
158 List payload=new ArrayList<>();
160 Response response=Mockito.mock(Response.class);
161 Mockito.when(client.get()).thenReturn(response);
162 Mockito.when(client.type(MediaType.APPLICATION_JSON)).thenReturn(client);
163 Mockito.when(response.getStatus()).thenReturn(HttpStatus.SC_OK);
164 EPApp appTest=new EPApp();
165 appTest.setAppRestEndpoint("https");
166 appTest.setAppBasicAuthPassword("testPassword");
167 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
168 ApplicationsRestClientServiceImpl appservice=serviceImpl.post(ApplicationsRestClientServiceImpl.class, 12L,payload,"/path",SystemType.APPLICATION);
169 Assert.assertNull(appservice);
174 @Test(expected=ClassCastException.class)
175 public void unt_post_successwithException2() throws HTTPException, CipherUtilException{
176 PowerMockito.mockStatic(CipherUtil.class);
177 PowerMockito.mockStatic(WebClient.class);
178 WebClient client=Mockito.mock(WebClient.class);
179 PowerMockito.when(WebClient.create(Matchers.anyString())).thenReturn(client);
180 PowerMockito.when(CipherUtil.decryptPKC(Matchers.anyString())).thenReturn("password1234");
181 /*Map<String, String> payload = new HashMap<String, String>();
182 payload.put("payload1", Long.toString(1));*/
183 List payload=new ArrayList<>();
185 Response response=Mockito.mock(Response.class);
186 Mockito.when(client.post(Matchers.any())).thenReturn(response);
187 Mockito.when(client.type(MediaType.APPLICATION_JSON)).thenReturn(client);
188 Mockito.when(response.getStatus()).thenReturn(HttpStatus.SC_OK);
189 EPApp appTest=new EPApp();
190 appTest.setAppRestEndpoint("https");
191 appTest.setAppBasicAuthPassword("testPassword");
192 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
193 ApplicationsRestClientServiceImpl appservice=serviceImpl.post(ApplicationsRestClientServiceImpl.class, 12L,payload,"/path",SystemType.APPLICATION);
194 Assert.assertNull(appservice);
199 @Test(expected=ClassCastException.class)
200 public void unt_post_successwithException3() throws HTTPException, CipherUtilException{
201 PowerMockito.mockStatic(CipherUtil.class);
202 PowerMockito.mockStatic(WebClient.class);
203 WebClient client=Mockito.mock(WebClient.class);
204 PowerMockito.when(WebClient.create(Matchers.anyString())).thenReturn(client);
205 PowerMockito.when(CipherUtil.decryptPKC(Matchers.anyString())).thenReturn("password1234");
206 /*Map<String, String> payload = new HashMap<String, String>();
207 payload.put("payload1", Long.toString(1));*/
208 List payload=new ArrayList<>();
210 Response response=Mockito.mock(Response.class);
211 Mockito.when(client.post(Matchers.any())).thenReturn(response);
212 Mockito.when(client.type(MediaType.APPLICATION_JSON)).thenReturn(client);
213 Mockito.when(response.getStatus()).thenReturn(1);
214 EPApp appTest=new EPApp();
215 appTest.setAppRestEndpoint("https");
216 appTest.setAppBasicAuthPassword("testPassword");
217 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
218 ApplicationsRestClientServiceImpl appservice=serviceImpl.post(ApplicationsRestClientServiceImpl.class, 12L,payload,"/path",SystemType.APPLICATION);
219 Assert.assertNull(appservice);
224 @Test(expected=ClassCastException.class)
225 public void unt_get_successwithException3() throws HTTPException{
226 PowerMockito.mockStatic(WebClient.class);
227 WebClient client=Mockito.mock(WebClient.class);
228 PowerMockito.when(WebClient.create(Matchers.anyString())).thenReturn(client);
229 Response response=Mockito.mock(Response.class);
230 Mockito.when(client.get()).thenReturn(response);
231 Mockito.when(client.type(MediaType.APPLICATION_JSON)).thenReturn(client);
232 Mockito.when(response.getStatus()).thenReturn(1);
233 EPApp appTest=new EPApp();
234 appTest.setAppRestEndpoint("https");
235 appTest.setAppBasicAuthPassword("testPassword");
236 Mockito.when(appsCacheService.getApp(Matchers.anyLong())).thenReturn(appTest);
237 ApplicationsRestClientServiceImpl appservice=serviceImpl.get(ApplicationsRestClientServiceImpl.class, 12L,"/path" , Matchers.anyBoolean());
238 Assert.assertNull(appservice);