Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using NUnit.Framework;
using RestApi.Models.V1;
using RestApi.Models;
using RestApi.Test.Models;

namespace RestApi.Test.V1
namespace RestApi.Test.ApiTests
{
[TestFixture]
public class ExampleTest : TestBase
public class ExampleV1Test : TestBase
{
[TestCase("")]
[TestCase("/")]
Expand All @@ -23,7 +21,7 @@ await CatchWebException(async () =>
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV1Example + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5));
}
Expand All @@ -35,12 +33,12 @@ public async Task TestExampleWithDate(string date, string[] dates)
{
await CatchWebException(async () =>
{
WeatherForecast[] data;
WeatherForecastV1[] data;
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV1Example}/{date}")))
{
data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5));
Assert.That(data[0].Date, Is.EqualTo(dates[0]));
Expand All @@ -63,7 +61,7 @@ public async Task TestExampleWithInvalidDate(string date)
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV1Example}/{date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
}

Assert.Fail("This test should have ended up with an error response.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using System.Text.Json;
using System.Threading.Tasks;
using NUnit.Framework;
using RestApi.Models.V2;
using RestApi.Models;
using RestApi.Test.Models;

namespace RestApi.Test.V2
namespace RestApi.Test.ApiTests
{
[TestFixture]
public class ExampleTest : TestBase
public class ExampleV2Test : TestBase
{
[TestCase("")]
[TestCase("/20200303")]
Expand All @@ -22,7 +22,7 @@ public async Task TestExample(string route)
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV2Example + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
}

Assert.Fail("This test should have ended up with an error response.");
Expand All @@ -48,7 +48,7 @@ await CatchWebException(async () =>
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV2ExampleWF + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo(5 * 3));
}
Expand All @@ -66,7 +66,7 @@ await CatchWebException(async () =>
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo(5 * 3));
Assert.That(data.Min(d => d.Date),
Expand All @@ -86,7 +86,7 @@ public async Task TestExampleWFWithInvalidDate(string date)
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
}

Assert.Fail("This test should have ended up with an error response.");
Expand Down Expand Up @@ -115,7 +115,7 @@ await CatchWebException(async () =>
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}{(days == null ? "" : $"&days={days}")}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo(days ?? 5));
Assert.That(data.Min(d => d.Date),
Expand All @@ -133,7 +133,7 @@ public async Task TestExampleWFWithInvalidAreaAndValidDate(string area, string d
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
}
Assert.Fail("This test should have ended up with an error response.");
}
Expand All @@ -159,7 +159,7 @@ public async Task TestExampleWFWithValidAreaAndInvalidDate(string area, string d
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
}

Assert.Fail("This test should have ended up with an error response.");
Expand Down
25 changes: 15 additions & 10 deletions RestApi.Test/SetUp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -59,29 +60,33 @@ public async Task SetUpBeforeAll()
DateTime start = DateTime.UtcNow;
DateTime timeout = DateTime.UtcNow + TimeSpan.FromSeconds(ApiServiceStartUpTimeoutSeconds);
Exception exception = null;
while (DateTime.UtcNow < timeout)
while (true)
{
try
{
await Task.Delay(500);
_logger.LogDebug("Checking service startup...");
var client = new System.Net.WebClient();
using (stream = await client.OpenReadTaskAsync(new Uri(UrlToSwagger))) { }
var client = new WebClient();
using (stream = await client.OpenReadTaskAsync(new Uri(ApiServiceBaseUrl))) { }
var wakeup = DateTime.UtcNow - start;
_logger.LogInformation("API service started successfully.");
break;
}
catch (Exception e)
{
exception = e;
}
}
if (e is WebException && ((WebException)e).Status != WebExceptionStatus.UnknownError)
{
break;
}

