Skip to content

Commit

Permalink
Issue #115: Develop HTTP Extension
Browse files Browse the repository at this point in the history
* Added the missing javadoc to the engine and the HTTP Extension
* Fixed a bug on the HTTP extension
  • Loading branch information
CherfaElyes committed Apr 9, 2024
1 parent 0db2850 commit bcfe300
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ public IConfiguration toProtocol(String defaultUsername, char[] defaultPassword)
final ObjectNode configuration = JsonNodeFactory.instance.objectNode();
configuration.set("https", BooleanNode.valueOf(isHttps()));
configuration.set("username", new TextNode(username == null ? defaultUsername : username));
configuration.set("password", new TextNode(username == null ? defaultPassword.toString() : password.toString()));
configuration.set(
"password",
new TextNode(username == null ? String.valueOf(defaultPassword) : String.valueOf(password))
);
configuration.set("port", new IntNode(getOrDeducePortNumber()));
configuration.set("timeout", new TextNode(timeout));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sentrysoftware.metricshub.cli.service.protocol;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mockStatic;

Expand Down Expand Up @@ -56,19 +55,21 @@ void testToProtocol() throws InvalidConfigurationException {
HttpTestConfiguration result = (HttpTestConfiguration) httpConfigCli.toProtocol(null, null);

assertNotNull(result);
assertEquals(expected.getHttps(), result.getHttps());
assertEquals(expected.getTimeout(), result.getTimeout());
assertEquals(expected.getPort(), result.getPort());
assertEquals(expected, result);
// assertEquals(expected.getHttps(), result.getHttps());
// assertEquals(expected.getTimeout(), result.getTimeout());
// assertEquals(expected.getPort(), result.getPort());

httpConfigCli = new HttpConfigCli();
httpConfigCli.setTimeout(timeout);
httpConfigCli.setHttpOrHttps(httpOrHttps);

result = (HttpTestConfiguration) httpConfigCli.toProtocol(username, password);
assertNotNull(result);
assertEquals(expected.getHttps(), result.getHttps());
assertEquals(expected.getTimeout(), result.getTimeout());
assertEquals(expected.getPort(), result.getPort());
assertEquals(expected, result);
// assertEquals(expected.getHttps(), result.getHttps());
// assertEquals(expected.getTimeout(), result.getTimeout());
// assertEquals(expected.getPort(), result.getPort());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@
import java.util.concurrent.TimeoutException;

/**
* Helper class for Thread operations
* Provides utility methods for thread management, including executing tasks
* with a specified timeout. This class is part of the MetricsHub Engine's
* common helper utilities.
*/
public class ThreadHelper {

/**
* Run the given {@link Callable} using the passed timeout in seconds.
* Executes a {@link Callable} task with a specified timeout. This method
* creates a single-threaded executor to run the provided task. If the task
* completes within the given timeout, its result is returned. Otherwise, a
* {@link TimeoutException} is thrown.
*
* @param <T>
* @param callable
* @param timeout
* @return {@link T} result returned by the callable.
* @throws InterruptedException
* @throws ExecutionException
* @throws TimeoutException
* @param <T> the type of the result returned by the {@code callable}
* @param callable the task to be executed
* @param timeout the maximum time to wait for the task to complete, in seconds
* @return the result of the executed task
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws ExecutionException if the computation threw an exception
* @throws TimeoutException if the wait timed out
*/
public static <T> T execute(final Callable<T> callable, final long timeout)
throws InterruptedException, ExecutionException, TimeoutException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,30 @@
import lombok.EqualsAndHashCode;
import lombok.NonNull;

/**
* Processes template variables within JSON nodes by substituting placeholders
* with actual values from a set of connector variables. This processor extends
* the {@link AbstractNodeProcessor} to allow for chained node processing in the
* context of JSON data parsing and manipulation within the MetricsHub Engine.
*
* <p>It leverages regular expressions to identify placeholders matching a specific
* pattern and replaces them with corresponding values provided in a map of connector
* variables. This mechanism facilitates dynamic data manipulation based on the
* configuration provided at runtime.</p>
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class TemplateVariableProcessor extends AbstractNodeProcessor {

@NonNull
private Map<String, String> connectorVariables = new HashMap<>();

/**
* Constructs a new {@code TemplateVariableProcessor} with the specified map of connector
* variables and the next processor in the chain. This constructor initializes the processor
* with a set of key-value pairs representing the variables to be substituted within JSON
* templates and optionally, a next {@link AbstractNodeProcessor} for further processing.
*/
@Builder
public TemplateVariableProcessor(@NonNull Map<String, String> connectorVariables, AbstractNodeProcessor next) {
super(next);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
import org.sentrysoftware.metricshub.engine.strategy.utils.PslUtils;
import org.sentrysoftware.metricshub.engine.telemetry.TelemetryManager;

/**
* This class is responsible for executing HTTP tests based on the provided
* {@link HttpCriterion} and generating {@link CriterionTestResult} based on the
* outcome of these tests. It utilizes an {@link HttpRequestExecutor} to perform
* the actual HTTP requests.
*/
@Slf4j
@AllArgsConstructor
public class HttpCriterionProcessor {
Expand All @@ -46,6 +52,7 @@ public class HttpCriterionProcessor {
*
* @param httpCriterion The HTTP criterion to process.
* @param connectorId The Id of the connector
* @param telemetryManager The telemetry manager providing access to host configuration.
* @return New {@link CriterionTestResult} instance.
*/
@WithSpan("Criterion HTTP Exec")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
import org.sentrysoftware.metricshub.engine.strategy.utils.RetryOperation;
import org.sentrysoftware.metricshub.engine.telemetry.TelemetryManager;

/**
* Executes HTTP requests configured through {@link HttpRequest} objects.
* This executor supports dynamic substitution of request parameters,
* authentication handling, and logging of request execution details.
*/
@Slf4j
public class HttpRequestExecutor {

Expand All @@ -59,6 +64,7 @@ public class HttpRequestExecutor {
*
* @param httpRequest The {@link HttpRequest} values.
* @param logMode Whether or not logging is enabled.
* @param telemetryManager The telemetry manager providing access to host configuration.
* @return The result of the execution of the given HTTP request.
*/
@WithSpan("HTTP")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import static org.sentrysoftware.metricshub.engine.common.helpers.KnownMonitorType.HOST;
import static org.sentrysoftware.metricshub.extension.http.HttpExtension.HTTP_UP_METRIC;

import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -126,4 +131,38 @@ void testIsSupportedConfigurationType() {
assertTrue(httpExtension.isSupportedConfigurationType("http"));
assertFalse(httpExtension.isSupportedConfigurationType("snmp"));
}

@Test
void testBuildConfiguration() throws InvalidConfigurationException {
final ObjectNode configuration = JsonNodeFactory.instance.objectNode();
configuration.set("https", BooleanNode.FALSE);
configuration.set("username", new TextNode("username"));
configuration.set("password", new TextNode("password"));
configuration.set("port", new IntNode(443));
configuration.set("timeout", new TextNode("120"));

assertEquals(
HttpConfiguration
.builder()
.https(false)
.username("username")
.password("password".toCharArray())
.port(443)
.timeout(120L)
.build(),
httpExtension.buildConfiguration("http", configuration, value -> value)
);

assertEquals(
HttpConfiguration
.builder()
.https(false)
.username("username")
.password("password".toCharArray())
.port(443)
.timeout(120L)
.build(),
httpExtension.buildConfiguration("http", configuration, null)
);
}
}

0 comments on commit bcfe300

Please sign in to comment.