Heartbeat implements Runnable { @VisibleForTesting protected String openPdpConnection(URL pdpUrl, OnapPDP pdp) { String newStatus = ""; HttpURLConnection connection = null; try { if (pdpUrl != null) { connection = (HttpURLConnection) pdpUrl.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(heartbeatTimeout); String encoding = CheckPDP.getEncoding(pdp.getId()); if (encoding != null) { connection.setRequestProperty("Authorization", "Basic " + encoding); } connection.connect(); if (connection.getResponseCode() == 204) { newStatus = connection.getHeaderField(XacmlRestProperties.PROP_PDP_HTTP_HEADER_HB); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Heartbeat '" + pdp.getId() + "' status='" + newStatus + "'"); } } else { newStatus = PDPStatus.Status.UNKNOWN.toString(); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " Heartbeat connect response code " + connection.getResponseCode() + ": " + pdp.getId()); } } } catch (UnknownHostException e) { newStatus = PDPStatus.Status.NO_SUCH_HOST.toString(); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET, HEARTBEATSTRING + pdp.getId() + "' NO_SUCH_HOST"); } catch (SocketTimeoutException e) { newStatus = PDPStatus.Status.CANNOT_CONNECT.toString(); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET, HEARTBEATSTRING + pdp.getId() + "' connection timeout"); } catch (ConnectException e) { newStatus = PDPStatus.Status.CANNOT_CONNECT.toString(); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET, HEARTBEATSTRING + pdp.getId() + "' cannot connect"); } catch (Exception e) { newStatus = PDPStatus.Status.UNKNOWN.toString(); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET, HEARTBEATSTRING + pdp.getId() + "' connect exception"); } finally { if (connection != null) connection.disconnect(); } return newStatus; } Heartbeat(PAPPolicyEngine papEngine2); synchronized boolean isHeartBeatRunning(); synchronized void terminate(); @Override void run();  }
@Test public void testOpen() throws MalformedURLException { Heartbeat hb = new Heartbeat(null); OnapPDP pdp = new StdPDP(); assertThatCode(() -> { URL url = new URL("http: hb.openPdpConnection(url, pdp); }).doesNotThrowAnyException(); assertThatCode(() -> { URL url = new URL("http: hb.openPdpConnection(url, pdp); }).doesNotThrowAnyException(); assertThatCode(() -> { URL url = new URL("http: hb.openPdpConnection(url, pdp); }).doesNotThrowAnyException(); }