### Question:
DBDeviceGroupManager extends DBManager<DeviceGroup> implements DeviceGroupManager { @Override public final Set<DeviceGroup> getGroupsAssignedToPolicy(final Policy policy) throws DeviceGroupManagerException { if (policy == null) { LOGGER.error("policy provided was null"); throw new DeviceGroupManagerException("policy provided was null"); } final SessionFactory factory = getFactory(); Set<DeviceGroup> groups = new HashSet<>(); Session session = factory.getCurrentSession(); Transaction tx = session.beginTransaction(); try { LOGGER.debug("retrieving policy mapper from db where policy = {}", policy); List policyMapperList = session.createCriteria(PolicyMapper.class) .add(Restrictions.eq("policy", policy)).list(); session.getTransaction().commit(); if (policyMapperList == null) { LOGGER.debug("no policy mapper found for policy {}", policy); } else { for (Object o : policyMapperList) { if (o instanceof PolicyMapper) { groups.add(((PolicyMapper) o).getDeviceGroup()); } } } } catch (Exception e) { final String msg = "unable to get default policy"; LOGGER.error(msg, e); LOGGER.debug("rolling back transaction"); tx.rollback(); throw new DeviceGroupManagerException(msg, e); } return groups; } DBDeviceGroupManager(final SessionFactory factory); @Override final DeviceGroup saveDeviceGroup(final DeviceGroup deviceGroup); @Override final void updateDeviceGroup(final DeviceGroup deviceGroup); @Override final Set<DeviceGroup> getDeviceGroupSet(); @Override final DeviceGroup getDeviceGroup(final String name); @Override final Set<DeviceGroup> getGroupsAssignedToPolicy(final Policy policy); @Override final boolean deleteDeviceGroup(final String name); @Override final FilteredRecordsList<DeviceGroup> getOrderedDeviceGroupList(
            final String columnToOrder, final boolean ascending, final int firstResult,
            final int maxResults, final String search);  }### Answer:
@Test public void testGetGroupsForPolicy() throws Exception { LOGGER.debug("testGetGroupsForPolicy test started"); final DeviceGroupManager groupManager = new DBDeviceGroupManager(sessionFactory); final PolicyManager policyManager = new DBPolicyManager(sessionFactory); final AppraiserManager appraiserManager = new DBAppraiserManager(sessionFactory); Appraiser appraiser = new TestAppraiser("Test Appraiser"); Appraiser savedAppraiser = appraiserManager.saveAppraiser(appraiser); TestPolicy policy = new TestPolicy("TEST_POLICY"); policy = (TestPolicy) policyManager.savePolicy(policy); DeviceGroup deviceGroup = createDeviceGroup("Test Device Group", "Test Device"); DeviceGroup deviceGroup2 = createDeviceGroup("Test Device Group 2", "Test Device 2"); policyManager.setPolicy(savedAppraiser, deviceGroup, policy); policyManager.setPolicy(savedAppraiser, deviceGroup2, policy); Set<DeviceGroup> groups = groupManager.getGroupsAssignedToPolicy(policy); Assert.assertEquals(groups.size(), 2); Assert.assertTrue(groups.contains(deviceGroup)); Assert.assertTrue(groups.contains(deviceGroup2)); }