MonitoringManagerImpl implements MonitoringManager { @Override public boolean isCompliant(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { Device device; try { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); device = service.getDevice(deviceIdentifier, false); } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } try { PolicyManagementDAOFactory.openConnection(); NonComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo() .getId()); if (complianceData != null && !complianceData.isStatus()) { return false; } } catch (MonitoringDAOException e) { throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return true; } 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 is compliant", dependsOnMethods = "testAddProfileThrowingMonitoringDAOException2") public void testIsCompliant() throws Exception { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); monitoringManager.isCompliant(deviceIdentifier); }
@Test(description = "This test case tests is compliant", dependsOnMethods = "testIsCompliant") public void testIsCompliant2() throws Exception { MonitoringDAO monitoringDAO = spy(MonitoringDAOImpl.class); doReturn(null).when(monitoringDAO).getCompliance(anyInt(),anyInt()); Object oldObj = changeFieldValue(monitoringManager, "monitoringDAO", monitoringDAO); try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); boolean compliant = monitoringManager.isCompliant(deviceIdentifier); Assert.assertEquals(compliant,false); } finally { changeFieldValue(monitoringManager, "monitoringDAO", oldObj); } }
@Test(description = "This test case tests handling DeviceManagementException when testing policy compliance", dependsOnMethods = "testIsCompliant2") public void testIsCompliantThrowingDeviceManagementException() throws Exception { DeviceManagementProviderService serviceMock = mock(DeviceManagementProviderService.class); doThrow(new DeviceManagementException()).when(serviceMock).getDevice(any(DeviceIdentifier.class), anyBoolean()); PolicyManagementDataHolder instance = PolicyManagementDataHolder.getInstance(); DeviceManagementProviderService service = instance.getDeviceManagementService(); instance.setDeviceManagementService(serviceMock); try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); monitoringManager.isCompliant(deviceIdentifier); } catch (Exception e) { if (!(e.getCause() instanceof DeviceManagementException)) { throw e; } } finally { instance.setDeviceManagementService(service); } }
@Test(description = "This test case tests handling MonitoringDAOException when checking policy compliance", dependsOnMethods = "testIsCompliantThrowingDeviceManagementException") public void testIsCompliantThrowingMonitoringDAOException() throws Exception { MonitoringDAO monitoringDAO = spy(MonitoringDAOImpl.class); doThrow(new MonitoringDAOException()).when(monitoringDAO).getCompliance(anyInt(),anyInt()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); testThrowingException(monitoringManager, deviceIdentifier, d -> monitoringManager.isCompliant((DeviceIdentifier) d), "monitoringDAO", monitoringDAO, MonitoringDAOException.class); }
@Test(description = "This test case tests handling SQLException when checking is compliant", dependsOnMethods = "testIsCompliantThrowingMonitoringDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testIsCompliantThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); monitoringManager.isCompliant(deviceIdentifier); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }