Search in sources :

Example 11 with HealthCheckExecutionResult

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

the class ResultJsonSerializer method serialize.

public String serialize(final Result overallResult, final List<HealthCheckExecutionResult> executionResults, final String jsonpCallback, boolean includeDebug) {
    LOG.debug("Sending json response... ");
    JsonObjectBuilder result = Json.createObjectBuilder();
    try {
        result.add("overallResult", overallResult.getStatus().toString());
        JsonArrayBuilder resultsJsonArr = Json.createArrayBuilder();
        for (HealthCheckExecutionResult healthCheckResult : executionResults) {
            resultsJsonArr.add(getJsonForSimpleResult(healthCheckResult, includeDebug));
        }
        result.add("results", resultsJsonArr);
    } catch (JsonException e) {
        LOG.info("Could not serialize health check result: " + e, e);
    }
    StringWriter writer = new StringWriter();
    Json.createGenerator(writer).write(result.build()).close();
    String resultStr = writer.toString();
    if (StringUtils.isNotBlank(jsonpCallback)) {
        resultStr = jsonpCallback + "(" + resultStr + ");";
    }
    return resultStr;
}
Also used : JsonException(javax.json.JsonException) StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 12 with HealthCheckExecutionResult

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

the class HealthCheckExecutorImplTest method testCollectResultsFromFuturesCriticalTimeout.

@Test
public void testCollectResultsFromFuturesCriticalTimeout() throws Exception {
    List<HealthCheckFuture> futures = new LinkedList<HealthCheckFuture>();
    futures.add(future);
    Set<HealthCheckExecutionResult> results = new TreeSet<HealthCheckExecutionResult>();
    when(future.isDone()).thenReturn(false);
    // use an old date now (simulating a future that has run for an hour)
    when(future.getCreatedTime()).thenReturn(new Date(new Date().getTime() - 1000 * 60 * 60));
    healthCheckExecutorImpl.collectResultsFromFutures(futures, results);
    assertEquals(1, results.size());
    HealthCheckExecutionResult result = results.iterator().next();
    verify(future, times(0)).get();
    assertEquals(Result.Status.CRITICAL, result.getHealthCheckResult().getStatus());
    assertEquals(1, getLogEntryCount(result));
}
Also used : TreeSet(java.util.TreeSet) LinkedList(java.util.LinkedList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date) Test(org.junit.Test)

Example 13 with HealthCheckExecutionResult

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

the class HealthCheckExecutorImplTest method testCollectResultsFromFuturesTimeout.

@Test
public void testCollectResultsFromFuturesTimeout() throws Exception {
    // add an earlier result with status ok (that will be shown as part of the log)
    addResultToCache(Status.OK);
    List<HealthCheckFuture> futures = new LinkedList<HealthCheckFuture>();
    futures.add(future);
    Set<HealthCheckExecutionResult> results = new TreeSet<HealthCheckExecutionResult>();
    when(future.isDone()).thenReturn(false);
    // simulating a future that was created 5sec ago
    when(future.getCreatedTime()).thenReturn(new Date(new Date().getTime() - 1000 * 5));
    healthCheckExecutorImpl.collectResultsFromFutures(futures, results);
    verify(future, times(0)).get();
    assertEquals(1, results.size());
    HealthCheckExecutionResult result = results.iterator().next();
    assertEquals(Result.Status.WARN, result.getHealthCheckResult().getStatus());
    // 3 because previous result exists and is part of log
    assertEquals(3, getLogEntryCount(result));
}
Also used : TreeSet(java.util.TreeSet) LinkedList(java.util.LinkedList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date) Test(org.junit.Test)

Example 14 with HealthCheckExecutionResult

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

the class HealthCheckExecutorImplTest method testCollectResultsFromFuturesWarnTimeoutWithPreviousCritical.

@Test
public void testCollectResultsFromFuturesWarnTimeoutWithPreviousCritical() throws Exception {
    // an earlier result with critical
    addResultToCache(Status.CRITICAL);
    List<HealthCheckFuture> futures = new LinkedList<HealthCheckFuture>();
    futures.add(future);
    Set<HealthCheckExecutionResult> results = new TreeSet<HealthCheckExecutionResult>();
    when(future.isDone()).thenReturn(false);
    // simulating a future that was created 5sec ago
    when(future.getCreatedTime()).thenReturn(new Date(new Date().getTime() - 1000 * 5));
    healthCheckExecutorImpl.collectResultsFromFutures(futures, results);
    assertEquals(1, results.size());
    HealthCheckExecutionResult result = results.iterator().next();
    verify(future, times(0)).get();
    // expect CRITICAL because previous result (before timeout) was CRITICAL (and not only WARN)
    assertEquals(Result.Status.CRITICAL, result.getHealthCheckResult().getStatus());
    assertEquals(3, getLogEntryCount(result));
}
Also used : TreeSet(java.util.TreeSet) LinkedList(java.util.LinkedList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date) Test(org.junit.Test)

Example 15 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project acs-aem-commons by Adobe-Consulting-Services.

the class HealthCheckStatusEmailer method resultToPlainText.

/**
 * Gererates the plain-text email sections for sets of Health Check Execution Results.
 *
 * @param title The section title
 * @param results the  Health Check Execution Results to render as plain text
 * @return the String for this section to be embedded in the e-mail
 */
protected String resultToPlainText(final String title, final List<HealthCheckExecutionResult> results) {
    final StringBuilder sb = new StringBuilder();
    sb.append(title);
    sb.append(System.lineSeparator());
    if (results.size() == 0) {
        sb.append("No " + StringUtils.lowerCase(title) + " could be found!");
        sb.append(System.lineSeparator());
    } else {
        sb.append(StringUtils.repeat("-", NUM_DASHES));
        sb.append(System.lineSeparator());
        for (final HealthCheckExecutionResult result : results) {
            sb.append(StringUtils.rightPad("[ " + result.getHealthCheckResult().getStatus().name() + " ]", HEALTH_CHECK_STATUS_PADDING));
            sb.append("  ");
            sb.append(result.getHealthCheckMetadata().getTitle());
            sb.append(System.lineSeparator());
        }
    }
    return sb.toString();
}
Also used : HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Aggregations

HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)34 Result (org.apache.sling.hc.api.Result)18 Test (org.junit.Test)16 Date (java.util.Date)9 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)8 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)7 ArrayList (java.util.ArrayList)6 TreeSet (java.util.TreeSet)6 LinkedList (java.util.LinkedList)5 Matchers.anyString (org.mockito.Matchers.anyString)5 SimpleDateFormat (java.text.SimpleDateFormat)4 DateFormat (java.text.DateFormat)3 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)3 ServiceReference (org.osgi.framework.ServiceReference)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 ResultLog (org.apache.sling.hc.api.ResultLog)2 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)2