Skip to content

Commit

Permalink
external message to mqtt publish transform should not fail on blank h…
Browse files Browse the repository at this point in the history
…eader values
  • Loading branch information
JeffreyThijs committed Jan 13, 2023
1 parent 21f1b0d commit 362390e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ private static Set<UserProperty> getUserPropertiesOrEmptySet(
) {
return externalMessageHeaders.stream()
.filter(header -> !KNOWN_MQTT_HEADER_NAMES.contains(header.getKey()))
.filter(header -> header.getValue() != null && !header.getValue().isBlank())
.map(header -> {
final var headerKey = header.getKey();
final String headerValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,32 @@ public void transformExternalMessageWithInvalidRetainValueYieldsTransformationFa
.hasCauseInstanceOf(InvalidHeaderValueException.class);
}

@Test
public void transformFullyFledgedExternalMessageWithBlankHeaderReturnsExpectedTransformationSuccessResult() {
final var correlationId = testNameCorrelationId.getCorrelationId();
final var genericMqttPublish = GenericMqttPublish.builder(MQTT_TOPIC, MQTT_QOS)
.retain(RETAIN)
.payload(PAYLOAD)
.correlationData(ByteBufferUtils.fromUtf8String(correlationId.toString()))
.contentType(CONTENT_TYPE.getValue())
.responseTopic(REPLY_TO_TOPIC)
.userProperties(USER_PROPERTIES)
.build();
Mockito.when(externalMessage.getHeaders())
.thenReturn(DittoHeaders.newBuilder()
.putHeader(MqttHeader.MQTT_TOPIC.getName(), MQTT_TOPIC.toString())
.putHeader(MqttHeader.MQTT_QOS.getName(), String.valueOf(MQTT_QOS.getCode()))
.putHeader(MqttHeader.MQTT_RETAIN.getName(), String.valueOf(genericMqttPublish.isRetain()))
.putHeader("ablankheader", "")
.correlationId(correlationId)
.putHeader(ExternalMessage.REPLY_TO_HEADER, REPLY_TO_TOPIC.toString())
.contentType(CONTENT_TYPE)
.putHeaders(USER_PROPERTIES.stream()
.collect(Collectors.toMap(UserProperty::name, UserProperty::value)))
.build());
Mockito.when(externalMessage.getBytePayload()).thenReturn(genericMqttPublish.getPayload());

assertThat(ExternalMessageToMqttPublishTransformer.transform(externalMessage, mqttPublishTarget))
.isEqualTo(TransformationSuccess.of(externalMessage, genericMqttPublish));
}
}

0 comments on commit 362390e

Please sign in to comment.