diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 91e1eff05c7..eab6f6214fb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -154,11 +154,5 @@ public interface PolicyAdministratorPoint { List getProfiles() throws PolicyManagementException; -// Feature addFeature(Feature feature) throws FeatureManagementException; -// -// Feature updateFeature(Feature feature) throws FeatureManagementException; - - boolean deleteFeature(int featureId) throws FeatureManagementException; - int getPolicyCount() throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index e64b8214df7..00d7280a9d4 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -96,15 +96,6 @@ public interface FeatureDAO { */ List getFeaturesForProfile(int profileId) throws FeatureManagerDAOException; - /** - * This method is used remove a feature. - * - * @param featureId id of the removing feature. - * @return returns true if success. - * @throws FeatureManagerDAOException - */ - boolean deleteFeature(int featureId) throws FeatureManagerDAOException; - /** * This method is used to remove set of features of given profile. * @@ -123,6 +114,13 @@ public interface FeatureDAO { */ boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException; + /** + * This method is used to remove a profile feature of given feature id. + * + * @param featureId id of the feature. + * @return returns true if success. + * @throws FeatureManagerDAOException + */ boolean deleteProfileFeatures(int featureId) throws FeatureManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/AbstractFeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/AbstractFeatureDAO.java index 2c3da0e30c5..8d04fdec5b5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/AbstractFeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/AbstractFeatureDAO.java @@ -30,6 +30,7 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; +import javax.naming.OperationNotSupportedException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; @@ -49,13 +50,13 @@ public abstract class AbstractFeatureDAO implements FeatureDAO { @Override public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { - return null; + throw new UnsupportedOperationException(); } @Override public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { - return null; + throw new UnsupportedOperationException(); } @Override @@ -311,30 +312,6 @@ public abstract class AbstractFeatureDAO implements FeatureDAO { return featureList; } - @Override - public boolean deleteFeature(int featureId) throws FeatureManagerDAOException { - Connection conn; - PreparedStatement stmt = null; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - try { - conn = this.getConnection(); - String query = "DELETE FROM DM_FEATURES WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, featureId); - stmt.setInt(2, tenantId); - if(stmt.executeUpdate() > 0) { - return true; - } - return false; - } catch (SQLException e) { - throw new FeatureManagerDAOException("Unable to delete the feature " + featureId + " (Feature ID) " + - "from database.", e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - } - private Connection getConnection() throws FeatureManagerDAOException { return PolicyManagementDAOFactory.getConnection(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 50d58c3bf48..d595a3c4eb3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -338,22 +338,6 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { } } - /* @Override - public Feature addFeature(Feature feature) throws FeatureManagementException { - return featureManager.addFeature(feature); - } - - @Override - public Feature updateFeature(Feature feature) throws FeatureManagementException { - return featureManager.updateFeature(feature); - - }*/ - - @Override - public boolean deleteFeature(int featureId) throws FeatureManagementException { - return featureManager.deleteFeature(featureId); - } - @Override public int getPolicyCount() throws PolicyManagementException { return PolicyCacheManagerImpl.getInstance().getAllPolicies().size(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java index 9c4673a84a3..e0c6f3fdbed 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java @@ -21,36 +21,27 @@ package org.wso2.carbon.policy.mgt.core.mgt; import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import java.util.List; public interface FeatureManager { - /*Feature addFeature(Feature feature) throws FeatureManagementException; - - public List addFeatures(List features) throws FeatureManagementException; - - Feature updateFeature(Feature feature) throws FeatureManagementException;*/ - - boolean deleteFeature(Feature feature) throws FeatureManagementException; - ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException; ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException; - List addProfileFeatures(List features, int profileId) throws FeatureManagementException; - - List updateProfileFeatures(List features, int profileId) throws FeatureManagementException; + List addProfileFeatures(List features, int profileId) + throws FeatureManagementException; + List updateProfileFeatures(List features, int profileId) + throws FeatureManagementException; List getAllFeatures(String deviceType) throws FeatureManagementException; List getFeaturesForProfile(int profileId) throws FeatureManagementException; - boolean deleteFeature(int featureId) throws FeatureManagementException; - boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java index edaceae3f8b..dff12ac54f9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java @@ -21,13 +21,15 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO; import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO; +import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import java.sql.SQLException; @@ -36,130 +38,28 @@ import java.util.List; public class FeatureManagerImpl implements FeatureManager { private FeatureDAO featureDAO; + private ProfileDAO profileDAO; private static Log log = LogFactory.getLog(FeatureManagerImpl.class); public FeatureManagerImpl() { featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); - } - - /*@Override - public Feature addFeature(Feature feature) throws FeatureManagementException { - try { - PolicyManagementDAOFactory.beginTransaction(); - feature = featureDAO.addFeature(feature); - PolicyManagementDAOFactory.commitTransaction(); - - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while adding feature (" + feature.getName() + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while adding feature (" + feature.getName() + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } - return feature; - }*/ - - /*@Override - public List addFeatures(List features) throws FeatureManagementException { - try { - PolicyManagementDAOFactory.beginTransaction(); - features = featureDAO.addFeatures(features); - PolicyManagementDAOFactory.commitTransaction(); - - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while adding feature (" + features.size()+ ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while adding feature (" + features.size() + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } - - return features; - }*/ - - /* @Override - public Feature updateFeature(Feature feature) throws FeatureManagementException { - try { - PolicyManagementDAOFactory.beginTransaction(); - feature = featureDAO.updateFeature(feature); - PolicyManagementDAOFactory.commitTransaction(); - - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while updating feature (" + feature.getName() + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while updating feature (" + feature.getName() + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } - return feature; - }*/ - - @Override - public boolean deleteFeature(Feature feature) throws FeatureManagementException { - boolean bool; - try { - PolicyManagementDAOFactory.beginTransaction(); - bool = featureDAO.deleteFeature(feature.getId()); - PolicyManagementDAOFactory.commitTransaction(); - } catch (FeatureManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new FeatureManagementException("Error occurred while deleting the feature (" + feature.getName() + - ")", e); - } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new FeatureManagementException("Error occurred while deleting the feature (" + feature.getName() + - ") from database", e); - } finally { - PolicyManagementDAOFactory.closeConnection(); - } - return bool; + profileDAO = PolicyManagementDAOFactory.getProfileDAO(); } @Override public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); + Profile profile = profileDAO.getProfile(profileId); + if (profile == null) { + throw new FeatureManagementException( + "Could not find a profile with the profile id: " + profileId); + } feature = featureDAO.addProfileFeature(feature, profileId); PolicyManagementDAOFactory.commitTransaction(); - } catch (PolicyManagerDAOException | FeatureManagerDAOException e) { + } catch (ProfileManagerDAOException | PolicyManagerDAOException | FeatureManagerDAOException e) { throw new FeatureManagementException("Error occurred while adding profile feature (" + - feature.getFeatureCode() + " - " + profileId + ")", e); + feature.getFeatureCode() + " - " + profileId + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -168,16 +68,22 @@ public class FeatureManagerImpl implements FeatureManager { @Override public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws - FeatureManagementException { + FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); + Profile profile = profileDAO.getProfile(profileId); + if (profile == null) { + throw new FeatureManagementException( + "Could not find a profile with the profile id: " + profileId); + } feature = featureDAO.updateProfileFeature(feature, profileId); PolicyManagementDAOFactory.commitTransaction(); - } catch (FeatureManagerDAOException | PolicyManagerDAOException e) { + } catch (ProfileManagerDAOException | FeatureManagerDAOException | PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while updating feature (" + - feature.getFeatureCode() + " - " + profileId + ") in database.", e); + feature.getFeatureCode() + " - " + profileId + + ") in database.", e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -186,19 +92,23 @@ public class FeatureManagerImpl implements FeatureManager { @Override public List addProfileFeatures(List features, int profileId) throws - FeatureManagementException { + FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); + Profile profile = profileDAO.getProfile(profileId); + if (profile == null) { + throw new FeatureManagementException( + "Could not find a profile with the profile id: " + profileId); + } features = featureDAO.addProfileFeatures(features, profileId); PolicyManagementDAOFactory.commitTransaction(); - } catch (FeatureManagerDAOException e) { + } catch (ProfileManagerDAOException | FeatureManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while adding the features to profile id (" + - profileId + ")", e); + profileId + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while adding the features to profile id (" + - profileId + ") to the database", e); + profileId + ") to the database", e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -207,25 +117,28 @@ public class FeatureManagerImpl implements FeatureManager { @Override public List updateProfileFeatures(List features, int profileId) throws - FeatureManagementException { + FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); + Profile profile = profileDAO.getProfile(profileId); + if (profile == null) { + throw new FeatureManagementException( + "Could not find a profile with the profile id: " + profileId); + } features = featureDAO.updateProfileFeatures(features, profileId); PolicyManagementDAOFactory.commitTransaction(); - } catch (FeatureManagerDAOException e) { + } catch (ProfileManagerDAOException | FeatureManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while updating the features to profile id (" + - profileId + ")", e); + profileId + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while updating the features to profile id (" + - profileId + ") to the database", e); + profileId + ") to the database", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return features; } - @Override public List getAllFeatures(String deviceType) throws FeatureManagementException { @@ -245,8 +158,13 @@ public class FeatureManagerImpl implements FeatureManager { public List getFeaturesForProfile(int profileId) throws FeatureManagementException { try { PolicyManagementDAOFactory.openConnection(); + Profile profile = profileDAO.getProfile(profileId); + if (profile == null) { + throw new FeatureManagementException( + "Could not find a profile with the profile id: " + profileId); + } return featureDAO.getFeaturesForProfile(profileId); - } catch (FeatureManagerDAOException e) { + } catch (ProfileManagerDAOException | FeatureManagerDAOException e) { throw new FeatureManagementException("Error occurred while getting the features", e); } catch (SQLException e) { throw new FeatureManagementException("Error occurred while opening a connection to the data source", e); @@ -255,27 +173,6 @@ public class FeatureManagerImpl implements FeatureManager { } } - @Override - public boolean deleteFeature(int featureId) throws FeatureManagementException { - boolean bool; - try { - PolicyManagementDAOFactory.beginTransaction(); - bool = featureDAO.deleteFeature(featureId); - PolicyManagementDAOFactory.commitTransaction(); - } catch (FeatureManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new FeatureManagementException("Error occurred while deleting the feature - id (" + - featureId + ")", e); - } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new FeatureManagementException("Error occurred while deleting the feature - id (" + featureId + - ") from database.", e); - } finally { - PolicyManagementDAOFactory.closeConnection(); - } - return bool; - } - @Override public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException { boolean bool; @@ -286,11 +183,10 @@ public class FeatureManagerImpl implements FeatureManager { } catch (FeatureManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + - profile.getProfileName() + ")", e); + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + - profile.getProfileName() + ") from database", e); + profile.getProfileName() + ") from database", e); } finally { PolicyManagementDAOFactory.closeConnection(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index bf1e00e1652..160d512ee52 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -75,7 +75,6 @@ public class ProfileManagerImpl implements ProfileManager { throw new ProfileManagementException("Error occurred while adding the profile features (" + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new ProfileManagementException("Error occurred while adding the profile (" + profile.getProfileName() + ") to the database", e); } finally { @@ -105,7 +104,6 @@ public class ProfileManagerImpl implements ProfileManager { throw new ProfileManagementException("Error occurred while updating the profile features (" + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new ProfileManagementException("Error occurred while updating the profile (" + profile.getProfileName() + ") to the database", e); } finally { @@ -132,7 +130,6 @@ public class ProfileManagerImpl implements ProfileManager { throw new ProfileManagementException("Error occurred while deleting the features from profile (" + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new ProfileManagementException("Error occurred while deleting the profile (" + profile.getProfileName() + ") from database", e); } finally { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java index ec37dab9aa0..230291206b5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java @@ -22,7 +22,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.PoolProperties; import org.testng.Assert; -import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeSuite; import org.testng.internal.collections.Pair; import org.w3c.dom.Document; @@ -36,7 +35,6 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -48,7 +46,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; -import org.wso2.carbon.ntask.common.TaskException; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig; @@ -100,7 +97,7 @@ public abstract class BasePolicyManagementDAOTest { DeviceConfigurationManager.getInstance().initConfig(); } - protected void initializeServices() throws Exception{ + protected void initializeServices() throws Exception { initDatSource(); initSQLScript(); @@ -155,7 +152,7 @@ public abstract class BasePolicyManagementDAOTest { } PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants - .SUPER_TENANT_DOMAIN_NAME); + .SUPER_TENANT_DOMAIN_NAME); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); } @@ -260,20 +257,21 @@ public abstract class BasePolicyManagementDAOTest { } public interface Command { - void call(Profile profile) throws Exception; + void call(Object paramObj) throws Exception; } - protected void testThrowingException(Profile profile, Command command, String fieldName, Object mockObj, - Class exceptionClass) throws Exception { - Object oldObj = changeFieldValue(profileManager, fieldName, mockObj); + protected void testThrowingException(Object targetObj, Object paramObj, Command command, String fieldName, + Object mockObj, + Class exceptionClass) throws Exception { + Object oldObj = changeFieldValue(targetObj, fieldName, mockObj); try { - command.call(profile); + command.call(paramObj); } catch (Exception e) { if (!(e.getCause().getClass().getName().equals(exceptionClass.getName()))) { throw e; } } finally { - changeFieldValue(profileManager, fieldName, oldObj); + changeFieldValue(targetObj, fieldName, oldObj); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java new file mode 100644 index 00000000000..7a0a756fafd --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java @@ -0,0 +1,490 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file 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. + */ +package org.wso2.carbon.policy.mgt.core.mgt.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.powermock.api.mockito.PowerMockito; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.testng.internal.collections.Pair; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; +import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest; +import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; +import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO; +import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; +import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; +import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService; +import org.wso2.carbon.policy.mgt.core.util.FeatureCreator; +import org.wso2.carbon.policy.mgt.core.util.ProfileCreator; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.List; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { + + private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class); + + private static final String DEVICE4 = "device4"; + private static final String GROUP4 = "group4"; + private static final String POLICY4 = "policy4"; + private static final String DEVICE_TYPE_D = "deviceTypeD"; + + private OperationManager operationManager; + private FeatureManager featureManager; + private Profile profile1; + private List profileFeaturesList1; + + @BeforeClass + public void initialize() throws Exception { + log.info("Initializing feature manager tests"); + super.initializeServices(); + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + deviceMgtService.registerDeviceType(new TypeXDeviceManagementService(DEVICE_TYPE_D)); + operationManager = new OperationManagerImpl(DEVICE_TYPE_D); + featureManager = new FeatureManagerImpl(); + + enrollDevice(DEVICE4, DEVICE_TYPE_D); + createDeviceGroup(GROUP4); + DeviceGroup group4 = groupMgtService.getGroup(GROUP4); + addDeviceToGroup(new DeviceIdentifier(DEVICE4, DEVICE_TYPE_D), GROUP4); + + Profile profile = new Profile(); + profile.setTenantId(tenantId); + profile.setCreatedDate(new Timestamp(System.currentTimeMillis())); + profile.setDeviceType(DEVICE_TYPE_D); + } + + @Test(description = "This test case tests handling UnsupportedOperationException when adding new profile feature", + expectedExceptions = {UnsupportedOperationException.class}) + public void testAddProfileFeature() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + featureManager.addProfileFeature(profileFeature, profile1.getProfileId()); + } + + + @Test(description = "This test case tests adding new profile feature to a non existent profile", + dependsOnMethods = "testAddProfileFeature", + expectedExceptions = {FeatureManagementException.class}) + public void testAddProfileFeatureThrowingFeatureManagementException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + int nonExistentProfileId = 9999; + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + //Adding profile + featureManager.addProfileFeature(profileFeature, nonExistentProfileId); + } + + @Test(description = "This test case tests handling ProfileManagerDAOException when adding new profile feature", + dependsOnMethods = "testAddProfileFeature") + public void testAddProfileFeatureThrowingProfileManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + ProfileDAO profileDAO = mock(ProfileDAO.class); + when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); + + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + testThrowingException(featureManager, + profileFeature, + p -> featureManager.addProfileFeature((ProfileFeature) p, profile1.getProfileId()), + "profileDAO", profileDAO, + ProfileManagerDAOException.class); + } + + @Test(description = "This test case tests handling FeatureManagerDAOException when adding new profile feature", + dependsOnMethods = "testAddProfileFeatureThrowingProfileManagerDAOException") + public void testAddProfileFeatureThrowingFeatureManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + FeatureDAO featureDAO = mock(FeatureDAO.class); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); + + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + testThrowingException(featureManager, + profileFeature, + p -> featureManager.addProfileFeature((ProfileFeature) p, profile1.getProfileId()), + "featureDAO", featureDAO, + FeatureManagerDAOException.class); + } + + @Test(description = "This test case tests handling SQLException when adding new profile feature", + dependsOnMethods = "testAddProfileFeatureThrowingFeatureManagerDAOException", + expectedExceptions = IllegalTransactionStateException.class) + public void testAddProfileThrowingIllegalTransactionStateException() throws Exception { + //Creating profile object + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + + Pair> pair = mockConnection(); + PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); + try { + featureManager.addProfileFeature(profileFeature, profile.getProfileId()); + } finally { + PolicyManagementDAOFactory.init(pair.second().first()); + } + } + + @Test(description = "This test case tests adding new profile features", + dependsOnMethods = "testAddProfileThrowingIllegalTransactionStateException") + public void testAddProfileFeatures() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + //Adding profile + profile1 = profileManager.addProfile(profile); + profileFeaturesList1 = featureManager.addProfileFeatures(profile.getProfileFeaturesList(), + profile1.getProfileId()); + Assert.assertEquals(profileFeaturesList1.size(), profile.getProfileFeaturesList().size()); + } + + @Test(description = "This test case tests adding new profile features to a non existent profile", + dependsOnMethods = "testAddProfileFeatures", + expectedExceptions = {FeatureManagementException.class}) + public void testAddProfileFeaturesThrowingFeatureManagementException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + int nonExistentProfileId = 9999; + //Adding profile + featureManager.addProfileFeatures(profile.getProfileFeaturesList(), nonExistentProfileId); + } + + @Test(description = "This test case tests handling ProfileManagerDAOException when adding new profile feature", + dependsOnMethods = "testAddProfileFeatures") + public void testAddProfileFeaturesThrowingProfileManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + ProfileDAO profileDAO = mock(ProfileDAO.class); + when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); + + List profileFeaturesList = profile.getProfileFeaturesList(); + testThrowingException(featureManager, + profileFeaturesList, + p -> featureManager.addProfileFeatures((List) p, profile1.getProfileId()), + "profileDAO", profileDAO, + ProfileManagerDAOException.class); + } + + @Test(description = "This test case tests handling FeatureManagerDAOException when adding new profile feature", + dependsOnMethods = "testAddProfileFeaturesThrowingProfileManagerDAOException") + public void testAddProfileFeaturesThrowingFeatureManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + FeatureDAO featureDAO = mock(FeatureDAO.class); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); + + List profileFeaturesList = profile.getProfileFeaturesList(); + testThrowingException(featureManager, + profileFeaturesList, + p -> featureManager.addProfileFeatures((List) p, profile1.getProfileId()), + "featureDAO", featureDAO, + FeatureManagerDAOException.class); + } + + @Test(description = "This test case tests handling SQLException when adding new profile feature", + dependsOnMethods = "testAddProfileFeaturesThrowingFeatureManagerDAOException", + expectedExceptions = IllegalTransactionStateException.class) + public void testAddProfileFeaturesThrowingIllegalTransactionStateException() throws Exception { + //Creating profile object + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + List profileFeaturesList = profile.getProfileFeaturesList(); + + Pair> pair = mockConnection(); + PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); + try { + featureManager.addProfileFeatures(profileFeaturesList, profile.getProfileId()); + } finally { + PolicyManagementDAOFactory.init(pair.second().first()); + } + } + + @Test(description = "This test case tests handling UnsupportedOperationException when updating a profile feature", + expectedExceptions = {UnsupportedOperationException.class}) + public void testUpdateProfileFeature() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + featureManager.updateProfileFeature(profileFeature, profile1.getProfileId()); + } + + @Test(description = "This test case tests updating a non existent profile feature", + expectedExceptions = {FeatureManagementException.class}, + dependsOnMethods = "testUpdateProfileFeature") + public void testUpdateProfileFeatureThrowingFeatureManagementException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + int nonExistentProfileId = 9999; + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + featureManager.updateProfileFeature(profileFeature, nonExistentProfileId); + } + + @Test(description = "This test case tests handling ProfileManagerDAOException when adding new profile feature", + dependsOnMethods = "testAddProfileFeature") + public void testUpdateProfileFeatureThrowingProfileManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + ProfileDAO profileDAO = mock(ProfileDAO.class); + when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); + + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + testThrowingException(featureManager, + profileFeature, + p -> featureManager.updateProfileFeature((ProfileFeature) p, profile1.getProfileId()), + "profileDAO", profileDAO, + ProfileManagerDAOException.class); + } + + @Test(description = "This test case tests handling FeatureManagerDAOException when adding new profile feature", + dependsOnMethods = "testUpdateProfileFeatureThrowingProfileManagerDAOException") + public void testUpdateProfileFeatureThrowingFeatureManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + FeatureDAO featureDAO = mock(FeatureDAO.class); + when(featureDAO.updateProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); + + ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); + testThrowingException(featureManager, + profileFeature, + p -> featureManager.updateProfileFeature((ProfileFeature) p, profile1.getProfileId()), + "featureDAO", featureDAO, + FeatureManagerDAOException.class); + } + + @Test(description = "This test case tests updating profile features", + dependsOnMethods = "testAddProfileFeatures") + public void testUpdateProfileFeatures() throws Exception { + String newFeatureCode = "C002"; + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + int createdProfileId = profileManager.addProfile(profile).getProfileId(); + profileFeaturesList1.get(0).setFeatureCode(newFeatureCode); + List updatedProfileFeatures = featureManager.updateProfileFeatures(profileFeaturesList1, + createdProfileId); + Assert.assertEquals(updatedProfileFeatures.get(0).getFeatureCode(), newFeatureCode); + } + + @Test(description = "This test case tests handling FeatureManagementException when updating profile features", + dependsOnMethods = "testUpdateProfileFeatures", + expectedExceptions = {FeatureManagementException.class}) + public void testUpdateProfileFeaturesThrowingFeatureManagementException() throws Exception { + String newFeatureCode = "C002"; + int nonExistentProfileId = 9999; + profileFeaturesList1.get(0).setFeatureCode(newFeatureCode); + List updatedProfileFeatures = featureManager.updateProfileFeatures(profileFeaturesList1, + nonExistentProfileId); + Assert.assertEquals(updatedProfileFeatures.get(0).getFeatureCode(), newFeatureCode); + } + + @Test(description = "This test case tests handling ProfileManagerDAOException when adding new profile feature", + dependsOnMethods = "testUpdateProfileFeaturesThrowingFeatureManagementException") + public void testUpdateProfileFeaturesThrowingProfileManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + ProfileDAO profileDAO = mock(ProfileDAO.class); + when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); + + List profileFeaturesList = profile.getProfileFeaturesList(); + testThrowingException(featureManager, + profileFeaturesList, + p -> featureManager + .updateProfileFeatures((List) p, profile1.getProfileId()), + "profileDAO", profileDAO, + ProfileManagerDAOException.class); + } + + @Test(description = "This test case tests handling FeatureManagerDAOException when adding new profile feature", + dependsOnMethods = "testUpdateProfileFeaturesThrowingProfileManagerDAOException") + public void testUpdateProfileFeaturesThrowingFeatureManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + FeatureDAO featureDAO = mock(FeatureDAO.class); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); + + List profileFeaturesList = profile.getProfileFeaturesList(); + testThrowingException(featureManager, + profileFeaturesList, + p -> featureManager + .updateProfileFeatures((List) p, profile1.getProfileId()), + "featureDAO", featureDAO, + FeatureManagerDAOException.class); + } + + @Test(description = "This test case tests handling SQLException when adding new profile feature", + dependsOnMethods = "testUpdateProfileFeaturesThrowingFeatureManagerDAOException", + expectedExceptions = IllegalTransactionStateException.class) + public void testUpdateProfileFeaturesThrowingIllegalTransactionStateException() throws Exception { + //Creating profile object + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + List profileFeaturesList = profile.getProfileFeaturesList(); + + Pair> pair = mockConnection(); + PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); + try { + featureManager.updateProfileFeatures(profileFeaturesList, profile.getProfileId()); + } finally { + PolicyManagementDAOFactory.init(pair.second().first()); + } + } + + @Test(description = "This test case tests retrieving all features of a device type", + dependsOnMethods = "testUpdateProfileFeaturesThrowingIllegalTransactionStateException", + expectedExceptions = FeatureManagementException.class) + public void testGetAllFeatures() throws Exception { + featureManager.getAllFeatures(DEVICE_TYPE_D); + } + + @Test(description = "This test case tests handling SQLException when all features of a device type", + dependsOnMethods = "testGetAllFeatures", + expectedExceptions = {IllegalTransactionStateException.class, FeatureManagementException.class}) + public void testGetAllFeaturesThrowingIllegalTransactionStateException() throws Exception { + Pair> pair = mockConnection(); + PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); + try { + featureManager.getAllFeatures(DEVICE_TYPE_D); + } finally { + PolicyManagementDAOFactory.init(pair.second().first()); + } + } + + @Test(description = "This test case tests retrieving features of a profile", + dependsOnMethods = "testGetAllFeaturesThrowingIllegalTransactionStateException") + public void testGetFeaturesForProfile() throws Exception { + featureManager.getFeaturesForProfile(profile1.getProfileId()); + } + + @Test(description = "This test case tests retrieving features to a non existent profile", + dependsOnMethods = "testGetFeaturesForProfile", + expectedExceptions = {FeatureManagementException.class}) + public void testGetFeaturesForProfileThrowingFeatureManagementException() throws Exception { + int nonExistentProfileId = 9999; + featureManager.getFeaturesForProfile(nonExistentProfileId); + } + + @Test(description = "This test case tests handling ProfileManagerDAOException when retrieving features of a " + + "profile", + dependsOnMethods = "testGetFeaturesForProfile") + public void testGetFeaturesForProfileThrowingProfileManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + ProfileDAO profileDAO = mock(ProfileDAO.class); + when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); + + testThrowingException(featureManager, + null, + p -> featureManager.getFeaturesForProfile(profile1.getProfileId()), + "profileDAO", profileDAO, + ProfileManagerDAOException.class); + } + + @Test(description = "This test case tests handling FeatureManagerDAOException when retrieving features of a " + + "profile", + dependsOnMethods = "testGetFeaturesForProfileThrowingProfileManagerDAOException") + public void testGetFeaturesForProfileThrowingFeatureManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + FeatureDAO featureDAO = mock(FeatureDAO.class); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); + + testThrowingException(featureManager, + null, + p -> featureManager.getFeaturesForProfile(profile1.getProfileId()), + "featureDAO", featureDAO, + FeatureManagerDAOException.class); + } + + @Test(description = "This test case tests handling SQLException when retrieving features of a profile", + dependsOnMethods = "testGetFeaturesForProfileThrowingFeatureManagerDAOException", + expectedExceptions = IllegalTransactionStateException.class) + public void testGetFeaturesForProfileThrowingIllegalTransactionStateException() throws Exception { + Pair> pair = mockConnection(); + PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); + try { + featureManager.getFeaturesForProfile(profile1.getProfileId()); + } finally { + PolicyManagementDAOFactory.init(pair.second().first()); + } + } + + @Test(description = "This test case tests handling FeatureManagerDAOException when deleting features of a profile", + dependsOnMethods = "testGetFeaturesForProfile") + public void testDeleteFeaturesOfProfileThrowingFeatureManagerDAOException() throws Exception { + Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); + profile1 = profileManager.addProfile(profile); + + FeatureDAO featureDAO = mock(FeatureDAO.class); + when(featureDAO.deleteFeaturesOfProfile(any(Profile.class))).thenThrow(new FeatureManagerDAOException()); + + testThrowingException(featureManager, + profile1, + p -> featureManager.deleteFeaturesOfProfile(profile), + "featureDAO", featureDAO, + FeatureManagerDAOException.class); + } + + @Test(description = "This test case tests handling SQLException when deleting features of a profile", + dependsOnMethods = "testDeleteFeaturesOfProfileThrowingFeatureManagerDAOException", + expectedExceptions = IllegalTransactionStateException.class) + public void testDeleteFeaturesOfProfileThrowingIllegalTransactionStateException() throws Exception { + Pair> pair = mockConnection(); + PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); + try { + featureManager.deleteFeaturesOfProfile(profile1); + } finally { + PolicyManagementDAOFactory.init(pair.second().first()); + } + } + + @Test(description = "This test case tests deleting features of a profile", + dependsOnMethods = "testDeleteFeaturesOfProfileThrowingIllegalTransactionStateException") + public void testDeleteFeaturesOfProfile() throws Exception { + featureManager.deleteFeaturesOfProfile(profile1); + } +} \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java index 2cbcfc4e22e..622a3e04141 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file 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. + */ package org.wso2.carbon.policy.mgt.core.mgt.impl; import org.apache.commons.logging.Log; @@ -13,14 +30,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; -import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.ProfileManagementException; import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; @@ -30,9 +40,7 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO; import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; -import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService; -import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest; import org.wso2.carbon.policy.mgt.core.util.FeatureCreator; import org.wso2.carbon.policy.mgt.core.util.ProfileCreator; @@ -89,7 +97,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { when(profileDAO.addProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException()); //Creating profile object Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C); - testThrowingException(profile, p -> profileManager.addProfile(p), "profileDAO", profileDAO, + testThrowingException(profileManager, profile, p -> profileManager.addProfile((Profile) p), "profileDAO", + profileDAO, ProfileManagerDAOException.class); } @@ -101,7 +110,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { new FeatureManagerDAOException()); //Creating profile object Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C); - testThrowingException(profile, p -> profileManager.addProfile(p), "featureDAO", featureDAO, + testThrowingException(profileManager, profile, p -> profileManager.addProfile((Profile) p), "featureDAO", + featureDAO, FeatureManagerDAOException.class); } @@ -139,7 +149,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { String newProfileName = "Updated Test Profile"; Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); savedProfile.setProfileName(newProfileName); - testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "profileDAO", profileDAO, + testThrowingException(profileManager, savedProfile, p -> profileManager.updateProfile((Profile) p), + "profileDAO", profileDAO, ProfileManagerDAOException.class); } @@ -153,7 +164,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { String newProfileName = "Updated Test Profile"; Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); savedProfile.setProfileName(newProfileName); - testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "featureDAO", featureDAO, + testThrowingException(profileManager, savedProfile, p -> profileManager.updateProfile((Profile) p), + "featureDAO", featureDAO, FeatureManagerDAOException.class); } @@ -196,7 +208,9 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testGetProfileThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); - testThrowingException(profile1, p -> profileManager.getProfile(p.getProfileId()), "profileDAO", profileDAO, + testThrowingException(profileManager, profile1, p -> profileManager.getProfile(((Profile) p).getProfileId()), + "profileDAO", + profileDAO, ProfileManagerDAOException.class); } @@ -205,7 +219,9 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testGetProfileThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.getFeaturesForProfile(anyInt())).thenThrow(new FeatureManagerDAOException()); - testThrowingException(profile1, p -> profileManager.getProfile(p.getProfileId()), "featureDAO", featureDAO, + testThrowingException(profileManager, profile1, p -> profileManager.getProfile(((Profile) p).getProfileId()), + "featureDAO", + featureDAO, FeatureManagerDAOException.class); } @@ -235,7 +251,7 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testGetAllProfilesThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.getAllProfiles()).thenThrow(new ProfileManagerDAOException()); - testThrowingException(profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO, + testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO, ProfileManagerDAOException.class); } @@ -244,7 +260,7 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testGetAllProfilesThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); - testThrowingException(profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO, + testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO, FeatureManagerDAOException.class); } @@ -275,7 +291,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.getProfilesOfDeviceType(anyString())).thenThrow(new ProfileManagerDAOException()); - testThrowingException(profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "profileDAO", + testThrowingException(profileManager, profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), + "profileDAO", profileDAO, ProfileManagerDAOException.class); } @@ -286,7 +303,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); - testThrowingException(profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "featureDAO", + testThrowingException(profileManager, profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), + "featureDAO", featureDAO, FeatureManagerDAOException.class); } @@ -311,7 +329,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testDeleteProfileThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.deleteProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException()); - testThrowingException(profile1, p -> profileManager.deleteProfile(profile1), "profileDAO", profileDAO, + testThrowingException(profileManager, profile1, p -> profileManager.deleteProfile(profile1), "profileDAO", + profileDAO, ProfileManagerDAOException.class); } @@ -320,7 +339,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void testDeleteProfileThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.deleteFeaturesOfProfile(any(Profile.class))).thenThrow(new FeatureManagerDAOException()); - testThrowingException(profile1, p -> profileManager.deleteProfile(profile1), "featureDAO", featureDAO, + testThrowingException(profileManager, profile1, p -> profileManager.deleteProfile(profile1), "featureDAO", + featureDAO, FeatureManagerDAOException.class); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml index 7b8b22fe2da..7cb72faa845 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml @@ -36,6 +36,7 @@ +