diff options
author | GUJJA <kg811t@research.att.com> | 2018-02-16 11:45:40 -0500 |
---|---|---|
committer | GUJJA <kg811t@research.att.com> | 2018-02-16 12:13:32 -0500 |
commit | 1376951553a11ed93a9fde45b7f26b51c0b31e9c (patch) | |
tree | b4a62851ba83a49507dff0da3913e476b1309c58 /ecomp-portal-BE-common | |
parent | 2036d5039c90736c54484e539f37dfb8f661e4f1 (diff) |
Added Junits
Issue-ID: PORTAL-162, PORTAL-163, PORTAL-168
Includes JUNITS, maven dependency version changes
Change-Id: Ib13b03ece5816bffa263dd9180180d9c6070cebd
Signed-off-by:GUJJA <kg811t@research.att.com>
Diffstat (limited to 'ecomp-portal-BE-common')
12 files changed, 2234 insertions, 268 deletions
diff --git a/ecomp-portal-BE-common/pom.xml b/ecomp-portal-BE-common/pom.xml index a728433a..261cf31b 100644 --- a/ecomp-portal-BE-common/pom.xml +++ b/ecomp-portal-BE-common/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>org.onap.portal</groupId> <artifactId>onap-portal-parent</artifactId> - <version>2.2</version> + <version>2.2.0</version> </parent> <artifactId>portal-be-common</artifactId> @@ -239,9 +239,19 @@ <version>2.3.1</version> </dependency> <dependency> - <groupId>javax.servlet</groupId> - <artifactId>jstl</artifactId> - <version>1.2</version> + <groupId>org.apache.taglibs</groupId> + <artifactId>taglibs-standard-spec</artifactId> + <version>1.2.5</version> + </dependency> + <dependency> + <groupId>org.apache.taglibs</groupId> + <artifactId>taglibs-standard-impl</artifactId> + <version>1.2.5</version> + </dependency> + <dependency> + <groupId>org.apache.taglibs</groupId> + <artifactId>taglibs-standard-jstlel</artifactId> + <version>1.2.5</version> </dependency> <!-- Apache Tiles --> <dependency> @@ -406,8 +416,8 @@ </dependency> <dependency> <groupId>org.bouncycastle</groupId> - <artifactId>bcprov-jdk16</artifactId> - <version>1.45</version> + <artifactId>bcprov-jdk15on</artifactId> + <version>1.59</version> </dependency> <dependency> <groupId>commons-codec</groupId> diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/ExternalAccessRolesController.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/ExternalAccessRolesController.java index ff254e85..1e955c8b 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/ExternalAccessRolesController.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/ExternalAccessRolesController.java @@ -146,7 +146,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl @ApiOperation(value = "Gets user roles for an application which is upgraded to newer version.", response = String.class, responseContainer="List") @RequestMapping(value = { - "/v2/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json") + "/v1/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json") public String getV2UserList(HttpServletRequest request, HttpServletResponse response, @PathVariable("loginId") String loginId) throws Exception { String answer = null; @@ -186,7 +186,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl @ApiOperation(value = "Gets roles for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer="Json") @RequestMapping(value = { - "/v2/roles" }, method = RequestMethod.GET, produces = "application/json") + "/v1/roles" }, method = RequestMethod.GET, produces = "application/json") public List<CentralV2Role> getV2RolesForApp(HttpServletRequest request, HttpServletResponse response) throws Exception { logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getV2RolesForApp"); List<CentralV2Role> answer = null; @@ -233,7 +233,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl @ApiOperation(value = "Gets all role functions for an application which is upgraded to newer version.", response = CentralV2RoleFunction.class, responseContainer="Json") @RequestMapping(value = { - "/v2/functions" }, method = RequestMethod.GET, produces = "application/json") + "/v1/functions" }, method = RequestMethod.GET, produces = "application/json") public List<CentralV2RoleFunction> getV2RoleFunctionsList(HttpServletRequest request, HttpServletResponse response) throws Exception { List<CentralV2RoleFunction> cenRoleFuncList = null; @@ -276,7 +276,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl @ApiOperation(value = "Gets v2 role information for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer="Json") @RequestMapping(value = { - "/v2/role/{role_id}" }, method = RequestMethod.GET, produces = "application/json") + "/v1/role/{role_id}" }, method = RequestMethod.GET, produces = "application/json") public CentralV2Role getV2RoleInfo(HttpServletRequest request, HttpServletResponse response, @PathVariable("role_id") Long roleId) throws Exception { CentralV2Role answer = null; @@ -312,7 +312,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl } @ApiOperation(value = "Gets role information for an application provided by function code.", response = CentralV2RoleFunction.class, responseContainer = "Json") - @RequestMapping(value = { "/v2/function/{code}" }, method = RequestMethod.GET, produces = "application/json") + @RequestMapping(value = { "/v1/function/{code}" }, method = RequestMethod.GET, produces = "application/json") public CentralV2RoleFunction getV2RoleFunction(HttpServletRequest request, HttpServletResponse response, @PathVariable("code") String code) throws Exception { CentralV2RoleFunction centralV2RoleFunction = null; @@ -341,10 +341,20 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl EPApp requestedApp = applicationList.get(0); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); CentralV2RoleFunction availableRoleFunction = mapper.readValue(data, CentralV2RoleFunction.class); - CentralV2RoleFunction domainRoleFunction = externalAccessRolesService.getRoleFunction(availableRoleFunction.getCode(), - requestedApp.getUebKey()); + CentralV2RoleFunction domainRoleFunction = null; + if(availableRoleFunction.getType()!=null && availableRoleFunction.getAction()!= null) { + String code = availableRoleFunction.getType()+"|"+availableRoleFunction.getCode()+"|"+availableRoleFunction.getAction(); + domainRoleFunction = externalAccessRolesService.getRoleFunction(code, + requestedApp.getUebKey()); + } else { + domainRoleFunction = externalAccessRolesService.getRoleFunction(availableRoleFunction.getCode(), + requestedApp.getUebKey()); + } + boolean saveOrUpdateResponse = false; - if (domainRoleFunction != null) { + if (domainRoleFunction != null && domainRoleFunction.getCode().equals(availableRoleFunction.getCode()) + && domainRoleFunction.getType().equals(availableRoleFunction.getType()) + && domainRoleFunction.getAction().equals(availableRoleFunction.getAction())) { domainRoleFunction.setName(availableRoleFunction.getName()); saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(domainRoleFunction, requestedApp); @@ -760,11 +770,11 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl public List<EcompUser> getUsersOfApplication(HttpServletRequest request, HttpServletResponse response) throws Exception { List<EcompUser> users = null; try { + fieldsValidation(request); users = externalAccessRolesService.getAllAppUsers(request.getHeader(UEBKEY)); } catch (Exception e) { - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + sendErrorResponse(response, e); logger.error(EELFLoggerDelegate.errorLogger, "getUsersOfApplication failed", e); - throw new Exception(e.getMessage()); } return users; } @@ -816,10 +826,10 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl } @ApiOperation(value = "Gets ecompUser of an application.", response = CentralUser.class, responseContainer = "List") - @RequestMapping(value = { "/ecompUser/{loginId}" }, method = RequestMethod.GET, produces = "application/json") + @RequestMapping(value = { "/v2/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json") public String getEcompUser(HttpServletRequest request, HttpServletResponse response, @PathVariable("loginId") String loginId) throws Exception { - EcompUser user = new EcompUser(); + EcompUser user = null; ObjectMapper mapper = new ObjectMapper(); CentralUser answer = null; try { @@ -831,33 +841,38 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl user = UserUtils.convertToEcompUser(ecompUser); } } catch (Exception e) { + sendErrorResponse(response, e); logger.error(EELFLoggerDelegate.errorLogger, "getEcompUser failed", e); - throw e; } - return mapper.writeValueAsString(user); + if (user != null) { + return mapper.writeValueAsString(user); + } + return null; } @ApiOperation(value = "Gets user ecomp role for an application.", response = CentralUser.class, responseContainer = "List") - @RequestMapping(value = { "/ecompRoles" }, method = RequestMethod.GET, produces = "application/json") + @RequestMapping(value = { "/v2/roles" }, method = RequestMethod.GET, produces = "application/json") public List<EcompRole> getEcompRolesOfApplication(HttpServletRequest request, HttpServletResponse response) throws Exception { - List<EcompRole> ecompRoles = new ArrayList<EcompRole>(); + List<EcompRole> ecompRoles = null; ObjectMapper mapper = new ObjectMapper(); List<CentralV2Role> cenRole = null; try { fieldsValidation(request); EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0); // Sync all roles from external system into Ecomp portal DB - logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Entering into syncApplicationRolesWithEcompDB"); - externalAccessRolesService.syncApplicationRolesWithEcompDB(app); + logger.debug(EELFLoggerDelegate.debugLogger, + "getRolesForApp: Entering into syncApplicationRolesWithEcompDB"); + externalAccessRolesService.syncApplicationRolesWithEcompDB(app); logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Finished syncApplicationRolesWithEcompDB"); cenRole = externalAccessRolesService.getActiveRoles(request.getHeader(UEBKEY)); } catch (Exception e) { sendErrorResponse(response, e); logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles failed", e); } - String res = mapper.writeValueAsString(cenRole); - if (res != null) { + if (cenRole != null) { + String res = mapper.writeValueAsString(cenRole); + ecompRoles = new ArrayList<>(); List<Role> roles = mapper.readValue(res, TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class)); for (Role role : roles) diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/RoleManageController.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/RoleManageController.java index 1433fb21..c9a6f5d1 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/RoleManageController.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/RoleManageController.java @@ -500,7 +500,19 @@ public class RoleManageController extends EPRestrictedBaseController { if (isAuthorizedUser(user, requestedApp)) { fieldsValidation(requestedApp); if (requestedApp.getCentralAuth()) { - saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(roleFunc, requestedApp); + CentralV2RoleFunction domainRoleFunction = externalAccessRolesService.getRoleFunction(roleFunc.getCode(), + requestedApp.getUebKey()); + if (domainRoleFunction != null && domainRoleFunction.getCode().equals(roleFunc.getCode()) + && domainRoleFunction.getType().equals(roleFunc.getType()) + && domainRoleFunction.getAction().equals(roleFunc.getAction())) { + domainRoleFunction.setName(roleFunc.getName()); + saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(domainRoleFunction, + requestedApp); + } else { + roleFunc.setAppId(requestedApp.getId()); + saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(roleFunc, + requestedApp); + } if (saveOrUpdateResponse) { EPUser requestedUser = externalAccessRolesService.getUser(user.getOrgUserId()).get(0); EPApp app = externalAccessRolesService.getApp(requestedApp.getUebKey()).get(0); @@ -557,11 +569,19 @@ public class RoleManageController extends EPRestrictedBaseController { if (requestedApp.getCentralAuth()) { ObjectMapper mapper = new ObjectMapper(); String data = roleFunc; + boolean getDelFuncResponse = false; CentralV2RoleFunction availableRoleFunction = mapper.readValue(data, CentralV2RoleFunction.class); - CentralV2RoleFunction domainRoleFunction = externalAccessRolesService - .getRoleFunction(availableRoleFunction.getCode(), requestedApp.getUebKey()); - boolean getDelFuncResponse = externalAccessRolesService - .deleteCentralRoleFunction(domainRoleFunction.getCode(), requestedApp); + String code = availableRoleFunction.getType() + "|" + availableRoleFunction.getCode() + "|" + + availableRoleFunction.getAction(); + CentralV2RoleFunction domainRoleFunction = externalAccessRolesService.getRoleFunction(code, + requestedApp.getUebKey()); + if (domainRoleFunction.getCode().contains("|")) { + getDelFuncResponse = externalAccessRolesService + .deleteCentralRoleFunction(code, requestedApp); + } else { + getDelFuncResponse = externalAccessRolesService + .deleteCentralRoleFunction(domainRoleFunction.getCode(), requestedApp); + } if (getDelFuncResponse) { logger.info(EELFLoggerDelegate.applicationLogger, "deleteRoleFunction: succeeded for app {}, role {}", requestedApp.getId(), diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/AdminRolesServiceImpl.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/AdminRolesServiceImpl.java index 653e974b..e0ce086e 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/AdminRolesServiceImpl.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/AdminRolesServiceImpl.java @@ -251,6 +251,7 @@ public class AdminRolesServiceImpl implements AdminRolesService { if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) { // Add user admin role for list of centralized applications in external system addAdminRoleInExternalSystem(user, localSession, newAppsWhereUserIsAdmin); + result = true; } } catch (Exception e) { EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e); @@ -409,7 +410,7 @@ public class AdminRolesServiceImpl implements AdminRolesService { @SuppressWarnings("unchecked") @Override public boolean isSuperAdmin(EPUser user) { - if ((user != null) /* && (user.getId() == null) */ && (user.getOrgUserId() != null)) { + if ((user != null) && (user.getOrgUserId() != null)) { String sql = "SELECT user.USER_ID, user.org_user_id, userrole.ROLE_ID, userrole.APP_ID FROM fn_user_role userrole " + "INNER JOIN fn_user user ON user.USER_ID = userrole.USER_ID " + "WHERE user.org_user_id = '" + user.getOrgUserId() + "' " + "AND userrole.ROLE_ID = '" + SYS_ADMIN_ROLE_ID + "' " @@ -425,21 +426,6 @@ public class AdminRolesServiceImpl implements AdminRolesService { "Exception occurred while executing isSuperAdmin operation", e); } } - // else - // { - // User currentUser = user != null ? (User) - // dataAccessService.getDomainObject(User.class, user.getId(), null) : - // null; - // if (currentUser != null && currentUser.getId() != null) { - // for (UserApp userApp : currentUser.getUserApps()) { - // if (userApp.getApp().getId().equals(ECOMP_APP_ID) && - // userApp.getRole().getId().equals(SYS_ADMIN_ROLE_ID)) { - // // Super Administrator role is global, no need to keep iterating - // return true; - // } - // } - // } - // } return false; } @@ -450,9 +436,7 @@ public class AdminRolesServiceImpl implements AdminRolesService { : null; if (currentUser != null && currentUser.getId() != null) { for (EPUserApp userApp : currentUser.getEPUserApps()) { - if (// !userApp.getApp().getId().equals(ECOMP_APP_ID) - // && - userApp.getRole().getId().equals(ACCOUNT_ADMIN_ROLE_ID)) { + if (userApp.getRole().getId().equals(ACCOUNT_ADMIN_ROLE_ID)) { // Account Administrator sees only the applications // he/she is Administrator return true; diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImpl.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImpl.java index af5b5001..b1804d8c 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImpl.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImpl.java @@ -1285,7 +1285,15 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic final Map<String, Long> params = new HashMap<>(); CentralV2User userAppList = new CentralV2User(); CentralV2User user1 = null; + final Map<String, Long> params1 = new HashMap<>(); + List<EPRole> globalRoleList = new ArrayList<>(); + try { + if (app.getId() != PortalConstants.PORTAL_APP_ID) { + params1.put("userId", userInfo.getId()); + params1.put("appId", app.getId()); + globalRoleList = dataAccessService.executeNamedQuery("userAppGlobalRoles", params1, null); + } userAppList.setUserApps(new TreeSet<CentralV2UserApp>()); for (EPUserApp userApp : userAppSet) { if (userApp.getRole().getActive()) { @@ -1306,41 +1314,46 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic epApp.getThumbnail(), epApp.getUsername(), epApp.getUebKey(), epApp.getUebSecret(), epApp.getUebTopicName()); cua.setApp(cenApp); + Long appId = null; + if (globalRole.toLowerCase().startsWith("global_") + && epApp.getId().equals(PortalConstants.PORTAL_APP_ID) + && !epApp.getId().equals(app.getId())) { + appId = app.getId(); + EPRole result = null; + if (globalRoleList.size() > 0) + result = globalRoleList.stream() + .filter(x -> userApp.getRole().getId().equals(x.getId())).findAny() + .orElse(null); + if (result == null) + continue; + } else { + appId = userApp.getApp().getId(); + } params.put("roleId", userApp.getRole().getId()); - params.put(APP_ID, userApp.getApp().getId()); - CentralV2Role centralRole; - List<EPRole> globalRoleList; - globalRoleList = getGlobalRolesOfPortal(); - EPRole result = globalRoleList.stream().filter(x -> userApp.getRole().getId().equals(x.getId())) - .findAny().orElse(null); - if (result != null && userApp.getApp().getId() != app.getId()) { - userApp.getRole().setId(result.getId()); - centralRole = getGlobalRoleForRequestedApp(app.getId(), userApp.getRole().getId()); - cua.setRole(centralRole); + params.put(APP_ID, appId); + List<CentralV2RoleFunction> appRoleFunctionList = dataAccessService + .executeNamedQuery("getAppRoleFunctionList", params, null); + SortedSet<CentralV2RoleFunction> roleFunctionSet = new TreeSet<>(); + for (CentralV2RoleFunction roleFunc : appRoleFunctionList) { + String functionCode = EcompPortalUtils.getFunctionCode(roleFunc.getCode()); + CentralV2RoleFunction cenRoleFunc = new CentralV2RoleFunction(roleFunc.getId(), + functionCode, roleFunc.getName(), null, null); + roleFunctionSet.add(cenRoleFunc); + } + Long userRoleId = null; + if (globalRole.toLowerCase().startsWith("global_") + || epApp.getId().equals(PortalConstants.PORTAL_APP_ID)) { + userRoleId = userApp.getRole().getId(); } else { - List<CentralV2RoleFunction> appRoleFunctionList = dataAccessService - .executeNamedQuery("getAppRoleFunctionList", params, null); - SortedSet<CentralV2RoleFunction> roleFunctionSet = new TreeSet<>(); - for (CentralV2RoleFunction roleFunc : appRoleFunctionList) { - String functionCode = EcompPortalUtils.getFunctionCode(roleFunc.getCode()); - CentralV2RoleFunction cenRoleFunc = new CentralV2RoleFunction(roleFunc.getId(), - functionCode, roleFunc.getName(), null, null); - roleFunctionSet.add(cenRoleFunc); - } - Long userRoleId = null; - if (globalRole.toLowerCase().startsWith("global_") - && epApp.getId().equals(PortalConstants.PORTAL_APP_ID)) { - userRoleId = userApp.getRole().getId(); - } else { - userRoleId = userApp.getRole().getAppRoleId(); - } - CentralV2Role cenRole = new CentralV2Role(userRoleId, userApp.getRole().getCreated(), - userApp.getRole().getModified(), userApp.getRole().getCreatedId(), - userApp.getRole().getModifiedId(), userApp.getRole().getRowNum(), - userApp.getRole().getName(), userApp.getRole().getActive(), - userApp.getRole().getPriority(), roleFunctionSet, null, null); - cua.setRole(cenRole); + userRoleId = userApp.getRole().getAppRoleId(); } + CentralV2Role cenRole = new CentralV2Role(userRoleId, userApp.getRole().getCreated(), + userApp.getRole().getModified(), userApp.getRole().getCreatedId(), + userApp.getRole().getModifiedId(), userApp.getRole().getRowNum(), + userApp.getRole().getName(), userApp.getRole().getActive(), + userApp.getRole().getPriority(), roleFunctionSet, null, null); + cua.setRole(cenRole); + userAppList.getUserApps().add(cua); } } @@ -1480,9 +1493,8 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic @SuppressWarnings("unchecked") @Override public CentralV2RoleFunction getRoleFunction(String functionCode, String uebkey) throws Exception { - if (functionCode.contains("|")) - functionCode = EcompPortalUtils.getFunctionCode(functionCode); - functionCode = encodeFunctionCode(functionCode); + String code = EcompPortalUtils.getFunctionCode(functionCode); + String encodedCode = encodeFunctionCode(code); CentralV2RoleFunction roleFunc = null; EPApp app = getApp(uebkey).get(0); List<CentralV2RoleFunction> getRoleFuncList = null; @@ -1492,11 +1504,15 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic params.put(APP_ID, String.valueOf(app.getId())); getRoleFuncList = dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null); if (getRoleFuncList.isEmpty()) { - return roleFunc; + params.put(FUNCTION_CODE_PARAMS, encodedCode); + getRoleFuncList = dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null); + if (getRoleFuncList.isEmpty()) { + return roleFunc; + } } else { if (getRoleFuncList.size() > 1) { - CentralV2RoleFunction cenV2RoleFunction = appFunctionListFilter(functionCode, getRoleFuncList); - if(cenV2RoleFunction == null) + CentralV2RoleFunction cenV2RoleFunction = appFunctionListFilter(encodedCode, getRoleFuncList); + if (cenV2RoleFunction == null) return roleFunc; roleFunc = checkIfPipesExitsInFunctionCode(cenV2RoleFunction); } else { @@ -1528,7 +1544,6 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic return roleFunc; } - @SuppressWarnings("unchecked") @Override public boolean saveCentralRoleFunction(CentralV2RoleFunction domainCentralRoleFunction, EPApp app) throws Exception { boolean saveOrUpdateFunction = false; @@ -1536,41 +1551,15 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic domainCentralRoleFunction.setCode(encodeFunctionCode(domainCentralRoleFunction.getCode())); final Map<String, String> functionParams = new HashMap<>(); functionParams.put("appId", String.valueOf(app.getId())); - List<CentralV2RoleFunction> appRoleFuncWithPipe = new ArrayList<>(); - // If request coming from portal application we use type, instance/code and action to fetch record - if(domainCentralRoleFunction.getType()!=null && domainCentralRoleFunction.getAction()!=null){ - functionParams.put(FUNCTION_CODE_PARAMS, domainCentralRoleFunction.getType()+FUNCTION_PIPE - +domainCentralRoleFunction.getCode()+FUNCTION_PIPE+domainCentralRoleFunction.getAction()); - appRoleFuncWithPipe = dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, functionParams, null); - if(appRoleFuncWithPipe.isEmpty()){ - functionParams.put(FUNCTION_CODE_PARAMS, domainCentralRoleFunction.getCode()); - appRoleFuncWithPipe = dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, functionParams, null); - } - } - // If request coming from SDK applications we use just function code to fetch record - else{ - functionParams.put(FUNCTION_CODE_PARAMS, domainCentralRoleFunction.getCode()); - } - CentralV2RoleFunction appFunctionCode = null; - if(!appRoleFuncWithPipe.isEmpty()){ - // Make sure we extract correct record if similar records are found as query uses like condition - appFunctionCode = appFunctionListFilter(domainCentralRoleFunction.getCode(), appRoleFuncWithPipe); - if(appFunctionCode == null){ - appFunctionCode = domainCentralRoleFunction; - } - } else{ - appFunctionCode = domainCentralRoleFunction; - } - appFunctionCode.setName(domainCentralRoleFunction.getName()); if(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) { - addRoleFunctionInExternalSystem(appFunctionCode, app); + addRoleFunctionInExternalSystem(domainCentralRoleFunction, app); } if(domainCentralRoleFunction.getType() != null && domainCentralRoleFunction.getAction() != null){ - appFunctionCode.setCode(domainCentralRoleFunction.getType()+ + domainCentralRoleFunction.setCode(domainCentralRoleFunction.getType()+ FUNCTION_PIPE+domainCentralRoleFunction.getCode()+FUNCTION_PIPE+domainCentralRoleFunction.getAction()); } - appFunctionCode.setAppId(app.getId()); - dataAccessService.saveDomainObject(appFunctionCode, null); + domainCentralRoleFunction.setAppId(app.getId()); + dataAccessService.saveDomainObject(domainCentralRoleFunction, null); saveOrUpdateFunction = true; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "saveCentralRoleFunction: failed", e); diff --git a/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/EP.hbm.xml b/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/EP.hbm.xml index 2488a2e5..5968b3ce 100644 --- a/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/EP.hbm.xml +++ b/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/EP.hbm.xml @@ -2106,10 +2106,12 @@ where fn_role.app_id = fn_app.app_id and fn_app.enabled='Y' and fn_role.active_y from fn_user fu, fn_role fr, fn_user_role fur where fu.user_id = fur.user_id and fu.active_yn='Y' and fur.role_id = fr.role_id and fr.app_id =:appId and fr.active_yn='Y' union - select distinct fu.org_id, fu.manager_id, fu.first_name, fu.middle_name, fu.last_name, fu.phone, fu.email, fu.hrid, fu.org_user_id, fu.org_code, fu.org_manager_userid, fu.job_title, fu.login_id, fu.active_yn , fr.role_id, fr.role_name - from fn_user fu, fn_role fr, fn_user_role fur - where fu.user_id = fur.user_id and fu.active_yn='Y' and fur.app_id=:appId and fr.role_name like 'global%' and fr.active_yn='Y' - ; + select distinct fu.org_id, fu.manager_id, fu.first_name, fu.middle_name, fu.last_name, fu.phone, + fu.email, fu.hrid, fu.org_user_id, fu.org_code, fu.org_manager_userid, fu.job_title, + fu.login_id, fu.active_yn , fr.role_id, fr.role_name + from fn_user_role a, fn_role fr, fn_user fu + where a.role_id in (select b.role_id from ep_app_role_function b where b.role_app_id = 1 and b.app_id =:appId) and a.user_id =fu.user_id and a.role_id = fr.role_id and fr.active_yn='Y' and fu.active_yn='Y' + ; ]]> </sql-query> @@ -2347,14 +2349,16 @@ where fn_role.app_id = fn_app.app_id and fn_app.enabled='Y' and fn_role.active_y ; ]]> - </sql-query> - - <query name="getBasicauthAccount"> - select id,password from BasicAuthCredentials - </query> + </sql-query> + + <sql-query name="userAppGlobalRoles"> + <return alias="userAppGlobalRoles" class="org.onap.portalapp.portal.domain.EPRole" /> + <![CDATA[ + select fr.role_id , fr.role_name ,fr.active_yn, fr.priority, fr.app_id, fr.app_role_id + from fn_user_role a, fn_role fr, fn_user fu + where a.role_id in (select b.role_id from ep_app_role_function b where b.role_app_id = 1 and b.app_id =:appId) and a.user_id =fu.user_id and a.role_id = fr.role_id and fr.active_yn='Y' and fu.active_yn='Y' and fu.user_id =:userId + ; + ]]> + </sql-query> - <query name="getMicroserviceInfo"> - select id,password from MicroserviceData - </query> - </hibernate-mapping> diff --git a/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/Fusion.hbm.xml b/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/Fusion.hbm.xml index d197bc5f..0487006d 100644 --- a/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/Fusion.hbm.xml +++ b/ecomp-portal-BE-common/src/main/webapp/WEB-INF/fusion/orm/Fusion.hbm.xml @@ -353,9 +353,5 @@ Portal app mappings and features are in EP.hbm.xml. select sum(total_lines) TOTAL_LINES from demo_kpi_sourcecode_stats group by category1 ]]> </sql-query> - - <query name="getAppPassword"> - select id,appPassword from App - </query> </hibernate-mapping> diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/ExternalAccessRolesControllerTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/ExternalAccessRolesControllerTest.java index 859cc4c2..47a1394e 100644 --- a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/ExternalAccessRolesControllerTest.java +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/ExternalAccessRolesControllerTest.java @@ -76,6 +76,7 @@ import org.onap.portalsdk.core.domain.Role; import org.onap.portalsdk.core.restful.domain.EcompUser; import org.springframework.beans.BeanUtils; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.client.HttpClientErrorException; import com.fasterxml.jackson.core.JsonProcessingException; @@ -624,7 +625,17 @@ public class ExternalAccessRolesControllerTest { EcompUser user = new EcompUser(); user.setOrgUserId("guestT"); users.add(user); + StringWriter sw = new StringWriter(); + PrintWriter writer = new PrintWriter(sw); + Mockito.when(mockedResponse.getWriter()).thenReturn(writer); + List<EPApp> applicationList = new ArrayList<EPApp>(); + EPApp app = mockApp(); + app.setCentralAuth(true); + applicationList.add(app); + Mockito.when(externalAccessRolesService.getApp(mockedRequest.getHeader(uebKey))).thenReturn(applicationList); Mockito.when(externalAccessRolesService.getAllAppUsers(mockedRequest.getHeader(uebKey))).thenReturn(users); + ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(externalAccessRolesService.getNameSpaceIfExists(app)).thenReturn(response); List<EcompUser> expectedUsers = externalAccessRolesController.getUsersOfApplication(mockedRequest, mockedResponse); assertEquals(expectedUsers, users); } diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/AdminRolesServiceImplTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/AdminRolesServiceImplTest.java new file mode 100644 index 00000000..ef9bda0c --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/AdminRolesServiceImplTest.java @@ -0,0 +1,427 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under 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.assertTrue; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.SortedSet; + +import org.hibernate.SQLQuery; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.json.simple.JSONObject; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.core.MockEPUser; +import org.onap.portalapp.portal.domain.EPApp; +import org.onap.portalapp.portal.domain.EPRole; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.domain.EPUserApp; +import org.onap.portalapp.portal.domain.UserRole; +import org.onap.portalapp.portal.transport.AppNameIdIsAdmin; +import org.onap.portalapp.portal.transport.AppsListWithAdminRole; +import org.onap.portalapp.portal.utils.EPCommonSystemProperties; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.portal.utils.PortalConstants; +import org.onap.portalsdk.core.service.DataAccessService; +import org.onap.portalsdk.core.service.DataAccessServiceImpl; +import org.onap.portalsdk.core.util.SystemProperties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ EcompPortalUtils.class, PortalConstants.class, SystemProperties.class, + EPCommonSystemProperties.class }) +public class AdminRolesServiceImplTest { + + @Mock + DataAccessService dataAccessService = new DataAccessServiceImpl(); + + @Mock + EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl(); + + @Mock + SearchServiceImpl searchServiceImpl = new SearchServiceImpl(); + + @Mock + SessionFactory sessionFactory; + + @Mock + Session session; + + @Mock + Transaction transaction; + + @Mock + RestTemplate template = new RestTemplate(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + Mockito.when(sessionFactory.openSession()).thenReturn(session); + Mockito.when(session.beginTransaction()).thenReturn(transaction); + } + + @After + public void after() { + session.close(); + } + + @InjectMocks + AdminRolesServiceImpl adminRolesServiceImpl = new AdminRolesServiceImpl(); + + private Long ACCOUNT_ADMIN_ROLE_ID = 999L; + + public EPApp mockApp() { + EPApp app = new EPApp(); + app.setName("Test"); + app.setImageUrl("test"); + app.setNameSpace("com.test.app"); + app.setCentralAuth(true); + app.setDescription("test"); + app.setNotes("test"); + app.setUrl("test"); + app.setId((long) 1); + app.setAppRestEndpoint("test"); + app.setAlternateUrl("test"); + app.setName("test"); + app.setMlAppName("test"); + app.setMlAppAdminId("test"); + app.setUsername("test"); + app.setAppPassword("test"); + app.setOpen(false); + app.setEnabled(true); + app.setUebKey("test"); + app.setUebSecret("test"); + app.setUebTopicName("test"); + app.setAppType(1); + return app; + } + + MockEPUser mockUser = new MockEPUser(); + + @SuppressWarnings("deprecation") + @Test + public void getAppsWithAdminRoleStateForUserTest() { + 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); + List<EPUserApp> userAppList = new ArrayList<>(); + EPUserApp epUserApp = new EPUserApp(); + EPRole role = new EPRole(); + role.setActive(true); + role.setId(1l); + role.setName("test role"); + epUserApp.setApp(app); + epUserApp.setRole(role); + epUserApp.setUserId(1l); + 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()); + } + + @SuppressWarnings({ "deprecation", "unchecked" }) + @Test + public void setAppsWithAdminRoleStateForUserTest() { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(PortalConstants.class); + PowerMockito.mockStatic(SystemProperties.class); + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + app.setId(1l); + EPApp app2 = mockApp(); + app2.setName("app2"); + app2.setNameSpace("com.test.app2"); + app2.setId(2l); + EPApp app3 = mockApp(); + app3.setName("app3"); + app3.setNameSpace("com.test.app3"); + app3.setId(3l); + List<EPApp> apps = new ArrayList<>(); + apps.add(app); + apps.add(app2); + apps.add(app3); + Mockito.when(epAppCommonServiceImpl.getAppsFullList()).thenReturn(apps); + + List<EPUser> localUserList = new ArrayList<>(); + localUserList.add(user); + Mockito.when( + dataAccessService.getList(EPUser.class, " where org_user_id='" + user.getOrgUserId() + "'", null, null)) + .thenReturn(localUserList); + List<EPUserApp> oldAppsWhereUserIsAdmin = new ArrayList<EPUserApp>(); + EPUserApp epUserApp = new EPUserApp(); + EPRole role = new EPRole(); + role.setActive(true); + role.setId(999l); + role.setName("app5"); + epUserApp.setApp(app); + epUserApp.setRole(role); + epUserApp.setUserId(1l); + oldAppsWhereUserIsAdmin.add(epUserApp); + Mockito.when(dataAccessService.getList(EPUserApp.class, + " where userId = " + user.getId() + " and role.id = " + ACCOUNT_ADMIN_ROLE_ID, null, + null)).thenReturn(oldAppsWhereUserIsAdmin); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + EPApp app4 = mockApp(); + app4.setId(6l); + app4.setName("app7"); + app4.setNameSpace("com.test.app7"); + List<EPApp> apps2 = new ArrayList<>(); + apps2.add(app); + apps2.add(app2); + apps2.add(app3); + apps2.add(app4); + Mockito.when(dataAccessService.executeNamedQuery("getCentralizedApps", null, null)).thenReturn(apps2); + Mockito.when( + EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn(true); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); + JSONObject getUserRoles = new JSONObject(); + ResponseEntity<String> getResponse = new ResponseEntity<>(getUserRoles.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse); + + AppsListWithAdminRole newAppsListWithAdminRoles = new AppsListWithAdminRole(); + ArrayList<AppNameIdIsAdmin> appsRoles = new ArrayList<>(); + AppNameIdIsAdmin appNameIdIsAdmin = new AppNameIdIsAdmin(); + appNameIdIsAdmin.setAppName("app1"); + appNameIdIsAdmin.setId(2l); + appNameIdIsAdmin.setIsAdmin(true); + appNameIdIsAdmin.setRestrictedApp(false); + AppNameIdIsAdmin appNameIdIsAdmin2 = new AppNameIdIsAdmin(); + appNameIdIsAdmin2.setAppName("app2"); + appNameIdIsAdmin2.setId(3l); + appNameIdIsAdmin2.setIsAdmin(true); + appNameIdIsAdmin2.setRestrictedApp(false); + appsRoles.add(appNameIdIsAdmin); + appsRoles.add(appNameIdIsAdmin2); + newAppsListWithAdminRoles.setOrgUserId(user.getOrgUserId()); + newAppsListWithAdminRoles.setAppsRoles(appsRoles); + Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin.id)).thenReturn(app2); + Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin2.id)).thenReturn(app3); + JSONObject getUserRoles2 = new JSONObject(); + JSONObject getUserRoles3 = new JSONObject(); + JSONObject getUserRoles4 = new JSONObject(); + JSONObject finalUserRoles = new JSONObject(); + getUserRoles2.put("role", "com.test.app3.Account_Administrator"); + getUserRoles3.put("role", "com.test.app3.admin"); + getUserRoles4.put("role", "com.test.app3.owner"); + List<JSONObject> userRoles = new ArrayList<>(); + userRoles.add(getUserRoles2); + userRoles.add(getUserRoles3); + userRoles.add(getUserRoles4); + finalUserRoles.put("userRole", userRoles); + ResponseEntity<String> getResponse2 = new ResponseEntity<>(finalUserRoles.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse2); + boolean actual = adminRolesServiceImpl.setAppsWithAdminRoleStateForUser(newAppsListWithAdminRoles); + assertTrue(actual); + } + + @Test + public void isSuperAdminTest() { + EPUser user = mockUser.mockEPUser(); + user.setId(1l); + SQLQuery SqlQuery = Mockito.mock(SQLQuery.class); + Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery); + List<UserRole> userRoleList = new ArrayList<>(); + UserRole userRole = new UserRole(); + userRole.setFirstName("Hello"); + userRole.setLastName("World"); + userRole.setRoleId(1l); + userRole.setRoleName("test"); + userRole.setUser_Id(1l); + userRoleList.add(userRole); + Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap())) + .thenReturn(userRoleList); + boolean actual = adminRolesServiceImpl.isSuperAdmin(user); + assertTrue(actual); + } + + @Test + public void isSuperAdminExceptionTest() { + EPUser user = mockUser.mockEPUser(); + user.setId(1l); + SQLQuery SqlQuery = Mockito.mock(SQLQuery.class); + Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery); + Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeSQLQuery(Matchers.anyString(), + Matchers.any(), Matchers.anyMap()); + boolean actual = adminRolesServiceImpl.isSuperAdmin(user); + assertFalse(actual); + } + + @Test + public void isAccountAdminTest() { + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + app.setId(2l); + SortedSet<EPUserApp> userApps = user.getEPUserApps(); + EPUserApp epUserApp = new EPUserApp(); + EPRole role = new EPRole(); + role.setActive(true); + role.setId(999l); + role.setName("test role"); + epUserApp.setApp(app); + epUserApp.setRole(role); + epUserApp.setUserId(1l); + userApps.add(epUserApp); + user.setUserApps(userApps); + Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap())) + .thenReturn(user); + boolean actual = adminRolesServiceImpl.isAccountAdmin(user); + assertTrue(actual); + } + + @Test + public void isAccountAdminExceptionTest() { + EPUser user = mockUser.mockEPUser(); + Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(), + Matchers.anyLong(), Matchers.anyMap()); + boolean actual = adminRolesServiceImpl.isAccountAdmin(user); + assertFalse(actual); + } + + @Test + public void isUserTest() { + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + app.setId(2l); + SortedSet<EPUserApp> userApps = user.getEPUserApps(); + EPUserApp epUserApp = new EPUserApp(); + EPRole role = new EPRole(); + role.setActive(true); + role.setId(2l); + role.setName("test role"); + epUserApp.setApp(app); + epUserApp.setRole(role); + epUserApp.setUserId(1l); + userApps.add(epUserApp); + user.setUserApps(userApps); + Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap())) + .thenReturn(user); + boolean actual = adminRolesServiceImpl.isUser(user); + assertTrue(actual); + } + + @Test + public void isUserExceptionTest() { + EPUser user = mockUser.mockEPUser(); + Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(), + Matchers.anyLong(), Matchers.anyMap()); + boolean actual = adminRolesServiceImpl.isUser(user); + assertFalse(actual); + } + + @Test + public void getRolesByAppTest() { + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + List<EPRole> expected = new ArrayList<>(); + EPRole role = new EPRole(); + role.setActive(true); + role.setId(1l); + role.setName("test role"); + expected.add(role); + Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap())) + .thenReturn(expected); + List<EPRole> actual = adminRolesServiceImpl.getRolesByApp(user, app.getId()); + assertEquals(expected, actual); + } + + @Test + public void isAccountAdminOfApplicationTest() { + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + SortedSet<EPUserApp> userApps = user.getEPUserApps(); + EPUserApp epUserApp = new EPUserApp(); + EPRole role = new EPRole(); + role.setActive(true); + role.setId(999l); + role.setName("test role"); + epUserApp.setApp(app); + epUserApp.setRole(role); + epUserApp.setUserId(1l); + userApps.add(epUserApp); + user.setUserApps(userApps); + Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap())) + .thenReturn(user); + boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app); + assertTrue(actual); + } + + @Test + public void isAccountAdminOfApplicationExceptionTest() { + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(), + Matchers.anyLong(), Matchers.anyMap()); + boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app); + assertFalse(actual); + } +} diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java index 0fe5a2c6..726a235f 100644 --- a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/ExternalAccessRolesServiceImplTest.java @@ -39,12 +39,15 @@ package org.onap.portalapp.portal.service; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -55,6 +58,8 @@ import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; import org.json.JSONObject; import org.junit.After; import org.junit.Before; @@ -67,14 +72,19 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.onap.portalapp.portal.core.MockEPUser; import org.onap.portalapp.portal.domain.CentralV2RoleFunction; +import org.onap.portalapp.portal.domain.CentralizedApp; import org.onap.portalapp.portal.domain.EPApp; import org.onap.portalapp.portal.domain.EPAppRoleFunction; import org.onap.portalapp.portal.domain.EPRole; import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.domain.EPUserApp; import org.onap.portalapp.portal.exceptions.InactiveApplicationException; import org.onap.portalapp.portal.exceptions.InvalidUserException; import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.transport.BulkUploadRoleFunction; import org.onap.portalapp.portal.transport.BulkUploadUserRoles; +import org.onap.portalapp.portal.transport.CentralRole; +import org.onap.portalapp.portal.transport.CentralUser; import org.onap.portalapp.portal.transport.CentralV2Role; import org.onap.portalapp.portal.transport.EcompUserRoles; import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator; @@ -83,6 +93,7 @@ import org.onap.portalapp.portal.transport.LocalRole; import org.onap.portalapp.portal.utils.EPCommonSystemProperties; import org.onap.portalapp.portal.utils.EcompPortalUtils; import org.onap.portalapp.portal.utils.PortalConstants; +import org.onap.portalapp.util.EPUserUtils; import org.onap.portalsdk.core.domain.Role; import org.onap.portalsdk.core.domain.RoleFunction; import org.onap.portalsdk.core.restful.domain.EcompUser; @@ -100,8 +111,11 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; +import com.fasterxml.jackson.databind.ObjectMapper; + @RunWith(PowerMockRunner.class) -@PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, EPCommonSystemProperties.class }) +@PrepareForTest({ EcompPortalUtils.class, Criterion.class, Restrictions.class, SystemProperties.class, + EPCommonSystemProperties.class }) public class ExternalAccessRolesServiceImplTest { @Mock DataAccessService dataAccessService = new DataAccessServiceImpl(); @@ -112,9 +126,9 @@ public class ExternalAccessRolesServiceImplTest { @InjectMocks ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl(); - @Mock + @Mock EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl(); - + @Mock SessionFactory sessionFactory; @@ -123,14 +137,14 @@ public class ExternalAccessRolesServiceImplTest { @Mock Transaction transaction; - + @Before public void setup() { MockitoAnnotations.initMocks(this); Mockito.when(sessionFactory.openSession()).thenReturn(session); Mockito.when(session.beginTransaction()).thenReturn(transaction); } - + @After public void after() { session.close(); @@ -146,40 +160,10 @@ public class ExternalAccessRolesServiceImplTest { private static final String FUNCTION_CODE_PARAMS = "functionCode"; - private static final String AND_FUNCTION_CD_EQUALS = " and function_cd = '"; - - private static final String OWNER = ".owner"; - - private static final String ADMIN = ".admin"; - - private static final String ACCOUNT_ADMINISTRATOR = ".Account_Administrator"; - private static final String FUNCTION_PIPE = "|"; - private static final String IS_NULL_STRING = "null"; - - private static final String EXTERNAL_AUTH_PERMS = "perms"; - - private static final String EXTERNAL_AUTH_ROLE_DESCRIPTION = "description"; - - private static final String IS_EMPTY_JSON_STRING = "{}"; - - private static final String CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE = "Connecting to External Auth system"; - - private static final String APP_ROLE_ID = "appRoleId"; - private static final String APP_ID = "appId"; - private static final String PRIORITY = "priority"; - - private static final String ACTIVE = "active"; - - private static final String ROLE_NAME = "name"; - - private static final String ID = "id"; - - private static final String APP_ID_EQUALS = " app_id = "; - MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); @@ -369,6 +353,9 @@ public class ExternalAccessRolesServiceImplTest { @Test public void deleteCentralRoleFunctionTest() throws Exception { + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(EPCommonSystemProperties.class); final Map<String, String> params = new HashMap<>(); EPApp app = mockApp(); params.put("functionCode", "menu_fun_code"); @@ -379,20 +366,109 @@ public class ExternalAccessRolesServiceImplTest { centralRoleFunctionList.add(domainCentralRoleFunction); Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null)) .thenReturn(centralRoleFunctionList); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse); HttpHeaders headers = new HttpHeaders(); - PowerMockito.mockStatic(EcompPortalUtils.class); Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers); - Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class, "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null); - assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app)); } @Test + public void deleteRoleForApplicationTest() throws Exception { + EPApp app = mockApp(); + app.setId(2l); + List<EPApp> appList = new ArrayList<>(); + appList.add(app); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + final Map<String, String> deleteRoleParams = new HashMap<>(); + deleteRoleParams.put(APP_ROLE_NAME_PARAM, "test_delete"); + deleteRoleParams.put(APP_ID, String.valueOf(app.getId())); + List<EPRole> epRoleList = new ArrayList<>(); + EPRole epRole = new EPRole(); + epRole.setName("test_delete"); + epRole.setId(1l); + epRole.setActive(true); + epRole.setAppRoleId(11l); + epRoleList.add(epRole); + Mockito.when( + dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, deleteRoleParams, null)) + .thenReturn(epRoleList); + final Map<String, Long> appRoleFuncsParams = new HashMap<>(); + appRoleFuncsParams.put("appId", app.getId()); + appRoleFuncsParams.put("roleId", epRole.getId()); + List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); + EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction(); + epAppRoleFunction.setCode("test_code"); + epAppRoleFunction.setAppId(app.getId()); + epAppRoleFunction.setRoleAppId(null); + appRoleFunctionList.add(epAppRoleFunction); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctionList); + SQLQuery SqlQuery = Mockito.mock(SQLQuery.class); + Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery); + PowerMockito.mockStatic(EcompPortalUtils.class); + ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse); + assertTrue(externalAccessRolesServiceImpl.deleteRoleForApplication(epRole.getName(), app.getUebKey())); + } + + @Test + public void deleteRoleForPortalApplicationTest() throws Exception { + EPApp app = mockApp(); + app.setId(1l); + List<EPApp> appList = new ArrayList<>(); + appList.add(app); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + final Map<String, String> deleteRoleParams2 = new HashMap<>(); + deleteRoleParams2.put(APP_ROLE_NAME_PARAM, "test_delete"); + List<EPRole> epRoleList2 = new ArrayList<>(); + EPRole epRole = new EPRole(); + epRole.setName("test_delete"); + epRole.setId(1l); + epRole.setActive(true); + epRoleList2.add(epRole); + Mockito.when(dataAccessService.executeNamedQuery(GET_PORTAL_APP_ROLES_QUERY, deleteRoleParams2, null)) + .thenReturn(epRoleList2); + final Map<String, Long> appRoleFuncsParams = new HashMap<>(); + appRoleFuncsParams.put("appId", app.getId()); + appRoleFuncsParams.put("roleId", epRole.getId()); + List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); + EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction(); + epAppRoleFunction.setCode("test_code"); + epAppRoleFunction.setAppId(app.getId()); + epAppRoleFunction.setRoleAppId(null); + appRoleFunctionList.add(epAppRoleFunction); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctionList); + SQLQuery SqlQuery = Mockito.mock(SQLQuery.class); + Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery); + PowerMockito.mockStatic(EcompPortalUtils.class); + ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse); + assertTrue(externalAccessRolesServiceImpl.deleteRoleForApplication(epRole.getName(), app.getUebKey())); + } + + @Test public void deleteCentralRoleFunctionFailTest() throws Exception { final Map<String, String> params = new HashMap<>(); EPApp app = mockApp(); @@ -433,6 +509,69 @@ public class ExternalAccessRolesServiceImplTest { } @Test + public void getRoleFunctionTest() throws Exception { + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(SystemProperties.class); + EPApp app = mockApp(); + List<EPApp> appList = new ArrayList<>(); + appList.add(app); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + CentralV2RoleFunction expected = new CentralV2RoleFunction(null, "type_code", "test_name", null, "test_type", + "*", null); + Mockito.when(EcompPortalUtils.getFunctionCode("test_type|type_code|*")).thenReturn("type_code"); + Mockito.when(EcompPortalUtils.getFunctionType("test_type|type_code|*")).thenReturn("test_type"); + Mockito.when(EcompPortalUtils.getFunctionAction("test_type|type_code|*")).thenReturn("*"); + List<CentralV2RoleFunction> getRoleFuncList = new ArrayList<>(); + CentralV2RoleFunction getCenRole = new CentralV2RoleFunction("test_type|type_code|*", "test_name"); + getRoleFuncList.add(getCenRole); + final Map<String, String> params = new HashMap<>(); + params.put(FUNCTION_CODE_PARAMS, "test_type|type_code|*"); + params.put(APP_ID, String.valueOf(app.getId())); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null)) + .thenReturn(getRoleFuncList); + CentralV2RoleFunction actual = externalAccessRolesServiceImpl.getRoleFunction("test_type|type_code|*", + app.getUebKey()); + assertEquals(expected.getCode(), actual.getCode()); + } + + @Test + public void getRoleFunctionMutilpleFilterTest() throws Exception { + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(SystemProperties.class); + EPApp app = mockApp(); + List<EPApp> appList = new ArrayList<>(); + appList.add(app); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + CentralV2RoleFunction expected = new CentralV2RoleFunction(null, "type_code", "test_name", null, "test_type", + "*", null); + Mockito.when(EcompPortalUtils.getFunctionCode("test_type|type_code|*")).thenReturn("type_code"); + Mockito.when(EcompPortalUtils.getFunctionCode("test_type_1|type_code_1|*")).thenReturn("type_code_1"); + Mockito.when(EcompPortalUtils.getFunctionType("test_type|type_code|*")).thenReturn("test_type"); + Mockito.when(EcompPortalUtils.getFunctionAction("test_type|type_code|*")).thenReturn("*"); + List<CentralV2RoleFunction> getRoleFuncList = new ArrayList<>(); + CentralV2RoleFunction getCenRole = new CentralV2RoleFunction("test_type|type_code|*", "test_name"); + CentralV2RoleFunction getCenRole2 = new CentralV2RoleFunction("test_type_1|type_code_1|*", "test_name_1"); + getRoleFuncList.add(getCenRole); + getRoleFuncList.add(getCenRole2); + final Map<String, String> params = new HashMap<>(); + params.put(FUNCTION_CODE_PARAMS, "test_type|type_code|*"); + params.put(APP_ID, String.valueOf(app.getId())); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null)) + .thenReturn(getRoleFuncList); + CentralV2RoleFunction actual = externalAccessRolesServiceImpl.getRoleFunction("test_type|type_code|*", + app.getUebKey()); + assertEquals(expected.getCode(), actual.getCode()); + } + + @Test public void getUserTest() throws InvalidUserException { List<EPUser> userList = new ArrayList<>(); EPUser user = mockUser.mockEPUser(); @@ -446,6 +585,51 @@ public class ExternalAccessRolesServiceImplTest { } @Test + public void getV2UserWithRolesTest() throws Exception { + EPApp app = mockApp(); + app.setId(2l); + EPUser user = mockUser.mockEPUser(); + List<EPApp> appList = new ArrayList<>(); + appList.add(app); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + final Map<String, String> userParams = new HashMap<>(); + userParams.put("org_user_id", user.getOrgUserId()); + List<EPUser> userList = new ArrayList<>(); + Set<EPUserApp> userAppSet = user.getEPUserApps(); + EPUserApp epUserApp = new EPUserApp(); + EPRole epRole = new EPRole(); + epRole.setName("test"); + epRole.setId(1l); + epRole.setActive(true); + epRole.setAppRoleId(11l); + epUserApp.setApp(app); + epUserApp.setUserId(user.getId()); + epUserApp.setRole(epRole); + userAppSet.add(epUserApp); + user.setUserApps(userAppSet); + userList.add(user); + Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)) + .thenReturn(userList); + final Map<String, Long> params = new HashMap<>(); + List<CentralV2RoleFunction> appRoleFunctionList = new ArrayList<>(); + CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(); + centralV2RoleFunction.setCode("test_type|test_code|*"); + centralV2RoleFunction.setName("test name"); + centralV2RoleFunction.setAppId(app.getId()); + appRoleFunctionList.add(centralV2RoleFunction); + params.put("roleId", epUserApp.getRole().getId()); + params.put(APP_ID, epUserApp.getApp().getId()); + Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null)) + .thenReturn(appRoleFunctionList); + String actual = externalAccessRolesServiceImpl.getV2UserWithRoles(user.getOrgUserId(), app.getUebKey()); + String notExpected = ""; + assertNotEquals(actual, notExpected); + } + + @Test public void saveCentralRoleFunctionNewTestForV2() throws Exception { PowerMockito.mockStatic(EcompPortalUtils.class); EPApp app = mockApp(); @@ -533,17 +717,21 @@ public class ExternalAccessRolesServiceImplTest { } @SuppressWarnings("deprecation") - @Test(expected = IndexOutOfBoundsException.class) + @Test public void getAllAppUsersTest() throws Exception { - List<EPApp> expectedapps = new ArrayList<>(); EPApp app = new EPApp(); app.setEnabled(true); app.setId((long) 10); - expectedapps.add(app); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); List<EPRole> applicationRoles = new ArrayList<>(); Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles); Mockito.when(dataAccessService.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null)) - .thenReturn(expectedapps); + .thenReturn(appList); final Map<String, Long> appParams = new HashMap<>(); appParams.put("appId", app.getId()); List<EcompUserRoles> userList = new ArrayList<>(); @@ -551,16 +739,14 @@ public class ExternalAccessRolesServiceImplTest { ecompUserRoles.setOrgUserId("guestT"); ecompUserRoles.setRoleId((long) 1); ecompUserRoles.setRoleName("test"); - EcompUserRoles ecompUserRoles2 = new EcompUserRoles(); ecompUserRoles2.setOrgUserId("guestT"); ecompUserRoles2.setRoleId((long) 2); ecompUserRoles2.setRoleName("test new"); userList.add(ecompUserRoles); userList.add(ecompUserRoles2); - Mockito.when(dataAccessService.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList); - List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(uebKey); + List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(app.getUebKey()); assertEquals(usersfinalList.get(0).getRoles().size(), 2); } @@ -641,6 +827,95 @@ public class ExternalAccessRolesServiceImplTest { } @Test + public void getRoleFuncListTest() throws Exception { + EPApp app = mockApp(); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + final Map<String, Long> params = new HashMap<>(); + params.put(APP_ID, app.getId()); + List<CentralV2RoleFunction> expected = new ArrayList<>(); + CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|type_code|*", "test_name"); + expected.add(centralV2RoleFunction); + Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params, null)).thenReturn(expected); + List<CentralV2RoleFunction> actual = externalAccessRolesServiceImpl.getRoleFuncList(app.getUebKey()); + assertEquals(expected, actual); + } + + @Test + public void getRoleInfoTest() throws Exception { + EPApp app = mockApp(); + app.setId(2l); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<EPRole> globalRoles = new ArrayList<>(); + EPRole globalRole = new EPRole(); + globalRole.setName("global_test"); + globalRole.setId(2l); + globalRole.setActive(true); + globalRoles.add(globalRole); + Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles); + List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>(); + GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction(); + mockGlobalRole.setActive(true); + mockGlobalRole.setAppId(app.getId()); + mockGlobalRole.setRoleId(2l); + mockGlobalRole.setRoleName("global_test"); + mockGlobalRole.setFunctionCd("test_type|test_code|*"); + mockGlobalRole.setFunctionName("test name"); + mockGlobalRoles.add(mockGlobalRole); + Map<String, Long> params = new HashMap<>(); + params.put("roleId", 2l); + params.put("requestedAppId", 2l); + Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", params, null)) + .thenReturn(mockGlobalRoles); + CentralV2Role actual = externalAccessRolesServiceImpl.getRoleInfo(2l, app.getUebKey()); + assertNotEquals(null, actual); + } + + @Test + public void getPartnerRoleInfoTest() throws Exception { + EPApp app = mockApp(); + app.setId(2l); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<EPRole> applicationRoles = new ArrayList<>(); + EPRole appRole = new EPRole(); + appRole.setActive(true); + appRole.setAppId(app.getId()); + appRole.setAppRoleId(100l); + appRole.setId(10l); + appRole.setName("test"); + applicationRoles.add(appRole); + final Map<String, Long> getPartnerAppRoleParams = new HashMap<>(); + getPartnerAppRoleParams.put("appRoleId", 10l); + getPartnerAppRoleParams.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)) + .thenReturn(applicationRoles); + final Map<String, Long> params = new HashMap<>(); + params.put("roleId", appRole.getId()); + params.put(APP_ID, app.getId()); + List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>(); + CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|type_code|*", "test_name"); + cenRoleFuncList.add(centralV2RoleFunction); + Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null)) + .thenReturn(cenRoleFuncList); + CentralV2Role actual = externalAccessRolesServiceImpl.getRoleInfo(10l, app.getUebKey()); + assertNotEquals(null, actual); + } + + @Test public void saveRoleForPortalApplicationNewTest() throws Exception { PowerMockito.mockStatic(EcompPortalUtils.class); EPApp app = mockApp(); @@ -690,7 +965,7 @@ public class ExternalAccessRolesServiceImplTest { app.setId(1l); Role addRoleTest = new Role(); SortedSet<RoleFunction> roleFuncSet = new TreeSet<>(); - RoleFunction roleFunc= new RoleFunction(); + RoleFunction roleFunc = new RoleFunction(); roleFunc.setName("Test Name"); roleFunc.setCode("test_type|test_instance|*"); RoleFunction roleFunc2 = new RoleFunction(); @@ -738,7 +1013,7 @@ public class ExternalAccessRolesServiceImplTest { mockJsonObjectPerm2.put("type", "com.test.app.test_type2"); mockJsonObjectPerm2.put("instance", "test_instance2"); mockJsonObjectPerm2.put("action", "*"); - List<JSONObject> permsList = new ArrayList<>(); + List<JSONObject> permsList = new ArrayList<>(); permsList.add(mockJsonObjectPerm1); permsList.add(mockJsonObjectPerm2); mockJsonObjectRole.put("name", "com.test.app.Test"); @@ -771,37 +1046,405 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when( EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) .thenReturn(true); - Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)).thenReturn("@test.com"); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); + ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload); + List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); + final Map<String, Long> appRoleFuncsParams = new HashMap<>(); + appRoleFuncsParams.put("appId", app.getId()); + appRoleFuncsParams.put("roleId", getEPRole.getId()); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctionList); + Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance"); + Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3"); + final Map<String, String> getAppFunctionParams = new HashMap<>(); + getAppFunctionParams.put("appId", String.valueOf(app.getId())); + getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode()); + List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>(); + CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name"); + v2RoleFunction.add(v2RoleFunction1); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)) + .thenReturn(v2RoleFunction); + final Map<String, String> getAppFunctionParams2 = new HashMap<>(); + getAppFunctionParams2.put("appId", String.valueOf(app.getId())); + getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode()); + List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>(); + CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3"); + v2RoleFunction2.add(v2RoleFunction3); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)) + .thenReturn(v2RoleFunction2); + ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest, + app.getUebKey()); + ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, ""); + assertEquals(expected, actual); + } + + @Test + public void saveRoleExitsInDbButNotInExtAuthSystemTest() throws Exception { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPApp app = mockApp(); + app.setId(1l); + Role addRoleTest = new Role(); + SortedSet<RoleFunction> roleFuncSet = new TreeSet<>(); + RoleFunction roleFunc = new RoleFunction(); + roleFunc.setName("Test Name"); + roleFunc.setCode("test_type|test_instance|*"); + RoleFunction roleFunc2 = new RoleFunction(); + roleFunc2.setName("Test Name3"); + roleFunc2.setCode("test_type3|test_instance3|*"); + roleFuncSet.add(roleFunc); + roleFuncSet.add(roleFunc2); + addRoleTest.setActive(true); + addRoleTest.setName("Test2"); + addRoleTest.setId(2l); + addRoleTest.setRoleFunctions(roleFuncSet); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<EPRole> globalRoles = new ArrayList<>(); + EPRole globalRole = new EPRole(); + globalRole.setName("global_test"); + globalRole.setId(1111l); + globalRole.setActive(true); + globalRoles.add(globalRole); + Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles); + List<EPRole> epRoleList = new ArrayList<>(); + EPRole getEPRole = new EPRole(); + getEPRole.setName("Test"); + getEPRole.setId(2l); + getEPRole.setActive(true); + epRoleList.add(getEPRole); + final Map<String, Long> getPortalAppRoleParams = new HashMap<>(); + getPortalAppRoleParams.put("roleId", addRoleTest.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null)) + .thenReturn(epRoleList); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + HttpHeaders headers = new HttpHeaders(); + Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers); + JSONObject mockJsonObjectFinalRole = new JSONObject(); + ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse); + final Map<String, String> params = new HashMap<>(); + params.put("uebKey", app.getUebKey()); + params.put("roleId", String.valueOf(getEPRole.getId())); + List<BulkUploadUserRoles> userRolesList = new ArrayList<>(); + BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles(); + bulkUploadUserRoles.setAppNameSpace("com.test.app"); + bulkUploadUserRoles.setOrgUserId("guestT"); + bulkUploadUserRoles.setRoleName("Test2"); + userRolesList.add(bulkUploadUserRoles); + Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null)) + .thenReturn(userRolesList); + Mockito.when( + EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn(true); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); + List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); + final Map<String, Long> appRoleFuncsParams = new HashMap<>(); + appRoleFuncsParams.put("appId", app.getId()); + appRoleFuncsParams.put("roleId", getEPRole.getId()); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctionList); + Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance"); + Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3"); + final Map<String, String> getAppFunctionParams = new HashMap<>(); + getAppFunctionParams.put("appId", String.valueOf(app.getId())); + getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode()); + List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>(); + CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name"); + v2RoleFunction.add(v2RoleFunction1); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)) + .thenReturn(v2RoleFunction); + final Map<String, String> getAppFunctionParams2 = new HashMap<>(); + getAppFunctionParams2.put("appId", String.valueOf(app.getId())); + getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode()); + List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>(); + CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3"); + v2RoleFunction2.add(v2RoleFunction3); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)) + .thenReturn(v2RoleFunction2); + ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest, + app.getUebKey()); + ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, ""); + assertEquals(expected, actual); + } + + @Test + public void saveGlobalRoleForPortalApplicationUpdateTest() throws Exception { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPApp app = mockApp(); + app.setId(1l); + Role addRoleTest = new Role(); + SortedSet<RoleFunction> roleFuncSet = new TreeSet<>(); + RoleFunction roleFunc = new RoleFunction(); + roleFunc.setName("Test Name"); + roleFunc.setCode("test_type|test_instance|*"); + RoleFunction roleFunc2 = new RoleFunction(); + roleFunc2.setName("Test Name3"); + roleFunc2.setCode("test_type3|test_instance3|*"); + roleFuncSet.add(roleFunc); + roleFuncSet.add(roleFunc2); + addRoleTest.setActive(true); + addRoleTest.setName("global_test2"); + addRoleTest.setId(1111l); + addRoleTest.setRoleFunctions(roleFuncSet); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<EPRole> globalRoles = new ArrayList<>(); + EPRole globalRole = new EPRole(); + globalRole.setName("global_test"); + globalRole.setId(1111l); + globalRole.setActive(true); + globalRoles.add(globalRole); + Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles); + List<EPRole> epRoleList = new ArrayList<>(); + EPRole getEPRole = new EPRole(); + getEPRole.setName("global_test"); + getEPRole.setId(1111l); + getEPRole.setActive(true); + epRoleList.add(getEPRole); + final Map<String, Long> getPortalAppRoleParams = new HashMap<>(); + getPortalAppRoleParams.put("roleId", addRoleTest.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null)) + .thenReturn(epRoleList); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + HttpHeaders headers = new HttpHeaders(); + Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers); + JSONObject mockJsonObjectRole = new JSONObject(); + JSONObject mockJsonObjectFinalRole = new JSONObject(); + JSONObject mockJsonObjectPerm1 = new JSONObject(); + JSONObject mockJsonObjectPerm2 = new JSONObject(); + mockJsonObjectPerm1.put("type", "com.test.app.test_type"); + mockJsonObjectPerm1.put("instance", "test_instance"); + mockJsonObjectPerm1.put("action", "*"); + mockJsonObjectPerm2.put("type", "com.test.app.test_type2"); + mockJsonObjectPerm2.put("instance", "test_instance2"); + mockJsonObjectPerm2.put("action", "*"); + List<JSONObject> permsList = new ArrayList<>(); + permsList.add(mockJsonObjectPerm1); + permsList.add(mockJsonObjectPerm2); + mockJsonObjectRole.put("name", "com.test.app.global_test"); + mockJsonObjectRole.put("perms", permsList); + mockJsonObjectRole.put("description", + "{\"id\":\"1111\",\"name\":\"global_test\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"null\",\"appRoleId\":\"null\"}"); + List<JSONObject> roleList = new ArrayList<>(); + roleList.add(mockJsonObjectRole); + mockJsonObjectFinalRole.put("role", roleList); + ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse); + ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse); + final Map<String, String> params = new HashMap<>(); + params.put("uebKey", app.getUebKey()); + params.put("roleId", String.valueOf(getEPRole.getId())); + List<BulkUploadUserRoles> userRolesList = new ArrayList<>(); + BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles(); + bulkUploadUserRoles.setAppNameSpace("com.test.app"); + bulkUploadUserRoles.setOrgUserId("guestT"); + bulkUploadUserRoles.setRoleName("Test2"); + userRolesList.add(bulkUploadUserRoles); + Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null)) + .thenReturn(userRolesList); + Mockito.when( + EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn(true); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload); - List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); + List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); final Map<String, Long> appRoleFuncsParams = new HashMap<>(); appRoleFuncsParams.put("appId", app.getId()); appRoleFuncsParams.put("roleId", getEPRole.getId()); - Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)).thenReturn(appRoleFunctionList); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctionList); Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance"); Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3"); - final Map<String, String> getAppFunctionParams = new HashMap<>(); + final Map<String, String> getAppFunctionParams = new HashMap<>(); getAppFunctionParams.put("appId", String.valueOf(app.getId())); getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode()); List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>(); - CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name"); + CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name"); v2RoleFunction.add(v2RoleFunction1); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)).thenReturn(v2RoleFunction); - final Map<String, String> getAppFunctionParams2 = new HashMap<>(); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)) + .thenReturn(v2RoleFunction); + final Map<String, String> getAppFunctionParams2 = new HashMap<>(); getAppFunctionParams2.put("appId", String.valueOf(app.getId())); getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode()); List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>(); - CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3"); + CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3"); v2RoleFunction2.add(v2RoleFunction3); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)).thenReturn(v2RoleFunction2); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)) + .thenReturn(v2RoleFunction2); ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest, app.getUebKey()); ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, ""); assertEquals(expected, actual); } - + + @Test + public void saveRoleForPartnerApplicationUpdateTest() throws Exception { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPApp app = mockApp(); + app.setId(2l); + Role addRoleTest = new Role(); + SortedSet<RoleFunction> roleFuncSet = new TreeSet<>(); + RoleFunction roleFunc = new RoleFunction(); + roleFunc.setName("Test Name"); + roleFunc.setCode("test_type|test_instance|*"); + RoleFunction roleFunc2 = new RoleFunction(); + roleFunc2.setName("Test Name3"); + roleFunc2.setCode("test_type3|test_instance3|*"); + roleFuncSet.add(roleFunc); + roleFuncSet.add(roleFunc2); + addRoleTest.setActive(false); + addRoleTest.setName("Test2"); + addRoleTest.setId(22l); + addRoleTest.setRoleFunctions(roleFuncSet); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<EPRole> globalRoles = new ArrayList<>(); + EPRole globalRole = new EPRole(); + globalRole.setName("global_test"); + globalRole.setId(1111l); + globalRole.setActive(true); + globalRoles.add(globalRole); + Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles); + List<EPRole> epRoleList = new ArrayList<>(); + EPRole getEPRole = new EPRole(); + getEPRole.setName("Test2"); + getEPRole.setId(2l); + getEPRole.setActive(true); + getEPRole.setAppRoleId(22l); + epRoleList.add(getEPRole); + final Map<String, Long> getPortalAppRoleParams = new HashMap<>(); + getPortalAppRoleParams.put("appRoleId", addRoleTest.getId()); + getPortalAppRoleParams.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPortalAppRoleParams, null)) + .thenReturn(epRoleList); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + HttpHeaders headers = new HttpHeaders(); + Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers); + JSONObject mockJsonObjectRole = new JSONObject(); + JSONObject mockJsonObjectFinalRole = new JSONObject(); + JSONObject mockJsonObjectPerm1 = new JSONObject(); + JSONObject mockJsonObjectPerm2 = new JSONObject(); + mockJsonObjectPerm1.put("type", "com.test.app.test_type"); + mockJsonObjectPerm1.put("instance", "test_instance"); + mockJsonObjectPerm1.put("action", "*"); + mockJsonObjectPerm2.put("type", "com.test.app.test_type2"); + mockJsonObjectPerm2.put("instance", "test_instance2"); + mockJsonObjectPerm2.put("action", "*"); + List<JSONObject> permsList = new ArrayList<>(); + permsList.add(mockJsonObjectPerm1); + permsList.add(mockJsonObjectPerm2); + mockJsonObjectRole.put("name", "com.test.app.Test"); + mockJsonObjectRole.put("perms", permsList); + mockJsonObjectRole.put("description", + "{\"id\":\"2\",\"name\":\"Test2\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"2\",\"appRoleId\":\"22\"}"); + List<JSONObject> roleList = new ArrayList<>(); + roleList.add(mockJsonObjectRole); + mockJsonObjectFinalRole.put("role", roleList); + ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK); + final Map<String, String> getPartnerAppRoleParams = new HashMap<>(); + getPartnerAppRoleParams.put(APP_ROLE_NAME_PARAM, addRoleTest.getName()); + getPartnerAppRoleParams.put("appId", String.valueOf(app.getId())); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, + getPartnerAppRoleParams, null)).thenReturn(epRoleList); + ResponseEntity<String> updateRoleResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateRoleResponse); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse); + ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse); + final Map<String, String> params = new HashMap<>(); + params.put("uebKey", app.getUebKey()); + params.put("roleId", String.valueOf(getEPRole.getId())); + List<BulkUploadUserRoles> userRolesList = new ArrayList<>(); + BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles(); + bulkUploadUserRoles.setAppNameSpace("com.test.app"); + bulkUploadUserRoles.setOrgUserId("guestT"); + bulkUploadUserRoles.setRoleName("Test2"); + userRolesList.add(bulkUploadUserRoles); + Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null)) + .thenReturn(userRolesList); + Mockito.when( + EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn(true); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); + ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload); + List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); + final Map<String, Long> appRoleFuncsParams = new HashMap<>(); + appRoleFuncsParams.put("appId", app.getId()); + appRoleFuncsParams.put("roleId", getEPRole.getId()); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctionList); + Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance"); + Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3"); + final Map<String, String> getAppFunctionParams = new HashMap<>(); + getAppFunctionParams.put("appId", String.valueOf(app.getId())); + getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode()); + List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>(); + CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name"); + v2RoleFunction.add(v2RoleFunction1); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)) + .thenReturn(v2RoleFunction); + final Map<String, String> getAppFunctionParams2 = new HashMap<>(); + getAppFunctionParams2.put("appId", String.valueOf(app.getId())); + getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode()); + List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>(); + CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3"); + v2RoleFunction2.add(v2RoleFunction3); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)) + .thenReturn(v2RoleFunction2); + ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest, + app.getUebKey()); + ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, ""); + assertEquals(expected, actual); + } + @Test public void saveGlobalRoleFunctionsForPartnerApplicationUpdateTest() throws Exception { PowerMockito.mockStatic(EPCommonSystemProperties.class); @@ -811,7 +1454,7 @@ public class ExternalAccessRolesServiceImplTest { app.setId(2l); Role addRoleTest = new Role(); SortedSet<RoleFunction> roleFuncSet = new TreeSet<>(); - RoleFunction roleFunc= new RoleFunction(); + RoleFunction roleFunc = new RoleFunction(); roleFunc.setName("Test Name"); roleFunc.setCode("test_type|test_instance|*"); RoleFunction roleFunc2 = new RoleFunction(); @@ -856,6 +1499,7 @@ public class ExternalAccessRolesServiceImplTest { JSONObject mockJsonObjectPerm = new JSONObject(); JSONObject mockJsonObjectPerm2 = new JSONObject(); JSONObject mockJsonObjectPerm3 = new JSONObject(); + JSONObject mockJsonObjectPerm4 = new JSONObject(); JSONObject mockJsonObjectFinalPerm = new JSONObject(); mockJsonObjectPerm.put("type", "com.test.app.test_type"); mockJsonObjectPerm.put("instance", "test_instance"); @@ -869,10 +1513,15 @@ public class ExternalAccessRolesServiceImplTest { mockJsonObjectPerm3.put("instance", "test_instance3"); mockJsonObjectPerm3.put("action", "*"); mockJsonObjectPerm3.put("description", "Test Name3"); + mockJsonObjectPerm4.put("type", "com.test.app.test_type4"); + mockJsonObjectPerm4.put("instance", "test_instance4"); + mockJsonObjectPerm4.put("action", "*"); + mockJsonObjectPerm4.put("description", "Test Name4"); List<JSONObject> mockJson = new ArrayList<>(); mockJson.add(mockJsonObjectPerm); mockJson.add(mockJsonObjectPerm2); mockJson.add(mockJsonObjectPerm3); + mockJson.add(mockJsonObjectPerm4); mockJsonObjectFinalPerm.put("perm", mockJson); ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), @@ -882,44 +1531,63 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse); Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance"); - final Map<String, Long> epAppRoleFuncParams = new HashMap<>(); + final Map<String, Long> epAppRoleFuncParams = new HashMap<>(); epAppRoleFuncParams.put("requestedAppId", app.getId()); - epAppRoleFuncParams.put("roleId",globalRole.getId()); - List<GlobalRoleWithApplicationRoleFunction> globalRoleFunctionList = new ArrayList<>(); - Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", epAppRoleFuncParams, null)).thenReturn(globalRoleFunctionList); + epAppRoleFuncParams.put("roleId", globalRole.getId()); + List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>(); + GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction(); + mockGlobalRole.setActive(true); + mockGlobalRole.setAppId(app.getId()); + mockGlobalRole.setRoleId(1111l); + mockGlobalRole.setRoleName("global_test"); + mockGlobalRole.setFunctionCd("test_type4|test_instance4|*"); + mockGlobalRole.setFunctionName("test name"); + mockGlobalRoles.add(mockGlobalRole); + Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", epAppRoleFuncParams, null)) + .thenReturn(mockGlobalRoles); + ResponseEntity<String> delResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse); final Map<String, Long> appRoleFuncsParams = new HashMap<>(); appRoleFuncsParams.put("appId", app.getId()); appRoleFuncsParams.put("roleId", globalRole.getId()); - List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); + List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction(); epAppRoleFunction.setAppId(app.getId()); epAppRoleFunction.setRoleAppId("1"); epAppRoleFunction.setCode("test"); epAppRoleFunction.setRoleId(1111l); appRoleFunctionList.add(epAppRoleFunction); - Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)).thenReturn(appRoleFunctionList); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctionList); final Map<String, String> getAppFunctionParams = new HashMap<>(); Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance"); Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3"); + Mockito.when(EcompPortalUtils.getFunctionCode("test_type4|test_instance4|*")).thenReturn("test_instance4"); getAppFunctionParams.put("appId", String.valueOf(app.getId())); getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode()); List<CentralV2RoleFunction> roleFunction = new ArrayList<>(); - CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, roleFunc.getCode(), roleFunc.getName(), app.getId(), null); + CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, roleFunc.getCode(), + roleFunc.getName(), app.getId(), null); roleFunction.add(centralV2RoleFunction); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)).thenReturn(roleFunction); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)) + .thenReturn(roleFunction); final Map<String, String> getAppFunctionParams2 = new HashMap<>(); getAppFunctionParams2.put("appId", String.valueOf(app.getId())); getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode()); List<CentralV2RoleFunction> roleFunction2 = new ArrayList<>(); - CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, roleFunc2.getCode(), roleFunc2.getName(), app.getId(), null); + CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, roleFunc2.getCode(), + roleFunc2.getName(), app.getId(), null); roleFunction2.add(centralV2RoleFunction2); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)).thenReturn(roleFunction2); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)) + .thenReturn(roleFunction2); ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest, app.getUebKey()); ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, ""); assertEquals(expected, actual); } - + @Test public void syncRoleFunctionFromExternalAccessSystemTest() { PowerMockito.mockStatic(EPCommonSystemProperties.class); @@ -935,18 +1603,24 @@ public class ExternalAccessRolesServiceImplTest { mockJsonObjectPerm.put("instance", "test_instance"); mockJsonObjectPerm.put("action", "*"); mockJsonObjectPerm.put("description", "test_name"); - List<String> rolePermList = new ArrayList<>(); + List<String> rolePermList = new ArrayList<>(); rolePermList.add("com.test.app|test1"); mockJsonObjectPerm.put("roles", rolePermList); mockJsonObjectPerm2.put("type", "com.test.app.test_type2"); mockJsonObjectPerm2.put("instance", "test_instance2"); + List<String> rolePermList2 = new ArrayList<>(); + rolePermList2.add("com.test.app|test1"); + rolePermList2.add("com.test.app|test2"); + rolePermList2.add("com.test.app|test6"); + rolePermList2.add("com.test.app.new|global_test"); mockJsonObjectPerm2.put("action", "*"); + mockJsonObjectPerm2.put("roles", rolePermList2); mockJsonObjectPerm2.put("description", "test_name2"); mockJsonObjectPerm3.put("type", "com.test.app.access"); mockJsonObjectPerm3.put("instance", "test_instance3"); mockJsonObjectPerm3.put("action", "*"); mockJsonObjectPerm3.put("description", "test_name3"); - List<JSONObject> permsList = new ArrayList<>(); + List<JSONObject> permsList = new ArrayList<>(); permsList.add(mockJsonObjectPerm); permsList.add(mockJsonObjectPerm2); permsList.add(mockJsonObjectPerm3); @@ -956,10 +1630,17 @@ public class ExternalAccessRolesServiceImplTest { Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); final Map<String, Long> params = new HashMap<>(); params.put(APP_ID, app.getId()); - List<CentralV2RoleFunction> appFunctions = new ArrayList<>(); - CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*", "test_name", app.getId(), null); + List<CentralV2RoleFunction> appFunctions = new ArrayList<>(); + CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*", + "test_name", app.getId(), null); + CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_instance2", "test_name2", + app.getId(), null); + CentralV2RoleFunction centralV2RoleFunction3 = new CentralV2RoleFunction(null, "test_instance5", "test_name5", + app.getId(), null); appFunctions.add(centralV2RoleFunction); - Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params,null)).thenReturn(appFunctions); + appFunctions.add(centralV2RoleFunction2); + appFunctions.add(centralV2RoleFunction3); + Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params, null)).thenReturn(appFunctions); List<EPRole> globalRoles = new ArrayList<>(); EPRole globalRole = new EPRole(); globalRole.setName("global_test"); @@ -976,43 +1657,109 @@ public class ExternalAccessRolesServiceImplTest { getEPRole2.setName("global_test"); getEPRole2.setId(1111l); getEPRole2.setActive(true); + EPRole getEPRole3 = new EPRole(); + getEPRole3.setName("test2"); + getEPRole3.setId(4l); + getEPRole3.setActive(true); getCurrentRoleList.add(getEPRole); getCurrentRoleList.add(getEPRole2); - Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRolesList", null, null)).thenReturn(getCurrentRoleList); + getCurrentRoleList.add(getEPRole3); + final Map<String, Long> appParams = new HashMap<>(); + appParams.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)) + .thenReturn(getCurrentRoleList); final Map<String, String> appSyncFuncsParams = new HashMap<>(); appSyncFuncsParams.put("appId", String.valueOf(app.getId())); appSyncFuncsParams.put("functionCd", ""); List<CentralV2RoleFunction> roleFunctionList = new ArrayList<>(); - Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, - null)).thenReturn(roleFunctionList); + Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null)) + .thenReturn(roleFunctionList); String code = centralV2RoleFunction.getCode(); appSyncFuncsParams.put("functionCd", code); - CentralV2RoleFunction getCentralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*", "test_name", app.getId(), null); + CentralV2RoleFunction getCentralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*", + "test_name", app.getId(), null); roleFunctionList.add(getCentralV2RoleFunction); - Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, - null)).thenReturn(roleFunctionList); + Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null)) + .thenReturn(roleFunctionList); + List<LocalRole> localRoles = new ArrayList<>(); + LocalRole localRole = new LocalRole(); + localRole.setRoleId(2); + localRole.setRolename("test1"); + LocalRole localRole2 = new LocalRole(); + localRole2.setRoleId(3); + localRole2.setRolename("test3"); + localRoles.add(localRole); + localRoles.add(localRole2); final Map<String, String> appRoleFuncParams = new HashMap<>(); - appRoleFuncParams.put("functionCd", roleFunctionList.get(0).getCode()); + appRoleFuncParams.put("functionCd", "test_type2|test_instance2|*"); appRoleFuncParams.put("appId", String.valueOf(app.getId())); - Mockito.when(dataAccessService.executeNamedQuery("getCurrentAppRoleFunctions", - appRoleFuncParams, null)).thenReturn(new ArrayList<LocalRole>()); + Mockito.when(dataAccessService.executeNamedQuery("getCurrentAppRoleFunctions", appRoleFuncParams, null)) + .thenReturn(localRoles); Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app", app.getNameSpace())).thenReturn(true); Mockito.when(EcompPortalUtils.getFunctionCode("test_type2|test_instance2|*")).thenReturn("test_instance2"); appSyncFuncsParams.put("functionCd", "test_instance2"); List<CentralV2RoleFunction> roleFunctionList2 = new ArrayList<>(); - Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, - null)).thenReturn(roleFunctionList2); + Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null)) + .thenReturn(roleFunctionList2); String code2 = "test_type2|test_instance2|*"; appSyncFuncsParams.put("functionCd", code2); - CentralV2RoleFunction getCentralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_type2|test_instance2|*", "test_name2", app.getId(), null); + CentralV2RoleFunction getCentralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_type2|test_instance2|*", + "test_name2", app.getId(), null); roleFunctionList2.add(getCentralV2RoleFunction2); - Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, - null)).thenReturn(roleFunctionList2); + Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null)) + .thenReturn(roleFunctionList2); + final Map<String, Long> params3 = new HashMap<>(); + params3.put("appId", app.getId()); + params3.put("roleId", getEPRole2.getId()); + List<EPAppRoleFunction> currentGlobalRoleFunctionsList = new ArrayList<>(); + EPAppRoleFunction addGlobalRoleFunction = new EPAppRoleFunction(); + addGlobalRoleFunction.setAppId(app.getId()); + addGlobalRoleFunction.setCode("test_type|test_instance|*"); + addGlobalRoleFunction.setRoleId(1111l); + currentGlobalRoleFunctionsList.add(addGlobalRoleFunction); + Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", params3, null)) + .thenReturn(currentGlobalRoleFunctionsList); + final Map<String, String> roleParams = new HashMap<>(); + roleParams.put(APP_ROLE_NAME_PARAM, "test6"); + roleParams.put("appId", String.valueOf(app.getId())); + List<EPRole> roleCreated = new ArrayList<>(); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, roleParams, null)) + .thenReturn(roleCreated); + final Map<String, String> getRoleByNameParams = new HashMap<>(); + getRoleByNameParams.put(APP_ROLE_NAME_PARAM, "test6"); + getRoleByNameParams.put("appId", String.valueOf(app.getId())); + EPRole getNewEPRole = new EPRole(); + getNewEPRole.setName("test6"); + getNewEPRole.setId(8l); + getNewEPRole.setActive(true); + List<EPRole> roleCreated2 = new ArrayList<>(); + roleCreated2.add(getNewEPRole); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, + getRoleByNameParams, null)).thenReturn(roleCreated2); + EPRole getNewEPRoleFinal = new EPRole(); + getNewEPRoleFinal.setName("test6"); + getNewEPRoleFinal.setId(8l); + getNewEPRoleFinal.setActive(true); + getNewEPRoleFinal.setAppRoleId(8l); + final Map<String, String> getRoleByNameParams2 = new HashMap<>(); + getRoleByNameParams2.put(APP_ROLE_NAME_PARAM, "test6"); + getRoleByNameParams2.put("appId", String.valueOf(app.getId())); + List<EPRole> roleCreated3 = new ArrayList<>(); + roleCreated3.add(getNewEPRole); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, + getRoleByNameParams2, null)).thenReturn(roleCreated3); + List<EPRole> roleInfo = new ArrayList<>(); + roleInfo.add(getNewEPRoleFinal); + final Map<String, Long> getPartnerAppRoleParams = new HashMap<>(); + getPartnerAppRoleParams.put("appRoleId", getNewEPRoleFinal.getId()); + getPartnerAppRoleParams.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)) + .thenReturn(roleInfo); externalAccessRolesServiceImpl.syncRoleFunctionFromExternalAccessSystem(app); } - + @Test - public void syncApplicationRolesWithEcompDBTest(){ + public void syncApplicationRolesWithEcompDBTest() { PowerMockito.mockStatic(EPCommonSystemProperties.class); PowerMockito.mockStatic(EcompPortalUtils.class); PowerMockito.mockStatic(SystemProperties.class); @@ -1029,7 +1776,7 @@ public class ExternalAccessRolesServiceImplTest { mockJsonObjectPerm2.put("type", "com.test.app.test_type2"); mockJsonObjectPerm2.put("instance", "test_instance2"); mockJsonObjectPerm2.put("action", "*"); - List<JSONObject> permsList = new ArrayList<>(); + List<JSONObject> permsList = new ArrayList<>(); permsList.add(mockJsonObjectPerm1); permsList.add(mockJsonObjectPerm2); mockJsonObjectRole.put("name", "com.test.app.Test"); @@ -1037,7 +1784,7 @@ public class ExternalAccessRolesServiceImplTest { mockJsonObjectRole.put("description", "{\"id\":\"2\",\"name\":\"test1\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"2\",\"appRoleId\":\"2\"}"); mockJsonObjectRole2.put("name", "com.test.app.Test2"); - List<JSONObject> permsList2 = new ArrayList<>(); + List<JSONObject> permsList2 = new ArrayList<>(); permsList2.add(mockJsonObjectPerm1); mockJsonObjectRole2.put("perms", permsList2); List<JSONObject> roleList = new ArrayList<>(); @@ -1064,24 +1811,30 @@ public class ExternalAccessRolesServiceImplTest { getCurrentRoleList.add(getEPRole2); final Map<String, Long> appParams = new HashMap<>(); appParams.put("appId", app.getId()); - Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)).thenReturn(getCurrentRoleList); - Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type", app.getNameSpace())).thenReturn(true); - Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type2", app.getNameSpace())).thenReturn(true); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)) + .thenReturn(getCurrentRoleList); + Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type", app.getNameSpace())) + .thenReturn(true); + Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type2", app.getNameSpace())) + .thenReturn(true); List<EPAppRoleFunction> appRoleFunctions = new ArrayList<>(); EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction(); epAppRoleFunction.setAppId(app.getId()); epAppRoleFunction.setCode("test_type|test_instance|*"); epAppRoleFunction.setRoleId(getEPRole.getId()); appRoleFunctions.add(epAppRoleFunction); - final Map<String, Long> appRoleFuncsParams = new HashMap<>(); + final Map<String, Long> appRoleFuncsParams = new HashMap<>(); appRoleFuncsParams.put("appId", app.getId()); appRoleFuncsParams.put("roleId", Long.valueOf(getEPRole.getId())); - Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)).thenReturn(appRoleFunctions); - List<CentralV2RoleFunction> getV2RoleFunction = new ArrayList<>(); - final Map<String, String> appFuncsParams = new HashMap<>(); + Mockito.when( + dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)) + .thenReturn(appRoleFunctions); + List<CentralV2RoleFunction> getV2RoleFunction = new ArrayList<>(); + final Map<String, String> appFuncsParams = new HashMap<>(); appFuncsParams.put("appId", String.valueOf(app.getId())); appFuncsParams.put("functionCd", "test_instance2"); - Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null)).thenReturn(getV2RoleFunction); + Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null)) + .thenReturn(getV2RoleFunction); appFuncsParams.put("functionCd", "test_type2|test_instance2|*"); CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(); centralV2RoleFunction.setAppId(app.getId()); @@ -1098,18 +1851,23 @@ public class ExternalAccessRolesServiceImplTest { getEPRoleDeactivate.setAppId(app.getId()); getEPRoleDeactivate.setAppRoleId(3l); roleListDeactivate.add(getEPRoleDeactivate); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, extRoleParams, null)).thenReturn(roleListDeactivate); - Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null)).thenReturn(getV2RoleFunction); + Mockito.when( + dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, extRoleParams, null)) + .thenReturn(roleListDeactivate); + Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null)) + .thenReturn(getV2RoleFunction); List<EPRole> updateLocalFromExtAuth = new ArrayList<>(); updateLocalFromExtAuth.add(getEPRole); final Map<String, String> roleParams = new HashMap<>(); roleParams.put(APP_ROLE_NAME_PARAM, getEPRole.getName()); roleParams.put(APP_ID, app.getId().toString()); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, roleParams, null)).thenReturn(updateLocalFromExtAuth); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, roleParams, null)) + .thenReturn(updateLocalFromExtAuth); roleParams.put(APP_ROLE_NAME_PARAM, getEPRole2.getName()); List<EPRole> updateLocalFromExtAuth2 = new ArrayList<>(); updateLocalFromExtAuth.add(getEPRole); - Mockito.when(dataAccessService.executeNamedQuery("getRoletoUpdateInExternalAuthSystem", roleParams, null)).thenReturn(updateLocalFromExtAuth2); + Mockito.when(dataAccessService.executeNamedQuery("getRoletoUpdateInExternalAuthSystem", roleParams, null)) + .thenReturn(updateLocalFromExtAuth2); final Map<String, String> globalRoleParams = new HashMap<>(); globalRoleParams.put("appId", String.valueOf(app.getId())); globalRoleParams.put("appRoleName", "Test2"); @@ -1120,17 +1878,20 @@ public class ExternalAccessRolesServiceImplTest { addRoleInLocal.setAppId(app.getId()); addRoleInLocal.setActive(true); addNewRoleList.add(addRoleInLocal); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, globalRoleParams, null)).thenReturn(addNewRoleList); + Mockito.when( + dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, globalRoleParams, null)) + .thenReturn(addNewRoleList); final Map<String, String> params = new HashMap<>(); params.put(APP_ROLE_NAME_PARAM, "Test2"); params.put(APP_ID, app.getId().toString()); addRoleInLocal.setAppRoleId(4l); addNewRoleList.add(addRoleInLocal); - Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, params, null)).thenReturn(addNewRoleList); + Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, params, null)) + .thenReturn(addNewRoleList); externalAccessRolesServiceImpl.syncApplicationRolesWithEcompDB(app); } - - @Test + + @Test public void deleteDependencyRoleRecord() throws Exception { PowerMockito.mockStatic(EPCommonSystemProperties.class); PowerMockito.mockStatic(EcompPortalUtils.class); @@ -1158,7 +1919,7 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)) .thenReturn(epRoleList); Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); - JSONObject getUser = new JSONObject(); + JSONObject getUser = new JSONObject(); getUser.put("name", "com.test.app.test1"); ResponseEntity<String> getResponse = new ResponseEntity<>(getUser.toString(), HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), @@ -1167,12 +1928,13 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse); Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery); - ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l, app.getUebKey(), user.getOrgUserId()); + ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l, + app.getUebKey(), user.getOrgUserId()); ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, ""); assertEquals(expected, actual); } - - @Test + + @Test public void deleteDependencyRoleRecordForPortal() throws Exception { PowerMockito.mockStatic(EPCommonSystemProperties.class); PowerMockito.mockStatic(EcompPortalUtils.class); @@ -1199,7 +1961,7 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPartnerAppRoleParams, null)) .thenReturn(epRoleList); Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); - JSONObject getUser = new JSONObject(); + JSONObject getUser = new JSONObject(); getUser.put("name", "com.test.app.test1"); ResponseEntity<String> getResponse = new ResponseEntity<>(getUser.toString(), HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), @@ -1208,12 +1970,13 @@ public class ExternalAccessRolesServiceImplTest { Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse); Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery); - ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l, app.getUebKey(), user.getOrgUserId()); + ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l, + app.getUebKey(), user.getOrgUserId()); ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, ""); assertEquals(expected, actual); } - - @Test + + @Test public void bulkUploadFunctionsTest() throws Exception { PowerMockito.mockStatic(EPCommonSystemProperties.class); PowerMockito.mockStatic(EcompPortalUtils.class); @@ -1236,12 +1999,12 @@ public class ExternalAccessRolesServiceImplTest { roleFuncList.add(roleFunc); roleFuncList.add(roleFunc2); Mockito.when(dataAccessService.executeNamedQuery("getAllFunctions", null, null)).thenReturn(roleFuncList); - JSONObject perm = new JSONObject(); - JSONObject permList = new JSONObject(); - perm.put("type", app.getNameSpace()+".access"); + JSONObject perm = new JSONObject(); + JSONObject permList = new JSONObject(); + perm.put("type", app.getNameSpace() + ".access"); perm.put("instance", "type_instance"); perm.put("action", "*"); - List<JSONObject> addPerms = new ArrayList<>(); + List<JSONObject> addPerms = new ArrayList<>(); addPerms.add(perm); permList.put("perm", addPerms); ResponseEntity<String> getResponse = new ResponseEntity<>(permList.toString(), HttpStatus.OK); @@ -1254,9 +2017,9 @@ public class ExternalAccessRolesServiceImplTest { Integer expected = 2; assertEquals(expected, actual); } - + @Test - public void bulkUploadRolesTest() throws Exception{ + public void bulkUploadRolesTest() throws Exception { PowerMockito.mockStatic(EPCommonSystemProperties.class); PowerMockito.mockStatic(EcompPortalUtils.class); PowerMockito.mockStatic(SystemProperties.class); @@ -1283,7 +2046,8 @@ public class ExternalAccessRolesServiceImplTest { epRoleList.add(getEPRole2); final Map<String, Long> appParams = new HashMap<>(); appParams.put("appId", app.getId()); - Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)).thenReturn(epRoleList); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)) + .thenReturn(epRoleList); List<EPRole> epRoleList1 = new ArrayList<>(); EPRole ePRole = new EPRole(); ePRole.setName("Test"); @@ -1293,8 +2057,9 @@ public class ExternalAccessRolesServiceImplTest { epRoleList1.add(ePRole); final Map<String, Long> getPartnerAppRoleParams = new HashMap<>(); getPartnerAppRoleParams.put("appRoleId", ePRole.getId()); - getPartnerAppRoleParams.put("appId", app.getId()); - Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)).thenReturn(epRoleList1); + getPartnerAppRoleParams.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)) + .thenReturn(epRoleList1); List<EPRole> epRoleList2 = new ArrayList<>(); EPRole ePRole2 = new EPRole(); ePRole2.setName("Test2"); @@ -1304,8 +2069,9 @@ public class ExternalAccessRolesServiceImplTest { epRoleList2.add(ePRole2); final Map<String, Long> getPartnerAppRoleParams2 = new HashMap<>(); getPartnerAppRoleParams2.put("appRoleId", ePRole2.getId()); - getPartnerAppRoleParams2.put("appId", app.getId()); - Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams2, null)).thenReturn(epRoleList2); + getPartnerAppRoleParams2.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams2, null)) + .thenReturn(epRoleList2); ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse); @@ -1313,9 +2079,9 @@ public class ExternalAccessRolesServiceImplTest { Integer expected = 2; assertEquals(expected, actual); } - + @Test - public void bulkUploadUserRolesTest() throws Exception{ + public void bulkUploadUserRolesTest() throws Exception { PowerMockito.mockStatic(EPCommonSystemProperties.class); PowerMockito.mockStatic(EcompPortalUtils.class); PowerMockito.mockStatic(SystemProperties.class); @@ -1341,7 +2107,8 @@ public class ExternalAccessRolesServiceImplTest { userRolesList.add(bulkUploadUserRoles2); final Map<String, String> appParams = new HashMap<>(); appParams.put("uebKey", app.getUebKey()); - Mockito.when(dataAccessService.executeNamedQuery("getBulkUserRoles", appParams, null)).thenReturn(userRolesList); + Mockito.when(dataAccessService.executeNamedQuery("getBulkUserRoles", appParams, null)) + .thenReturn(userRolesList); ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse); @@ -1349,6 +2116,271 @@ public class ExternalAccessRolesServiceImplTest { Integer expected = 2; assertEquals(expected, actual); } - - + + @Test + public void getUserRolesTest() throws Exception { + EPApp app = mockApp(); + EPUser user = mockUser.mockEPUser(); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + final Map<String, String> userParams = new HashMap<>(); + userParams.put("org_user_id", user.getOrgUserId()); + List<EPUser> userList = new ArrayList<>(); + Set<EPUserApp> userAppSet = user.getEPUserApps(); + EPUserApp epUserApp = new EPUserApp(); + EPRole epRole = new EPRole(); + epRole.setName("test"); + epRole.setId(1l); + epRole.setActive(true); + epRole.setAppRoleId(11l); + epUserApp.setApp(app); + epUserApp.setUserId(user.getId()); + epUserApp.setRole(epRole); + userAppSet.add(epUserApp); + user.setUserApps(userAppSet); + userList.add(user); + Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)) + .thenReturn(userList); + CentralUser actual = externalAccessRolesServiceImpl.getUserRoles(user.getOrgUserId(), app.getUebKey()); + assertNotNull(actual); + } + + @SuppressWarnings("unchecked") + @Test + public void getActiveRolesTest() throws Exception { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(EPUserUtils.class); + PowerMockito.mockStatic(Restrictions.class); + PowerMockito.mockStatic(Criterion.class); + EPApp app = mockApp(); + app.setId(1l); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<EPRole> epRoles = new ArrayList<>(); + EPRole getEPRole = new EPRole(); + getEPRole.setName("Test"); + getEPRole.setId(2l); + getEPRole.setActive(true); + EPRole getEPRole2 = new EPRole(); + getEPRole2.setName("Test2"); + getEPRole2.setId(3l); + getEPRole2.setActive(true); + epRoles.add(getEPRole); + epRoles.add(getEPRole2); + List<Criterion> restrictionsList = new ArrayList<Criterion>(); + Criterion active_ynCrt = Restrictions.eq("active", Boolean.TRUE); + Criterion appIdCrt = Restrictions.isNull("appId"); + Criterion andCrit = Restrictions.and(active_ynCrt, appIdCrt); + restrictionsList.add(andCrit); + Mockito.when((List<EPRole>) dataAccessService.getList(EPRole.class, null, restrictionsList, null)) + .thenReturn(epRoles); + final Map<String, Long> params = new HashMap<>(); + params.put("roleId", getEPRole.getId()); + params.put(APP_ID, app.getId()); + List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>(); + CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|test_instance|*", + "test_name"); + CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction("test_instance2", "test_name2"); + cenRoleFuncList.add(centralV2RoleFunction); + cenRoleFuncList.add(centralV2RoleFunction2); + Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null)) + .thenReturn(cenRoleFuncList); + final Map<String, Long> params2 = new HashMap<>(); + params2.put("roleId", getEPRole2.getId()); + params2.put(APP_ID, app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params2, null)) + .thenReturn(cenRoleFuncList); + List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>(); + GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction(); + mockGlobalRole.setActive(true); + mockGlobalRole.setAppId(app.getId()); + mockGlobalRole.setRoleId(1111l); + mockGlobalRole.setRoleName("global_test"); + mockGlobalRole.setFunctionCd("test_type|test_code|*"); + mockGlobalRole.setFunctionName("test name"); + mockGlobalRoles.add(mockGlobalRole); + Map<String, Long> params3 = new HashMap<>(); + params3.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleWithApplicationRoleFunctions", params3, null)) + .thenReturn(mockGlobalRoles); + Mockito.when(EcompPortalUtils.getFunctionCode(centralV2RoleFunction.getCode())).thenReturn("test_instance"); + Mockito.when(EcompPortalUtils.getFunctionCode(centralV2RoleFunction2.getCode())).thenReturn("test_instance2"); + List<CentralV2Role> actual = externalAccessRolesServiceImpl.getActiveRoles(app.getUebKey()); + assertEquals(actual.size(), 3); + } + + @Test + public void bulkUploadRolesFunctionsTest() throws Exception { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPApp app = mockApp(); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<EPRole> applicationRoles = new ArrayList<>(); + EPRole getEPRole = new EPRole(); + getEPRole.setName("Test"); + getEPRole.setId(2l); + getEPRole.setActive(true); + EPRole getEPRole2 = new EPRole(); + getEPRole2.setName("Test2"); + getEPRole2.setId(3l); + getEPRole2.setActive(true); + applicationRoles.add(getEPRole); + applicationRoles.add(getEPRole2); + final Map<String, Long> appParams = new HashMap<>(); + appParams.put("appId", app.getId()); + Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)) + .thenReturn(applicationRoles); + final Map<String, Long> params = new HashMap<>(); + params.put("roleId", getEPRole.getId()); + List<BulkUploadRoleFunction> appRoleFunc = new ArrayList<>(); + BulkUploadRoleFunction bulkUploadRoleFunction = new BulkUploadRoleFunction(); + bulkUploadRoleFunction.setFunctionCd("testcode"); + bulkUploadRoleFunction.setFunctionName("test_name"); + BulkUploadRoleFunction bulkUploadRoleFunction2 = new BulkUploadRoleFunction(); + bulkUploadRoleFunction2.setFunctionCd("menu_testcode2"); + bulkUploadRoleFunction2.setFunctionName("test_name2"); + appRoleFunc.add(bulkUploadRoleFunction); + appRoleFunc.add(bulkUploadRoleFunction2); + Mockito.when(dataAccessService.executeNamedQuery("uploadAllRoleFunctions", params, null)) + .thenReturn(appRoleFunc); + final Map<String, Long> params2 = new HashMap<>(); + params2.put("roleId", getEPRole2.getId()); + List<BulkUploadRoleFunction> appRoleFunc2 = new ArrayList<>(); + appRoleFunc2.add(bulkUploadRoleFunction); + appRoleFunc2.add(bulkUploadRoleFunction2); + Mockito.when(dataAccessService.executeNamedQuery("uploadAllRoleFunctions", params2, null)) + .thenReturn(appRoleFunc2); + ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.CREATED); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + Integer actual = externalAccessRolesServiceImpl.bulkUploadRolesFunctions(app.getUebKey()); + Integer expected = 4; + assertEquals(actual, expected); + } + + @Test + public void bulkUploadPartnerRoleFunctionsTest() throws Exception { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(SystemProperties.class); + EPApp app = mockApp(); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + List<Role> roles = new ArrayList<>(); + Role role = new Role(); + role.setName("Test"); + role.setId(2l); + role.setActive(true); + Role role2 = new Role(); + role2.setName("Test2"); + role2.setId(3l); + role2.setActive(true); + roles.add(role); + roles.add(role2); + SortedSet<RoleFunction> roleFuncSet = new TreeSet<>(); + RoleFunction roleFunc = new RoleFunction(); + roleFunc.setName("Test Name"); + roleFunc.setCode("testcode"); + RoleFunction roleFunc2 = new RoleFunction(); + roleFunc2.setName("Test Name3"); + roleFunc2.setCode("menu_testcode2"); + roleFuncSet.add(roleFunc); + roleFuncSet.add(roleFunc2); + role.setRoleFunctions(roleFuncSet); + role2.setRoleFunctions(roleFuncSet); + ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse); + externalAccessRolesServiceImpl.bulkUploadPartnerRoleFunctions(app.getUebKey(), roles); + } + + @Test + public void getMenuFunctionsListTest() throws Exception { + EPApp app = mockApp(); + List<EPApp> appList = new ArrayList<>(); + final Map<String, String> appUebkeyParams = new HashMap<>(); + appList.add(app); + appUebkeyParams.put("appKey", app.getUebKey()); + Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null)) + .thenReturn(appList); + final Map<String, Long> appParams = new HashMap<>(); + appParams.put(APP_ID, app.getId()); + List<String> expected = new ArrayList<>(); + expected.add("test_menu1"); + expected.add("test_menu2"); + Mockito.when(dataAccessService.executeNamedQuery("getMenuFunctions", appParams, null)).thenReturn(expected); + List<String> actual = externalAccessRolesServiceImpl.getMenuFunctionsList(app.getUebKey()); + assertEquals(expected, actual); + } + + @Test + public void getCentralizedAppsOfUserTest() { + EPUser user = mockUser.mockEPUser(); + Map<String, String> params = new HashMap<>(); + params.put("userId", user.getOrgUserId()); + List<CentralizedApp> expected = new ArrayList<>(); + CentralizedApp centralizedApp = new CentralizedApp(); + centralizedApp.setAppId(2); + centralizedApp.setAppName("testapp1"); + expected.add(centralizedApp); + Mockito.when(dataAccessService.executeNamedQuery("getCentralizedAppsOfUser", params, null)) + .thenReturn(expected); + List<CentralizedApp> actual = externalAccessRolesServiceImpl.getCentralizedAppsOfUser(user.getOrgUserId()); + assertEquals(expected, actual); + } + + @Test + public void ConvertCentralRoleToRoleTest() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + Role role = new Role(); + role.setName("Test"); + role.setId(3l); + role.setActive(true); + SortedSet<RoleFunction> roleFuncSet = new TreeSet<>(); + RoleFunction roleFunc = new RoleFunction(); + roleFunc.setName("Test Name"); + roleFunc.setCode("testcode"); + RoleFunction roleFunc2 = new RoleFunction(); + roleFunc2.setName("Test Name3"); + roleFunc2.setCode("menu_testcode2"); + roleFuncSet.add(roleFunc); + roleFuncSet.add(roleFunc2); + role.setRoleFunctions(roleFuncSet); + String roleInfo = mapper.writeValueAsString(role); + Role actual = externalAccessRolesServiceImpl.ConvertCentralRoleToRole(roleInfo); + assertNotNull(actual); + } + + @Test + public void convertV2CentralRoleListToOldVerisonCentralRoleListTest() { + List<CentralV2Role> v2CenRoleList = new ArrayList<>(); + CentralV2Role cenV2Role = new CentralV2Role(2l, "test1"); + CentralV2RoleFunction CentralV2Role = new CentralV2RoleFunction("testcode", "test_name"); + SortedSet<CentralV2RoleFunction> setV2RoleFuncs = new TreeSet<>(); + setV2RoleFuncs.add(CentralV2Role); + cenV2Role.setRoleFunctions(setV2RoleFuncs); + v2CenRoleList.add(cenV2Role); + List<CentralRole> actual = externalAccessRolesServiceImpl.convertV2CentralRoleListToOldVerisonCentralRoleList(v2CenRoleList); + assertEquals(1, actual.size()); + } } diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/PortalAdminServiceImplTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/PortalAdminServiceImplTest.java new file mode 100644 index 00000000..ffad364c --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/PortalAdminServiceImplTest.java @@ -0,0 +1,300 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under 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.assertNull; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.core.MockEPUser; +import org.onap.portalapp.portal.domain.EPApp; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.transport.FieldsValidator; +import org.onap.portalapp.portal.transport.PortalAdmin; +import org.onap.portalapp.portal.utils.EPCommonSystemProperties; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.portal.utils.PortalConstants; +import org.onap.portalsdk.core.service.DataAccessService; +import org.onap.portalsdk.core.service.DataAccessServiceImpl; +import org.onap.portalsdk.core.util.SystemProperties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ EcompPortalUtils.class, Criterion.class, Restrictions.class, PortalConstants.class, + SystemProperties.class, EPCommonSystemProperties.class }) +public class PortalAdminServiceImplTest { + + @Mock + DataAccessService dataAccessService = new DataAccessServiceImpl(); + + @Mock + EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl(); + + @Mock + SearchServiceImpl searchServiceImpl = new SearchServiceImpl(); + + @Mock + SessionFactory sessionFactory; + + @Mock + Session session; + + @Mock + Transaction transaction; + + @Mock + RestTemplate template = new RestTemplate(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + Mockito.when(sessionFactory.openSession()).thenReturn(session); + Mockito.when(session.beginTransaction()).thenReturn(transaction); + } + + @After + public void after() { + session.close(); + } + + @InjectMocks + PortalAdminServiceImpl portalAdminServiceImpl = new PortalAdminServiceImpl(); + + public EPApp mockApp() { + EPApp app = new EPApp(); + app.setName("Test"); + app.setImageUrl("test"); + app.setNameSpace("com.test.app"); + app.setCentralAuth(true); + app.setDescription("test"); + app.setNotes("test"); + app.setUrl("test"); + app.setId((long) 1); + app.setAppRestEndpoint("test"); + app.setAlternateUrl("test"); + app.setName("test"); + app.setMlAppName("test"); + app.setMlAppAdminId("test"); + app.setUsername("test"); + app.setAppPassword("test"); + app.setOpen(false); + app.setEnabled(true); + app.setUebKey("test"); + app.setUebSecret("test"); + app.setUebTopicName("test"); + app.setAppType(1); + return app; + } + + MockEPUser mockUser = new MockEPUser(); + + @SuppressWarnings("unchecked") + @Test + public void getPortalAdminsTest() { + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(EPCommonSystemProperties.class); + List<PortalAdmin> portalAdmins = new ArrayList<>(); + PortalAdmin portalAdmin = new PortalAdmin(); + portalAdmin.setFirstName("guest"); + portalAdmin.setLastName("test"); + portalAdmin.setLoginId("test"); + portalAdmin.setUserId(1l); + portalAdmins.add(portalAdmin); + Map<String, String> params = new HashMap<>(); + params.put("adminRoleId", "1"); + Mockito.when((List<PortalAdmin>) dataAccessService.executeNamedQuery("getPortalAdmins", params, null)) + .thenReturn(portalAdmins); + List<PortalAdmin> actual = portalAdminServiceImpl.getPortalAdmins(); + assertEquals(1, actual.size()); + } + + @Test + public void getPortalAdminsExceptionTest() { + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(EPCommonSystemProperties.class); + List<PortalAdmin> portalAdmins = new ArrayList<>(); + PortalAdmin portalAdmin = new PortalAdmin(); + portalAdmin.setFirstName("guest"); + portalAdmin.setLastName("test"); + portalAdmin.setLoginId("test"); + portalAdmin.setUserId(1l); + portalAdmins.add(portalAdmin); + Map<String, String> params = new HashMap<>(); + params.put("adminRoleId", "1"); + Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeNamedQuery("getPortalAdmins", params, + null); + List<PortalAdmin> actual = portalAdminServiceImpl.getPortalAdmins(); + assertNull(actual); + } + + @SuppressWarnings("unchecked") + @Test + public void createPortalAdminNewUserTest() { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(PortalConstants.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(Restrictions.class); + PowerMockito.mockStatic(Criterion.class); + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + List<EPUser> users = new ArrayList<>(); + List<Criterion> restrictionsList = new ArrayList<Criterion>(); + Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getOrgUserId()); + restrictionsList.add(orgUserIdCriterion); + Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)) + .thenReturn(users); + List<PortalAdmin> portalAdmins = new ArrayList<>(); + Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any() , Matchers.anyMap())) + .thenReturn(portalAdmins); + Mockito.when(searchServiceImpl.searchUserByUserId(user.getOrgUserId())).thenReturn(user); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when( + EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn(true); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); + Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app); + ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse); + FieldsValidator actual = portalAdminServiceImpl.createPortalAdmin(user.getOrgUserId()); + FieldsValidator expected = new FieldsValidator(); + expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK)); + assertEquals(expected, actual); + } + + @SuppressWarnings("unchecked") + @Test + public void createPortalAdminExistingUserTest() { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(PortalConstants.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(Restrictions.class); + PowerMockito.mockStatic(Criterion.class); + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + List<EPUser> users = new ArrayList<>(); + users.add(user); + List<Criterion> restrictionsList = new ArrayList<Criterion>(); + Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getOrgUserId()); + restrictionsList.add(orgUserIdCriterion); + Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)) + .thenReturn(users); + List<PortalAdmin> portalAdmins = new ArrayList<>(); + Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any() , Matchers.anyMap())) + .thenReturn(portalAdmins); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when( + EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn(true); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); + Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app); + ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse); + FieldsValidator actual = portalAdminServiceImpl.createPortalAdmin(user.getOrgUserId()); + FieldsValidator expected = new FieldsValidator(); + expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK)); + assertEquals(expected, actual); + } + + @SuppressWarnings("unchecked") + @Test + public void deletePortalAdminTest() { + PowerMockito.mockStatic(EPCommonSystemProperties.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + PowerMockito.mockStatic(PortalConstants.class); + PowerMockito.mockStatic(SystemProperties.class); + PowerMockito.mockStatic(Restrictions.class); + PowerMockito.mockStatic(Criterion.class); + EPUser user = mockUser.mockEPUser(); + EPApp app = mockApp(); + List<EPUser> users = new ArrayList<>(); + users.add(user); + List<Criterion> restrictionsList = new ArrayList<Criterion>(); + Criterion orgUserIdCriterion = Restrictions.eq("id", user.getId()); + restrictionsList.add(orgUserIdCriterion); + Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)) + .thenReturn(users); + Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true); + Mockito.when( + EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn(true); + Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) + .thenReturn("@test.com"); + Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app); + ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE), + Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse); + FieldsValidator actual = portalAdminServiceImpl.deletePortalAdmin(user.getId()); + FieldsValidator expected = new FieldsValidator(); + expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK)); + assertEquals(expected, actual); + } + +} diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/UserServiceImplTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/UserServiceImplTest.java new file mode 100644 index 00000000..b8bb1652 --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/UserServiceImplTest.java @@ -0,0 +1,178 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under 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 java.io.ByteArrayInputStream; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; +import org.json.simple.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.core.MockEPUser; +import org.onap.portalapp.portal.domain.EPApp; +import org.onap.portalapp.portal.domain.EPUser; +import org.onap.portalapp.portal.utils.EPCommonSystemProperties; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalapp.portal.utils.PortalConstants; +import org.onap.portalsdk.core.onboarding.util.CipherUtil; +import org.onap.portalsdk.core.service.DataAccessService; +import org.onap.portalsdk.core.service.DataAccessServiceImpl; +import org.onap.portalsdk.core.util.SystemProperties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, PortalConstants.class, + EPCommonSystemProperties.class, Criterion.class, CipherUtil.class, Restrictions.class }) +public class UserServiceImplTest { + + @Mock + DataAccessService dataAccessService = new DataAccessServiceImpl(); + + @InjectMocks + UserServiceImpl userServiceImpl= new UserServiceImpl(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + public EPApp mockApp() { + EPApp app = new EPApp(); + app.setName("Test"); + app.setImageUrl("test"); + app.setNameSpace("com.test.app"); + app.setCentralAuth(true); + app.setDescription("test"); + app.setNotes("test"); + app.setUrl("test"); + app.setId((long) 10); + app.setAppRestEndpoint("test"); + app.setAlternateUrl("test"); + app.setName("test"); + app.setMlAppName("test"); + app.setMlAppAdminId("test"); + app.setUsername("test"); + app.setAppPassword("test"); + app.setOpen(false); + app.setEnabled(true); + app.setUebKey("test"); + app.setUebSecret("test"); + app.setUebTopicName("test"); + app.setAppType(1); + return app; + } + + MockEPUser mockUser = new MockEPUser(); + + @SuppressWarnings("unchecked") + @Test + public void getUserByUserIdExceptionTest() throws Exception { + PowerMockito.mockStatic(SystemProperties.class); + EPUser user = mockUser.mockEPUser(); + Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC"); + Mockito.when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER)).thenReturn("http://www.test.com"); + HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); + JSONObject response = new JSONObject(); + JSONObject userJson = new JSONObject(); + userJson.put("id", 1); + userJson.put("givenName", "Guest"); + userJson.put("familyName", "Test"); + userJson.put("email", "test@123.com"); + List<JSONObject> userListJson = new ArrayList<>(); + userListJson.add(userJson); + response.put("response", userListJson); + ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8")); + PowerMockito.when(connection.getInputStream()).thenReturn(getBody); + userServiceImpl.getUserByUserId(user.getOrgUserId()); + } + + @SuppressWarnings("unchecked") + @Test + public void saveNewUserTest() throws Exception { + PowerMockito.mockStatic(Restrictions.class); + PowerMockito.mockStatic(Criterion.class); + PowerMockito.mockStatic(CipherUtil.class); + EPUser user = mockUser.mockEPUser(); + List<EPUser> users = new ArrayList<>(); + Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz"); + List<Criterion> restrictionsList = new ArrayList<Criterion>(); + Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId()); + restrictionsList.add(orgUserIdCriterion); + Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(users); + String actual = userServiceImpl.saveNewUser(user, "No"); + assertEquals("success", actual); + } + + @SuppressWarnings("unchecked") + @Test + public void saveExistingUserTest() throws Exception { + PowerMockito.mockStatic(Restrictions.class); + PowerMockito.mockStatic(Criterion.class); + PowerMockito.mockStatic(CipherUtil.class); + EPUser user = mockUser.mockEPUser(); + user.setLoginPwd("xyz"); + List<EPUser> users = new ArrayList<>(); + users.add(user); + EPUser oldUser = mockUser.mockEPUser(); + oldUser.setLoginPwd("abc"); + List<EPUser> oldUsers = new ArrayList<>(); + oldUsers.add(oldUser); + Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz"); + List<Criterion> restrictionsList = new ArrayList<Criterion>(); + Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId()); + restrictionsList.add(orgUserIdCriterion); + Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(oldUsers); + String actual = userServiceImpl.saveNewUser(user, "No"); + assertEquals("success", actual); + } + + +} |