-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle message attributes of type `Number` or with a custom suffix label. Resolves #1556.
- Loading branch information
1 parent
56b8877
commit 9482817
Showing
5 changed files
with
286 additions
and
32 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/JustSaying/Messaging/MessageSerialization/MessageAttributeParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace JustSaying.Messaging.MessageSerialization; | ||
|
||
internal static class MessageAttributeParser | ||
{ | ||
public static MessageAttributeValue Parse(string dataType, string dataValue) | ||
{ | ||
// Check for a prefix instead of an exact match as SQS supports custom-type labels, or example, "Binary.gif". | ||
// See https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html#sqs-message-attributes. | ||
bool isBinary = dataType?.StartsWith("Binary", StringComparison.Ordinal) is true; | ||
|
||
return new() | ||
{ | ||
DataType = dataType, | ||
StringValue = !isBinary ? dataValue : null, | ||
BinaryValue = isBinary ? Convert.FromBase64String(dataValue) : null | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
tests/JustSaying.UnitTests/Messaging/Serialization/Newtonsoft/WhenDeserializingMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using JustSaying.Messaging.MessageSerialization; | ||
using JustSaying.Models; | ||
|
||
namespace JustSaying.UnitTests.Messaging.Serialization.Newtonsoft; | ||
|
||
public class WhenDeserializingMessage : XBehaviourTest<MessageSerializationRegister> | ||
{ | ||
protected override MessageSerializationRegister CreateSystemUnderTest() => | ||
new( | ||
new NonGenericMessageSubjectProvider(), | ||
new NewtonsoftSerializationFactory()); | ||
|
||
protected override void Given() | ||
{ | ||
RecordAnyExceptionsThrown(); | ||
} | ||
|
||
protected override void WhenAction() | ||
{ | ||
SystemUnderTest.AddSerializer<CustomMessage>(); | ||
} | ||
|
||
[Fact] | ||
public void DeserializesMessage() | ||
{ | ||
// Arrange | ||
var body = | ||
""" | ||
{ | ||
"Subject": "CustomMessage", | ||
"Message":"{\"Custom\":\"Text\"}" | ||
} | ||
"""; | ||
|
||
// Act | ||
var actual = SystemUnderTest.DeserializeMessage(body); | ||
|
||
// Assert | ||
actual.ShouldNotBeNull(); | ||
actual.Message.ShouldNotBeNull(); | ||
actual.MessageAttributes.ShouldNotBeNull(); | ||
|
||
var message = actual.Message.ShouldBeOfType<CustomMessage>(); | ||
|
||
message.Custom.ShouldBe("Text"); | ||
} | ||
|
||
[Fact] | ||
public void DeserializesMessageWithMessageAttributes() | ||
{ | ||
// Arrange | ||
var body = | ||
""" | ||
{ | ||
"Subject": "CustomMessage", | ||
"Message":"{\"Custom\":\"Text\"}", | ||
"MessageAttributes": { | ||
"Text": { | ||
"Type": "String", | ||
"Value": "foo" | ||
}, | ||
"Integer": { | ||
"Type": "Number", | ||
"Value": "42" | ||
}, | ||
"BinaryData": { | ||
"Type": "Binary", | ||
"Value": "SnVzdCBFYXQgVGFrZWF3YXkuY29t" | ||
}, | ||
"CustomBinaryData": { | ||
"Type": "Binary.jet", | ||
"Value": "SnVzdFNheWluZw==" | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
// Act | ||
var actual = SystemUnderTest.DeserializeMessage(body); | ||
|
||
// Assert | ||
actual.ShouldNotBeNull(); | ||
actual.Message.ShouldNotBeNull(); | ||
|
||
var message = actual.Message.ShouldBeOfType<CustomMessage>(); | ||
message.Custom.ShouldBe("Text"); | ||
|
||
actual.MessageAttributes.ShouldNotBeNull(); | ||
|
||
var attribute = actual.MessageAttributes.Get("Text"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("String"); | ||
attribute.StringValue.ShouldBe("foo"); | ||
attribute.BinaryValue.ShouldBeNull(); | ||
|
||
attribute = actual.MessageAttributes.Get("Integer"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("Number"); | ||
attribute.StringValue.ShouldBe("42"); | ||
attribute.BinaryValue.ShouldBeNull(); | ||
|
||
attribute = actual.MessageAttributes.Get("BinaryData"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("Binary"); | ||
attribute.StringValue.ShouldBeNull(); | ||
attribute.BinaryValue.ShouldBe([.. "Just Eat Takeaway.com"u8]); | ||
|
||
attribute = actual.MessageAttributes.Get("CustomBinaryData"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("Binary.jet"); | ||
attribute.StringValue.ShouldBeNull(); | ||
attribute.BinaryValue.ShouldBe([.. "JustSaying"u8]); | ||
} | ||
|
||
private sealed class CustomMessage : Message | ||
{ | ||
public string Custom { get; set; } | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...s/JustSaying.UnitTests/Messaging/Serialization/SystemTextJson/WhenDeserializingMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using JustSaying.Messaging.MessageSerialization; | ||
using JustSaying.Models; | ||
|
||
namespace JustSaying.UnitTests.Messaging.Serialization.SystemTextJson; | ||
|
||
public class WhenDeserializingMessage : XBehaviourTest<MessageSerializationRegister> | ||
{ | ||
protected override MessageSerializationRegister CreateSystemUnderTest() => | ||
new( | ||
new NonGenericMessageSubjectProvider(), | ||
new SystemTextJsonSerializationFactory()); | ||
|
||
protected override void Given() | ||
{ | ||
RecordAnyExceptionsThrown(); | ||
} | ||
|
||
protected override void WhenAction() | ||
{ | ||
SystemUnderTest.AddSerializer<CustomMessage>(); | ||
} | ||
|
||
[Fact] | ||
public void DeserializesMessage() | ||
{ | ||
// Arrange | ||
var body = | ||
""" | ||
{ | ||
"Subject": "CustomMessage", | ||
"Message":"{\"Custom\":\"Text\"}" | ||
} | ||
"""; | ||
|
||
// Act | ||
var actual = SystemUnderTest.DeserializeMessage(body); | ||
|
||
// Assert | ||
actual.ShouldNotBeNull(); | ||
actual.Message.ShouldNotBeNull(); | ||
actual.MessageAttributes.ShouldNotBeNull(); | ||
|
||
var message = actual.Message.ShouldBeOfType<CustomMessage>(); | ||
|
||
message.Custom.ShouldBe("Text"); | ||
} | ||
|
||
[Fact] | ||
public void DeserializesMessageWithMessageAttributes() | ||
{ | ||
// Arrange | ||
var body = | ||
""" | ||
{ | ||
"Subject": "CustomMessage", | ||
"Message":"{\"Custom\":\"Text\"}", | ||
"MessageAttributes": { | ||
"Text": { | ||
"Type": "String", | ||
"Value": "foo" | ||
}, | ||
"Integer": { | ||
"Type": "Number", | ||
"Value": "42" | ||
}, | ||
"BinaryData": { | ||
"Type": "Binary", | ||
"Value": "SnVzdCBFYXQgVGFrZWF3YXkuY29t" | ||
}, | ||
"CustomBinaryData": { | ||
"Type": "Binary.jet", | ||
"Value": "SnVzdFNheWluZw==" | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
// Act | ||
var actual = SystemUnderTest.DeserializeMessage(body); | ||
|
||
// Assert | ||
actual.ShouldNotBeNull(); | ||
actual.Message.ShouldNotBeNull(); | ||
|
||
var message = actual.Message.ShouldBeOfType<CustomMessage>(); | ||
message.Custom.ShouldBe("Text"); | ||
|
||
actual.MessageAttributes.ShouldNotBeNull(); | ||
|
||
var attribute = actual.MessageAttributes.Get("Text"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("String"); | ||
attribute.StringValue.ShouldBe("foo"); | ||
attribute.BinaryValue.ShouldBeNull(); | ||
|
||
attribute = actual.MessageAttributes.Get("Integer"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("Number"); | ||
attribute.StringValue.ShouldBe("42"); | ||
attribute.BinaryValue.ShouldBeNull(); | ||
|
||
attribute = actual.MessageAttributes.Get("BinaryData"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("Binary"); | ||
attribute.StringValue.ShouldBeNull(); | ||
attribute.BinaryValue.ShouldBe([.. "Just Eat Takeaway.com"u8]); | ||
|
||
attribute = actual.MessageAttributes.Get("CustomBinaryData"); | ||
|
||
attribute.ShouldNotBeNull(); | ||
attribute.DataType.ShouldBe("Binary.jet"); | ||
attribute.StringValue.ShouldBeNull(); | ||
attribute.BinaryValue.ShouldBe([.. "JustSaying"u8]); | ||
} | ||
|
||
private sealed class CustomMessage : Message | ||
{ | ||
public string Custom { get; set; } | ||
} | ||
} |