Skip to content

Commit

Permalink
Merge pull request #22 from lillo42/improve-test
Browse files Browse the repository at this point in the history
Improve test
  • Loading branch information
lillo42 authored Feb 16, 2020
2 parents d3607bc + 7af180c commit 3561cdf
Show file tree
Hide file tree
Showing 14 changed files with 1,483 additions and 605 deletions.
8 changes: 8 additions & 0 deletions Mozzila.IoT.WebThing.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mozilla.IoT.WebThing.Accept
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiThing", "sample\MultiThing\MultiThing.csproj", "{3CDFC9FB-F240-419A-800D-79C506CBDAE2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Github", "Github", "{425538CC-334D-4DB3-B529-48EA7CD778BF}"
ProjectSection(SolutionItems) = preProject
.github\workflows\pull-request.yml = .github\workflows\pull-request.yml
.github\workflows\build-master.yml = .github\workflows\build-master.yml
.github\workflows\release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -106,5 +113,6 @@ Global
{6FB673AA-FD52-4509-97C8-28572549F609} = {370B1F76-EFE0-44D4-A395-59F5EF266112}
{0D709627-98FA-4A39-8631-90C982ADED44} = {65C51E32-2901-4983-A238-0F931D9EB651}
{3CDFC9FB-F240-419A-800D-79C506CBDAE2} = {370B1F76-EFE0-44D4-A395-59F5EF266112}
{425538CC-334D-4DB3-B529-48EA7CD778BF} = {E90FFA85-A210-450A-AA08-528D7F8962C2}
EndGlobalSection
EndGlobal
116 changes: 65 additions & 51 deletions test/Mozilla.IoT.WebThing.AcceptanceTest/Http/Action.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.TestHost;
Expand All @@ -14,15 +15,22 @@ namespace Mozilla.IoT.WebThing.AcceptanceTest.Http
{
public class Action
{
private static readonly TimeSpan s_timeout = TimeSpan.FromSeconds(30);
private readonly HttpClient _client;
public Action()
{
var host = Program.GetHost().GetAwaiter().GetResult();
_client = host.GetTestServer().CreateClient();
}

[Theory]
[InlineData(50, 2_000)]
public async Task Create(int level, int duration)
{
var host = await Program.CreateHostBuilder(null)
.StartAsync();
var client = host.GetTestServer().CreateClient();

var response = await client.PostAsync("/things/Lamp/actions",
var source = new CancellationTokenSource();
source.CancelAfter(s_timeout);

var response = await _client.PostAsync("/things/action/actions",
new StringContent($@"
{{
""fade"": {{
Expand All @@ -31,12 +39,13 @@ public async Task Create(int level, int duration)
""duration"": {duration}
}}
}}
}}"));
}}"), source.Token).ConfigureAwait(false);

response.IsSuccessStatusCode.Should().BeTrue();
response.StatusCode.Should().Be(HttpStatusCode.Created);
response.Content.Headers.ContentType.ToString().Should().Be( "application/json");

var message = await response.Content.ReadAsStringAsync();
var message = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var json = JsonConvert.DeserializeObject<Fade>(message, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
Expand All @@ -45,7 +54,7 @@ public async Task Create(int level, int duration)
json.Input.Should().NotBeNull();
json.Input.Level.Should().Be(level);
json.Input.Duration.Should().Be(duration);
json.Href.Should().StartWith("/things/lamp/actions/fade/");
json.Href.Should().StartWith("/things/action/actions/fade/");
json.Status.Should().NotBeNullOrEmpty();
json.TimeRequested.Should().BeBefore(DateTime.UtcNow);
}
Expand All @@ -54,11 +63,10 @@ public async Task Create(int level, int duration)
[InlineData(50, 2_000)]
public async Task CreateInSpecificUrl(int level, int duration)
{
var host = await Program.CreateHostBuilder(null)
.StartAsync();
var client = host.GetTestServer().CreateClient();
var source = new CancellationTokenSource();
source.CancelAfter(s_timeout);

var response = await client.PostAsync("/things/lamp/actions/fade",
var response = await _client.PostAsync("/things/action/actions/fade",
new StringContent($@"
{{
""fade"": {{
Expand All @@ -67,12 +75,14 @@ public async Task CreateInSpecificUrl(int level, int duration)
""duration"": {duration}
}}
}}
}}"));
}}"), source.Token).ConfigureAwait(false);


response.IsSuccessStatusCode.Should().BeTrue();
response.StatusCode.Should().Be(HttpStatusCode.Created);
response.Content.Headers.ContentType.ToString().Should().Be( "application/json");

var message = await response.Content.ReadAsStringAsync();
var message = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var json = JsonConvert.DeserializeObject<Fade>(message, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
Expand All @@ -81,19 +91,18 @@ public async Task CreateInSpecificUrl(int level, int duration)
json.Input.Should().NotBeNull();
json.Input.Level.Should().Be(level);
json.Input.Duration.Should().Be(duration);
json.Href.Should().StartWith("/things/lamp/actions/fade/");
json.Href.Should().StartWith("/things/action/actions/fade/");
json.Status.Should().NotBeNullOrEmpty();
json.TimeRequested.Should().BeBefore(DateTime.UtcNow);
}

[Fact]
public async Task InvalidAction()
{
var host = await Program.CreateHostBuilder(null)
.StartAsync();
var client = host.GetTestServer().CreateClient();
var source = new CancellationTokenSource();
source.CancelAfter(s_timeout);

var response = await client.PostAsync("/things/lamp/actions/aaaa",
var response = await _client.PostAsync("/things/action/actions/aaaa",
new StringContent(@"
{
""aaaa"": {
Expand All @@ -102,19 +111,19 @@ public async Task InvalidAction()
""duration"": 100
}
}
}"));
}"), source.Token).ConfigureAwait(false);

response.IsSuccessStatusCode.Should().BeFalse();
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

[Fact]
public async Task TryCreateActionWithOtherName()
{
var host = await Program.CreateHostBuilder(null)
.StartAsync();
var client = host.GetTestServer().CreateClient();
var source = new CancellationTokenSource();
source.CancelAfter(s_timeout);

var response = await client.PostAsync("/things/lamp/actions/fade",
var response = await _client.PostAsync("/things/action/actions/fade",
new StringContent(@"
{
""aaaa"": {
Expand All @@ -123,7 +132,8 @@ public async Task TryCreateActionWithOtherName()
""duration"": 100
}
}
}"));
}"), source.Token).ConfigureAwait(false);

response.IsSuccessStatusCode.Should().BeFalse();
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}
Expand All @@ -133,11 +143,10 @@ public async Task TryCreateActionWithOtherName()
[InlineData(101, 2_000)]
public async Task TryCreateWithInvalidParameter(int level, int duration)
{
var host = await Program.CreateHostBuilder(null)
.StartAsync();
var client = host.GetTestServer().CreateClient();
var source = new CancellationTokenSource();
source.CancelAfter(s_timeout);

var response = await client.PostAsync("/things/Lamp/actions",
var response = await _client.PostAsync("/things/action/actions",
new StringContent($@"
{{
""fade"": {{
Expand All @@ -146,17 +155,18 @@ public async Task TryCreateWithInvalidParameter(int level, int duration)
""duration"": {duration}
}}
}}
}}"));
}}"), source.Token).ConfigureAwait(false);

response.IsSuccessStatusCode.Should().BeFalse();
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);

response = await client.GetAsync("/things/Lamp/actions");
response = await _client.GetAsync("/things/action/actions", source.Token).ConfigureAwait(false);
response.IsSuccessStatusCode.Should().BeTrue();
response.StatusCode.Should().Be(HttpStatusCode.OK);

response.Content.Headers.ContentType.ToString().Should().Be( "application/json");

var message = await response.Content.ReadAsStringAsync();
var message = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var json = JToken.Parse(message);
json.Type.Should().Be(JTokenType.Array);
((JArray)json).Should().HaveCount(0);
Expand All @@ -165,40 +175,42 @@ public async Task TryCreateWithInvalidParameter(int level, int duration)
[Fact]
public async Task LongRunner()
{
var host = await Program.CreateHostBuilder(null)
.StartAsync();
var client = host.GetTestServer().CreateClient();
var source = new CancellationTokenSource();
source.CancelAfter(s_timeout);

var response = await client.PostAsync("/things/Lamp/actions",
var response = await _client.PostAsync("/things/action/actions",
new StringContent($@"
{{
""longRun"": {{
}}
}}"));
}}"), source.Token).ConfigureAwait(false);

response.IsSuccessStatusCode.Should().BeTrue();
response.StatusCode.Should().Be(HttpStatusCode.Created);
response.Content.Headers.ContentType.ToString().Should().Be( "application/json");

var message = await response.Content.ReadAsStringAsync();
var message = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var json = JsonConvert.DeserializeObject<LongRun>(message, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});

json.Href.Should().StartWith("/things/lamp/actions/longRun/");
json.Href.Should().StartWith("/things/action/actions/longRun/");
json.Status.Should().NotBeNullOrEmpty();
json.TimeRequested.Should().BeBefore(DateTime.UtcNow);

await Task.Delay(3_000);
await Task.Delay(3_000).ConfigureAwait(false);

response = await client.GetAsync($"/things/lamp/actions/longRun/{json.Href.Substring(json.Href.LastIndexOf('/') + 1)}");
response = await _client.GetAsync($"/things/action/actions/longRun/{json.Href.Substring(json.Href.LastIndexOf('/') + 1)}", source.Token)
.ConfigureAwait(false);

message = await response.Content.ReadAsStringAsync();
json = JsonConvert.DeserializeObject<LongRun>(message, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});

json.Href.Should().StartWith("/things/lamp/actions/longRun/");
json.Href.Should().StartWith("/things/action/actions/longRun/");
json.Status.Should().NotBeNullOrEmpty();
json.Status.Should().Be("completed");
json.TimeRequested.Should().BeBefore(DateTime.UtcNow);
Expand All @@ -209,34 +221,36 @@ public async Task LongRunner()
[Fact]
public async Task CancelAction()
{
var host = await Program.CreateHostBuilder(null)
.StartAsync();
var client = host.GetTestServer().CreateClient();
var source = new CancellationTokenSource();
source.CancelAfter(s_timeout);

var response = await client.PostAsync("/things/Lamp/actions",
var response = await _client.PostAsync("/things/action/actions",
new StringContent($@"
{{
""LongRun"": {{
}}
}}"));
}}"), source.Token).ConfigureAwait(false);

response.IsSuccessStatusCode.Should().BeTrue();
response.StatusCode.Should().Be(HttpStatusCode.Created);
response.Content.Headers.ContentType.ToString().Should().Be( "application/json");

var message = await response.Content.ReadAsStringAsync();
var message = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var json = JsonConvert.DeserializeObject<LongRun>(message, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});

json.Href.Should().StartWith("/things/lamp/actions/longRun/");
json.Href.Should().StartWith("/things/action/actions/longRun/");
json.Status.Should().NotBeNullOrEmpty();
json.TimeRequested.Should().BeBefore(DateTime.UtcNow);

response = await client.DeleteAsync($"/things/lamp/actions/longRun/{json.Href.Substring(json.Href.LastIndexOf('/') + 1)}");
response = await _client.DeleteAsync($"/things/action/actions/longRun/{json.Href.Substring(json.Href.LastIndexOf('/') + 1)}", source.Token)
.ConfigureAwait(false);
response.StatusCode.Should().Be(HttpStatusCode.NoContent);

response = await client.GetAsync($"/things/lamp/actions/longRun/{json.Href.Substring(json.Href.LastIndexOf('/') + 1)}");
response = await _client.GetAsync($"/things/action/actions/longRun/{json.Href.Substring(json.Href.LastIndexOf('/') + 1)}", source.Token)
.ConfigureAwait(false);
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

Expand Down
Loading

0 comments on commit 3561cdf

Please sign in to comment.