FeatureManagerImpl implements FeatureManager { @Override public List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, 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); } features = featureDAO.updateProfileFeatures(features, profileId); PolicyManagementDAOFactory.commitTransaction(); } catch (ProfileManagerDAOException | FeatureManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while updating the features to profile id (" + profileId + ")", e); } catch (PolicyManagerDAOException e) { throw new FeatureManagementException("Error occurred while updating the features to profile id (" + profileId + ") to the database", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return features; } 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 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 SQLException when adding new profile feature", dependsOnMethods = "testUpdateProfileFeaturesThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testUpdateProfileFeaturesThrowingIllegalTransactionStateException() throws Exception { 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()); } }