MonitoringManagerImpl implements MonitoringManager { @Override public List<ComplianceFeature> checkPolicyCompliance( DeviceIdentifier deviceIdentifier, Object deviceResponse) throws PolicyComplianceException { List<ComplianceFeature> complianceFeatures = new ArrayList<>(); try { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManager manager = PolicyManagementDataHolder.getInstance().getPolicyManager(); Device device = service.getDevice(deviceIdentifier, false); Policy policy = manager.getAppliedPolicyToDevice(deviceIdentifier); if (policy != null) { PolicyMonitoringManager monitoringService = PolicyManagementDataHolder.getInstance(). getDeviceManagementService().getPolicyMonitoringManager(deviceIdentifier.getType()); NonComplianceData complianceData; try { PolicyManagementDAOFactory.openConnection(); NonComplianceData cmd = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, policy, deviceResponse); if (cmd != null) { complianceData.setId(cmd.getId()); complianceData.setPolicy(policy); complianceFeatures = complianceData.getComplianceFeatures(); complianceData.setDeviceId(device.getId()); complianceData.setPolicyId(policy.getId()); } } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a data source connection", e); } catch (MonitoringDAOException e) { throw new PolicyComplianceException( "Unable to add the none compliance features to database for device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } if (complianceFeatures != null && !complianceFeatures.isEmpty()) { try { PolicyManagementDAOFactory.beginTransaction(); monitoringDAO.setDeviceAsNoneCompliance(device.getId(), device.getEnrolmentInfo().getId(), policy.getId()); if (log.isDebugEnabled()) { log.debug("Compliance status primary key " + complianceData.getId()); } monitoringDAO.deleteNoneComplianceData(complianceData.getId()); monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(), complianceFeatures); PolicyManagementDAOFactory.commitTransaction(); } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException( "Unable to add the none compliance features to database for device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData); List<ProfileFeature> profileFeatures = policy.getProfile().getProfileFeaturesList(); for (ComplianceFeature compFeature : complianceFeatures) { for (ProfileFeature profFeature : profileFeatures) { if (profFeature.getFeatureCode().equalsIgnoreCase(compFeature.getFeatureCode())) { compFeature.setFeature(profFeature); } } } } else { try { PolicyManagementDAOFactory.beginTransaction(); monitoringDAO.setDeviceAsCompliance(device.getId(), device.getEnrolmentInfo().getId(), policy .getId()); monitoringDAO.deleteNoneComplianceData(complianceData.getId()); PolicyManagementDAOFactory.commitTransaction(); } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException( "Unable to remove the none compliance features from database for device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } } } else { if (log.isDebugEnabled()) { log.debug("There is no policy applied to this device, hence compliance monitoring was not called."); } } } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable tor retrieve device data from DB for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } catch (PolicyManagerDAOException | PolicyManagementException e) { throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } return complianceFeatures; } MonitoringManagerImpl(); @Override List<ComplianceFeature> checkPolicyCompliance(
            DeviceIdentifier deviceIdentifier,
            Object deviceResponse); @Override boolean isCompliant(DeviceIdentifier deviceIdentifier); @Override NonComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier); @Override void addMonitoringOperation(List<Device> devices); @Override List<String> getDeviceTypes();  }
@Test(description = "This test case tests checking policy compliance") public void testCheckPolicyCompliance() throws Exception { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); monitoringManager.checkPolicyCompliance(deviceIdentifier, new ArrayList<ComplianceFeature>()); }
@Test(description = "This test case tests handling ProfileManagerDAOException when checking policy compliance", dependsOnMethods = "testCheckPolicyCompliance") public void testCheckPolicyComplianceThrowingProfileManagerDAOException() throws Exception { MonitoringDAO monitoringDAO = mock(MonitoringDAO.class); when(monitoringDAO.getCompliance(anyInt(),anyInt())).thenThrow(new MonitoringDAOException()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); testThrowingException(monitoringManager, null, p -> monitoringManager.checkPolicyCompliance(deviceIdentifier, new ArrayList<ComplianceFeature>()), "monitoringDAO", monitoringDAO, MonitoringDAOException.class); }
@Test(description = "This test case tests handling PolicyComplianceException when checking policy compliance", dependsOnMethods = "testCheckPolicyComplianceThrowingProfileManagerDAOException", expectedExceptions = PolicyComplianceException.class) public void testAddProfileThrowingPolicyComplianceException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doAnswer(new Answer<Connection>() { int callCounter = 0; @Override public Connection answer(InvocationOnMock invocationOnMock) throws Throwable { if(callCounter > 0){ Field currentConnectionField = PolicyManagementDAOFactory.class.getDeclaredField("currentConnection"); currentConnectionField.setAccessible(true); ThreadLocal<Connection> threadLocal = new ThreadLocal<>(); threadLocal.set(pair.first()); currentConnectionField.set(null, threadLocal); throw new SQLException(); } callCounter++; return pair.second().first().getConnection(); } }).when(pair.second().second()).getConnection(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); try { monitoringManager.checkPolicyCompliance(deviceIdentifier, new ArrayList<ComplianceFeature>()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling PolicyComplianceException when checking policy compliance", dependsOnMethods = "testAddProfileThrowingPolicyComplianceException") public void testAddProfileThrowingMonitoringDAOException() throws Exception { MonitoringDAO monitoringDAO = mock(MonitoringDAO.class); when(monitoringDAO.getCompliance(anyInt(), anyInt())).thenThrow( new MonitoringDAOException()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); testThrowingException(monitoringManager, deviceIdentifier, d -> monitoringManager.checkPolicyCompliance((DeviceIdentifier) d, new ArrayList<ComplianceFeature>()), "monitoringDAO", monitoringDAO, MonitoringDAOException.class); }
@Test(description = "This test case tests handling PolicyComplianceException when checking policy compliance", dependsOnMethods = "testAddProfileThrowingMonitoringDAOException") public void testAddProfileThrowingMonitoringDAOException2() throws Exception { MonitoringDAO monitoringDAO = spy(MonitoringDAOImpl.class); doThrow(new MonitoringDAOException()).when(monitoringDAO).deleteNoneComplianceData(anyInt()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); testThrowingException(monitoringManager, deviceIdentifier, d -> monitoringManager.checkPolicyCompliance((DeviceIdentifier) d, new ArrayList<ComplianceFeature>()), "monitoringDAO", monitoringDAO, MonitoringDAOException.class); }