Search in sources :

Example 6 with HealthCheckExecutionOptions

use of org.apache.sling.hc.api.execution.HealthCheckExecutionOptions in project sling by apache.

the class HealthCheckMBeanTest method testBean.

@Test
public void testBean() throws Exception {
    final ServiceReference ref = new ServiceReference() {

        @Override
        public boolean isAssignableTo(Bundle bundle, String className) {
            return false;
        }

        @Override
        public Bundle[] getUsingBundles() {
            return null;
        }

        @Override
        public String[] getPropertyKeys() {
            return null;
        }

        @Override
        public Object getProperty(String key) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public int compareTo(Object reference) {
            return 0;
        }
    };
    final HealthCheckMBean mbean = new HealthCheckMBean(ref, new ExtendedHealthCheckExecutor() {

        @SuppressWarnings("deprecation")
        @Override
        public List<HealthCheckExecutionResult> execute(String... tags) {
            return null;
        }

        @Override
        public HealthCheckExecutionResult execute(ServiceReference ref) {
            // TODO Auto-generated method stub
            return new HealthCheckExecutionResult() {

                @Override
                public Result getHealthCheckResult() {
                    // TODO Auto-generated method stub
                    return testHealthCheck.execute();
                }

                @Override
                public HealthCheckMetadata getHealthCheckMetadata() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public Date getFinishedAt() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public long getElapsedTimeInMs() {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public boolean hasTimedOut() {
                    // TODO Auto-generated method stub
                    return false;
                }
            };
        }

        @SuppressWarnings("deprecation")
        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckExecutionOptions options, String... tags) {
            return null;
        }

        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckSelector selector) {
            return null;
        }

        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckSelector selector, HealthCheckExecutionOptions options) {
            return null;
        }
    });
    final ObjectName name = new ObjectName(OBJECT_NAME);
    jmxServer.registerMBean(mbean, name);
    try {
        resultOk = true;
        assertJmxValue(OBJECT_NAME, "ok", "true", true);
        Thread.sleep(1500);
        resultOk = false;
        assertJmxValue(OBJECT_NAME, "ok", "true", false);
        Thread.sleep(1500);
        assertJmxValue(OBJECT_NAME, "log", "contains message=Result is not ok!", true);
    } finally {
        jmxServer.unregisterMBean(name);
    }
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) Bundle(org.osgi.framework.Bundle) ExtendedHealthCheckExecutor(org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor) Date(java.util.Date) ServiceReference(org.osgi.framework.ServiceReference) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ObjectName(javax.management.ObjectName) HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) List(java.util.List) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 7 with HealthCheckExecutionOptions

use of org.apache.sling.hc.api.execution.HealthCheckExecutionOptions in project sling by apache.

the class HealthCheckExecutorServletTest method testDoGetTxt.

@Test
public void testDoGetTxt() throws ServletException, IOException {
    final String testTag = "testTag";
    doReturn(testTag).when(request).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
    doReturn(HealthCheckExecutorServlet.FORMAT_TXT).when(request).getParameter(HealthCheckExecutorServlet.PARAM_FORMAT.name);
    doReturn("true").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
    int timeout = 5000;
    doReturn(timeout + "").when(request).getParameter(HealthCheckExecutorServlet.PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
    final List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.WARN);
    HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
    options.setCombineTagsWithOr(true);
    options.setOverrideGlobalTimeout(timeout);
    doReturn(executionResults).when(healthCheckExecutor).execute(selector(new String[] { testTag }, new String[0]), eq(options));
    healthCheckExecutorServlet.doGet(request, response);
    verifyZeroInteractions(htmlSerializer);
    verifyZeroInteractions(jsonSerializer);
    verifyZeroInteractions(verboseTxtSerializer);
    verify(txtSerializer).serialize(resultEquals(new Result(Result.Status.WARN, "Overall Status WARN")));
}
Also used : HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) Matchers.anyString(org.mockito.Matchers.anyString) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 8 with HealthCheckExecutionOptions

use of org.apache.sling.hc.api.execution.HealthCheckExecutionOptions in project sling by apache.

the class HealthCheckExecutorServletTest method testDoGetHtml.

@Test
public void testDoGetHtml() throws ServletException, IOException {
    final String testTag = "testTag";
    doReturn(testTag).when(request).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
    doReturn("false").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
    final List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.CRITICAL);
    doReturn(executionResults).when(healthCheckExecutor).execute(selector(new String[] { testTag }, new String[0]), eq(new HealthCheckExecutionOptions()));
    healthCheckExecutorServlet.doGet(request, response);
    verifyZeroInteractions(jsonSerializer);
    verifyZeroInteractions(txtSerializer);
    verifyZeroInteractions(verboseTxtSerializer);
    verify(htmlSerializer).serialize(resultEquals(new Result(Result.Status.CRITICAL, "Overall Status CRITICAL")), eq(executionResults), contains("Supported URL parameters"), eq(false));
}
Also used : HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) Matchers.anyString(org.mockito.Matchers.anyString) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 9 with HealthCheckExecutionOptions

