From 685a5c076d04a18718f3234c04120a4ed66cf4b6 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Sun, 15 Oct 2017 09:45:23 +0530 Subject: [PATCH] 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")