From 2f68e0c1cdc8a772e3f56dedacc9095c8e21eacc Mon Sep 17 00:00:00 2001 From: rajitha Date: Wed, 7 Jun 2023 15:05:17 +0530 Subject: [PATCH] Fix test failure --- .../impl/policy/AbstractPolicyDAOImpl.java | 3 +- .../mgt/core/enforcement/DelegationTask.java | 4 ++- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 32 +++++++++++++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java index b681ce013a..cda5dcf3d5 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/dao/impl/policy/AbstractPolicyDAOImpl.java @@ -1006,7 +1006,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - Policy policy = new Policy(); + Policy policy = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); @@ -1017,6 +1017,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO { resultSet = stmt.executeQuery(); while (resultSet.next()) { + policy = new Policy(); policy.setId(policyId); policy.setPolicyName(resultSet.getString("NAME")); policy.setTenantId(resultSet.getInt("TENANT_ID")); diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/enforcement/DelegationTask.java index c1bdd4fba7..60157906ff 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/enforcement/DelegationTask.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.policy.mgt.core.enforcement; +import io.entgra.device.mgt.core.policy.mgt.core.cache.guava.impl.GuavaPolicyCacheManagerImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.Device; @@ -50,7 +51,8 @@ public class DelegationTask extends DynamicPartitionedScheduleTask { UpdatedPolicyDeviceListBean updatedPolicyDeviceList = policyManager.applyChangesMadeToPolicies(); List deviceTypes = updatedPolicyDeviceList.getChangedDeviceTypes(); if (policyConfiguration.getCacheEnable()) { - PolicyCacheManagerImpl.getInstance().rePopulateCache(); + //PolicyCacheManagerImpl.getInstance().rePopulateCache(); + GuavaPolicyCacheManagerImpl.getInstance().rePopulateCache(); } if (log.isDebugEnabled()) { log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size()); diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 6bec219069..7f04ee5f94 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.policy.mgt.core.mgt.impl; import io.entgra.device.mgt.core.device.mgt.common.PolicyPaginationRequest; +import io.entgra.device.mgt.core.policy.mgt.core.cache.guava.impl.GuavaPolicyCacheManagerImpl; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -511,7 +512,8 @@ public class PolicyManagerImpl implements PolicyManager { try { List existingPolicies; if (policyConfiguration.getCacheEnable()) { - existingPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + //existingPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + existingPolicies = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies(); } else { existingPolicies = this.getPolicies(); } @@ -795,11 +797,22 @@ public class PolicyManagerImpl implements PolicyManager { try { PolicyManagementDAOFactory.openConnection(); policy = policyDAO.getPolicy(policyId); + + if(policy == null) { + return null; + } + roleNames = policyDAO.getPolicyAppliedRoles(policyId); userNames = policyDAO.getPolicyAppliedUsers(policyId); policy.setRoles(roleNames); policy.setUsers(userNames); + policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policyId)); + List deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policyId); + if(!deviceGroupWrappers.isEmpty()) { + deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); + } + policy.setDeviceGroups(deviceGroupWrappers); if (log.isDebugEnabled()) { log.debug("Retrieving corrective actions of policy " + policy.getPolicyName() + " having policy id " + policy.getId()); @@ -810,6 +823,8 @@ public class PolicyManagerImpl implements PolicyManager { policyId + ")", e); } catch (SQLException e) { throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } catch (GroupManagementException e) { + throw new PolicyManagementException("Error occurred while getting device groups", e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -915,7 +930,8 @@ public class PolicyManagerImpl implements PolicyManager { List tempPolicyList; if (policyConfiguration.getCacheEnable()) { - tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + //tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + tempPolicyList = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies(); } else { tempPolicyList = this.getPolicies(); } @@ -937,7 +953,8 @@ public class PolicyManagerImpl implements PolicyManager { List policies = new ArrayList<>(); List allPolicies; if (policyConfiguration.getCacheEnable()) { - allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + //allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + allPolicies = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies(); } else { allPolicies = this.getPolicies(); } @@ -968,7 +985,8 @@ public class PolicyManagerImpl implements PolicyManager { List tempPolicyList; if (policyConfiguration.getCacheEnable()) { - tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + //tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + tempPolicyList = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies(); } else { tempPolicyList = this.getPolicies(); } @@ -1000,7 +1018,8 @@ public class PolicyManagerImpl implements PolicyManager { } List tempPolicyList; if (policyConfiguration.getCacheEnable()) { - tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + //tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + tempPolicyList = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies(); } else { tempPolicyList = this.getPolicies(); } @@ -1098,7 +1117,8 @@ public class PolicyManagerImpl implements PolicyManager { try { List allPolicies; if (policyConfiguration.getCacheEnable()) { - allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + //allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + allPolicies = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies(); } else { allPolicies = this.getPolicies(); }