-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor and write tests for JsonHelper in DataController #116
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,273 @@ | ||
#nullable enable | ||
|
||
using System.Text.Json.Serialization; | ||
using Altinn.App.Core.Features; | ||
using Altinn.App.Core.Helpers; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Altinn.App.Core.Tests.Helpers; | ||
|
||
public class JsonHelperTests | ||
{ | ||
/// <summary> | ||
/// Helper method to setup and get the dictionary of the diffs | ||
/// </summary> | ||
public async Task<Dictionary<string, object?>?> DoTest<TModel>(TModel model, Func<TModel, bool> processDataWriteImpl) | ||
where TModel : class | ||
{ | ||
var instance = new Instance(); | ||
var logger = new Mock<ILogger>().Object; | ||
var guid = Guid.Empty; | ||
var dataProcessorMock = new Mock<IDataProcessor>(); | ||
Func<Instance, Guid, object, Task<bool>> dataProcessWrite = (instance, guid, model) => Task.FromResult(processDataWriteImpl((TModel)model)); | ||
dataProcessorMock | ||
.Setup((d) => d.ProcessDataWrite(It.IsAny<Instance>(), It.IsAny<Guid>(), It.IsAny<object>())) | ||
.Returns(dataProcessWrite); | ||
|
||
return await JsonHelper.ProcessDataWriteWithDiff(instance, guid, model, dataProcessorMock.Object, logger); | ||
} | ||
|
||
public class TestModel | ||
{ | ||
public int IntegerTest { get; set; } | ||
|
||
public int? NullableIntTest { get; set; } | ||
|
||
public decimal DecimalTest { get; set; } | ||
|
||
public decimal? NullableDecimalTest { get; set; } | ||
|
||
public string StringTest { get; set; } = null!; | ||
|
||
public string? NullableStringTest { get; set; } | ||
|
||
// [Newtonsoft.Json.JsonProperty("jsonPropertyName")] | ||
[JsonPropertyName("jsonPropertyName")] | ||
public string? NotJsonPropertyNameTest { get; set; } | ||
|
||
// [JsonPropertyName("newtonsoftString")] | ||
[Newtonsoft.Json.JsonProperty("newtonsoftString")] | ||
public string? NewtonsoftAttributeWorks { get; set; } | ||
|
||
public TestModel? RecursiveTest { get; set; } | ||
|
||
public TestModel NonNullableRecursiveTest { get; set; } = default!; | ||
|
||
public List<int> PrimitiveList { get; set; } = default!; | ||
|
||
public List<TestModel> ListTest { get; set; } = default!; | ||
|
||
public List<TestModel>? NullableListTest { get; set; } | ||
} | ||
|
||
[Fact] | ||
public async Task SimpleNoChangeTest() | ||
{ | ||
var data = new TestModel(); | ||
var diff = await DoTest(data, (model) => false); | ||
diff.Should().BeNull(); | ||
} | ||
|
||
[Fact(Skip = "Curently empty diffs are sendt to frontend, so the result here is an empty dictionary")] | ||
public async Task SimpleNoChangeTestTrueReturn() | ||
{ | ||
var data = new TestModel(); | ||
var diff = await DoTest(data, (model) => true); | ||
diff.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task InitializingPropertiesLeadsToNoDiff() | ||
{ | ||
var data = new TestModel(); | ||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.ListTest = new(); | ||
model.PrimitiveList = new(); | ||
model.NullableListTest = new(); | ||
return true; | ||
}); | ||
|
||
// Might be null in the future | ||
diff.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task InitializingNonNullablePropertiesCreatesDiff() | ||
{ | ||
var data = new TestModel(); | ||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.RecursiveTest = new(); | ||
return true; | ||
}); | ||
|
||
// Not sure if RecursiveTest should be null here, but apparently it does not hurt | ||
diff.Should().Equal(new Dictionary<string, object?>() | ||
{ | ||
{"RecursiveTest.IntegerTest", 0}, | ||
{"RecursiveTest.DecimalTest", 0M}, | ||
{"RecursiveTest", null}, | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task NullIsNotZero() | ||
{ | ||
var data = new TestModel() | ||
{ | ||
NullableIntTest = null, | ||
}; | ||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.NullableIntTest = 0; | ||
return true; | ||
}); | ||
|
||
diff.Should().Contain("NullableIntTest", 0); | ||
diff.Should().HaveCount(1); | ||
} | ||
|
||
[Fact] | ||
public async Task ZeroIsNotNull() | ||
{ | ||
var data = new TestModel() | ||
{ | ||
NullableIntTest = 0, | ||
}; | ||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.NullableIntTest = null; | ||
return true; | ||
}); | ||
|
||
diff.Should().Contain("NullableIntTest", null); | ||
diff.Should().HaveCount(1); | ||
} | ||
|
||
[Fact] | ||
public async Task TestSystemTextJsonAnnotation() | ||
{ | ||
var data = new TestModel() | ||
{ | ||
NotJsonPropertyNameTest = "Original Value", | ||
}; | ||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.NotJsonPropertyNameTest = "New Value"; | ||
return true; | ||
}); | ||
|
||
diff.Should().Equal(new Dictionary<string, object?> | ||
{ | ||
{"jsonPropertyName", "New Value"}, | ||
}); | ||
} | ||
|
||
[Fact(Skip = "System.Text.Json annotation is required")] | ||
public async Task NewtonsoftJsonAnnotation() | ||
{ | ||
var data = new TestModel() | ||
{ | ||
NewtonsoftAttributeWorks = "Original Value", | ||
}; | ||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.NewtonsoftAttributeWorks = "New Value"; | ||
return true; | ||
}); | ||
|
||
diff.Should().Equal(new Dictionary<string, object?> | ||
{ | ||
{"newtonsoftString", "New Value"}, | ||
}); | ||
} | ||
|
||
[Theory] | ||
[InlineData(-1)] | ||
[InlineData(10)] | ||
[InlineData(100)] | ||
[InlineData(int.MinValue)] | ||
[InlineData(int.MaxValue)] | ||
public async Task ChangeInteger(int value) | ||
{ | ||
var data = new TestModel() | ||
{ | ||
RecursiveTest = new(), | ||
PrimitiveList = new(), | ||
}; | ||
|
||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.IntegerTest = value; | ||
model.NullableIntTest = value; | ||
model.RecursiveTest ??= new(); | ||
model.RecursiveTest.IntegerTest = value; | ||
model.PrimitiveList ??= new(); | ||
model.PrimitiveList.Add(value); | ||
return true; | ||
}); | ||
|
||
diff.Should().BeEquivalentTo(new Dictionary<string, object?>() | ||
{ | ||
{ "IntegerTest", value }, | ||
{ "NullableIntTest", value }, | ||
{ "RecursiveTest.IntegerTest", value }, | ||
{ "PrimitiveList[0]", value }, | ||
}); | ||
} | ||
|
||
[Theory] | ||
[InlineData("-1")] | ||
[InlineData("10")] | ||
[InlineData("100")] | ||
[InlineData(int.MinValue)] | ||
[InlineData(int.MaxValue)] | ||
[InlineData(long.MinValue)] | ||
[InlineData(long.MaxValue)] | ||
[InlineData("9223372036854775808")] | ||
[InlineData("79228162514264337593543950335")] | ||
[InlineData("-79228162514264337593543950334")] | ||
public async Task ChangeDecimal(object rawValue) | ||
{ | ||
var value = rawValue switch | ||
{ | ||
string stringValue => decimal.Parse(stringValue), | ||
int intValue => (decimal)intValue, | ||
long longValue => (decimal)longValue, | ||
_ => throw new NotImplementedException(), | ||
}; | ||
var data = new TestModel() | ||
{ | ||
RecursiveTest = new(), | ||
ListTest = new() { new() }, | ||
NullableListTest = new() { new() }, | ||
}; | ||
var diff = await DoTest(data, (model) => | ||
{ | ||
model.DecimalTest = value; | ||
model.NullableDecimalTest = value; | ||
model.RecursiveTest ??= new(); | ||
model.RecursiveTest.DecimalTest = value; | ||
model.NullableListTest ??= new(); | ||
model.NullableListTest[0].DecimalTest = value; | ||
model.ListTest ??= new(); | ||
model.ListTest[0].DecimalTest = value; | ||
return true; | ||
}); | ||
|
||
// casting is weird (the current implementation of diff returns System.Numerics.BigInteger for large numbers) | ||
Func<object, bool> isMatch = x => (decimal?)(dynamic?)x == value; | ||
|
||
diff.Should().ContainKey("DecimalTest").WhoseValue.Should().Match(x => isMatch(x)); | ||
diff.Should().ContainKey("NullableDecimalTest").WhoseValue.Should().Match(x => isMatch(x)); | ||
diff.Should().ContainKey("RecursiveTest.DecimalTest").WhoseValue.Should().Match(x => isMatch(x)); | ||
diff.Should().ContainKey("NullableListTest[0].DecimalTest").WhoseValue.Should().Match(x => isMatch(x)); | ||
diff.Should().ContainKey("ListTest[0].DecimalTest").WhoseValue.Should().Match(x => isMatch(x)); | ||
diff.Should().HaveCount(5); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Generic catch clause