### Question:
DBPolicyManager extends DBManager<Policy> implements PolicyManager { public final int getGroupCountForPolicy(final Policy policy) { int count = 0; if (policy != null) { final SessionFactory factory = getFactory(); Transaction tx = null; Session session = factory.getCurrentSession(); try { tx = session.beginTransaction(); LOGGER.debug("retrieving group use count for policy {}", policy); final Criteria cr = session.createCriteria(PolicyMapper.class) .add(Restrictions.eq("policy", policy)) .setProjection(Projections.projectionList() .add(Projections.count("policy"))); final Object result = cr.uniqueResult(); if (result != null && result instanceof Long) { count = ((Long) result).intValue(); } } catch (Exception e) { count = -1; final String msg = "There was an error retrieving the group use count for a policy (ID: " + policy.getId() + ", Name: " + policy.getName() + ")."; LOGGER.error(msg, e); } finally { if (tx != null) { tx.rollback(); } } } return count; } DBPolicyManager(final SessionFactory factory); @Override final Policy savePolicy(final Policy policy); @Override final void updatePolicy(final Policy policy); @Override final List<Policy> getPolicyList(final Class<? extends Policy> clazz); @Override List<Policy> getPoliciesContainingBaseline(
            final Class<? extends Policy> clazz,
            final Baseline baseline
    ); @Override final Policy getPolicy(final String name); @Override final Policy getPolicy(final Serializable id); @Override final void setDefaultPolicy(final Appraiser appraiser,
            final Policy policy); @Override final Policy getCompletePolicy(final String name); @Override final Policy getDefaultPolicy(final Appraiser appraiser); @Override final Policy getPolicy(
            final Appraiser appraiser,
            final Device device); @Override final Policy getPolicy(final Appraiser appraiser,
            final DeviceGroup deviceGroup); @Override final void setPolicy(final Appraiser appraiser,
            final DeviceGroup deviceGroup, final Policy policy); final int getGroupCountForPolicy(final Policy policy);  }### Answer:
@Test public void testGetGroupCountForPolicy() throws Exception { LOGGER.debug("started testGetGroupCountForPolicy"); final PolicyManager policyManager = new DBPolicyManager(sessionFactory); final Policy policy1 = new TestPolicy("Policy1"); final Policy policy2 = new TestPolicy("Policy2"); final Policy policy3 = new TestPolicy("Policy3"); final Policy policy4 = new TestPolicy("Policy4"); final DeviceGroup deviceGroup1 = createDeviceGroupWithDevice("Group1", "Device1"); final DeviceGroup deviceGroup2 = createDeviceGroupWithDevice("Group2", "Device2"); final DeviceGroup deviceGroup3 = createDeviceGroupWithDevice("Group3", "Device3"); policyManager.savePolicy(policy1); policyManager.savePolicy(policy2); policyManager.savePolicy(policy3); policyManager.savePolicy(policy4); policyManager.setPolicy(appraiser, deviceGroup1, policy2); policyManager.setPolicy(appraiser, deviceGroup2, policy4); policyManager.setPolicy(appraiser, deviceGroup3, policy4); Assert.assertEquals(policyManager.getGroupCountForPolicy(policy1), 0); Assert.assertEquals(policyManager.getGroupCountForPolicy(policy2), 1); Assert.assertEquals(policyManager.getGroupCountForPolicy(policy3), 0); Assert.assertEquals(policyManager.getGroupCountForPolicy(policy4), 2); }
@Test public void testGetGroupCountForPolicyNull() { LOGGER.debug("started testGetGroupCountForPolicyNull"); final PolicyManager policyManager = new DBPolicyManager(sessionFactory); Assert.assertEquals(policyManager.getGroupCountForPolicy(null), 0); }