diff --git a/sdk/core/Azure.Core/CHANGELOG.md b/sdk/core/Azure.Core/CHANGELOG.md index 0ba7e43a08cb..aa02c755b3e4 100644 --- a/sdk/core/Azure.Core/CHANGELOG.md +++ b/sdk/core/Azure.Core/CHANGELOG.md @@ -4,6 +4,8 @@ ### Features Added +- Add `GetRehydrationToken` to `Operation` for rehydration purpose. + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/core/Azure.Core/api/Azure.Core.net461.cs b/sdk/core/Azure.Core/api/Azure.Core.net461.cs index c68df937125e..7d6246afcef8 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net461.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net461.cs @@ -137,6 +137,7 @@ protected Operation() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override int GetHashCode() { throw null; } public abstract Azure.Response GetRawResponse(); + public virtual Azure.Core.RehydrationToken? GetRehydrationToken() { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override string? ToString() { throw null; } public abstract Azure.Response UpdateStatus(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); @@ -524,6 +525,23 @@ public static partial class MultipartResponse public static Azure.Response[] Parse(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } public static System.Threading.Tasks.Task ParseAsync(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } } + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] + public readonly partial struct RehydrationToken : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel, System.ClientModel.Primitives.IPersistableModel + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public string? Id { get { throw null; } } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + object System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + object System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } public abstract partial class Request : System.IDisposable { protected Request() { } diff --git a/sdk/core/Azure.Core/api/Azure.Core.net472.cs b/sdk/core/Azure.Core/api/Azure.Core.net472.cs index c68df937125e..7d6246afcef8 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net472.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net472.cs @@ -137,6 +137,7 @@ protected Operation() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override int GetHashCode() { throw null; } public abstract Azure.Response GetRawResponse(); + public virtual Azure.Core.RehydrationToken? GetRehydrationToken() { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override string? ToString() { throw null; } public abstract Azure.Response UpdateStatus(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); @@ -524,6 +525,23 @@ public static partial class MultipartResponse public static Azure.Response[] Parse(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } public static System.Threading.Tasks.Task ParseAsync(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } } + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] + public readonly partial struct RehydrationToken : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel, System.ClientModel.Primitives.IPersistableModel + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public string? Id { get { throw null; } } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + object System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + object System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } public abstract partial class Request : System.IDisposable { protected Request() { } diff --git a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs index c6ac195d3e6e..948e617f1186 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs @@ -137,6 +137,7 @@ protected Operation() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override int GetHashCode() { throw null; } public abstract Azure.Response GetRawResponse(); + public virtual Azure.Core.RehydrationToken? GetRehydrationToken() { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override string? ToString() { throw null; } public abstract Azure.Response UpdateStatus(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); @@ -524,6 +525,23 @@ public static partial class MultipartResponse public static Azure.Response[] Parse(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } public static System.Threading.Tasks.Task ParseAsync(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } } + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] + public readonly partial struct RehydrationToken : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel, System.ClientModel.Primitives.IPersistableModel + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public string? Id { get { throw null; } } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + object System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + object System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } public abstract partial class Request : System.IDisposable { protected Request() { } diff --git a/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs b/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs index c68df937125e..7d6246afcef8 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs @@ -137,6 +137,7 @@ protected Operation() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override int GetHashCode() { throw null; } public abstract Azure.Response GetRawResponse(); + public virtual Azure.Core.RehydrationToken? GetRehydrationToken() { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override string? ToString() { throw null; } public abstract Azure.Response UpdateStatus(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); @@ -524,6 +525,23 @@ public static partial class MultipartResponse public static Azure.Response[] Parse(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } public static System.Threading.Tasks.Task ParseAsync(Azure.Response response, bool expectCrLf, System.Threading.CancellationToken cancellationToken) { throw null; } } + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] + public readonly partial struct RehydrationToken : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel, System.ClientModel.Primitives.IPersistableModel + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public string? Id { get { throw null; } } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + object System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.Core.RehydrationToken System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + object System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } public abstract partial class Request : System.IDisposable { protected Request() { } diff --git a/sdk/core/Azure.Core/src/Azure.Core.csproj b/sdk/core/Azure.Core/src/Azure.Core.csproj index fe12f30f6c51..5763d70dddb1 100644 --- a/sdk/core/Azure.Core/src/Azure.Core.csproj +++ b/sdk/core/Azure.Core/src/Azure.Core.csproj @@ -1,4 +1,4 @@ - + This is the implementation of the Azure Client Pipeline Microsoft Azure Client Pipeline @@ -21,6 +21,7 @@ + diff --git a/sdk/core/Azure.Core/src/Operation.cs b/sdk/core/Azure.Core/src/Operation.cs index 0f12ae9f90ca..7ff18388e0f8 100644 --- a/sdk/core/Azure.Core/src/Operation.cs +++ b/sdk/core/Azure.Core/src/Operation.cs @@ -16,6 +16,11 @@ namespace Azure public abstract class Operation #pragma warning restore AZC0012 // Avoid single word type names { + /// + /// Get a token that can be used to rehydrate the operation. + /// + public virtual RehydrationToken? GetRehydrationToken() => null; + /// /// Gets an ID representing the operation that can be used to poll for /// the status of the long-running operation. diff --git a/sdk/core/Azure.Core/src/RehydrationToken.Serialization.cs b/sdk/core/Azure.Core/src/RehydrationToken.Serialization.cs new file mode 100644 index 000000000000..470323817458 --- /dev/null +++ b/sdk/core/Azure.Core/src/RehydrationToken.Serialization.cs @@ -0,0 +1,179 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.ClientModel.Primitives; +using System.Diagnostics; +using System.Text.Json; + +namespace Azure.Core +{ + public partial struct RehydrationToken : IJsonModel, IJsonModel + { + internal RehydrationToken DeserializeRehydrationToken(JsonElement element, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(RehydrationToken)} does not support '{format}' format."); + } + + if (element.ValueKind == JsonValueKind.Null) + { + throw new InvalidOperationException("Cannot deserialize a null value to a non-nullable RehydrationToken"); + } + + string? id = null; + string version = string.Empty; + string headerSource = string.Empty; + string nextRequestUri = string.Empty; + string initialUri = string.Empty; + RequestMethod requestMethod = default; + string? lastKnownLocation = null; + string finalStateVia = string.Empty; + + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("id"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + id = property.Value.GetString(); + } + if (property.NameEquals("version"u8)) + { + var versionString = property.Value.GetString(); + Debug.Assert(versionString is not null); + version = versionString!; + continue; + } + if (property.NameEquals("headerSource"u8)) + { + var headerSourceString = property.Value.GetString(); + Debug.Assert(headerSourceString is not null); + headerSource = headerSourceString!; + continue; + } + if (property.NameEquals("nextRequestUri"u8)) + { + var nextRequestUriString = property.Value.GetString(); + Debug.Assert(nextRequestUriString is not null); + nextRequestUri = nextRequestUriString!; + continue; + } + if (property.NameEquals("initialUri"u8)) + { + var initialUriString = property.Value.GetString(); + Debug.Assert(initialUriString is not null); + initialUri = initialUriString!; + continue; + } + if (property.NameEquals("requestMethod"u8)) + { + var requestMethodString = property.Value.GetString(); + Debug.Assert(requestMethodString is not null); + requestMethod = new RequestMethod(requestMethodString!); + continue; + } + if (property.NameEquals("lastKnownLocation"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + lastKnownLocation = property.Value.GetString(); + continue; + } + if (property.NameEquals("finalStateVia"u8)) + { + var finalStateViaString = property.Value.GetString(); + Debug.Assert(finalStateViaString is not null); + finalStateVia = finalStateViaString!; + continue; + } + } + return new RehydrationToken(id, version, headerSource, nextRequestUri, initialUri, requestMethod, lastKnownLocation, finalStateVia); + } + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + writer.WritePropertyName("id"u8); + writer.WriteStringValue(Id); + writer.WritePropertyName("version"u8); + writer.WriteStringValue(Version); + writer.WritePropertyName("headerSource"u8); + writer.WriteStringValue(HeaderSource.ToString()); + writer.WritePropertyName("nextRequestUri"u8); + writer.WriteStringValue(NextRequestUri); + writer.WritePropertyName("initialUri"u8); + writer.WriteStringValue(InitialUri); + writer.WritePropertyName("requestMethod"u8); + writer.WriteStringValue(RequestMethod.ToString()); + writer.WritePropertyName("lastKnownLocation"u8); + writer.WriteStringValue(LastKnownLocation); + writer.WritePropertyName("finalStateVia"u8); + writer.WriteStringValue(FinalStateVia.ToString()); + writer.WriteEndObject(); + } + + RehydrationToken IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(RehydrationToken)} does not support '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeRehydrationToken(document.RootElement, options); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options); + default: + throw new FormatException($"The model {nameof(RehydrationToken)} does not support '{options.Format}' format."); + } + } + + RehydrationToken IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data); + return DeserializeRehydrationToken(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(RehydrationToken)} does not support '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + => ((IPersistableModel)this).Write(options); + + object IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + => ((IPersistableModel)this).Create(data, options); + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + => ((IJsonModel)this).Write(writer, options); + + object IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + => ((IJsonModel)this).Create(ref reader, options); + } +} diff --git a/sdk/core/Azure.Core/src/RehydrationToken.cs b/sdk/core/Azure.Core/src/RehydrationToken.cs new file mode 100644 index 000000000000..13addaadc64f --- /dev/null +++ b/sdk/core/Azure.Core/src/RehydrationToken.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Azure.Core +{ + /// + /// Represents a token that can be used to rehydrate a long-running operation. + /// + public readonly partial struct RehydrationToken + { + /// + /// Gets an ID representing the operation that can be used to poll for + /// the status of the long-running operation. + /// + public string? Id { get; } + + // Version for this contract itself since we might change the members in the future. + internal string Version { get; } = "1.0.0"; + + // The below members are used to re-construct . + // Value of . + internal string HeaderSource { get; } + + // The polling Uri of the operation. + internal string NextRequestUri { get; } + + // The initial Uri of the operation. + internal string InitialUri { get; } + + // The Http request method of the operation. + internal RequestMethod RequestMethod { get; } + + // The last known location of the operation. + internal string? LastKnownLocation { get; } + + // The final state of the operation, could be azure-async-operation, location, original-uri or operation-location. + internal string FinalStateVia { get; } + + internal RehydrationToken(string? id, string? version, string headerSource, string nextRequestUri, string initialUri, RequestMethod requestMethod, string? lastKnownLocation, string finalStateVia) + { + Id = id; + if (version is not null) + { + Version = version; + } + HeaderSource = headerSource; + NextRequestUri = nextRequestUri; + InitialUri = initialUri; + RequestMethod = requestMethod; + LastKnownLocation = lastKnownLocation; + FinalStateVia = finalStateVia; + } + } +} diff --git a/sdk/core/Azure.Core/tests/RehydrationTokenTests.cs b/sdk/core/Azure.Core/tests/RehydrationTokenTests.cs new file mode 100644 index 000000000000..b93551c6454a --- /dev/null +++ b/sdk/core/Azure.Core/tests/RehydrationTokenTests.cs @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.ClientModel.Primitives; +using NUnit.Framework; + +namespace Azure.Core.Tests +{ + public class RehydrationTokenTests + { + [Test] + public void ThrowOnDeserializationWithNullRehydrationToken() + { + Assert.Throws(() => new RehydrationToken().DeserializeRehydrationToken(default, new ModelReaderWriterOptions("J"))); + } + + [Test] + public void ThrowOnDeserializationWithRehydrationTokenNullRequiredMember() + { + var data = BinaryData.FromString("\"requestMethod\": null}"); + Assert.That(() => ModelReaderWriter.Read(data, typeof(RehydrationToken)), Throws.Exception); + } + + [Test] + public void RoundTripForRehydrationToken() + { + var token = new RehydrationToken(Guid.NewGuid().ToString(), null, "headerSource", "nextRequestUri", "initialUri", RequestMethod.Get, "lastKnownLocation", OperationFinalStateVia.OperationLocation.ToString()); + var data = ModelReaderWriter.Write(token); + var deserializedToken = ModelReaderWriter.Read(data, typeof(RehydrationToken)); + Assert.AreEqual(token, deserializedToken); + } + } +}