ProfileManagerImpl implements ProfileManager { @Override public Profile getProfile(int profileId) throws ProfileManagementException { Profile profile; List<ProfileFeature> featureList; try { PolicyManagementDAOFactory.openConnection(); profile = profileDAO.getProfile(profileId); if (profile == null) { throw new ProfileManagementException("Profile is not available with profile id (" + profileId + ")"); } featureList = featureDAO.getFeaturesForProfile(profileId); profile.setProfileFeaturesList(featureList); } catch (ProfileManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profile id (" + profileId + ")", e); } catch (FeatureManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting features related profile id (" + profileId + ")", e); } catch (SQLException e) { throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return profile; } 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 profile", dependsOnMethods = "testAddProfile") public void testGetProfile() throws Exception { Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); Assert.assertEquals(profile1.getProfileName(), savedProfile.getProfileName()); Assert.assertEquals(profile1.getTenantId(), savedProfile.getTenantId()); Assert.assertEquals(profile1.getDeviceType(), savedProfile.getDeviceType()); }
@Test(description = "This test case tests retrieving non existent profile", dependsOnMethods = "testGetProfile", expectedExceptions = ProfileManagementException.class) public void testGetProfileThrowingProfileManagementException() throws Exception { int nonExistentProfileId = 9999; profileManager.getProfile(nonExistentProfileId); }
@Test(description = "This test case tests handling ProfileManagerDAOException when retrieving profile", dependsOnMethods = "testGetProfile") public void testGetProfileThrowingProfileManagerDAOException() throws Exception { ProfileDAO profileDAO = mock(ProfileDAOImpl.class); when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.getProfile(((Profile) p).getProfileId()), "profileDAO", profileDAO, ProfileManagerDAOException.class); }
@Test(description = "This test case tests handling FeatureManagerDAOException when retrieving profile", dependsOnMethods = "testGetProfileThrowingProfileManagerDAOException") public void testGetProfileThrowingFeatureManagerDAOException() throws Exception { FeatureDAO featureDAO = mock(FeatureDAO.class); when(featureDAO.getFeaturesForProfile(anyInt())).thenThrow(new FeatureManagerDAOException()); testThrowingException(profileManager, profile1, p -> profileManager.getProfile(((Profile) p).getProfileId()), "featureDAO", featureDAO, FeatureManagerDAOException.class); }
@Test(description = "This test case tests handling SQLException when retrieving profile", dependsOnMethods = "testGetProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetProfileThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.getProfile(profile1.getProfileId()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }