Skip to content

Commit

Permalink
extract SDK > cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DC2-DanielKrueger committed Apr 29, 2024
1 parent ebe007d commit 4bef4bc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<HttpAdapterConfig> 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();
Expand Down Expand Up @@ -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<HttpResponse<String>> responseFuture =
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ public class HttpProtocolAdapterFactory implements ProtocolAdapterFactory<HttpAd
}

@Override
public @NotNull ProtocolAdapter createAdapter(@NotNull final ProtocolAdapterInformation adapterInformation, @NotNull final ProtocolAdapterInput<HttpAdapterConfig> input) {
return new HttpProtocolAdapter(adapterInformation, input.getConfig(), input.getMetricRegistry(), input.getVersion(), input
);
public @NotNull ProtocolAdapter createAdapter(
@NotNull final ProtocolAdapterInformation adapterInformation,
@NotNull final ProtocolAdapterInput<HttpAdapterConfig> 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<String, Object> unconvertConfigObject(final @NotNull ObjectMapper objectMapper, final @NotNull CustomConfig config) {
public Map<String, Object> unconvertConfigObject(
final @NotNull ObjectMapper objectMapper,
final @NotNull CustomConfig config) {
return HttpConfigConverter.unconvertConfig(objectMapper, config);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -42,13 +40,15 @@ class HttpProtocolAdapterTest {

private @NotNull HttpProtocolAdapter httpProtocolAdapter;

private final @NotNull ProtocolAdapterInput protocolAdapterInput = mock();
@SuppressWarnings("unchecked")
private final @NotNull ProtocolAdapterInput<HttpAdapterConfig> 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
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

/**
Expand All @@ -46,14 +49,15 @@ public void testHttpAdapterReadsEmptyHeaders() throws IOException {
File configFile = loadTestConfigFile(tempDir, "http-config-empty-header.xml");
HiveMQConfigEntity configEntity = loadConfig(configFile);
Map<String, Object> 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
Expand All @@ -62,14 +66,15 @@ public void testHttpAdapterReadsPopulatedHeaders() throws IOException {
File configFile = loadTestConfigFile(tempDir, "http-config-with-headers.xml");
HiveMQConfigEntity configEntity = loadConfig(configFile);
Map<String, Object> 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) {
Expand Down

0 comments on commit 4bef4bc

Please sign in to comment.