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);
}
}
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")));
}
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));
}
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");
}
}
Aggregations