use of org.apache.sling.hc.api.execution.HealthCheckExecutionOptions in project sling by apache.

the class HealthCheckExecutorServlet method doGet.

protected void doGet(final HttpServletRequest request, final HttpServletResponse response, final String format) throws ServletException, IOException {
    HealthCheckSelector selector = HealthCheckSelector.empty();
    String pathInfo = request.getPathInfo();
    String pathTokensStr = StringUtils.removeStart(splitFormat(pathInfo)[0], "/");
    List<String> tags = new ArrayList<String>();
    List<String> names = new ArrayList<String>();
    if (StringUtils.isNotBlank(pathTokensStr)) {
        String[] pathTokens = pathTokensStr.split(PARAM_SPLIT_REGEX);
        for (String pathToken : pathTokens) {
            if (pathToken.indexOf(' ') >= 0) {
                // token contains space. assume it is a name
                names.add(pathToken);
            } else {
                tags.add(pathToken);
            }
        }
    }
    if (tags.size() == 0) {
        // if not provided via path use parameter or default
        tags = Arrays.asList(StringUtils.defaultIfEmpty(request.getParameter(PARAM_TAGS.name), "").split(PARAM_SPLIT_REGEX));
    }
    selector.withTags(tags.toArray(new String[0]));
    if (names.size() == 0) {
        // if not provided via path use parameter or default
        names = Arrays.asList(StringUtils.defaultIfEmpty(request.getParameter(PARAM_NAMES.name), "").split(PARAM_SPLIT_REGEX));
    }
    selector.withNames(names.toArray(new String[0]));
    final Boolean includeDebug = Boolean.valueOf(request.getParameter(PARAM_INCLUDE_DEBUG.name));
    final Map<Result.Status, Integer> statusMapping = request.getParameter(PARAM_HTTP_STATUS.name) != null ? getStatusMapping(request.getParameter(PARAM_HTTP_STATUS.name)) : null;
    HealthCheckExecutionOptions executionOptions = new HealthCheckExecutionOptions();
    executionOptions.setCombineTagsWithOr(Boolean.valueOf(StringUtils.defaultString(request.getParameter(PARAM_COMBINE_TAGS_WITH_OR.name), "true")));
    executionOptions.setForceInstantExecution(Boolean.valueOf(request.getParameter(PARAM_FORCE_INSTANT_EXECUTION.name)));
    String overrideGlobalTimeoutVal = request.getParameter(PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
    if (StringUtils.isNumeric(overrideGlobalTimeoutVal)) {
        executionOptions.setOverrideGlobalTimeout(Integer.valueOf(overrideGlobalTimeoutVal));
    }
    List<HealthCheckExecutionResult> executionResults = this.healthCheckExecutor.execute(selector, executionOptions);
    Result.Status mostSevereStatus = Result.Status.DEBUG;
    for (HealthCheckExecutionResult executionResult : executionResults) {
        Status status = executionResult.getHealthCheckResult().getStatus();
        if (status.ordinal() > mostSevereStatus.ordinal()) {
            mostSevereStatus = status;
        }
    }
    Result overallResult = new Result(mostSevereStatus, "Overall status " + mostSevereStatus);
    sendNoCacheHeaders(response);
    if (statusMapping != null) {
        Integer httpStatus = statusMapping.get(overallResult.getStatus());
        response.setStatus(httpStatus);
    }
    if (FORMAT_HTML.equals(format)) {
        sendHtmlResponse(overallResult, executionResults, request, response, includeDebug);
    } else if (FORMAT_JSON.equals(format)) {
        sendJsonResponse(overallResult, executionResults, null, response, includeDebug);
    } else if (FORMAT_JSONP.equals(format)) {
        String jsonpCallback = StringUtils.defaultIfEmpty(request.getParameter(PARAM_JSONP_CALLBACK.name), JSONP_CALLBACK_DEFAULT);
        sendJsonResponse(overallResult, executionResults, jsonpCallback, response, includeDebug);
    } else if (StringUtils.endsWith(format, FORMAT_TXT)) {
        sendTxtResponse(overallResult, response, StringUtils.equals(format, FORMAT_VERBOSE_TXT), executionResults, includeDebug);
    } else {
        response.setContentType("text/plain");
        response.getWriter().println("Invalid format " + format + " - supported formats: html|json|jsonp|txt|verbose.txt");
    }
}
Also used : Status(org.apache.sling.hc.api.Result.Status) ArrayList(java.util.ArrayList) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Status(org.apache.sling.hc.api.Result.Status) HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Aggregations

HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)8 Result (org.apache.sling.hc.api.Result)7 Test (org.junit.Test)5 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ArrayList (java.util.ArrayList)2 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)2 PrintWriter (java.io.PrintWriter)1 Date (java.util.Date)1 List (java.util.List)1 ObjectName (javax.management.ObjectName)1 Status (org.apache.sling.hc.api.Result.Status)1 ExtendedHealthCheckExecutor (org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor)1 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)1 Before (org.junit.Before)1 Bundle (org.osgi.framework.Bundle)1 ServiceReference (org.osgi.framework.ServiceReference)1