ProfileManagerImpl implements ProfileManager { @Override public List<Profile> getAllProfiles() throws ProfileManagementException { List<Profile> profileList; try { PolicyManagementDAOFactory.openConnection(); profileList = profileDAO.getAllProfiles(); List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures(); for (Profile profile : profileList) { List<ProfileFeature> list = new ArrayList<ProfileFeature>(); for (ProfileFeature profileFeature : featureList) { if (profile.getProfileId() == profileFeature.getProfileId()) { list.add(profileFeature); } } profile.setProfileFeaturesList(list); } } catch (ProfileManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profiles", e); } catch (FeatureManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting features related to profiles", e); } catch (SQLException e) { throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return profileList; } 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 retrieving all profiles", dependsOnMethods = "testAddProfile") public void testGetAllProfiles() throws Exception { profileManager.getAllProfiles(); }
@Test(description = "This test case tests handling ProfileManagerDAOException when retrieving all profiles", dependsOnMethods = "testGetAllProfiles") public void testGetAllProfilesThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.getAllProfiles()).thenThrow(new ProfileManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO, ProfileManagerDAOException.class); }
@Test(description = "This test case tests handling FeatureManagerDAOException when retrieving all profiles", dependsOnMethods = "testGetAllProfilesThrowingProfileManagerDAOException") public void testGetAllProfilesThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO, FeatureManagerDAOException.class); }
@Test(description = "This test case tests handling SQLException when retrieving all profiles", dependsOnMethods = "testGetAllProfilesThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetAllProfilesThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.getAllProfiles(); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }