-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding test that emulate new extension methods
- Loading branch information
Showing
6 changed files
with
175 additions
and
34 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
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,99 @@ | ||
using System.IO; | ||
using Microsoft.Kiota.Abstractions.Serialization; | ||
using Microsoft.Kiota.Abstractions.Store; | ||
using Microsoft.Kiota.Serialization.Json.Tests.Mocks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Kiota.Serialization.Json.Tests | ||
{ | ||
public class IParsableExtensionsTests | ||
{ | ||
private const string _jsonContentType = "application/json"; | ||
private readonly SerializationWriterFactoryRegistry _serializationWriterFactoryRegistry; | ||
|
||
public IParsableExtensionsTests() | ||
{ | ||
_serializationWriterFactoryRegistry = new SerializationWriterFactoryRegistry(); | ||
_serializationWriterFactoryRegistry.ContentTypeAssociatedFactories.TryAdd(_jsonContentType, new BackingStoreSerializationWriterProxyFactory(new JsonSerializationWriterFactory())); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void GetSerializationWriter_RetunsJsonSerializationWriter(bool? serializeOnlyChangedValues) | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
using var writer = serializeOnlyChangedValues.HasValue | ||
? _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, serializeOnlyChangedValues.Value) | ||
: _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType); | ||
|
||
// Assert | ||
Assert.NotNull(writer); | ||
Assert.IsType<JsonSerializationWriter>(writer); | ||
} | ||
|
||
[Fact] | ||
public void GetSerializationWriterSerializedChangedTrue_RetunsEmptyJson() | ||
{ | ||
// Arrange | ||
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" }; | ||
testUser.BackingStore.InitializationCompleted = true; | ||
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, true); | ||
|
||
// Act | ||
writer.WriteObjectValue(null, testUser); | ||
using var stream = writer.GetSerializedContent(); | ||
var serializedContent = GetStringFromStream(stream); | ||
|
||
// Assert | ||
Assert.NotNull(serializedContent); | ||
Assert.Equal("{}", serializedContent); | ||
} | ||
|
||
[Fact] | ||
public void GetSerializationWriterSerializedChangedTrue_ChangedName_ReturnsJustName() | ||
{ | ||
// Arrange | ||
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" }; | ||
testUser.BackingStore.InitializationCompleted = true; | ||
testUser.Name = "Stephan"; | ||
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, true); | ||
|
||
// Act | ||
writer.WriteObjectValue(null, testUser); | ||
using var stream = writer.GetSerializedContent(); | ||
var serializedContent = GetStringFromStream(stream); | ||
|
||
// Assert | ||
Assert.NotNull(serializedContent); | ||
Assert.Equal("{\"name\":\"Stephan\"}", serializedContent); | ||
} | ||
|
||
[Fact] | ||
public void GetSerializationWriterSerializedChangedFalse_SerializesEntireObject() | ||
{ | ||
// Arrange | ||
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" }; | ||
testUser.BackingStore.InitializationCompleted = true; | ||
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, false); | ||
|
||
// Act | ||
writer.WriteObjectValue(null, testUser); | ||
using var stream = writer.GetSerializedContent(); | ||
var serializedContent = GetStringFromStream(stream); | ||
|
||
// Assert | ||
Assert.NotNull(serializedContent); | ||
Assert.Equal("{\"id\":\"1\",\"name\":\"testUser\"}", serializedContent); | ||
} | ||
|
||
private static string GetStringFromStream(Stream stream) | ||
{ | ||
using var reader = new StreamReader(stream); | ||
return reader.ReadToEnd(); | ||
} | ||
} | ||
} |
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,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Kiota.Abstractions.Serialization; | ||
using Microsoft.Kiota.Abstractions.Store; | ||
|
||
namespace Microsoft.Kiota.Serialization.Json.Tests.Mocks | ||
{ | ||
public class BackedTestEntity : IParsable, IBackedModel | ||
{ | ||
public BackedTestEntity() | ||
{ | ||
BackingStore = new InMemoryBackingStore(); | ||
} | ||
|
||
public IBackingStore BackingStore { get; private set; } | ||
|
||
public string Id | ||
{ | ||
get { return BackingStore?.Get<string>("id"); } | ||
set { BackingStore?.Set("id", value); } | ||
} | ||
public string Name | ||
{ | ||
get { return BackingStore?.Get<string>("name"); } | ||
set { BackingStore?.Set("name", value); } | ||
} | ||
|
||
public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() => | ||
new Dictionary<string, Action<IParseNode>> { | ||
{ "id", n => { Id = n.GetStringValue(); } }, | ||
{ "name", n => { Name = n.GetStringValue(); } }, | ||
}; | ||
public void Serialize(ISerializationWriter writer) | ||
{ | ||
_ = writer ?? throw new ArgumentNullException(nameof(writer)); | ||
writer.WriteStringValue("id", Id); | ||
writer.WriteStringValue("name", Name); | ||
} | ||
} | ||
} |