forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADT: Fix the issue with non-default object serializers in Query APIs (A…
…zure#18379) * Fix the bug with non-default ObjectSerializer in QueryClient
- Loading branch information
Showing
11 changed files
with
4,460 additions
and
19 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
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
94 changes: 94 additions & 0 deletions
94
sdk/digitaltwins/Azure.DigitalTwins.Core/tests/NewtonsoftObjectSerializerTests.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,94 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Azure.Core.Serialization; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.DigitalTwins.Core.Tests | ||
{ | ||
/// <summary> | ||
/// Tests for custom ObjectSerializer. | ||
/// Users can specify their own serializer/deserializer and not go with the default JsonObjectSerializer. | ||
/// SDK needs to make sure it can properly use different serializers and the behavior is seamless. | ||
/// Specifically, we have a work around in the query code which requires use of a System.Text.Json serializer because it works on a JsonElement. | ||
/// When the user initializes with a non-default serializer, we must not use that to do this work. | ||
/// </summary> | ||
public class NewtonsoftObjectSerializerTests : E2eTestBase | ||
{ | ||
public NewtonsoftObjectSerializerTests(bool isAsync) | ||
: base(isAsync) | ||
{ | ||
} | ||
|
||
[Test] | ||
public async Task TestNewtonsoftObjectSerializerWithDigitalTwins() | ||
{ | ||
DigitalTwinsClient defaultClient = GetClient(); | ||
|
||
string roomTwinId = await GetUniqueTwinIdAsync(defaultClient, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); | ||
string floorModelId = await GetUniqueModelIdAsync(defaultClient, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); | ||
string roomModelId = await GetUniqueModelIdAsync(defaultClient, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); | ||
|
||
try | ||
{ | ||
// arrange | ||
|
||
// create room model | ||
string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); | ||
await CreateAndListModelsAsync(defaultClient, new List<string> { roomModel }).ConfigureAwait(false); | ||
|
||
// act | ||
|
||
// create room twin | ||
BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); | ||
await defaultClient.CreateOrReplaceDigitalTwinAsync<BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); | ||
|
||
// Create a client with NewtonsoftJsonObjectSerializer configured as the serializer. | ||
DigitalTwinsClient testClient = GetClient( | ||
new DigitalTwinsClientOptions | ||
{ | ||
Serializer = new NewtonsoftJsonObjectSerializer() | ||
}); | ||
|
||
// Get digital twin using the simple DigitalTwin model annotated with Newtonsoft attributes | ||
SimpleNewtonsoftDtModel getResponse = await testClient.GetDigitalTwinAsync<SimpleNewtonsoftDtModel>(roomTwinId).ConfigureAwait(false); | ||
|
||
getResponse.Id.Should().NotBeNullOrEmpty("Digital twin ID should not be null or empty"); | ||
|
||
// Query DigitalTwins using the simple DigitalTwin model annotated with Newtonsoft attributes | ||
AsyncPageable<SimpleNewtonsoftDtModel> queryResponse = testClient.QueryAsync<SimpleNewtonsoftDtModel>("SELECT * FROM DIGITALTWINS"); | ||
|
||
await foreach (SimpleNewtonsoftDtModel twin in queryResponse) | ||
{ | ||
twin.Id.Should().NotBeNullOrEmpty("Digital twin Id should not be null or empty"); | ||
} | ||
} | ||
finally | ||
{ | ||
// cleanup | ||
try | ||
{ | ||
// delete twin | ||
if (!string.IsNullOrWhiteSpace(roomTwinId)) | ||
{ | ||
await defaultClient.DeleteDigitalTwinAsync(roomTwinId).ConfigureAwait(false); | ||
} | ||
|
||
// delete models | ||
if (!string.IsNullOrWhiteSpace(roomModelId)) | ||
{ | ||
await defaultClient.DeleteModelAsync(roomModelId).ConfigureAwait(false); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.Fail($"Test clean up failed: {ex.Message}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.