FeatureManagerImpl implements FeatureManager { @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 (ProfileManagerDAOException | PolicyManagerDAOException | FeatureManagerDAOException e) { throw new FeatureManagementException("Error occurred while adding profile feature (" + feature.getFeatureCode() + " - " + profileId + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return feature; } FeatureManagerImpl(); @Override ProfileFeature addProfileFeature(ProfileFeature feature, int profileId); @Override ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId); @Override List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId); @Override List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId); @Override List<Feature> getAllFeatures(String deviceType); @Override List<ProfileFeature> getFeaturesForProfile(int profileId); @Override boolean deleteFeaturesOfProfile(Profile profile);  }
@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); 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 { 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()); } }