FeatureManagerImpl implements FeatureManager { @Override public ProfileFeature updateProfileFeature(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.updateProfileFeature(feature, profileId); PolicyManagementDAOFactory.commitTransaction(); } catch (ProfileManagerDAOException | FeatureManagerDAOException | PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new FeatureManagementException("Error occurred while updating feature (" + feature.getFeatureCode() + " - " + profileId + ") in database.", 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 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); }