From 030d7cd26d93907c404af62d3087df7f865e002f Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 13 Oct 2017 19:07:19 +0530 Subject: [PATCH 1/7] Removed unused code --- .../mgt/common/PolicyAdministratorPoint.java | 6 - .../policy/mgt/core/dao/FeatureDAO.java | 16 +- .../dao/impl/feature/AbstractFeatureDAO.java | 29 +-- .../impl/PolicyAdministratorPointImpl.java | 16 -- .../policy/mgt/core/mgt/FeatureManager.java | 19 +- .../mgt/core/mgt/impl/FeatureManagerImpl.java | 195 +++++------------- 6 files changed, 62 insertions(+), 219 deletions(-) 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 91e1eff05c..eab6f6214f 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 e64b8214df..00d7280a9d 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 2c3da0e30c..8d04fdec5b 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 50d58c3bf4..d595a3c4eb 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 9c4673a84a..e0c6f3fdbe 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 edaceae3f8..e507999633 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,24 @@ 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 +118,29 @@ 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 +160,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 +175,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 +185,11 @@ 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(); } From e51e344463143ab9df591b337bce8800544da07c Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 13 Oct 2017 19:07:46 +0530 Subject: [PATCH 2/7] Minor changes to the base test class --- .../mgt/core/BasePolicyManagementDAOTest.java | 20 ++++----- .../core/mgt/impl/ProfileManagerImplTest.java | 45 ++++++++++--------- 2 files changed, 33 insertions(+), 32 deletions(-) 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 ec37dab9aa..230291206b 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/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 2cbcfc4e22..92924e5aad 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 @@ -13,14 +13,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 +23,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 +80,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 +93,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 +132,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 +147,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 +191,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 +202,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 +234,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 +243,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 +274,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 +286,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 +312,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 +322,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); } From 54895f360b0122ac763d64df1fa62e9672822000 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 13 Oct 2017 19:08:11 +0530 Subject: [PATCH 3/7] Adding feature manager unit tests --- .../core/mgt/impl/FeatureManagerImplTest.java | 378 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 2 files changed, 379 insertions(+) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java 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 0000000000..bf42625abf --- /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,378 @@ +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.Feature; +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 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 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 ProfileManagerDAOException when adding new profile feature", + dependsOnMethods = "testUpdateProfileFeatures") + 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 features of a profile", + dependsOnMethods = "testUpdateProfileFeaturesThrowingIllegalTransactionStateException") + 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/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml index 7b8b22fe2d..7cb72faa84 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 @@ + From ec8a172b3b555f73e6d2a11e8f50b58b818304dc Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 13 Oct 2017 19:09:08 +0530 Subject: [PATCH 4/7] Adding license --- .../core/mgt/impl/FeatureManagerImplTest.java | 48 ++++++++++++++----- .../core/mgt/impl/ProfileManagerImplTest.java | 17 +++++++ 2 files changed, 53 insertions(+), 12 deletions(-) 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 index bf42625abf..3e12f8b562 100644 --- 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 @@ -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; @@ -9,7 +26,6 @@ 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.Feature; 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; @@ -87,7 +103,7 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { } @Test(description = "This test case tests handling ProfileManagerDAOException when adding new profile feature", - dependsOnMethods = "testAddProfileFeature") + dependsOnMethods = "testAddProfileFeature") public void testAddProfileFeatureThrowingProfileManagerDAOException() throws Exception { Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); profile1 = profileManager.addProfile(profile); @@ -110,7 +126,8 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { profile1 = profileManager.addProfile(profile); FeatureDAO featureDAO = mock(FeatureDAO.class); - when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow(new FeatureManagerDAOException()); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); testThrowingException(featureManager, @@ -144,7 +161,7 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { //Adding profile profile1 = profileManager.addProfile(profile); profileFeaturesList1 = featureManager.addProfileFeatures(profile.getProfileFeaturesList(), - profile1.getProfileId()); + profile1.getProfileId()); Assert.assertEquals(profileFeaturesList1.size(), profile.getProfileFeaturesList().size()); } @@ -182,7 +199,8 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { profile1 = profileManager.addProfile(profile); FeatureDAO featureDAO = mock(FeatureDAO.class); - when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow(new FeatureManagerDAOException()); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); List profileFeaturesList = profile.getProfileFeaturesList(); testThrowingException(featureManager, @@ -226,7 +244,7 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { int createdProfileId = profileManager.addProfile(profile).getProfileId(); profileFeaturesList1.get(0).setFeatureCode(newFeatureCode); List updatedProfileFeatures = featureManager.updateProfileFeatures(profileFeaturesList1, - createdProfileId); + createdProfileId); Assert.assertEquals(updatedProfileFeatures.get(0).getFeatureCode(), newFeatureCode); } @@ -242,7 +260,8 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { List profileFeaturesList = profile.getProfileFeaturesList(); testThrowingException(featureManager, profileFeaturesList, - p -> featureManager.updateProfileFeatures((List) p, profile1.getProfileId()), + p -> featureManager + .updateProfileFeatures((List) p, profile1.getProfileId()), "profileDAO", profileDAO, ProfileManagerDAOException.class); } @@ -254,12 +273,14 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { profile1 = profileManager.addProfile(profile); FeatureDAO featureDAO = mock(FeatureDAO.class); - when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow(new FeatureManagerDAOException()); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); List profileFeaturesList = profile.getProfileFeaturesList(); testThrowingException(featureManager, profileFeaturesList, - p -> featureManager.updateProfileFeatures((List) p, profile1.getProfileId()), + p -> featureManager + .updateProfileFeatures((List) p, profile1.getProfileId()), "featureDAO", featureDAO, FeatureManagerDAOException.class); } @@ -296,7 +317,8 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { } - @Test(description = "This test case tests handling ProfileManagerDAOException when retrieving features of a profile", + @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); @@ -312,14 +334,16 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { ProfileManagerDAOException.class); } - @Test(description = "This test case tests handling FeatureManagerDAOException when retrieving features of a profile", + @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()); + when(featureDAO.addProfileFeature(any(ProfileFeature.class), anyInt())).thenThrow( + new FeatureManagerDAOException()); testThrowingException(featureManager, null, 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 92924e5aad..622a3e0414 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; From 5ba6fb90f5b5897554024fbe64700cac81fb8c75 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Sun, 15 Oct 2017 09:44:46 +0530 Subject: [PATCH 5/7] Removed redundant rollbackTransaction causing IllegalStateException --- .../carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java | 3 --- 1 file changed, 3 deletions(-) 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 e507999633..dff12ac54f 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 @@ -107,7 +107,6 @@ public class FeatureManagerImpl implements FeatureManager { throw new FeatureManagementException("Error occurred while adding the features to profile id (" + profileId + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while adding the features to profile id (" + profileId + ") to the database", e); } finally { @@ -133,7 +132,6 @@ public class FeatureManagerImpl implements FeatureManager { throw new FeatureManagementException("Error occurred while updating the features to profile id (" + profileId + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while updating the features to profile id (" + profileId + ") to the database", e); } finally { @@ -187,7 +185,6 @@ public class FeatureManagerImpl implements FeatureManager { throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + profile.getProfileName() + ") from database", e); } finally { From 15885da9f9e8942d01f8ff976ca48dd3c62516d4 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Sun, 15 Oct 2017 09:44:56 +0530 Subject: [PATCH 6/7] Removed redundant rollbackTransaction causing IllegalStateException --- .../carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java | 3 --- 1 file changed, 3 deletions(-) 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 bf1e00e165..160d512ee5 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 { From 685a5c076d04a18718f3234c04120a4ed66cf4b6 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Sun, 15 Oct 2017 09:45:23 +0530 Subject: [PATCH 7/7] Added few more unit tests for the feature manager --- .../core/mgt/impl/FeatureManagerImplTest.java | 94 ++++++++++++++++++- 1 file changed, 91 insertions(+), 3 deletions(-) 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 index 3e12f8b562..7a0a756faf 100644 --- 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 @@ -102,6 +102,18 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { 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 { @@ -236,6 +248,51 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { 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 { @@ -248,8 +305,20 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { 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 = "testUpdateProfileFeatures") + dependsOnMethods = "testUpdateProfileFeaturesThrowingFeatureManagementException") public void testUpdateProfileFeaturesThrowingProfileManagerDAOException() throws Exception { Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); profile1 = profileManager.addProfile(profile); @@ -302,8 +371,28 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { } } + @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 = "testUpdateProfileFeaturesThrowingIllegalTransactionStateException") + dependsOnMethods = "testGetAllFeaturesThrowingIllegalTransactionStateException") public void testGetFeaturesForProfile() throws Exception { featureManager.getFeaturesForProfile(profile1.getProfileId()); } @@ -316,7 +405,6 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { featureManager.getFeaturesForProfile(nonExistentProfileId); } - @Test(description = "This test case tests handling ProfileManagerDAOException when retrieving features of a " + "profile", dependsOnMethods = "testGetFeaturesForProfile")