if (stream == null)
{
var error = $"Test target API service did not start up within {ApiServiceStartUpTimeoutSeconds} seconds";
_logger.LogError(error);
throw new Exception(error, exception);
if (DateTime.UtcNow > timeout)
{
var error = $"Test target API service did not start up within {ApiServiceStartUpTimeoutSeconds} seconds";
_logger.LogError(error);
throw new Exception(error, exception);
}
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions RestApi/Controllers/ErrorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
namespace RestApi.Controllers
{
[ApiController]
[ApiVersion("1")]
[ApiVersion("2")]
[ApiVersionNeutral]
[Route("api/[controller]")]
[OpenApiIgnore]
public class ErrorController : ControllerBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RestApi.Models.V1;
using NSwag.Annotations;
using RestApi.Models;

namespace RestApi.Controllers.V1
namespace RestApi.Controllers
{
[ApiController]
[ApiVersion("1")]
[Route("api/v{version:apiVersion}/[controller]")]
public partial class ExampleController : ControllerBase
[Route("api/v{version:apiVersion}/Example")]
[OpenApiTag("API Example: Weather Forecast")]
public partial class ExampleV1Controller : ControllerBase
{
private static readonly string[] Summaries = new[]
{
Expand All @@ -20,13 +22,13 @@ public partial class ExampleController : ControllerBase

private readonly ILogger _logger;

public ExampleController(ILogger<ExampleController> logger)
public ExampleV1Controller(ILogger<ExampleV1Controller> logger)
{
_logger = logger;
}

[HttpGet("{date}")]
public virtual IEnumerable<WeatherForecast> Get([FromRoute] string date)
public IEnumerable<WeatherForecastV1> Get([FromRoute] string date)
{
DateTime baseDate = DateTime.UtcNow;
if (date != null
Expand All @@ -42,17 +44,16 @@ public virtual IEnumerable<WeatherForecast> Get([FromRoute] string date)

var random = new Random();
return Enumerable.Range(0, 5)
.Select(index => new WeatherForecast
.Select(index => new WeatherForecastV1
{
Date = baseDate.AddDays(index).ToString("d MMM, yyyy"),
TemperatureC = random.Next(-20, 55),
Summary = Summaries[random.Next(Summaries.Length)],
})
.ToArray();
});
}

[HttpGet()]
public virtual IEnumerable<WeatherForecast> Get()
[HttpGet]
public IEnumerable<WeatherForecastV1> Get()
=> Get(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RestApi.Models.V2;
using NSwag.Annotations;
using RestApi.Models;

namespace RestApi.Controllers.V2
namespace RestApi.Controllers
{
[ApiController]
[ApiVersion("2")]
[Route("api/v{version:apiVersion}/[controller]")]
public partial class ExampleController : ControllerBase
[Route("api/v{version:apiVersion}/Example")]
[OpenApiTag("API Example: Weather Forecast")]
public partial class ExampleV2Controller : ControllerBase
{
private static readonly string[] Areas = new[]
{
Expand All @@ -25,13 +27,13 @@ public partial class ExampleController : ControllerBase

private readonly ILogger _logger;

public ExampleController(ILogger<ExampleController> logger)
public ExampleV2Controller(ILogger<ExampleV2Controller> logger)
{
_logger = logger;
}

[HttpGet("WeatherForecast/{area}")]
public virtual IEnumerable<WeatherForecast> GetWeatherForecast(
public IEnumerable<WeatherForecastV2> GetWeatherForecast(
[FromRoute] string area,
[FromQuery] string from,
[FromQuery] int? days = null)
Expand Down Expand Up @@ -62,19 +64,18 @@ public virtual IEnumerable<WeatherForecast> GetWeatherForecast(

var random = new Random();
return Enumerable.Range(0, days.Value)
.Select(index => areas.Select(a => new WeatherForecast
.Select(index => areas.Select(a => new WeatherForecastV2
{
Area = a,
Date = baseDate.AddDays(index),
TemperatureC = random.Next(-20, 55),
Summary = Summaries[random.Next(Summaries.Length)],
}))
.SelectMany(w => w)
.ToArray();
.SelectMany(w => w);
}

[HttpGet("WeatherForecast")]
public virtual IEnumerable<WeatherForecast> GetWeatherForecast(
public IEnumerable<WeatherForecastV2> GetWeatherForecast(
[FromQuery] string from,
[FromQuery] int? days)
=> GetWeatherForecast(null, from, days);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Text.Json.Serialization;
using NJsonSchema.Annotations;

namespace RestApi.Models.V1
namespace RestApi.Models
{
public class WeatherForecast
[JsonSchema(name: "WeatherForecast")]
public class WeatherForecastV1
{
[JsonPropertyName("date")]
public string Date { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Text.Json.Serialization;
using NJsonSchema.Annotations;

namespace RestApi.Models.V2
namespace RestApi.Models
{
public class WeatherForecast
[JsonSchema(name: "WeatherForecast")]
public class WeatherForecastV2
{
[JsonPropertyName("date")]
public DateTime Date { get; set; }
Expand Down