ProfileManagerImpl implements ProfileManager { @Override public boolean deleteProfile(Profile profile) throws ProfileManagementException { boolean bool = true; try { PolicyManagementDAOFactory.beginTransaction(); featureDAO.deleteFeaturesOfProfile(profile); bool = profileDAO.deleteProfile(profile); PolicyManagementDAOFactory.commitTransaction(); } catch (ProfileManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new ProfileManagementException("Error occurred while deleting the profile (" + profile.getProfileName() + ")", e); } catch (FeatureManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new ProfileManagementException("Error occurred while deleting the features from profile (" + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { throw new ProfileManagementException("Error occurred while deleting the profile (" + profile.getProfileName() + ") from database", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return bool; } ProfileManagerImpl(); @Override Profile addProfile(Profile profile); @Override Profile updateProfile(Profile profile); @Override boolean deleteProfile(Profile profile); @Override Profile getProfile(int profileId); @Override List<Profile> getAllProfiles(); @Override List<Profile> getProfilesOfDeviceType(String deviceType);  }
@Test(description = "This test case tests handling ProfileManagerDAOException when deleting a profile", dependsOnMethods = "testGetProfilesOfDeviceTypeThrowingIllegalTransactionStateException") public void testDeleteProfileThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.deleteProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.deleteProfile(profile1), "profileDAO", profileDAO, ProfileManagerDAOException.class); }
@Test(description = "This test case tests handling FeatureManagerDAOException when deleting a profile", dependsOnMethods = "testDeleteProfileThrowingProfileManagerDAOException") public void testDeleteProfileThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.deleteFeaturesOfProfile(any(Profile.class))).thenThrow(new FeatureManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.deleteProfile(profile1), "featureDAO", featureDAO, FeatureManagerDAOException.class); }
@Test(description = "This test case tests handling SQLException when deleting a profile", dependsOnMethods = "testDeleteProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testDeleteProfileThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.deleteProfile(profile1); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests deleting a profile", dependsOnMethods = "testDeleteProfileThrowingIllegalTransactionStateException", expectedExceptions = {ProfileManagementException.class}) public void testDeleteProfile() throws Exception { profileManager.deleteProfile(profile1); Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); }