Merge pull request #356 from MiYanni/validation-tests

Validation tests
This commit is contained in:
Michael Yanni 2019-12-17 14:13:37 -08:00 коммит произвёл GitHub
Родитель 23128ef130 29d8fcd34c
Коммит c0a825ca1f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
12 изменённых файлов: 564 добавлений и 1 удалений

Просмотреть файл

@ -85,7 +85,7 @@ $testNames = if ($name) { $name } else
#'storage',
#'subscriptionId-apiVersion',
'url',
#'validation',
'validation',
#'xml-service',
#'xms-error-responses',
'url-multi-collectionFormat'

Просмотреть файл

@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Threading.Tasks;
using AutoRest.TestServer.Tests.Infrastructure;
using NUnit.Framework;
using validation;
using validation.Models.V100;
namespace AutoRest.TestServer.Tests
{
public class ValidationTests : TestServerTestBase
{
public ValidationTests(TestServerVersion version) : base(version, "validation") { }
[Test]
public Task ConstantsInBody() => Test(async (host, pipeline) =>
{
var value = new Product
{
ConstString = "constant",
ConstInt = 0,
Child = new ChildProduct
{
ConstProperty = "constant"
},
ConstChild = new ConstantProduct
{
ConstProperty = "constant",
ConstProperty2 = "constant2"
}
};
var result = await AllOperations.PostWithConstantInBodyAsync(ClientDiagnostics, pipeline, value, host);
Assert.AreEqual(value.ConstString, result.Value.ConstString);
Assert.AreEqual(value.ConstInt, result.Value.ConstInt);
Assert.AreEqual(value.Child.ConstProperty, result.Value.Child.ConstProperty);
Assert.AreEqual(value.ConstChild.ConstProperty, result.Value.ConstChild.ConstProperty);
Assert.AreEqual(value.ConstChild.ConstProperty2, result.Value.ConstChild.ConstProperty2);
});
[Test]
public Task ConstantsInPath() => TestStatus(async (host, pipeline) => await AllOperations.GetWithConstantInPathAsync(ClientDiagnostics, pipeline, host));
}
}

Просмотреть файл

