ProfileManagerImpl implements ProfileManager { @Override public List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagementException { List<Profile> profileList; List<ProfileFeature> featureList; try { PolicyManagementDAOFactory.openConnection(); profileList = profileDAO.getProfilesOfDeviceType(deviceType); featureList = featureDAO.getAllProfileFeatures(); for (Profile profile : profileList) { List<ProfileFeature> profileFeatureList = new ArrayList<ProfileFeature>(); for (ProfileFeature profileFeature : featureList) { if (profile.getProfileId() == profileFeature.getProfileId()) { profileFeatureList.add(profileFeature); } } profile.setProfileFeaturesList(profileFeatureList); } } catch (ProfileManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profiles", e); } catch (FeatureManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profile features types", 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 profiles of a device type", dependsOnMethods = "testAddProfile") public void testGetProfilesOfDeviceType() throws Exception { profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C); }
@Test(description = "This test case tests handling ProfileManagerDAOException when retrieving all profiles of a " + "device type", dependsOnMethods = "testGetProfilesOfDeviceType") public void testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.getProfilesOfDeviceType(anyString())).thenThrow(new ProfileManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "profileDAO", profileDAO, ProfileManagerDAOException.class); }
@Test(description = "This test case tests handling FeatureManagerDAOException when retrieving all profiles of a " + "device type", dependsOnMethods = "testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException") public void testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "featureDAO", featureDAO, FeatureManagerDAOException.class); }
@Test(description = "This test case tests handling SQLException when retrieving all profiles of a device type", dependsOnMethods = "testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetProfilesOfDeviceTypeThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }