From 4bef4bcb2a3189bf1dc60657ceeda2251ca7179d Mon Sep 17 00:00:00 2001 From: Daniel Krueger Date: Mon, 29 Apr 2024 16:17:17 +0200 Subject: [PATCH] extract SDK > cleanup --- .../adapters/http/HttpProtocolAdapter.java | 25 ++++--------- .../http/HttpProtocolAdapterFactory.java | 15 +++++--- .../http/HttpProtocolAdapterTest.java | 37 ++++++++++++------- .../adapters/http/TestHttpAdapterConfig.java | 19 ++++++---- 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapter.java b/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapter.java index 8378d32749..49832600a4 100644 --- a/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapter.java +++ b/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapter.java @@ -15,7 +15,6 @@ */ package com.hivemq.edge.adapters.http; -import com.codahale.metrics.MetricRegistry; import com.fasterxml.jackson.databind.ObjectMapper; import com.hivemq.edge.adapters.http.model.HttpData; import com.hivemq.edge.modules.adapters.PollingProtocolAdapter; @@ -29,7 +28,6 @@ import com.hivemq.edge.modules.api.adapters.ProtocolAdapterInformation; import com.hivemq.edge.modules.api.adapters.ProtocolAdapterState; import com.hivemq.edge.modules.api.events.model.Event; -import com.hivemq.edge.modules.api.events.model.EventBuilder; import com.hivemq.edge.modules.config.AdapterSubscription; import com.hivemq.extension.sdk.api.annotations.NotNull; import com.hivemq.extension.sdk.api.annotations.Nullable; @@ -66,7 +64,6 @@ public class HttpProtocolAdapter implements PollingProtocolAdapter { private final @NotNull ProtocolAdapterInformation adapterInformation; private final @NotNull HttpAdapterConfig adapterConfig; - private final @NotNull MetricRegistry metricRegistry; private final @NotNull String version; private final @NotNull ProtocolAdapterState protocolAdapterState; private final @NotNull ModuleServices moduleServices; @@ -80,14 +77,10 @@ public class HttpProtocolAdapter implements PollingProtocolAdapter { public HttpProtocolAdapter( final @NotNull ProtocolAdapterInformation adapterInformation, - final @NotNull HttpAdapterConfig adapterConfig, - final @NotNull MetricRegistry metricRegistry, - final @NotNull String version, final @NotNull ProtocolAdapterInput input) { this.adapterInformation = adapterInformation; - this.adapterConfig = adapterConfig; - this.metricRegistry = metricRegistry; - this.version = version; + this.adapterConfig = input.getConfig(); + this.version = input.getVersion(); this.protocolAdapterState = input.getProtocolAdapterState(); this.moduleServices = input.moduleServices(); this.adapterFactories = input.adapterFactories(); @@ -231,7 +224,7 @@ private static boolean isSuccessStatusCode(final int statusCode) { builder.timeout(Duration.ofSeconds(timeout)); builder.setHeader(HttpConstants.USER_AGENT_HEADER, String.format(CLIENT_AGENT_PROPERTY_VALUE, version)); if (config.getHttpHeaders() != null && !config.getHttpHeaders().isEmpty()) { - config.getHttpHeaders().stream().forEach(hv -> builder.setHeader(hv.getName(), hv.getValue())); + config.getHttpHeaders().forEach(hv -> builder.setHeader(hv.getName(), hv.getValue())); } HttpRequest request = builder.build(); CompletableFuture> responseFuture = @@ -259,7 +252,10 @@ private static boolean isSuccessStatusCode(final int statusCode) { log.debug("Invalid JSON data was [{}]", bodyData); } moduleServices.eventService() - .fireEvent(eventBuilder(Event.SEVERITY.WARN).withMessage(String.format( + .fireEvent(adapterFactories.eventBuilderFactory() + .create(adapterConfig.getId(), adapterInformation.getProtocolId()) + .withSeverity(Event.SEVERITY.WARN) + .withMessage(String.format( "Http response on adapter '%s' could not be parsed as JSON data.", adapterConfig.getId())).build()); throw new RuntimeException("unable to parse JSON data from HTTP response"); @@ -349,11 +345,4 @@ public void checkServerTrusted( } } - private @NotNull EventBuilder eventBuilder( - final @NotNull Event.SEVERITY severity) { - return adapterFactories.eventBuilderFactory() - .create(adapterConfig.getId(), adapterInformation.getProtocolId()) - .withSeverity(severity); - } - } diff --git a/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java b/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java index 5fb6e7560f..de24d465c1 100644 --- a/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java +++ b/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java @@ -36,18 +36,23 @@ public class HttpProtocolAdapterFactory implements ProtocolAdapterFactory input) { - return new HttpProtocolAdapter(adapterInformation, input.getConfig(), input.getMetricRegistry(), input.getVersion(), input - ); + public @NotNull ProtocolAdapter createAdapter( + @NotNull final ProtocolAdapterInformation adapterInformation, + @NotNull final ProtocolAdapterInput input) { + return new HttpProtocolAdapter(adapterInformation, input); } @Override - public @NotNull HttpAdapterConfig convertConfigObject(final @NotNull ObjectMapper objectMapper, final @NotNull Map<@NotNull String, Object> config) { + public @NotNull HttpAdapterConfig convertConfigObject( + final @NotNull ObjectMapper objectMapper, + final @NotNull Map<@NotNull String, Object> config) { return HttpConfigConverter.convertConfig(objectMapper, config); } @Override - public Map unconvertConfigObject(final @NotNull ObjectMapper objectMapper, final @NotNull CustomConfig config) { + public Map unconvertConfigObject( + final @NotNull ObjectMapper objectMapper, + final @NotNull CustomConfig config) { return HttpConfigConverter.unconvertConfig(objectMapper, config); } diff --git a/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterTest.java b/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterTest.java index 6e2702be56..5ecdce8aea 100644 --- a/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterTest.java +++ b/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterTest.java @@ -1,6 +1,5 @@ package com.hivemq.edge.adapters.http; -import com.codahale.metrics.MetricRegistry; import com.google.common.collect.ImmutableMap; import com.hivemq.edge.adapters.http.model.HttpData; import com.hivemq.edge.modules.adapters.data.DataPoint; @@ -32,7 +31,6 @@ class HttpProtocolAdapterTest { - private final @NotNull MetricRegistry metricRegistry = new MetricRegistry(); private final @NotNull HttpAdapterConfig httpAdapterConfig = new HttpAdapterConfig("adapterId"); private final @NotNull ProtocolAdapterPublishService publishService = mock(ProtocolAdapterPublishService.class); private final @NotNull ModuleServices moduleServices = mock(ModuleServices.class); @@ -42,13 +40,15 @@ class HttpProtocolAdapterTest { private @NotNull HttpProtocolAdapter httpProtocolAdapter; - private final @NotNull ProtocolAdapterInput protocolAdapterInput = mock(); + @SuppressWarnings("unchecked") + private final @NotNull ProtocolAdapterInput protocolAdapterInput = mock(); @BeforeEach void setUp() { when(protocolAdapterInput.moduleServices()).thenReturn(moduleServices); - + when(protocolAdapterInput.getConfig()).thenReturn(httpAdapterConfig); + when(protocolAdapterInput.getVersion()).thenReturn("someVersion"); when(moduleServices.adapterPublishService()).thenReturn(publishService); when(moduleServices.eventService()).thenReturn(mock(EventService.class)); //noinspection unchecked @@ -59,17 +59,17 @@ void setUp() { protocolAdapterPublishBuilder.withAdapter(httpProtocolAdapter); when(publishService.publish()).thenReturn(protocolAdapterPublishBuilder); - httpProtocolAdapter = new HttpProtocolAdapter(HttpProtocolAdapterInformation.INSTANCE, - httpAdapterConfig, - metricRegistry, - "someVersion", - protocolAdapterInput); + httpProtocolAdapter = new HttpProtocolAdapter(HttpProtocolAdapterInformation.INSTANCE, protocolAdapterInput); } @Test void test_captureDataSample_expectedPayloadPresent() throws ExecutionException, InterruptedException { final AdapterSubscription subscription = new AdapterSubscriptionImpl("topic", 2, null); - final HttpData httpData = new HttpData(new AdapterSubscriptionImpl(), "http://localhost:8080", 200, "text/plain", new TestDataPointFactory()); + final HttpData httpData = new HttpData(subscription, + "http://localhost:8080", + 200, + "text/plain", + new TestDataPointFactory()); httpData.addDataPoint(RESPONSE_DATA, "hello world"); httpProtocolAdapter.poll().get(); @@ -82,8 +82,16 @@ void test_captureDataSample_expectedPayloadPresent() throws ExecutionException, @Test void test_captureDataSample_errorPayloadFormat() throws ExecutionException, InterruptedException { final AdapterSubscription subscription = new AdapterSubscriptionImpl("topic", 2, null); - final HttpData httpData = new HttpData(new AdapterSubscriptionImpl(),"http://localhost:8080", 200, "text/plain", new TestDataPointFactory()); - final HttpData payload = new HttpData(new AdapterSubscriptionImpl(),"http://localhost:8080", 404, "text/plain", new TestDataPointFactory()); + final HttpData httpData = new HttpData(subscription, + "http://localhost:8080", + 200, + "text/plain", + new TestDataPointFactory()); + final HttpData payload = new HttpData(subscription, + "http://localhost:8080", + 404, + "text/plain", + new TestDataPointFactory()); httpData.addDataPoint(RESPONSE_DATA, payload); httpProtocolAdapter.poll().get(); @@ -102,11 +110,12 @@ private static class TestDataPointFactory implements DataPointFactory { public @NotNull DataPoint create(final @NotNull String tagName, final @NotNull Object tagValue) { return new DataPoint() { @Override - public Object getTagValue() { + public @NotNull Object getTagValue() { return tagValue; } + @Override - public String getTagName() { + public @NotNull String getTagName() { return tagName; } }; diff --git a/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/TestHttpAdapterConfig.java b/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/TestHttpAdapterConfig.java index 88e9b6dd9c..9fa71eab08 100644 --- a/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/TestHttpAdapterConfig.java +++ b/modules/hivemq-edge-module-http/src/test/java/com/hivemq/edge/adapters/http/TestHttpAdapterConfig.java @@ -21,7 +21,6 @@ import com.hivemq.extension.sdk.api.annotations.NotNull; import com.hivemq.protocols.ProtocolAdapterUtils; import org.apache.commons.io.FileUtils; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -30,6 +29,10 @@ import java.io.InputStream; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.Mockito.mock; /** @@ -46,14 +49,15 @@ public void testHttpAdapterReadsEmptyHeaders() throws IOException { File configFile = loadTestConfigFile(tempDir, "http-config-empty-header.xml"); HiveMQConfigEntity configEntity = loadConfig(configFile); Map adapters = configEntity.getProtocolAdapterConfig(); - Assertions.assertNotNull(adapters.get("http"), "Adapter map should contain http adapter config"); - Assertions.assertTrue(adapters.get("http") instanceof Map, "Adapter should be an instance of a List"); + assertNotNull(adapters.get("http"), "Adapter map should contain http adapter config"); + assertInstanceOf(Map.class, adapters.get("http"), "Adapter should be an instance of a List"); + //noinspection rawtypes Map map = (Map) adapters.get("http"); ObjectMapper mapper = new ObjectMapper(); mapper = ProtocolAdapterUtils.createProtocolAdapterMapper(mapper); HttpProtocolAdapterFactory httpProtocolAdapterFactory = new HttpProtocolAdapterFactory(); HttpAdapterConfig config = httpProtocolAdapterFactory.convertConfigObject(mapper, map); - Assertions.assertNull(config.getHttpHeaders(), "Header array should be null to match coercion"); + assertNull(config.getHttpHeaders(), "Header array should be null to match coercion"); } @Test @@ -62,14 +66,15 @@ public void testHttpAdapterReadsPopulatedHeaders() throws IOException { File configFile = loadTestConfigFile(tempDir, "http-config-with-headers.xml"); HiveMQConfigEntity configEntity = loadConfig(configFile); Map adapters = configEntity.getProtocolAdapterConfig(); - Assertions.assertNotNull(adapters.get("http"), "Adapter map should contain http adapter config"); - Assertions.assertTrue(adapters.get("http") instanceof Map, "Adapter should be an instance of a List"); + assertNotNull(adapters.get("http"), "Adapter map should contain http adapter config"); + assertInstanceOf(Map.class, adapters.get("http"), "Adapter should be an instance of a List"); + //noinspection rawtypes Map map = (Map) adapters.get("http"); ObjectMapper mapper = new ObjectMapper(); mapper = ProtocolAdapterUtils.createProtocolAdapterMapper(mapper); HttpProtocolAdapterFactory httpProtocolAdapterFactory = new HttpProtocolAdapterFactory(); HttpAdapterConfig config = httpProtocolAdapterFactory.convertConfigObject(mapper, map); - Assertions.assertEquals(2, config.getHttpHeaders().size(), "Header array should contain 2 elements"); + assertEquals(2, config.getHttpHeaders().size(), "Header array should contain 2 elements"); } protected @NotNull HiveMQConfigEntity loadConfig(@NotNull final File configFile) {