From f3cd908ceb27c29dd939783742229907cd5b7f74 Mon Sep 17 00:00:00 2001 From: Radoslav Husar Date: Thu, 21 Nov 2024 18:38:31 +0100 Subject: [PATCH 1/2] chore: expand LoggerServlet to accept a log level defaulting to INFO, add a default message 'ping', fix formatter, expand test, update readme (resolves #397) --- README.adoc | 9 +++ .../common/ClusterBenchConstants.java | 8 +++ .../clusterbench/web/debug/LoggerServlet.java | 61 ++++++++++++++----- .../it/wildfly/LoggerServletIT.java | 57 ++++++++++++----- 4 files changed, 104 insertions(+), 31 deletions(-) diff --git a/README.adoc b/README.adoc index 6944e028..14b0dbe0 100644 --- a/README.adoc +++ b/README.adoc @@ -223,6 +223,15 @@ http://localhost:8080/clusterbench/jboss-node-name[/clusterbench/jboss-node-name Servlet which prints out the node name as a value of the `jboss.node.name` system property. +==== LoggerServlet + +http://localhost:8080/clusterbench/log?level=WARN&msg=Example%20warning.[/clusterbench/log?level=WARN&msg=Example%20warning.] + +Servlet which logs the provided message in the `msg` parameter to the server log. +If no message is provided, a simple `ping` message is logged. +The log message level can be optionally provided in the `level` parameter. +Logs at `INFO` level by default. + == Server Configurations The `scripts` directory at the root of the repository contains CLI scripts diff --git a/clusterbench-common/src/main/java/org/jboss/test/clusterbench/common/ClusterBenchConstants.java b/clusterbench-common/src/main/java/org/jboss/test/clusterbench/common/ClusterBenchConstants.java index 08834091..a37a279d 100644 --- a/clusterbench-common/src/main/java/org/jboss/test/clusterbench/common/ClusterBenchConstants.java +++ b/clusterbench-common/src/main/java/org/jboss/test/clusterbench/common/ClusterBenchConstants.java @@ -5,6 +5,9 @@ package org.jboss.test.clusterbench.common; +/** + * @author Radoslav Husar + */ public interface ClusterBenchConstants { String CREATE = "create"; @@ -14,4 +17,9 @@ public interface ClusterBenchConstants { String INVALIDATE = "invalidate"; String CARGOKB = "cargokb"; + + /** + * Standard plain text HTTP response body. + */ + String SUCCESS = "Success"; } diff --git a/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java b/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java index 4245987d..550023af 100644 --- a/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java +++ b/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java @@ -1,25 +1,56 @@ +/* + * Copyright The ClusterBench Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package org.jboss.test.clusterbench.web.debug; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.logging.Logger; +import org.jboss.test.clusterbench.common.ClusterBenchConstants; -@WebServlet("/log") +/** + * Logging servlet that logs provided message in 'msg' parameter. + * Can also be called without parameters as a 'ping' mechanism which + * Optionally, log level can be provided in the 'level' parameter. + * The default level is INFO. + * + * @author Tommasso Borgato + * @author Radoslav Husar + */ +@WebServlet(name = "LoggerServlet", urlPatterns = {"/log"}) public class LoggerServlet extends HttpServlet { - private static final Logger LOG = Logger.getLogger(LoggerServlet.class.getCanonicalName()); - - private void logMarker(String msg) { - LOG.info(msg); - } - - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws IOException { - String msg = request.getParameter("msg"); - logMarker(msg); - response.getWriter().print("Success"); - } + + // Servlet parameters and their default values + public static final String MESSAGE_PARAM = "msg"; + public static final String DEFAULT_MESSAGE = "ping"; + public static final String LEVEL_PARAM = "level"; + public static final Level DEFAULT_LEVEL = Level.INFO; + + private static final Logger LOG = Logger.getLogger(LoggerServlet.class.getCanonicalName()); + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + String requestLevel = request.getParameter(LEVEL_PARAM); + String message = request.getParameter(MESSAGE_PARAM); + + Level level = null; + if (requestLevel != null) { + try { + level = Level.parse(requestLevel); + } catch (Exception ignored) { + // Ignore silently and use default. + } + } + + LOG.log(level == null ? DEFAULT_LEVEL : level, message == null ? DEFAULT_MESSAGE : message); + + response.getWriter().print(ClusterBenchConstants.SUCCESS); + } } diff --git a/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java b/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java index 5f015390..b813372f 100644 --- a/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java +++ b/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java @@ -1,5 +1,11 @@ +/** + * @author Tommasso Borgato + * @author Radoslav Husar + */ package org.jboss.test.clusterbench.it.wildfly; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -10,24 +16,43 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import static org.junit.jupiter.api.Assertions.assertEquals; - +/** + * @author Tommasso Borgato + * @author Radoslav Husar + */ @ExtendWith(ArquillianExtension.class) @RunAsClient public class LoggerServletIT extends AbstractWildFlyIT { - /** - * Verifies log servlet is deployed - */ - @Test - public void test() throws Exception { - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet httpGet = new HttpGet("http://localhost:8080/clusterbench/log"); - try (CloseableHttpResponse response = httpClient.execute(httpGet)) { - assertEquals(200, response.getStatusLine().getStatusCode()); - String responseBody = EntityUtils.toString(response.getEntity()); - assertEquals("Success", responseBody); - } - } - } + /** + * Verifies log servlet is deployed + */ + @Test + public void test() throws Exception { + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpGet httpGet = new HttpGet("http://localhost:8080/clusterbench/log?msg=Basic%20info."); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { + assertEquals(200, response.getStatusLine().getStatusCode()); + String responseBody = EntityUtils.toString(response.getEntity()); + assertEquals("Success", responseBody); + } + + // Test providing a level + httpGet = new HttpGet("http://localhost:8080/clusterbench/log?level=WARN&msg=Test%20warning."); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { + assertEquals(200, response.getStatusLine().getStatusCode()); + String responseBody = EntityUtils.toString(response.getEntity()); + assertEquals("Success", responseBody); + } + + // Test providing non-existent level and no message should default to "INFO: ping". + httpGet = new HttpGet("http://localhost:8080/clusterbench/log?level=WEIRD"); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { + assertEquals(200, response.getStatusLine().getStatusCode()); + String responseBody = EntityUtils.toString(response.getEntity()); + assertEquals("Success", responseBody); + } + } + + } } From ae29117ae1fe20a8049b239b28b072d8d67080fb Mon Sep 17 00:00:00 2001 From: Radoslav Husar Date: Thu, 21 Nov 2024 19:10:43 +0100 Subject: [PATCH 2/2] Fix Tommaso's name. --- .../test/clusterbench/web/debug/JBossNodeNameServlet.java | 2 +- .../org/jboss/test/clusterbench/web/debug/LoggerServlet.java | 2 +- .../jboss/test/clusterbench/it/wildfly/LoggerServletIT.java | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/JBossNodeNameServlet.java b/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/JBossNodeNameServlet.java index 1c76385b..0594630f 100644 --- a/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/JBossNodeNameServlet.java +++ b/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/JBossNodeNameServlet.java @@ -15,7 +15,7 @@ import org.jboss.test.clusterbench.common.ClusterBenchConstants; /** - * @author Tommasso Borgato + * @author Tommaso Borgato * @author Radoslav Husar */ @WebServlet(name = "JBossNodeNameServlet", urlPatterns = { "/jboss-node-name" }) diff --git a/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java b/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java index 550023af..d65c9788 100644 --- a/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java +++ b/clusterbench-ee10-web/src/main/java/org/jboss/test/clusterbench/web/debug/LoggerServlet.java @@ -22,7 +22,7 @@ * Optionally, log level can be provided in the 'level' parameter. * The default level is INFO. * - * @author Tommasso Borgato + * @author Tommaso Borgato * @author Radoslav Husar */ @WebServlet(name = "LoggerServlet", urlPatterns = {"/log"}) diff --git a/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java b/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java index b813372f..182578f3 100644 --- a/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java +++ b/integration-tests/wildfly/src/test/java/org/jboss/test/clusterbench/it/wildfly/LoggerServletIT.java @@ -1,5 +1,5 @@ /** - * @author Tommasso Borgato + * @author Tommaso Borgato * @author Radoslav Husar */ package org.jboss.test.clusterbench.it.wildfly; @@ -17,7 +17,7 @@ import org.junit.jupiter.api.extension.ExtendWith; /** - * @author Tommasso Borgato + * @author Tommaso Borgato * @author Radoslav Husar */ @ExtendWith(ArquillianExtension.class)