Merge pull request #1069 from rasika/feature-manger-unit-tests

Adding Unit Tests for the Feature Manger
revert-70aa11f8
Madawa Soysa 7 years ago committed by GitHub
commit e50d7bc5d8

@ -154,11 +154,5 @@ public interface PolicyAdministratorPoint {
List<Profile> getProfiles() throws PolicyManagementException; List<Profile> 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; int getPolicyCount() throws PolicyManagementException;
} }

@ -96,15 +96,6 @@ public interface FeatureDAO {
*/ */
List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagerDAOException; List<ProfileFeature> 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. * 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; 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; boolean deleteProfileFeatures(int featureId) throws FeatureManagerDAOException;
} }

@ -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.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import javax.naming.OperationNotSupportedException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
@ -49,13 +50,13 @@ public abstract class AbstractFeatureDAO implements FeatureDAO {
@Override @Override
public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException {
return null; throw new UnsupportedOperationException();
} }
@Override @Override
public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws
FeatureManagerDAOException { FeatureManagerDAOException {
return null; throw new UnsupportedOperationException();
} }
@Override @Override
@ -311,30 +312,6 @@ public abstract class AbstractFeatureDAO implements FeatureDAO {
return featureList; 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 { private Connection getConnection() throws FeatureManagerDAOException {
return PolicyManagementDAOFactory.getConnection(); return PolicyManagementDAOFactory.getConnection();
} }

@ -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 @Override
public int getPolicyCount() throws PolicyManagementException { public int getPolicyCount() throws PolicyManagementException {
return PolicyCacheManagerImpl.getInstance().getAllPolicies().size(); return PolicyCacheManagerImpl.getInstance().getAllPolicies().size();

@ -21,36 +21,27 @@ package org.wso2.carbon.policy.mgt.core.mgt;
import org.wso2.carbon.device.mgt.common.Feature; 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.Profile;
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import java.util.List; import java.util.List;
public interface FeatureManager { public interface FeatureManager {
/*Feature addFeature(Feature feature) throws FeatureManagementException;
public List<Feature> addFeatures(List<Feature> features) throws FeatureManagementException;
Feature updateFeature(Feature feature) throws FeatureManagementException;*/
boolean deleteFeature(Feature feature) throws FeatureManagementException;
ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException; ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException;
ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException; ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException;
List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagementException; List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId)
throws FeatureManagementException;
List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagementException;
List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId)
throws FeatureManagementException;
List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException; List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException;
List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException; List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException;
boolean deleteFeature(int featureId) throws FeatureManagementException;
boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException; boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException;
} }

@ -21,13 +21,15 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Feature; 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.Profile;
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; 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.FeatureDAO;
import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; 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.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; 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 org.wso2.carbon.policy.mgt.core.mgt.FeatureManager;
import java.sql.SQLException; import java.sql.SQLException;
@ -36,130 +38,28 @@ import java.util.List;
public class FeatureManagerImpl implements FeatureManager { public class FeatureManagerImpl implements FeatureManager {
private FeatureDAO featureDAO; private FeatureDAO featureDAO;
private ProfileDAO profileDAO;
private static Log log = LogFactory.getLog(FeatureManagerImpl.class); private static Log log = LogFactory.getLog(FeatureManagerImpl.class);
public FeatureManagerImpl() { public FeatureManagerImpl() {
featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
} profileDAO = PolicyManagementDAOFactory.getProfileDAO();
/*@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<Feature> addFeatures(List<Feature> 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;
} }
@Override @Override
public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException { public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction(); 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); feature = featureDAO.addProfileFeature(feature, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException | FeatureManagerDAOException e) { } catch (ProfileManagerDAOException | PolicyManagerDAOException | FeatureManagerDAOException e) {
throw new FeatureManagementException("Error occurred while adding profile feature (" + throw new FeatureManagementException("Error occurred while adding profile feature (" +
feature.getFeatureCode() + " - " + profileId + ")", e); feature.getFeatureCode() + " - " + profileId + ")", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -168,16 +68,22 @@ public class FeatureManagerImpl implements FeatureManager {
@Override @Override
public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws
FeatureManagementException { FeatureManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction(); 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); feature = featureDAO.updateProfileFeature(feature, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException | PolicyManagerDAOException e) { } catch (ProfileManagerDAOException | FeatureManagerDAOException | PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new FeatureManagementException("Error occurred while updating feature (" + throw new FeatureManagementException("Error occurred while updating feature (" +
feature.getFeatureCode() + " - " + profileId + ") in database.", e); feature.getFeatureCode() + " - " + profileId +
") in database.", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -186,19 +92,23 @@ public class FeatureManagerImpl implements FeatureManager {
@Override @Override
public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws
FeatureManagementException { FeatureManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction(); 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); features = featureDAO.addProfileFeatures(features, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) { } catch (ProfileManagerDAOException | FeatureManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new FeatureManagementException("Error occurred while adding the features to profile id (" + throw new FeatureManagementException("Error occurred while adding the features to profile id (" +
profileId + ")", e); profileId + ")", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new FeatureManagementException("Error occurred while adding the features to profile id (" + throw new FeatureManagementException("Error occurred while adding the features to profile id (" +
profileId + ") to the database", e); profileId + ") to the database", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -207,25 +117,28 @@ public class FeatureManagerImpl implements FeatureManager {
@Override @Override
public List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws public List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws
FeatureManagementException { FeatureManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction(); 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); features = featureDAO.updateProfileFeatures(features, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) { } catch (ProfileManagerDAOException | FeatureManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new FeatureManagementException("Error occurred while updating the features to profile id (" + throw new FeatureManagementException("Error occurred while updating the features to profile id (" +
profileId + ")", e); profileId + ")", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new FeatureManagementException("Error occurred while updating the features to profile id (" + throw new FeatureManagementException("Error occurred while updating the features to profile id (" +
profileId + ") to the database", e); profileId + ") to the database", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
return features; return features;
} }
@Override @Override
public List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException { public List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException {
@ -245,8 +158,13 @@ public class FeatureManagerImpl implements FeatureManager {
public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException { public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException {
try { try {
PolicyManagementDAOFactory.openConnection(); 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); return featureDAO.getFeaturesForProfile(profileId);
} catch (FeatureManagerDAOException e) { } catch (ProfileManagerDAOException | FeatureManagerDAOException e) {
throw new FeatureManagementException("Error occurred while getting the features", e); throw new FeatureManagementException("Error occurred while getting the features", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new FeatureManagementException("Error occurred while opening a connection to the data source", 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 @Override
public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException { public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagementException {
boolean bool; boolean bool;
@ -286,11 +183,10 @@ public class FeatureManagerImpl implements FeatureManager {
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" +
profile.getProfileName() + ")", e); profile.getProfileName() + ")", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" +
profile.getProfileName() + ") from database", e); profile.getProfileName() + ") from database", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }

@ -75,7 +75,6 @@ public class ProfileManagerImpl implements ProfileManager {
throw new ProfileManagementException("Error occurred while adding the profile features (" + throw new ProfileManagementException("Error occurred while adding the profile features (" +
profile.getProfileName() + ")", e); profile.getProfileName() + ")", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new ProfileManagementException("Error occurred while adding the profile (" + throw new ProfileManagementException("Error occurred while adding the profile (" +
profile.getProfileName() + ") to the database", e); profile.getProfileName() + ") to the database", e);
} finally { } finally {
@ -105,7 +104,6 @@ public class ProfileManagerImpl implements ProfileManager {
throw new ProfileManagementException("Error occurred while updating the profile features (" + throw new ProfileManagementException("Error occurred while updating the profile features (" +
profile.getProfileName() + ")", e); profile.getProfileName() + ")", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new ProfileManagementException("Error occurred while updating the profile (" + throw new ProfileManagementException("Error occurred while updating the profile (" +
profile.getProfileName() + ") to the database", e); profile.getProfileName() + ") to the database", e);
} finally { } finally {
@ -132,7 +130,6 @@ public class ProfileManagerImpl implements ProfileManager {
throw new ProfileManagementException("Error occurred while deleting the features from profile (" + throw new ProfileManagementException("Error occurred while deleting the features from profile (" +
profile.getProfileName() + ")", e); profile.getProfileName() + ")", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new ProfileManagementException("Error occurred while deleting the profile (" + throw new ProfileManagementException("Error occurred while deleting the profile (" +
profile.getProfileName() + ") from database", e); profile.getProfileName() + ") from database", e);
} finally { } finally {

@ -22,7 +22,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.PoolProperties; import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeSuite;
import org.testng.internal.collections.Pair; import org.testng.internal.collections.Pair;
import org.w3c.dom.Document; 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.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; 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.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.authorization.DeviceAccessAuthorizationServiceImpl;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; 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.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; 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.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig;
@ -100,7 +97,7 @@ public abstract class BasePolicyManagementDAOTest {
DeviceConfigurationManager.getInstance().initConfig(); DeviceConfigurationManager.getInstance().initConfig();
} }
protected void initializeServices() throws Exception{ protected void initializeServices() throws Exception {
initDatSource(); initDatSource();
initSQLScript(); initSQLScript();
@ -155,7 +152,7 @@ public abstract class BasePolicyManagementDAOTest {
} }
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants
.SUPER_TENANT_DOMAIN_NAME); .SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
} }
@ -260,20 +257,21 @@ public abstract class BasePolicyManagementDAOTest {
} }
public interface Command { 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, protected void testThrowingException(Object targetObj, Object paramObj, Command command, String fieldName,
Class<?> exceptionClass) throws Exception { Object mockObj,
Object oldObj = changeFieldValue(profileManager, fieldName, mockObj); Class<?> exceptionClass) throws Exception {
Object oldObj = changeFieldValue(targetObj, fieldName, mockObj);
try { try {
command.call(profile); command.call(paramObj);
} catch (Exception e) { } catch (Exception e) {
if (!(e.getCause().getClass().getName().equals(exceptionClass.getName()))) { if (!(e.getCause().getClass().getName().equals(exceptionClass.getName()))) {
throw e; throw e;
} }
} finally { } finally {
changeFieldValue(profileManager, fieldName, oldObj); changeFieldValue(targetObj, fieldName, oldObj);
} }
} }

@ -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<ProfileFeature> 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<Connection, Pair<DataSource, DataSource>> 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<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
testThrowingException(featureManager,
profileFeaturesList,
p -> featureManager.addProfileFeatures((List<ProfileFeature>) 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<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
testThrowingException(featureManager,
profileFeaturesList,
p -> featureManager.addProfileFeatures((List<ProfileFeature>) 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<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
Pair<Connection, Pair<DataSource, DataSource>> 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<ProfileFeature> 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<ProfileFeature> 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<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
testThrowingException(featureManager,
profileFeaturesList,
p -> featureManager
.updateProfileFeatures((List<ProfileFeature>) 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<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
testThrowingException(featureManager,
profileFeaturesList,
p -> featureManager
.updateProfileFeatures((List<ProfileFeature>) 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<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
Pair<Connection, Pair<DataSource, DataSource>> 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<Connection, Pair<DataSource, DataSource>> 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<Connection, Pair<DataSource, DataSource>> 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<Connection, Pair<DataSource, DataSource>> 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);
}
}

@ -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; package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log; 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.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; 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.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.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.common.ProfileManagementException;
import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest; import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; 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.ProfileDAO;
import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; 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.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.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.FeatureCreator;
import org.wso2.carbon.policy.mgt.core.util.ProfileCreator; 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()); when(profileDAO.addProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException());
//Creating profile object //Creating profile object
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C); 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); ProfileManagerDAOException.class);
} }
@ -101,7 +110,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
new FeatureManagerDAOException()); new FeatureManagerDAOException());
//Creating profile object //Creating profile object
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C); 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); FeatureManagerDAOException.class);
} }
@ -139,7 +149,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
String newProfileName = "Updated Test Profile"; String newProfileName = "Updated Test Profile";
Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
savedProfile.setProfileName(newProfileName); savedProfile.setProfileName(newProfileName);
testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "profileDAO", profileDAO, testThrowingException(profileManager, savedProfile, p -> profileManager.updateProfile((Profile) p),
"profileDAO", profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -153,7 +164,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
String newProfileName = "Updated Test Profile"; String newProfileName = "Updated Test Profile";
Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
savedProfile.setProfileName(newProfileName); savedProfile.setProfileName(newProfileName);
testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "featureDAO", featureDAO, testThrowingException(profileManager, savedProfile, p -> profileManager.updateProfile((Profile) p),
"featureDAO", featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -196,7 +208,9 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfileThrowingProfileManagerDAOException() throws Exception { public void testGetProfileThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); 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); ProfileManagerDAOException.class);
} }
@ -205,7 +219,9 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfileThrowingFeatureManagerDAOException() throws Exception { public void testGetProfileThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.getFeaturesForProfile(anyInt())).thenThrow(new FeatureManagerDAOException()); 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); FeatureManagerDAOException.class);
} }
@ -235,7 +251,7 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetAllProfilesThrowingProfileManagerDAOException() throws Exception { public void testGetAllProfilesThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.getAllProfiles()).thenThrow(new ProfileManagerDAOException()); when(profileDAO.getAllProfiles()).thenThrow(new ProfileManagerDAOException());
testThrowingException(profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO, testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -244,7 +260,7 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetAllProfilesThrowingFeatureManagerDAOException() throws Exception { public void testGetAllProfilesThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException());
testThrowingException(profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO, testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -275,7 +291,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException() throws Exception { public void testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.getProfilesOfDeviceType(anyString())).thenThrow(new ProfileManagerDAOException()); 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, profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -286,7 +303,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException() throws Exception { public void testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); 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, featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -311,7 +329,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testDeleteProfileThrowingProfileManagerDAOException() throws Exception { public void testDeleteProfileThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.deleteProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException()); 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); ProfileManagerDAOException.class);
} }
@ -320,7 +339,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testDeleteProfileThrowingFeatureManagerDAOException() throws Exception { public void testDeleteProfileThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.deleteFeaturesOfProfile(any(Profile.class))).thenThrow(new FeatureManagerDAOException()); 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); FeatureManagerDAOException.class);
} }

@ -36,6 +36,7 @@
<parameter name="dbType" value="H2"/> <parameter name="dbType" value="H2"/>
<classes> <classes>
<class name="org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImplTest" /> <class name="org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImplTest" />
<class name="org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImplTest" />
<class name="org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImplTest"/> <class name="org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImplTest"/>
<class name="org.wso2.carbon.policy.mgt.core.task.TaskSchedulerServiceImplTest" /> <class name="org.wso2.carbon.policy.mgt.core.task.TaskSchedulerServiceImplTest" />
</classes> </classes>

Loading…
Cancel
Save