-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from microsoft/feature/serialization-helpers
feature/serialization helpers
- Loading branch information
Showing
26 changed files
with
689 additions
and
83 deletions.
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
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
89 changes: 89 additions & 0 deletions
89
Microsoft.Kiota.Abstractions.Tests/Serialization/DeserializationHelpersTests.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,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.Kiota.Abstractions.Serialization; | ||
using Microsoft.Kiota.Abstractions.Tests.Mocks; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.Kiota.Abstractions.Tests.Serialization; | ||
|
||
public class DeserializationHelpersTests | ||
{ | ||
private const string _jsonContentType = "application/json"; | ||
[Fact] | ||
public void DefensiveObject() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(null, (Stream)null, null)); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, (Stream)null, null)); | ||
using var stream = new MemoryStream(); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, stream, null)); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, "", null)); | ||
} | ||
[Fact] | ||
public void DefensiveObjectCollection() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(null, (Stream)null, null)); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, (Stream)null, null)); | ||
using var stream = new MemoryStream(); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, stream, null)); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, "", null)); | ||
} | ||
[Fact] | ||
public void DeserializesObjectWithoutReflection() | ||
{ | ||
var strValue = "{'id':'123'}"; | ||
var mockParseNode = new Mock<IParseNode>(); | ||
mockParseNode.Setup(x => x.GetObjectValue(It.IsAny<ParsableFactory<TestEntity>>())).Returns(new TestEntity() | ||
{ | ||
Id = "123" | ||
}); | ||
var mockJsonParseNodeFactory = new Mock<IParseNodeFactory>(); | ||
mockJsonParseNodeFactory.Setup(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>())).Returns(mockParseNode.Object); | ||
mockJsonParseNodeFactory.Setup(x => x.ValidContentType).Returns(_jsonContentType); | ||
ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockJsonParseNodeFactory.Object; | ||
|
||
var result = KiotaSerializer.Deserialize(_jsonContentType, strValue, TestEntity.CreateFromDiscriminatorValue); | ||
|
||
Assert.NotNull(result); | ||
} | ||
[Fact] | ||
public void DeserializesObjectWithReflection() | ||
{ | ||
var strValue = "{'id':'123'}"; | ||
var mockParseNode = new Mock<IParseNode>(); | ||
mockParseNode.Setup(x => x.GetObjectValue(It.IsAny<ParsableFactory<TestEntity>>())).Returns(new TestEntity() | ||
{ | ||
Id = "123" | ||
}); | ||
var mockJsonParseNodeFactory = new Mock<IParseNodeFactory>(); | ||
mockJsonParseNodeFactory.Setup(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>())).Returns(mockParseNode.Object); | ||
mockJsonParseNodeFactory.Setup(x => x.ValidContentType).Returns(_jsonContentType); | ||
ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockJsonParseNodeFactory.Object; | ||
|
||
var result = KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, strValue); | ||
|
||
Assert.NotNull(result); | ||
} | ||
[Fact] | ||
public void DeserializesCollectionOfObject() | ||
{ | ||
var strValue = "{'id':'123'}"; | ||
var mockParseNode = new Mock<IParseNode>(); | ||
mockParseNode.Setup(x => x.GetCollectionOfObjectValues(It.IsAny<ParsableFactory<TestEntity>>())).Returns(new List<TestEntity> { | ||
new TestEntity() | ||
{ | ||
Id = "123" | ||
} | ||
}); | ||
var mockJsonParseNodeFactory = new Mock<IParseNodeFactory>(); | ||
mockJsonParseNodeFactory.Setup(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>())).Returns(mockParseNode.Object); | ||
mockJsonParseNodeFactory.Setup(x => x.ValidContentType).Returns(_jsonContentType); | ||
ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockJsonParseNodeFactory.Object; | ||
|
||
var result = KiotaSerializer.DeserializeCollection(_jsonContentType, strValue, TestEntity.CreateFromDiscriminatorValue); | ||
|
||
Assert.NotNull(result); | ||
Assert.Single(result); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
Microsoft.Kiota.Abstractions.Tests/Serialization/Mocks/TestEntity.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,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Kiota.Abstractions.Serialization; | ||
|
||
namespace Microsoft.Kiota.Abstractions.Tests.Serialization.Mocks | ||
{ | ||
public class TestEntity : IParsable, IAdditionalDataHolder | ||
{ | ||
/// <summary>Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.</summary> | ||
public IDictionary<string, object> AdditionalData { get; set; } | ||
/// <summary>Read-only.</summary> | ||
public string Id { get; set; } | ||
/// <summary>Read-only.</summary> | ||
public TimeSpan? WorkDuration { get; set; } | ||
/// <summary>Read-only.</summary> | ||
public Date? BirthDay { get; set; } | ||
/// <summary>Read-only.</summary> | ||
public Time? StartWorkTime { get; set; } | ||
/// <summary>Read-only.</summary> | ||
public Time? EndWorkTime { get; set; } | ||
/// <summary>Read-only.</summary> | ||
public DateTimeOffset? CreatedDateTime { get; set; } | ||
/// <summary>Read-only.</summary> | ||
public string OfficeLocation { get; set; } | ||
/// <summary> | ||
/// Instantiates a new entity and sets the default values. | ||
/// </summary> | ||
public TestEntity() | ||
{ | ||
AdditionalData = new Dictionary<string, object>(); | ||
} | ||
/// <summary> | ||
/// The deserialization information for the current model | ||
/// </summary> | ||
public virtual IDictionary<string, Action<IParseNode>> GetFieldDeserializers() | ||
{ | ||
return new Dictionary<string, Action<IParseNode>> { | ||
{"id", n => { Id = n.GetStringValue(); } }, | ||
{"createdDateTime", n => { CreatedDateTime = n.GetDateTimeOffsetValue(); } }, | ||
{"officeLocation", n => { OfficeLocation = n.GetStringValue(); } }, | ||
{"workDuration", n => { WorkDuration = n.GetTimeSpanValue(); } }, | ||
{"birthDay", n => { BirthDay = n.GetDateValue(); } }, | ||
{"startWorkTime", n => { StartWorkTime = n.GetTimeValue(); } }, | ||
{"endWorkTime", n => { EndWorkTime = n.GetTimeValue(); } }, | ||
}; | ||
} | ||
/// <summary> | ||
/// Serializes information the current object | ||
/// <param name="writer">Serialization writer to use to serialize this model</param> | ||
/// </summary> | ||
public virtual void Serialize(ISerializationWriter writer) | ||
{ | ||
_ = writer ?? throw new ArgumentNullException(nameof(writer)); | ||
writer.WriteStringValue("id", Id); | ||
writer.WriteDateTimeOffsetValue("createdDateTime", CreatedDateTime); | ||
writer.WriteStringValue("officeLocation", OfficeLocation); | ||
writer.WriteTimeSpanValue("workDuration", WorkDuration); | ||
writer.WriteDateValue("birthDay", BirthDay); | ||
writer.WriteTimeValue("startWorkTime", StartWorkTime); | ||
writer.WriteTimeValue("endWorkTime", EndWorkTime); | ||
writer.WriteAdditionalData(AdditionalData); | ||
} | ||
public static TestEntity CreateFromDiscriminator(IParseNode parseNode) | ||
{ | ||
var discriminatorValue = parseNode.GetChildNode("@odata.type")?.GetStringValue(); | ||
return discriminatorValue switch | ||
{ | ||
"microsoft.graph.user" => new TestEntity(), | ||
"microsoft.graph.group" => new TestEntity(), | ||
_ => new TestEntity(), | ||
}; | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Microsoft.Kiota.Abstractions.Tests/Serialization/SerializationHelpersTests.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,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Microsoft.Kiota.Abstractions.Serialization; | ||
using Microsoft.Kiota.Abstractions.Tests.Mocks; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.Kiota.Abstractions.Tests.Serialization; | ||
|
||
public class SerializationHelpersTests | ||
{ | ||
private const string _jsonContentType = "application/json"; | ||
[Fact] | ||
public void DefensiveObject() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null, (TestEntity)null)); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (TestEntity)null)); | ||
} | ||
[Fact] | ||
public void DefensiveObjectCollection() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null, (IEnumerable<TestEntity>)null)); | ||
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (IEnumerable<TestEntity>)null)); | ||
} | ||
[Fact] | ||
public void SerializesObject() | ||
{ | ||
var mockSerializationWriter = new Mock<ISerializationWriter>(); | ||
mockSerializationWriter.Setup(x => x.GetSerializedContent()).Returns(new MemoryStream(UTF8Encoding.UTF8.GetBytes("{'id':'123'}"))); | ||
var mockSerializationWriterFactory = new Mock<ISerializationWriterFactory>(); | ||
mockSerializationWriterFactory.Setup(x => x.GetSerializationWriter(It.IsAny<string>())).Returns(mockSerializationWriter.Object); | ||
SerializationWriterFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockSerializationWriterFactory.Object; | ||
|
||
var result = KiotaSerializer.SerializeAsString(_jsonContentType, new TestEntity() | ||
{ | ||
Id = "123" | ||
}); | ||
|
||
Assert.Equal("{'id':'123'}", result); | ||
|
||
mockSerializationWriterFactory.Verify(x => x.GetSerializationWriter(It.IsAny<string>()), Times.Once); | ||
mockSerializationWriter.Verify(x => x.WriteObjectValue(It.IsAny<string>(), It.IsAny<TestEntity>()), Times.Once); | ||
mockSerializationWriter.Verify(x => x.GetSerializedContent(), Times.Once); | ||
} | ||
[Fact] | ||
public void SerializesObjectCollection() | ||
{ | ||
var mockSerializationWriter = new Mock<ISerializationWriter>(); | ||
mockSerializationWriter.Setup(x => x.GetSerializedContent()).Returns(new MemoryStream(UTF8Encoding.UTF8.GetBytes("[{'id':'123'}]"))); | ||
var mockSerializationWriterFactory = new Mock<ISerializationWriterFactory>(); | ||
mockSerializationWriterFactory.Setup(x => x.GetSerializationWriter(It.IsAny<string>())).Returns(mockSerializationWriter.Object); | ||
SerializationWriterFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockSerializationWriterFactory.Object; | ||
|
||
var result = KiotaSerializer.SerializeAsString(_jsonContentType, new List<TestEntity> { | ||
new() | ||
{ | ||
Id = "123" | ||
} | ||
}); | ||
|
||
Assert.Equal("[{'id':'123'}]", result); | ||
|
||
mockSerializationWriterFactory.Verify(x => x.GetSerializationWriter(It.IsAny<string>()), Times.Once); | ||
mockSerializationWriter.Verify(x => x.WriteCollectionOfObjectValues(It.IsAny<string>(), It.IsAny<IEnumerable<TestEntity>>()), Times.Once); | ||
mockSerializationWriter.Verify(x => x.GetSerializedContent(), Times.Once); | ||
} | ||
} |
Oops, something went wrong.