MetricService { public static void doGetPolicyMetrics(HttpServletResponse response) { Set<OnapPDPGroup> groups = new HashSet<>(); try { if (XACMLPapServlet.getPAPEngine() != null) { groups = XACMLPapServlet.getPAPEngine().getOnapPDPGroups(); } int pdpCount = 0; for (OnapPDPGroup group : groups) { Set<PDPPolicy> policies = group.getPolicies(); pdpCount += policies.size(); } List<Object> dataList = commonClassDao.getData(PolicyVersion.class); int papCount = dataList.size(); int totalCount = pdpCount + papCount; JSONObject json = new JSONObject(); json.put("papCount", papCount); json.put("pdpCount", pdpCount); json.put("totalCount", totalCount); if (pdpCount > 0 && papCount > 0 && totalCount > 0) { PolicyLogger.info( "Metrics have been found on the Policy Engine for the number of policies on the PAP and PDP."); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", "success"); response.addHeader("operation", "getMetrics"); response.addHeader("metrics", json.toString()); return; } else { String message = "The policy count on the PAP and PDP is 0. Please check the database and file system to correct this error."; response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(errorMsg, message); return; } } catch (Exception e) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Querying the Database: " + e.getMessage(); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error Querying the Database."); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(errorMsg, message); return; } } private  MetricService(); @VisibleForTesting @Autowired protected  MetricService(CommonClassDao commonClassDao); static void doGetPolicyMetrics(HttpServletResponse response);  }
@PrepareForTest({XACMLPapServlet.class}) @Test public void testNegativeGet() { PowerMockito.mockStatic(XACMLPapServlet.class); when(XACMLPapServlet.getPAPEngine()).thenReturn(null); MockHttpServletResponse response = new MockHttpServletResponse(); MetricService.doGetPolicyMetrics(response); assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatusCode()); }
@Test public void testService() { CommonClassDao dao = Mockito.mock(CommonClassDao.class); List<Object> value = new ArrayList<Object>(); when(dao.getData(PolicyVersion.class)).thenReturn(value); MetricService service = new MetricService(dao); assertNotNull(service); MockHttpServletResponse response = new MockHttpServletResponse(); MetricService.doGetPolicyMetrics(response); assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatusCode()); }