Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
David R. Williamson committed Mar 13, 2023
1 parent f3a7916 commit dcb0a26
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 46 deletions.
124 changes: 124 additions & 0 deletions iothub/device/tests/IncomingMessageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Text;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace Microsoft.Azure.Devices.Client.Tests
{
[TestClass]
[TestCategory("Unit")]
public class IncomingMessageTests
{
[TestMethod]
public void IncomingMessage_ValidatePayloadString()
{
// arrange
const string payload = "test message";

// act
var testMessage = new IncomingMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)));

// assert
testMessage.TryGetPayload(out string actualPayload);
actualPayload.Should().Be(payload);
}

[TestMethod]
public void IncomingMessage_ValidatePayloadInt()
{
// arrange
const int payload = 123;

// act
var testMessage = new IncomingMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)));

// assert
testMessage.TryGetPayload(out int actualPayload);
actualPayload.Should().Be(payload);
}

[TestMethod]
public void IncomingMessage_ValidatePayloadDateTime()
{
// arrange
DateTime payload = DateTime.Now;

// act
var testMessage = new IncomingMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)));

// assert
testMessage.TryGetPayload(out DateTime actualPayload);
actualPayload.Should().Be(payload);
}

[TestMethod]
public void IncomingMessage_ValidatePayloadDateTimeOffset()
{
// arrange
DateTimeOffset payload = DateTimeOffset.UtcNow;

// act
var testMessage = new IncomingMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)));

// assert
testMessage.TryGetPayload(out DateTimeOffset actualPayload);
actualPayload.Should().Be(payload);
}

[TestMethod]
public void IncomingMessage_ValidatePayloadTimeSpan()
{
// arrange
var payload = TimeSpan.FromSeconds(123);

// act
var testMessage = new IncomingMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)));

// assert
testMessage.TryGetPayload(out TimeSpan actualPayload);
actualPayload.Should().Be(payload);
}

[TestMethod]
public void IncomingMessage_ValidateProperties()
{
// arrange and act
var testMessage = new IncomingMessage(null)
{
InputName = "endpoint1",
MessageId = "123",
CorrelationId = "1234",
SequenceNumber = 123,
To = "destination",
UserId = "id",
CreatedOnUtc = new DateTimeOffset(DateTime.MinValue),
EnqueuedOnUtc = new DateTimeOffset(DateTime.MinValue),
ExpiresOnUtc = new DateTimeOffset(DateTime.MinValue),
MessageSchema = "schema",
ContentType = "type",
ContentEncoding = "encoding",
PayloadConvention = DefaultPayloadConvention.Instance,
};

// assert
testMessage.InputName.Should().Be("endpoint1");
testMessage.MessageId.Should().Be("123");
testMessage.CorrelationId.Should().Be("1234");
testMessage.SequenceNumber.Should().Be(123);
testMessage.To.Should().Be("destination");
testMessage.UserId.Should().Be("id");
testMessage.CreatedOnUtc.Should().Be(new DateTimeOffset(DateTime.MinValue));
testMessage.EnqueuedOnUtc.Should().Be(new DateTimeOffset(DateTime.MinValue));
testMessage.ExpiresOnUtc.Should().Be(new DateTimeOffset(DateTime.MinValue));
testMessage.MessageSchema.Should().Be("schema");
testMessage.ContentType.Should().Be("type");
testMessage.ContentEncoding.Should().Be("encoding");
testMessage.Properties.Should().NotBeNull();
testMessage.PayloadConvention.Should().Be(DefaultPayloadConvention.Instance);
}
}
}
46 changes: 0 additions & 46 deletions iothub/device/tests/IotHubModuleClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,52 +121,6 @@ public async Task ModuleClient_SetReceiveCallbackAsync_SetCallback_Mqtt()
innerHandler.Verify(x => x.SendMethodResponseAsync(It.IsAny<DirectMethodResponse>(), It.IsAny<CancellationToken>()), Times.Never);
}

[TestMethod]
public void ModuleClient_ValidateIncomingMessage()
{
// arrange
var testMessage = new IncomingMessage(Encoding.UTF8.GetBytes("test message"))
{
InputName = "endpoint1",
MessageId = "123",
CorrelationId = "1234",
SequenceNumber = 123,
To = "destination",
UserId = "id",
CreatedOnUtc = new DateTimeOffset(DateTime.MinValue),
EnqueuedOnUtc = new DateTimeOffset(DateTime.MinValue),
ExpiresOnUtc = new DateTimeOffset(DateTime.MinValue),
MessageSchema = "schema",
ContentType = "type",
ContentEncoding = "encoding",
PayloadConvention = DefaultPayloadConvention.Instance,
};

// act
var testMessage1 = new IncomingMessage(Encoding.UTF8.GetBytes("test message"));

// assert
testMessage.TryGetPayload(out bool boolPayload);
boolPayload.Should().BeFalse();
testMessage.TryGetPayload(out string payload);
payload.Should().Be("test message");
testMessage.InputName.Should().Be("endpoint1");
testMessage.MessageId.Should().Be("123");
testMessage.CorrelationId.Should().Be("1234");
testMessage.SequenceNumber.Should().Be(123);
testMessage.To.Should().Be("destination");
testMessage.UserId.Should().Be("id");
testMessage.CreatedOnUtc.Should().Be(new DateTimeOffset(DateTime.MinValue));
testMessage.EnqueuedOnUtc.Should().Be(new DateTimeOffset(DateTime.MinValue));
testMessage.ExpiresOnUtc.Should().Be(new DateTimeOffset(DateTime.MinValue));
testMessage.MessageSchema.Should().Be("schema");
testMessage.ContentType.Should().Be("type");
testMessage.ContentEncoding.Should().Be("encoding");
testMessage.Properties.Should().NotBeNull();
testMessage.PayloadConvention.Should().Be(DefaultPayloadConvention.Instance);
testMessage1.InputName.Should().BeNull();
}

[TestMethod]
public void ModuleClient_ValidateTelemetryMessage()
{
Expand Down

0 comments on commit dcb0a26

Please sign in to comment.