@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Text.Json;
namespace validation.Models.V100
{
public partial class ChildProductSerializer
{
internal static void Serialize(ChildProduct model, Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WritePropertyName("constProperty");
writer.WriteStringValue(model.ConstProperty);
if (model.Count != null)
{
writer.WritePropertyName("count");
writer.WriteNumberValue(model.Count.Value);
}
writer.WriteEndObject();
}
internal static ChildProduct Deserialize(JsonElement element)
{
var result = new ChildProduct();
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("constProperty"))
{
result.ConstProperty = property.Value.GetString();
continue;
}
if (property.NameEquals("count"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
result.Count = property.Value.GetInt32();
continue;
}
}
return result;
}
}
}

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace validation.Models.V100
{
public partial class ChildProduct
{
public string ConstProperty { get; set; }
public int? Count { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Text.Json;
namespace validation.Models.V100
{
public partial class ConstantProductSerializer
{
internal static void Serialize(ConstantProduct model, Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WritePropertyName("constProperty");
writer.WriteStringValue(model.ConstProperty);
writer.WritePropertyName("constProperty2");
writer.WriteStringValue(model.ConstProperty2);
writer.WriteEndObject();
}
internal static ConstantProduct Deserialize(JsonElement element)
{
var result = new ConstantProduct();
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("constProperty"))
{
result.ConstProperty = property.Value.GetString();
continue;
}
if (property.NameEquals("constProperty2"))
{
result.ConstProperty2 = property.Value.GetString();
continue;
}
}
return result;
}
}
}

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace validation.Models.V100
{
public partial class ConstantProduct
{
public string ConstProperty { get; set; }
public string ConstProperty2 { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Text.Json;
namespace validation.Models.V100
{
public partial class ErrorSerializer
{
internal static void Serialize(Error model, Utf8JsonWriter writer)
{
writer.WriteStartObject();
if (model.Code != null)
{
writer.WritePropertyName("code");
writer.WriteNumberValue(model.Code.Value);
}
if (model.Message != null)
{
writer.WritePropertyName("message");
writer.WriteStringValue(model.Message);
}
if (model.Fields != null)
{
writer.WritePropertyName("fields");
writer.WriteStringValue(model.Fields);
}
writer.WriteEndObject();
}
internal static Error Deserialize(JsonElement element)
{
var result = new Error();
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("code"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
result.Code = property.Value.GetInt32();
continue;
}
if (property.NameEquals("message"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
result.Message = property.Value.GetString();
continue;
}
if (property.NameEquals("fields"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
result.Fields = property.Value.GetString();
continue;
}
}
return result;
}
}
}

Просмотреть файл

@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace validation.Models.V100
{
public partial class Error
{
public int? Code { get; set; }
public string? Message { get; set; }
public string? Fields { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,110 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Text.Json;
namespace validation.Models.V100
{
public partial class ProductSerializer
{
internal static void Serialize(Product model, Utf8JsonWriter writer)
{
writer.WriteStartObject();
if (model.DisplayNames != null)
{
writer.WriteStartArray("display_names");
foreach (var item in model.DisplayNames)
{
writer.WriteStringValue(item);
}
writer.WriteEndArray();
}
if (model.Capacity != null)
{
writer.WritePropertyName("capacity");
writer.WriteNumberValue(model.Capacity.Value);
}
if (model.Image != null)
{
writer.WritePropertyName("image");
writer.WriteStringValue(model.Image);
}
writer.WritePropertyName("child");
ChildProductSerializer.Serialize(model.Child, writer);
writer.WritePropertyName("constChild");
ConstantProductSerializer.Serialize(model.ConstChild, writer);
writer.WritePropertyName("constInt");
writer.WriteNumberValue(model.ConstInt);
writer.WritePropertyName("constString");
writer.WriteStringValue(model.ConstString);
writer.WritePropertyName("constStringAsEnum");
writer.WriteStringValue(model.ConstStringAsEnum);
writer.WriteEndObject();
}
internal static Product Deserialize(JsonElement element)
{
var result = new Product();
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("display_names"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
result.DisplayNames = new List<string>();
foreach (var item in property.Value.EnumerateArray())
{
result.DisplayNames.Add(item.GetString());
}
continue;
}
if (property.NameEquals("capacity"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
result.Capacity = property.Value.GetInt32();
continue;
}
if (property.NameEquals("image"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
result.Image = property.Value.GetString();
continue;
}
if (property.NameEquals("child"))
{
result.Child = ChildProductSerializer.Deserialize(property.Value);
continue;
}
if (property.NameEquals("constChild"))
{
result.ConstChild = ConstantProductSerializer.Deserialize(property.Value);
continue;
}
if (property.NameEquals("constInt"))
{
result.ConstInt = property.Value.GetSingle();
continue;
}
if (property.NameEquals("constString"))
{
result.ConstString = property.Value.GetString();
continue;
}
if (property.NameEquals("constStringAsEnum"))
{
result.ConstStringAsEnum = property.Value.GetString();
continue;
}
}
return result;
}
}
}

Просмотреть файл

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
namespace validation.Models.V100
{
public partial class Product
{
public ICollection<string>? DisplayNames { get; set; }
public int? Capacity { get; set; }
public string? Image { get; set; }
public ChildProduct Child { get; set; }
public ConstantProduct ConstChild { get; set; }
public float ConstInt { get; set; }
public string ConstString { get; set; }
public string ConstStringAsEnum { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,192 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using validation.Models.V100;
namespace validation
{
internal static class AllOperations
{
public static async ValueTask<Response<Product>> ValidationOfMethodParametersAsync(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string subscriptionId, string resourceGroupName, int id, string host = "http://localhost:3000", CancellationToken cancellationToken = default)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
if (subscriptionId == null)
{
throw new ArgumentNullException(nameof(subscriptionId));
}
if (resourceGroupName == null)
{
throw new ArgumentNullException(nameof(resourceGroupName));
}
using var scope = clientDiagnostics.CreateScope("validation.ValidationOfMethodParameters");
scope.Start();
try
{
var request = pipeline.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri.Reset(new Uri($"{host}"));
request.Uri.AppendPath("/fakepath/", false);
request.Uri.AppendPath(subscriptionId, true);
request.Uri.AppendPath("/", false);
request.Uri.AppendPath(resourceGroupName, true);
request.Uri.AppendPath("/", false);
request.Uri.AppendPath(id, true);
request.Uri.AppendQuery("apiVersion", "1.0.0", true);
var response = await pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
switch (response.Status)
{
case 200:
{
using var document = await JsonDocument.ParseAsync(response.ContentStream, default, cancellationToken).ConfigureAwait(false);
var value = ProductSerializer.Deserialize(document.RootElement);
return Response.FromValue(value, response);
}
default:
throw new Exception();
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public static async ValueTask<Response<Product>> ValidationOfBodyAsync(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string subscriptionId, string resourceGroupName, int id, Product? body, string host = "http://localhost:3000", CancellationToken cancellationToken = default)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
if (subscriptionId == null)
{
throw new ArgumentNullException(nameof(subscriptionId));
}
if (resourceGroupName == null)
{
throw new ArgumentNullException(nameof(resourceGroupName));
}
using var scope = clientDiagnostics.CreateScope("validation.ValidationOfBody");
scope.Start();
try
{
var request = pipeline.CreateRequest();
request.Method = RequestMethod.Put;
request.Uri.Reset(new Uri($"{host}"));
request.Uri.AppendPath("/fakepath/", false);
request.Uri.AppendPath(subscriptionId, true);
request.Uri.AppendPath("/", false);
request.Uri.AppendPath(resourceGroupName, true);
request.Uri.AppendPath("/", false);
request.Uri.AppendPath(id, true);
request.Headers.Add("Content-Type", "application/json");
request.Uri.AppendQuery("apiVersion", "1.0.0", true);
using var content = new Utf8JsonRequestContent();
var writer = content.JsonWriter;
ProductSerializer.Serialize(body, writer);
request.Content = content;
var response = await pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
switch (response.Status)
{
case 200:
{
using var document = await JsonDocument.ParseAsync(response.ContentStream, default, cancellationToken).ConfigureAwait(false);
var value = ProductSerializer.Deserialize(document.RootElement);
return Response.FromValue(value, response);
}
default:
throw new Exception();
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public static async ValueTask<Response> GetWithConstantInPathAsync(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string host = "http://localhost:3000", CancellationToken cancellationToken = default)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
using var scope = clientDiagnostics.CreateScope("validation.GetWithConstantInPath");
scope.Start();
try
{
var request = pipeline.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri.Reset(new Uri($"{host}"));
request.Uri.AppendPath("/validation/constantsInPath/", false);
request.Uri.AppendPath("constant", true);
request.Uri.AppendPath("/value", false);
var response = await pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
switch (response.Status)
{
case 200:
return response;
default:
throw new Exception();
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public static async ValueTask<Response<Product>> PostWithConstantInBodyAsync(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Product? body, string host = "http://localhost:3000", CancellationToken cancellationToken = default)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
using var scope = clientDiagnostics.CreateScope("validation.PostWithConstantInBody");
scope.Start();
try
{
var request = pipeline.CreateRequest();
request.Method = RequestMethod.Post;
request.Uri.Reset(new Uri($"{host}"));
request.Uri.AppendPath("/validation/constantsInPath/", false);
request.Uri.AppendPath("constant", true);
request.Uri.AppendPath("/value", false);
request.Headers.Add("Content-Type", "application/json");
using var content = new Utf8JsonRequestContent();
var writer = content.JsonWriter;
ProductSerializer.Serialize(body, writer);
request.Content = content;
var response = await pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
switch (response.Status)
{
case 200:
{
using var document = await JsonDocument.ParseAsync(response.ContentStream, default, cancellationToken).ConfigureAwait(false);
var value = ProductSerializer.Deserialize(document.RootElement);
return Response.FromValue(value, response);
}
default:
throw new Exception();
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
}
}

Просмотреть файл

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>annotations</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Core" Version="1.0.0" />
<PackageReference Include="System.Text.Json" Version="4.6.0" />
</ItemGroup>
</Project>