JUnits for coverage
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / UserRolesCommonServiceImpl.java
index 80411f9..e3ce07f 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START==========================================
  * ONAP Portal
  * ===================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
@@ -90,6 +90,7 @@ import org.onap.portalapp.portal.transport.FieldsValidator;
 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
 import org.onap.portalapp.portal.transport.FunctionalMenuRole;
 import org.onap.portalapp.portal.transport.RemoteRole;
+import org.onap.portalapp.portal.transport.RemoteRoleV1;
 import org.onap.portalapp.portal.transport.RemoteUserWithRoles;
 import org.onap.portalapp.portal.transport.RoleInAppForUser;
 import org.onap.portalapp.portal.transport.RolesInAppForUser;
@@ -97,6 +98,7 @@ import org.onap.portalapp.portal.transport.UserApplicationRoles;
 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.SystemType;
 import org.onap.portalsdk.core.domain.Role;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.onap.portalsdk.core.restful.domain.EcompRole;
@@ -140,6 +142,9 @@ public class UserRolesCommonServiceImpl  {
        @Autowired
        private ExternalAccessRolesService externalAccessRolesService;
        
+       @Autowired
+       private AppsCacheService appsCacheService;
+       
        RestTemplate template = new RestTemplate();
        
        /**
@@ -477,6 +482,7 @@ public class UserRolesCommonServiceImpl  {
                                                        oldAppRole.setName(role.getName());
                                                        localSession.update(oldAppRole);
                                                }
+                                               oldAppRole.setActive(true);
                                                newRolesMap.remove(oldAppRole.getAppRoleId());
                                        } else {
                                                obsoleteRoles.add(oldAppRole);
@@ -646,6 +652,24 @@ public class UserRolesCommonServiceImpl  {
                Set<EcompRole> updatedUserRolesinRemote = constructUsersRemoteAppRoles(roleInAppForUserList);
                Set<EcompRole> updateUserRolesInEcomp = constructUsersEcompRoles(roleInAppForUserList);
                String userRolesAsString = mapper.writeValueAsString(updatedUserRolesinRemote);
+        EPApp externalApp = null;
+        SystemType type = SystemType.APPLICATION;
+               externalApp = appsCacheService.getApp(appId);
+               String appBaseUri = null;
+               Set<RemoteRoleV1> updatedUserRolesinRemoteV1 = new TreeSet<>();
+               if (externalApp != null) {
+                        appBaseUri = (type == SystemType.APPLICATION) ? externalApp.getAppRestEndpoint() : "";
+               }
+               if(appBaseUri != null && appBaseUri.endsWith("/api")){
+                       for(EcompRole eprole :updatedUserRolesinRemote)
+                       {
+                               RemoteRoleV1 role = new RemoteRoleV1();
+                               role.setId(eprole.getId());
+                               role.setName(eprole.getName());
+                               updatedUserRolesinRemoteV1.add(role);
+                       }
+                       userRolesAsString = mapper.writeValueAsString(updatedUserRolesinRemoteV1);
+               }
                applicationsRestClientService.post(EcompRole.class, appId, userRolesAsString,
                                String.format("/user/%s/roles", userId));
                // TODO: We should add code that verifies that the post operation did
@@ -794,16 +818,7 @@ public class UserRolesCommonServiceImpl  {
        private EPUser addRemoteUser(List<RoleInAppForUser> roleInAppForUserList, String userId, EPApp app, ObjectMapper mapper, SearchService searchService, ApplicationsRestClientService applicationsRestClientService) throws Exception{
                EPUser addRemoteUser = null;
                if (remoteUserShouldBeCreated(roleInAppForUserList)) {
-                       
                        createNewUserOnRemoteApp(userId, app, applicationsRestClientService, searchService, mapper, isAppUpgradeVersion(app));
-                       // If we succeed, we know that the new user was
-                       // persisted on remote app.
-                       addRemoteUser = getUserFromApp(userId, app, applicationsRestClientService);
-                       if (addRemoteUser == null) {
-                               logger.error(EELFLoggerDelegate.errorLogger,
-                                               "Failed to persist new user: " + userId + " in remote app. appId = " + app.getId());
-                               // return null;
-                       }
                }
                return addRemoteUser;
        }
@@ -867,14 +882,12 @@ public class UserRolesCommonServiceImpl  {
 
                                // if centralized app
                                if (app.getCentralAuth()) {
-                                       // We should add If user does not exist in remote application
                                        if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
-                                               EPUser remoteAppUser = null;
-                                               remoteAppUser = checkIfRemoteUserExits(userId, app, applicationsRestClientService);
-
-                                               if (remoteAppUser == null) {
+                                               try {
                                                        addRemoteUser(roleInAppForUserList, userId, app, mapper, searchService,
                                                                        applicationsRestClientService);
+                                               } catch (Exception e) {
+                                                       logger.debug(EELFLoggerDelegate.debugLogger, e.getMessage());
                                                }
                                        }
                                        
@@ -962,18 +975,7 @@ public class UserRolesCommonServiceImpl  {
                        ObjectMapper mapper = new ObjectMapper();
                        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
                        HttpEntity<String> getUserRolesEntity = new HttpEntity<>(headers);
-                       logger.debug(EELFLoggerDelegate.debugLogger, "Connecting to external system to get current user roles");
-                       ResponseEntity<String> getResponse = template
-                                       .exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
-                                                       + "roles/user/" + name, HttpMethod.GET, getUserRolesEntity, String.class);
-                       if (getResponse.getStatusCode().value() == 200) {
-                               logger.debug(EELFLoggerDelegate.debugLogger, "updateUserRolesInExternalSystem: Finished GET user roles from external system and received user roles {}",
-                                               getResponse.getBody());
-
-                       }else{
-                               logger.error(EELFLoggerDelegate.errorLogger, "updateUserRolesInExternalSystem: Failed GET user roles from external system and received user roles {}",getResponse.getBody() );
-                               EPLogUtil.logExternalAuthAccessAlarm(logger, getResponse.getStatusCode());
-                       }
+                       ResponseEntity<String> getResponse = externalAccessRolesService.getUserRolesFromExtAuthSystem(name, getUserRolesEntity);
                        List<ExternalAccessUserRoleDetail> userRoleDetailList = new ArrayList<>();
                        String res = getResponse.getBody();
                        JSONObject jsonObj = null;
@@ -1581,7 +1583,7 @@ public class UserRolesCommonServiceImpl  {
                                        List<CentralV2Role> cenRoleList = externalAccessRolesService.getRolesForApp(app.getUebKey());
                                        for(CentralV2Role cenRole : cenRoleList){
                                                Role role = new Role();
-                                               role.setActive(cenRole.isActive());
+                                               role.setActive(cenRole.getActive());
                                                role.setId(cenRole.getId());
                                                role.setName(cenRole.getName());
                                                role.setPriority(cenRole.getPriority());
@@ -1832,6 +1834,7 @@ public class UserRolesCommonServiceImpl  {
         * @see org.onap.portalapp.portal.service.UserRolesService#
         * getCachedAppRolesForUser(java.lang.Long, java.lang.Long)
         */
+       @SuppressWarnings("deprecation")
        public List<EPUserApp> getCachedAppRolesForUser(Long appId, Long userId) {
                // Find the records for this user-app combo, if any
                String filter = " where user_id = " + Long.toString(userId) + " and app_id = " + Long.toString(appId);