FeatureManagerImpl implements FeatureManager { @Override public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException { try { PolicyManagementDAOFactory.openConnection(); Profile profile = profileDAO.getProfile(profileId); if (profile == null) { throw new FeatureManagementException( "Could not find a profile with the profile id: " + profileId); } return featureDAO.getFeaturesForProfile(profileId); } catch (ProfileManagerDAOException | FeatureManagerDAOException e) { throw new FeatureManagementException("Error occurred while getting the features", e); } catch (SQLException e) { throw new FeatureManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } } 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 retrieving features of a profile", dependsOnMethods = "testGetAllFeaturesThrowingIllegalTransactionStateException") public void testGetFeaturesForProfile() throws Exception { featureManager.getFeaturesForProfile(profile1.getProfileId()); }
@Test(description = "This test case tests retrieving features to a non existent profile", dependsOnMethods = "testGetFeaturesForProfile", expectedExceptions = {FeatureManagementException.class}) public void testGetFeaturesForProfileThrowingFeatureManagementException() throws Exception { int nonExistentProfileId = 9999; featureManager.getFeaturesForProfile(nonExistentProfileId); }
@Test(description = "This test case tests handling ProfileManagerDAOException when retrieving features of a " + "profile", dependsOnMethods = "testGetFeaturesForProfile") public void testGetFeaturesForProfileThrowingProfileManagerDAOException() 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()); testThrowingException(featureManager, null, p -> featureManager.getFeaturesForProfile(profile1.getProfileId()), "profileDAO", profileDAO, ProfileManagerDAOException.class); }
@Test(description = "This test case tests handling SQLException when retrieving features of a profile", dependsOnMethods = "testGetFeaturesForProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetFeaturesForProfileThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { featureManager.getFeaturesForProfile(profile1.getProfileId()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }