Skip to content

Commit

Permalink
More tests for model deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne committed Dec 8, 2023
1 parent d2cd711 commit 3889386
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ await client.PostAsync($"/{org}/{app}/instances/{instanceId}/data?dataType=defau
readDataElementResponseParsed.Melding.Name.Should().Be("Ivar Nesje");
readDataElementResponseParsed.Melding.Toggle.Should().BeTrue();

// Verify that update response equals the following read response
// responseContent.Should().Be(readDataElementResponseContent);

_dataProcessor.Verify(p=>p.ProcessDataRead(It.IsAny<Instance>(), It.Is<Guid>(dataId => dataId == Guid.Parse(dataGuid)), It.IsAny<Skjema>()), Times.Exactly(2));
_dataProcessor.Verify(p => p.ProcessDataWrite(It.IsAny<Instance>(), It.Is<Guid>(dataId => dataId == Guid.Parse(dataGuid)), It.IsAny<Skjema>(), It.Is<Dictionary<string, string?>>(d => d.ContainsKey("melding.name"))), Times.Exactly(1)); // TODO: Shouldn't this be 2 because of the first write?
_dataProcessor.VerifyNoOtherCalls();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public async Task PostNewInstanceWithInvalidData_EnsureInvalidResponse()


[Fact]
public async Task PostNewInstanceWithWrong_EnsureBadRequest()
public async Task PostNewInstanceWithWrongPartname_EnsureBadRequest()
{
// Setup test data
string testName = nameof(PostNewInstanceWithWrong_EnsureBadRequest);
string testName = nameof(PostNewInstanceWithWrongPartname_EnsureBadRequest);
string org = "tdd";
string app = "contributer-restriction";
int instanceOwnerPartyId = 501337;
Expand Down
97 changes: 86 additions & 11 deletions test/Altinn.App.Core.Tests/Helpers/ModelDeserializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ namespace Altinn.App.Core.Tests.Helpers;

public class ModelDeserializerTests
{
private readonly ILogger _logger = new Mock<ILogger>().Object;

[XmlRoot("melding")]
public class Melding
{
[JsonPropertyName("test")]
[XmlElement("test")]
public string Test { get; set; }
}

[Fact]
public async Task TestDeserializeJson()
{
// Arrange
string json = @"{""test"":""test""}";
var logger = new Mock<ILogger>().Object;

// Act
var deserializer = new ModelDeserializer(logger, typeof(Melding));
var deserializer = new ModelDeserializer(_logger, typeof(Melding));
var result = await deserializer.DeserializeAsync(new MemoryStream(Encoding.UTF8.GetBytes(json)), "application/json");

// Assert
Expand All @@ -32,10 +41,9 @@ public async Task TestDeserializeXml()
{
// Arrange
string json = "<melding><test>test</test></melding>";
var logger = new Mock<ILogger>().Object;

// Act
var deserializer = new ModelDeserializer(logger, typeof(Melding));
var deserializer = new ModelDeserializer(_logger, typeof(Melding));
var result = await deserializer.DeserializeAsync(new MemoryStream(Encoding.UTF8.GetBytes(json)), "application/xml");

// Assert
Expand All @@ -48,22 +56,89 @@ public async Task TestDeserializeInvalidXml()
{
// Arrange
string json = "<melding><testFail>test</estFail></melding>";
var logger = new Mock<ILogger>().Object;

// Act
var deserializer = new ModelDeserializer(logger, typeof(Melding));
var deserializer = new ModelDeserializer(_logger, typeof(Melding));
var result = await deserializer.DeserializeAsync(new MemoryStream(Encoding.UTF8.GetBytes(json)), "application/xml");

// Assert
result.HasError.Should().BeTrue();
result.Error.Should().Contain("The 'testFail' start tag on line 1 position 11 does not match the end tag of 'estFail'. Line 1, position 26.");
}

[XmlRoot("melding")]
public class Melding
[Fact]
public async Task TestDeserializeMultipartWithInvalidFirstContent()
{
[JsonPropertyName("test")]
[XmlElement("test")]
public string Test { get; set; }
// Arrange
string json = @"{""test"":""test""}";
using var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "ddddd");
requestContent.Add(new StringContent("invalid", Encoding.UTF8, "application/xml"), "dddd");

// Act
var deserializer = new ModelDeserializer(_logger, typeof(Melding));

var result = await deserializer.DeserializeAsync(await requestContent.ReadAsStreamAsync(), requestContent.Headers.ContentType!.ToString());

// Assert
result.HasError.Should().BeTrue();
result.Error.Should().Contain("First entry in multipart serialization must have name=\"dataModel\"");
}

[Fact]
public async Task TestDeserializeMultipartWithInvalidSecondContent()
{
// Arrange
string json = @"{""test"":""test""}";
using var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "dataModel");
requestContent.Add(new StringContent("invalid", Encoding.UTF8, "application/xml"), "dddd");

// Act
var deserializer = new ModelDeserializer(_logger, typeof(Melding));

var result = await deserializer.DeserializeAsync(await requestContent.ReadAsStreamAsync(), requestContent.Headers.ContentType!.ToString());

// Assert
result.HasError.Should().BeTrue();
result.Error.Should().Contain("Second entry in multipart serialization must have name=\"previousValues\"");
}

[Fact]
public async Task TestDeserializeMultipart()
{
// Arrange
string json = @"{""test"":""test""}";
using var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "default");
requestContent.Add(new StringContent("invalid", Encoding.UTF8, "application/xml"), "dddd");

// Act
var deserializer = new ModelDeserializer(_logger, typeof(Melding));

var result = await deserializer.DeserializeAsync(await requestContent.ReadAsStreamAsync(), requestContent.Headers.ContentType!.ToString());

// Assert
result.HasError.Should().BeTrue();
result.Error.Should().Contain("First entry in multipart serialization must have name=\"dataModel\"");
}

[Fact]
public async Task TestDeserializeMultipart_UnknownContentType()
{
// Arrange
string json = @"{""test"":""test""}";
using var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), "default");
requestContent.Add(new StringContent("invalid", Encoding.UTF8, "application/xml"), "dddd");

// Act
var deserializer = new ModelDeserializer(_logger, typeof(Melding));

var result = await deserializer.DeserializeAsync(await requestContent.ReadAsStreamAsync(), "Unknown Content Type");

// Assert
result.HasError.Should().BeTrue();
result.Error.Should().Contain("Unknown content type Unknown Content Type. Cannot read the data.");
}
}

0 comments on commit 3889386

Please sign in to comment.