Skip to content

Commit

Permalink
re-add ID into configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
DC2-DanielKrueger committed Nov 26, 2024
1 parent f2358d6 commit 26c16cf
Show file tree
Hide file tree
Showing 30 changed files with 202 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SimulationSpecificAdapterConfig

private static final @NotNull String ID_REGEX = "^([a-zA-Z_0-9-_])*$";

@JsonProperty(value = "id", required = true)
@JsonProperty(value = "id", required = true, access = JsonProperty.Access.WRITE_ONLY)
@ModuleConfigField(title = "Identifier",
description = "Unique identifier for this protocol adapter",
format = ModuleConfigField.FieldType.IDENTIFIER,
Expand Down Expand Up @@ -81,7 +81,9 @@ public class SimulationSpecificAdapterConfig

@JsonCreator
public SimulationSpecificAdapterConfig(
@JsonProperty(value = "id", required = true) final @NotNull String id,
@JsonProperty(value = "id",
required = true,
access = JsonProperty.Access.WRITE_ONLY) final @NotNull String id,
@JsonProperty(value = "simulationToMqtt") final @Nullable SimulationToMqttConfig simulationToMqttConfig,
@JsonProperty("minValue") final @Nullable Integer minValue,
@JsonProperty("maxValue") final @Nullable Integer maxValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public EipProtocolAdapterFactory(@NotNull final ProtocolAdapterFactoryInput inpu
legacyEipAdapterConfig.getPublishChangedDataOnly(),
eipToMqttMappings);

final EipSpecificAdapterConfig eipSpecificAdapterConfig = new EipSpecificAdapterConfig(
final EipSpecificAdapterConfig eipSpecificAdapterConfig = new EipSpecificAdapterConfig(legacyEipAdapterConfig.getId(),
legacyEipAdapterConfig.getPort(),
legacyEipAdapterConfig.getHost(),
legacyEipAdapterConfig.getBackplane(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@
import java.util.List;
import java.util.Objects;

public class EipSpecificAdapterConfig implements ProtocolSpecificAdapterConfig , AdapterConfigWithPollingContexts {
public class EipSpecificAdapterConfig implements ProtocolSpecificAdapterConfig, AdapterConfigWithPollingContexts {

private static final int PORT_MIN = 1;
private static final int PORT_MAX = 65535;

private static final @NotNull String ID_REGEX = "^([a-zA-Z_0-9-_])*$";

@JsonProperty(value = "id", required = true, access = JsonProperty.Access.WRITE_ONLY)
@ModuleConfigField(title = "Identifier",
description = "Unique identifier for this protocol adapter",
format = ModuleConfigField.FieldType.IDENTIFIER,
required = true,
stringPattern = ID_REGEX,
stringMinLength = 1,
stringMaxLength = 1024)
private final @NotNull String id;

@JsonProperty(value = "host", required = true)
@ModuleConfigField(title = "Host",
description = "IP Address or hostname of the device you wish to connect to",
Expand Down Expand Up @@ -65,16 +77,20 @@ public class EipSpecificAdapterConfig implements ProtocolSpecificAdapterConfig ,

@JsonCreator
public EipSpecificAdapterConfig(
@JsonProperty(value = "id",
required = true,
access = JsonProperty.Access.WRITE_ONLY) final @NotNull String id,
@JsonProperty(value = "port", required = true) final int port,
@JsonProperty(value = "host", required = true) final @NotNull String host,
@JsonProperty(value = "backplane") final @Nullable Integer backplane,
@JsonProperty(value = "slot") final @Nullable Integer slot,
@JsonProperty(value = "eipToMqtt") final @Nullable EipToMqttConfig eipToMqttConfig) {
this.id = id;
this.host = host;
this.port = port;
this.backplane = Objects.requireNonNullElse(backplane, 1);
this.slot = Objects.requireNonNullElse(slot, 0);
if(eipToMqttConfig == null) {
if (eipToMqttConfig == null) {
this.eipToMqttConfig = new EipToMqttConfig(null, null, null, null);
} else {
this.eipToMqttConfig = eipToMqttConfig;
Expand Down Expand Up @@ -104,8 +120,8 @@ public int getSlot() {

@Override
public @NotNull List<? extends PollingContext> getPollingContexts() {
if (eipToMqttConfig==null){
return List.of();
if (eipToMqttConfig == null) {
return List.of();
}
return eipToMqttConfig.getMappings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void test_parameterized(
List.of());

final EipSpecificAdapterConfig config =
new EipSpecificAdapterConfig(44818, HOST, 1, 0, new EipToMqttConfig(1000, 10, true, List.of(eipToMqttMapping)));
new EipSpecificAdapterConfig("id",44818, HOST, 1, 0, new EipToMqttConfig(1000, 10, true, List.of(eipToMqttMapping)));



Expand Down Expand Up @@ -133,7 +133,7 @@ public void test_PublishChangedDataOnly_False() {
List.of());

final EipSpecificAdapterConfig config =
new EipSpecificAdapterConfig(44818, HOST, 1, 0, new EipToMqttConfig(1000, 10, false, List.of(eipToMqttMapping)));
new EipSpecificAdapterConfig("id",44818, HOST, 1, 0, new EipToMqttConfig(1000, 10, false, List.of(eipToMqttMapping)));



Expand Down Expand Up @@ -176,7 +176,7 @@ public void test_PublishChangedDataOnly_True() {
List.of());

final EipSpecificAdapterConfig config =
new EipSpecificAdapterConfig(44818, HOST, 1, 0, new EipToMqttConfig(1000, 10, true, List.of(eipToMqttMapping)));
new EipSpecificAdapterConfig("id",44818, HOST, 1, 0, new EipToMqttConfig(1000, 10, true, List.of(eipToMqttMapping)));


final ProtocolAdapterInput<EipSpecificAdapterConfig> inputMock = mock(ProtocolAdapterInput.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void convertConfigObject_defaults_valid() throws Exception {

@Test
public void unconvertConfigObject_full_valid() {
final EipSpecificAdapterConfig adapterConfig = new EipSpecificAdapterConfig(
final EipSpecificAdapterConfig adapterConfig = new EipSpecificAdapterConfig("id",
14,
"my.host.com",
15,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ConfigTagsTuple tryConvertLegacyConfig(
fileToMqttMappings);

return new ConfigTagsTuple(legacyFileAdapterConfig.getId(),
new FileSpecificAdapterConfig(fileToMqttConfig),
new FileSpecificAdapterConfig(legacyFileAdapterConfig.getId(), fileToMqttConfig),
tags,
fileToMqttMappings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,30 @@ public class FileSpecificAdapterConfig implements ProtocolSpecificAdapterConfig,

private static final @NotNull String ID_REGEX = "^([a-zA-Z_0-9-_])*$";

@JsonProperty(value = "id", required = true, access = JsonProperty.Access.WRITE_ONLY)
@ModuleConfigField(title = "Identifier",
description = "Unique identifier for this protocol adapter",
format = ModuleConfigField.FieldType.IDENTIFIER,
required = true,
stringPattern = ID_REGEX,
stringMinLength = 1,
stringMaxLength = 1024)
private final @NotNull String id;

@JsonProperty(value = "fileToMqtt", required = true)
@ModuleConfigField(title = "File To MQTT Config",
description = "The configuration for a data stream from File to MQTT",
required = true)
private final @Nullable FileToMqttConfig fileToMqttConfig;

public FileSpecificAdapterConfig(
@JsonProperty(value = "id",
required = true,
access = JsonProperty.Access.WRITE_ONLY) final @NotNull String id,
@JsonProperty(value = "fileToMqtt") final @Nullable FileToMqttConfig fileToMqttConfig) {
this.id = id;
if (fileToMqttConfig == null) {
this.fileToMqttConfig = new FileToMqttConfig(null ,null, null);
this.fileToMqttConfig = new FileToMqttConfig(null, null, null);
} else {
this.fileToMqttConfig = fileToMqttConfig;
}
Expand All @@ -51,8 +65,8 @@ public FileSpecificAdapterConfig(

@Override
public @NotNull List<? extends PollingContext> getPollingContexts() {
if (fileToMqttConfig==null){
return List.of();
if (fileToMqttConfig == null) {
return List.of();
}
return fileToMqttConfig.getMappings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public void convertConfigObject_fullConfig_valid() throws Exception {
final ProtocolAdapterConfig protocolAdapterConfig = getProtocolAdapterConfig(resource);

final FileSpecificAdapterConfig config = (FileSpecificAdapterConfig) protocolAdapterConfig.getAdapterConfig();
assertThat(protocolAdapterConfig.missingTags())
.isEmpty();
assertThat(protocolAdapterConfig.missingTags()).isEmpty();

assertThat(protocolAdapterConfig.getAdapterId()).isEqualTo("my-file-protocol-adapter");
assertThat(config.getFileToMqttConfig().getPollingIntervalMillis()).isEqualTo(10);
Expand Down Expand Up @@ -104,8 +103,7 @@ public void convertConfigObject_defaults_valid() throws Exception {
final ProtocolAdapterConfig protocolAdapterConfig = getProtocolAdapterConfig(resource);

final FileSpecificAdapterConfig config = (FileSpecificAdapterConfig) protocolAdapterConfig.getAdapterConfig();
assertThat(protocolAdapterConfig.missingTags())
.isEmpty();
assertThat(protocolAdapterConfig.missingTags()).isEmpty();

assertThat(protocolAdapterConfig.getAdapterId()).isEqualTo("my-file-protocol-adapter");
assertThat(config.getFileToMqttConfig().getPollingIntervalMillis()).isEqualTo(1000);
Expand All @@ -121,58 +119,43 @@ public void convertConfigObject_defaults_valid() throws Exception {
assertThat(subscription.getTagName()).isEqualTo("tag1");
});

assertThat(protocolAdapterConfig.getTags().stream().map(t -> (FileTag)t))
.contains(new FileTag("tag1", "decsription", new FileTagDefinition("pathy", ContentType.BINARY)));
assertThat(protocolAdapterConfig.getTags().stream().map(t -> (FileTag) t)).contains(new FileTag("tag1",
"decsription",
new FileTagDefinition("pathy", ContentType.BINARY)));
}

@Test
public void convertConfigObject_defaults_missing_tag() throws Exception {
final URL resource = getClass().getResource("/file-adapter-minimal-config-missing-tag.xml");
final ProtocolAdapterConfig protocolAdapterConfig = getProtocolAdapterConfig(resource);
assertThat(protocolAdapterConfig.missingTags())
.isPresent()
assertThat(protocolAdapterConfig.missingTags()).isPresent()
.hasValueSatisfying(set -> assertThat(set).contains("tag1"));
}

@Test
public void convertConfigObject_tagNameMissing_exception() throws Exception {
final URL resource = getClass().getResource("/file-adapter-tag-name-missing.xml");

assertThatThrownBy(() -> getProtocolAdapterConfig(resource))
.isInstanceOf(UnrecoverableException.class);
assertThatThrownBy(() -> getProtocolAdapterConfig(resource)).isInstanceOf(UnrecoverableException.class);
}

@Test
public void convertConfigObject_mqttTopicMissing_exception() throws Exception {
final URL resource = getClass().getResource("/file-adapter-mqtt-topic-missing.xml");

assertThatThrownBy(() -> getProtocolAdapterConfig(resource))
.isInstanceOf(UnrecoverableException.class);
assertThatThrownBy(() -> getProtocolAdapterConfig(resource)).isInstanceOf(UnrecoverableException.class);
}

@Test
public void unconvertConfigObject_full_valid() {
final FileSpecificAdapterConfig adapterConfig = new FileSpecificAdapterConfig(
new FileToMqttConfig(
12,
final FileSpecificAdapterConfig adapterConfig = new FileSpecificAdapterConfig("id",
new FileToMqttConfig(12,
13,
List.of(
new FileToMqttMapping(
"my/destination",
1,
null,
false,
true,
null,
"tag1")
)
)
);
List.of(new FileToMqttMapping("my/destination", 1, null, false, true, null, "tag1"))));

final ProtocolAdapterFactoryInput mockInput = mock(ProtocolAdapterFactoryInput.class);
when(mockInput.isWritingEnabled()).thenReturn(false);
final FileProtocolAdapterFactory fileProtocolAdapterFactory =
new FileProtocolAdapterFactory(mockInput);
final FileProtocolAdapterFactory fileProtocolAdapterFactory = new FileProtocolAdapterFactory(mockInput);
final Map<String, Object> config = fileProtocolAdapterFactory.unconvertConfigObject(mapper, adapterConfig);

final Map<String, Object> toMqtt = (Map<String, Object>) config.get("fileToMqtt");
Expand All @@ -183,27 +166,13 @@ public void unconvertConfigObject_full_valid() {

@Test
public void unconvertConfigObject_defaults_valid() {
final FileSpecificAdapterConfig adapterConfig = new FileSpecificAdapterConfig(
new FileToMqttConfig(
null,
null,
List.of(
new FileToMqttMapping(
"my/destination",
null,
null,
null,
null,
null,
"tag1")
)
)
);
final FileSpecificAdapterConfig adapterConfig = new FileSpecificAdapterConfig("id",new FileToMqttConfig(null,
null,
List.of(new FileToMqttMapping("my/destination", null, null, null, null, null, "tag1"))));

final ProtocolAdapterFactoryInput mockInput = mock(ProtocolAdapterFactoryInput.class);
when(mockInput.isWritingEnabled()).thenReturn(false);
final FileProtocolAdapterFactory fileProtocolAdapterFactory =
new FileProtocolAdapterFactory(mockInput);
final FileProtocolAdapterFactory fileProtocolAdapterFactory = new FileProtocolAdapterFactory(mockInput);
final Map<String, Object> config = fileProtocolAdapterFactory.unconvertConfigObject(mapper, adapterConfig);

final Map<String, Object> toMqtt = (Map<String, Object>) config.get("fileToMqtt");
Expand All @@ -213,8 +182,8 @@ public void unconvertConfigObject_defaults_valid() {

}

private @NotNull ProtocolAdapterConfig getProtocolAdapterConfig(final @NotNull URL resource) throws
URISyntaxException {
private @NotNull ProtocolAdapterConfig getProtocolAdapterConfig(final @NotNull URL resource)
throws URISyntaxException {
final File path = Path.of(resource.toURI()).toFile();

final HiveMQConfigEntity configEntity = loadConfig(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ public HttpProtocolAdapterFactory(@NotNull final ProtocolAdapterFactoryInput inp
// create tag first
final String newTagName = legacyHttpAdapterConfig.getId() + "-" + UUID.randomUUID();
ArrayList<HttpTag> tags = new ArrayList<>();
tags.add(new HttpTag(newTagName, "not set", new HttpTagDefinition(
legacyHttpAdapterConfig.getUrl(),
legacyHttpAdapterConfig.getHttpRequestMethod(),
legacyHttpAdapterConfig.getHttpConnectTimeoutSeconds(),
legacyHttpAdapterConfig.getHttpRequestBodyContentType(),
legacyHttpAdapterConfig.getHttpRequestBody(),
legacyHttpAdapterConfig.getHttpHeaders())));
tags.add(new HttpTag(newTagName,
"not set",
new HttpTagDefinition(legacyHttpAdapterConfig.getUrl(),
legacyHttpAdapterConfig.getHttpRequestMethod(),
legacyHttpAdapterConfig.getHttpConnectTimeoutSeconds(),
legacyHttpAdapterConfig.getHttpRequestBodyContentType(),
legacyHttpAdapterConfig.getHttpRequestBody(),
legacyHttpAdapterConfig.getHttpHeaders())));

final HttpToMqttMapping httpToMqttMapping = new HttpToMqttMapping(newTagName,
legacyHttpAdapterConfig.getDestination(),
Expand All @@ -95,7 +96,8 @@ public HttpProtocolAdapterFactory(@NotNull final ProtocolAdapterFactoryInput inp
List.of(httpToMqttMapping));

return new ConfigTagsTuple(legacyHttpAdapterConfig.getId(),
new HttpSpecificAdapterConfig(legacyHttpAdapterConfig.getHttpConnectTimeoutSeconds(),
new HttpSpecificAdapterConfig(legacyHttpAdapterConfig.getId(),
legacyHttpAdapterConfig.getHttpConnectTimeoutSeconds(),
httpToMqttConfig,
legacyHttpAdapterConfig.isAllowUntrustedCertificates()),
tags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public class BidirectionalHttpSpecificAdapterConfig extends HttpSpecificAdapterC

@JsonCreator
public BidirectionalHttpSpecificAdapterConfig(
@JsonProperty(value = "id", required = true, access = JsonProperty.Access.WRITE_ONLY) final @NotNull String id,
@JsonProperty(value = "httpConnectTimeoutSeconds") final @Nullable Integer httpConnectTimeoutSeconds,
@JsonProperty(value = "httpToMqtt") final @Nullable HttpToMqttConfig httpToMqttConfig,
@JsonProperty(value = "mqttToHttp", access = JsonProperty.Access.WRITE_ONLY) final @Nullable MqttToHttpConfig mqttToHttpConfig,
@JsonProperty(value = "mqttToHttp",
access = JsonProperty.Access.WRITE_ONLY) final @Nullable MqttToHttpConfig mqttToHttpConfig,
@JsonProperty(value = "allowUntrustedCertificates") final @Nullable Boolean allowUntrustedCertificates) {
super( httpConnectTimeoutSeconds, httpToMqttConfig, allowUntrustedCertificates);
super(id, httpConnectTimeoutSeconds, httpToMqttConfig, allowUntrustedCertificates);
this.mqttToHttpConfig = Objects.requireNonNullElseGet(mqttToHttpConfig, () -> new MqttToHttpConfig(List.of()));
}

Expand Down
Loading

0 comments on commit 26c16cf

Please sign in to comment.