1 package org.openecomp.portalapp.portal.test.service;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
7 import java.util.ArrayList;
8 import java.util.HashMap;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.InjectMocks;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.openecomp.portalapp.portal.domain.CentralRoleFunction;
24 import org.openecomp.portalapp.portal.domain.EPApp;
25 import org.openecomp.portalapp.portal.domain.EPAppRoleFunction;
26 import org.openecomp.portalapp.portal.domain.EPRole;
27 import org.openecomp.portalapp.portal.domain.EPUser;
28 import org.openecomp.portalapp.portal.service.ExternalAccessRolesServiceImpl;
29 import org.openecomp.portalapp.portal.test.core.MockEPUser;
30 import org.openecomp.portalapp.portal.transport.ExternalAccessPerms;
31 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
32 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
33 import org.openecomp.portalapp.test.framework.MockitoTestSuite;
34 import org.openecomp.portalsdk.core.domain.Role;
35 import org.openecomp.portalsdk.core.service.DataAccessService;
36 import org.openecomp.portalsdk.core.service.DataAccessServiceImpl;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.client.HttpClientErrorException;
47 import org.springframework.web.client.RestTemplate;
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, EPCommonSystemProperties.class })
51 public class ExternalAccessRolesServiceImplTest {
53 DataAccessService dataAccessService1 = new DataAccessServiceImpl();
56 RestTemplate template = new RestTemplate();
59 ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
63 MockitoAnnotations.initMocks(this);
66 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
68 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
69 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
70 NullPointerException nullPointerException = new NullPointerException();
71 MockEPUser mockUser = new MockEPUser();
73 public EPApp mockApp() {
74 EPApp app = new EPApp();
76 app.setImageUrl("test");
77 app.setDescription("test");
81 app.setAppRestEndpoint("test");
82 app.setAlternateUrl("test");
84 app.setMlAppName("test");
85 app.setMlAppAdminId("test");
86 app.setUsername("test");
87 app.setAppPassword("test");
89 app.setEnabled(false);
90 app.setUebKey("test");
91 app.setUebSecret("test");
92 app.setUebTopicName("test");
98 public void getAppRolesIfAppIsPortalTest() throws Exception {
99 List<EPRole> applicationRoles = new ArrayList<>();
100 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
101 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
102 assertEquals(expectedApplicationRoles, applicationRoles);
106 public void getAppRolesTest() throws Exception {
107 List<EPRole> applicationRoles = new ArrayList<>();
108 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
109 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
110 assertEquals(expectedApplicationRoles, applicationRoles);
113 // @SuppressWarnings("null")
114 // @Test(expected = java.lang.Exception.class)
115 // public void getAppRolesExceptionTest() throws Exception{
116 // List<EPRole> applicationRoles = new ArrayList<>();
117 // DataAccessService dataAccessService = null ;
118 // Mockito.when(dataAccessService.getList(EPRole.class, "where app_id = 10",
119 // null, null)).thenThrow(nullPointerException);
120 // List<EPRole> expectedApplicationRoles =
121 // externalAccessRolesServiceImpl.getAppRoles((long) 10);
122 // assertEquals(expectedApplicationRoles,applicationRoles);
125 @Test(expected = java.lang.Exception.class)
126 public void getAppExceptionTest() throws Exception {
127 List<EPApp> app = new ArrayList<>();
128 String uebKey = "test-ueb-key";
129 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
131 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
132 assertEquals(app, expectedapp);
135 @Test(expected = java.lang.Exception.class)
136 public void getAppTest() throws Exception {
137 List<EPApp> appList = new ArrayList<>();
138 EPApp app = mockApp();
140 String uebKey = "test-ueb-key";
141 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
142 .thenReturn(appList);
143 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
147 public void getAppErrorTest() throws Exception {
148 List<EPApp> appList = new ArrayList<>();
149 EPApp app = mockApp();
152 String uebKey = "test-ueb-key";
153 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
154 .thenReturn(appList);
155 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
156 assertEquals(appList, expectedapp);
160 public void addRoleTest() throws Exception {
161 HttpHeaders headers = new HttpHeaders();
162 PowerMockito.mockStatic(EcompPortalUtils.class);
163 PowerMockito.mockStatic(SystemProperties.class);
164 PowerMockito.mockStatic(EPCommonSystemProperties.class);
165 String uebKey = "test-ueb-key";
166 Role role = new Role();
167 role.setId((long) 25);
168 EPApp app = mockApp();
169 app.setEnabled(true);
170 app.setId((long) 10);
171 app.setNameSpace("test_namesapce");
172 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
173 List<EPApp> appList = new ArrayList<>();
175 List<EPRole> roleList = new ArrayList<>();
176 EPRole ePRole = new EPRole();
177 role.setName("Test Role");
178 roleList.add(ePRole);
179 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
180 .thenReturn(appList);
181 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId(), null, null))
182 .thenReturn(roleList);
183 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
184 .thenReturn("Testurl");
185 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
186 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
187 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
189 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
193 public void addRoleMethodNotAllowedTest() throws Exception {
194 HttpHeaders headers = new HttpHeaders();
195 PowerMockito.mockStatic(EcompPortalUtils.class);
196 PowerMockito.mockStatic(SystemProperties.class);
197 PowerMockito.mockStatic(EPCommonSystemProperties.class);
198 String uebKey = "test-ueb-key";
199 Role role = new Role();
200 role.setId((long) 25);
201 EPApp app = mockApp();
202 app.setEnabled(true);
203 app.setId((long) 10);
204 app.setNameSpace("test_namesapce");
205 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
206 List<EPApp> appList = new ArrayList<>();
208 List<EPRole> roleList = new ArrayList<>();
209 EPRole ePRole = new EPRole();
210 role.setName("Test Role");
211 roleList.add(ePRole);
212 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
213 .thenReturn(appList);
214 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId(), null, null))
215 .thenReturn(roleList);
216 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
217 .thenReturn("Testurl");
218 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
219 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
220 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
222 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
225 public EPApp getApp() {
226 EPApp app = new EPApp();
228 app.setImageUrl("test");
229 app.setDescription("test");
230 app.setNotes("test");
233 app.setAppRestEndpoint("test");
234 app.setAlternateUrl("test");
236 app.setMlAppName("test");
237 app.setMlAppAdminId("test");
238 app.setUsername("test");
239 app.setAppPassword("test");
241 app.setEnabled(false);
242 app.setUebKey("test");
243 app.setUebSecret("test");
244 app.setUebTopicName("test");
250 public void deleteCentralRoleFunctionTest() throws Exception {
251 final Map<String, String> params = new HashMap<>();
252 EPApp app = mockApp();
253 params.put("functionCd", "menu_fun_code");
254 params.put("appId", String.valueOf(10));
255 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
256 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
257 domainCentralRoleFunction.setCode("menu_fun_code");
258 centralRoleFunctionList.add(domainCentralRoleFunction);
259 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
260 .thenReturn(centralRoleFunctionList);
261 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
262 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
263 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
264 HttpHeaders headers = new HttpHeaders();
265 PowerMockito.mockStatic(EcompPortalUtils.class);
266 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
268 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
269 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
271 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
275 public void deleteCentralRoleFunctionFailTest() throws Exception {
276 final Map<String, String> params = new HashMap<>();
277 EPApp app = mockApp();
278 params.put("functionCd", "menu_fun_code");
279 params.put("appId", String.valueOf(10));
280 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
281 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
282 domainCentralRoleFunction.setCode("menu_fun_code");
283 centralRoleFunctionList.add(domainCentralRoleFunction);
284 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
285 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
286 .thenReturn(centralRoleFunctionList);
287 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.NOT_FOUND);
288 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
289 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
290 HttpHeaders headers = new HttpHeaders();
291 PowerMockito.mockStatic(EcompPortalUtils.class);
292 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
294 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
295 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
297 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
301 public void deleteCentralRoleFunctionExceptionTest() {
302 final Map<String, String> params = new HashMap<>();
303 EPApp app = mockApp();
304 params.put("functionCd", "menu_fun_code");
305 params.put("appId", String.valueOf(10));
306 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
307 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
308 domainCentralRoleFunction.setCode("menu_fun_code");
309 centralRoleFunctionList.add(domainCentralRoleFunction);
310 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
311 .thenThrow(nullPointerException);
312 assertFalse(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
316 public void getUserTest() {
317 List<EPUser> userList = new ArrayList<>();
318 EPUser user = mockUser.mockEPUser();
320 Mockito.when(dataAccessService1.getList(EPUser.class, " where org_user_id = '" + "guestT" + "'", null, null))
321 .thenReturn(userList);
322 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
323 assertEquals(expectedUserList, userList);
326 public void saveCentralRoleFunctionTest() throws Exception {
327 CentralRoleFunction centralRoleFunction = new CentralRoleFunction();
328 centralRoleFunction.setCode("menu_test");
329 EPApp app = mockApp();
331 final Map<String, String> params = new HashMap<>();
332 params.put("functionCd", "menu_test");
333 params.put("appId", String.valueOf(1));
334 ExternalAccessPerms extPerms = new ExternalAccessPerms();
335 PowerMockito.mockStatic(EcompPortalUtils.class);
336 HttpHeaders headers = new HttpHeaders();
337 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
338 List<CentralRoleFunction> appRoleFunc = new ArrayList<>();
339 appRoleFunc.add(centralRoleFunction);
340 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params,
341 null)).thenReturn(appRoleFunc);
342 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
343 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
344 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);