Merge "Fix sql injection vulnerability"
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / AdminRolesServiceImplTest.java
index ef9bda0..f14d3fd 100644 (file)
@@ -4,6 +4,8 @@
  * ===================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
  * under the Apache License, Version 2.0 (the "License");
  *
  * ============LICENSE_END============================================
  *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * 
  */
 package org.onap.portalapp.portal.service;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
@@ -92,6 +95,9 @@ public class AdminRolesServiceImplTest {
        @Mock
        DataAccessService dataAccessService = new DataAccessServiceImpl();
 
+    @Mock
+    ExternalAccessRolesService externalAccessRolesService;
+
        @Mock
        EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
 
@@ -127,6 +133,8 @@ public class AdminRolesServiceImplTest {
 
        private Long ACCOUNT_ADMIN_ROLE_ID = 999L;
        
+       private Long ECOMP_APP_ID = 1L;
+       
        public EPApp mockApp() {
                EPApp app = new EPApp();
                app.setName("Test");
@@ -178,7 +186,27 @@ public class AdminRolesServiceImplTest {
                userAppList.add(epUserApp);
                Mockito.when(dataAccessService.getList(EPUserApp.class,
                                " where userId = " + user.getId() + " and role.id = " + 999, null, null)).thenReturn(userAppList);
-               adminRolesServiceImpl.getAppsWithAdminRoleStateForUser(user.getOrgUserId());
+               List<EPApp> appsList = new ArrayList<>();
+               appsList.add(app);
+               Mockito.when(dataAccessService.getList(EPApp.class,
+                               "  where ( enabled = 'Y' or id = " + ECOMP_APP_ID + ")", null, null)).thenReturn(appsList);
+               AppsListWithAdminRole  actual = adminRolesServiceImpl.getAppsWithAdminRoleStateForUser(user.getOrgUserId());
+               assertNotNull(actual);
+       }
+       
+       @Test
+       public void getAppsWithAdminRoleStateForUserTestWithException() {
+               EPUser user = mockUser.mockEPUser();
+               EPApp app = mockApp();
+               app.setId(1l);
+               List<EPUser> users = new ArrayList<>();
+               users.add(user);
+               Map<String, String> userParams = new HashMap<>();
+               userParams.put("org_user_id", user.getOrgUserId());
+               Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)).thenReturn(users);
+               AppsListWithAdminRole  actual = adminRolesServiceImpl.getAppsWithAdminRoleStateForUser(user.getOrgUserId());
+
+               
        }
 
        @SuppressWarnings({ "deprecation", "unchecked" })
@@ -347,6 +375,25 @@ public class AdminRolesServiceImplTest {
                assertFalse(actual);
        }
 
+    @Test
+    public void isAccountAdminUserNull() {
+        boolean actual = adminRolesServiceImpl.isAccountAdmin(null);
+        assertFalse(actual);
+    }
+
+    @Test
+    public void isRoleAdminTest() {
+        EPUser user = mockUser.mockEPUser();
+        List<String> roles = new ArrayList<>();
+        roles.add("approver\\|");
+        Mockito.when(dataAccessService.executeNamedQuery(
+            Matchers.eq("getRoleFunctionsOfUserforAlltheApplications"), Matchers.any(), Matchers.any()))
+                .thenReturn(roles);
+        Mockito.when(externalAccessRolesService.getFunctionCodeType(Matchers.anyString())).thenReturn("approver");
+        boolean actual = adminRolesServiceImpl.isRoleAdmin(user);
+        assertTrue(actual);
+    }
+
        @Test
        public void isUserTest() {
                EPUser user = mockUser.mockEPUser();
@@ -409,8 +456,11 @@ public class AdminRolesServiceImplTest {
                epUserApp.setUserId(1l);
                userApps.add(epUserApp);
                user.setUserApps(userApps);
-               Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
-                               .thenReturn(user);
+               List<Integer> userAdminApps =  new ArrayList<>();
+               userAdminApps.add(1);
+               userAdminApps.add(2);
+               Mockito.when(dataAccessService.executeNamedQuery(Matchers.anyString(), Matchers.anyMap(), Matchers.anyMap()))
+                               .thenReturn(userAdminApps);
                boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);
                assertTrue(actual);
        }