Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

#112 Use XUnit fixture for integrationtest #114

Merged
merged 5 commits into from
Mar 22, 2019
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
23 changes: 10 additions & 13 deletions maturity-level-two/tests/Codit.IntegrationTest/CarTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Xunit;
using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.Hosting;
//using Microsoft.AspNetCore.TestHost;
//using Microsoft.AspNetCore.Hosting;
using System.Net.Http;
using System.Net;
using System.Threading.Tasks;
Expand All @@ -10,17 +10,14 @@

namespace Codit.IntegrationTest
{
[Collection("TestServer")]
public class CarTest
{
private readonly HttpClient _httpClient;
public CarTest()
{
if (_httpClient != null) return;
var srv = new TestServer(new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>());
TestServerFixture fixture;

_httpClient = srv.CreateClient();
public CarTest(TestServerFixture fixture)
{
this.fixture = fixture;
}

[Fact]
Expand All @@ -29,7 +26,7 @@ public async Task GetCars_Ok_TestAsync()
//Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "/codito/v1/car");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
Expand All @@ -41,7 +38,7 @@ public async Task GetSingleTeam_Ok_TestAsync()
int id = 1;
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/car/{id}");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Arrange
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
Expand All @@ -53,7 +50,7 @@ public async Task GetSingleTeam_NotFound_TestAsync()
int id = -1;
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/car/{id}");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.Content.Headers.Should().NotBeNullOrEmpty();
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@

namespace Codit.IntegrationTest
{
[Collection("TestServer")]
public class CustomizationTest
{
private readonly HttpClient _httpClient;
public CustomizationTest()
{
if (_httpClient != null) return;
var srv = new TestServer(new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>());
TestServerFixture fixture;

_httpClient = srv.CreateClient();
public CustomizationTest(TestServerFixture fixture)
{
this.fixture = fixture;
}

[Fact]
Expand All @@ -31,7 +28,7 @@ public async Task GetCustomizations_Ok_TestAsync()
//Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "/codito/v1/customization");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
Expand All @@ -43,7 +40,7 @@ public async Task GetSingleCustomization_Ok_TestAsync()
int id = 1;
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
Expand All @@ -56,7 +53,7 @@ public async Task GetSingleCustomiation_NotFound_TestAsync()
int id = -1;
var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
Expand All @@ -74,7 +71,7 @@ public async Task CreateNewCustomization_Created_TestAsync()
};
var request = TestExtensions.GetJsonRequest(customization, "POST", $"/codito/v1/customization/");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.Created);
}
Expand All @@ -91,39 +88,39 @@ public async Task CreateNewCustomization_BadRequest_TestAsync()
};
var request = TestExtensions.GetJsonRequest(newCustomization, "POST", "/codito/v1/customization/");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);

}

[Fact]
public async Task DeleteCustomization_NoContent_TestAsync()
public async Task DeleteCustomization_NotFound_TestAsync()
{
//Arrange
int id = -1;
var request = new HttpRequestMessage(HttpMethod.Delete, $"/codito/v1/customization/{id}");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);

}

[Fact]
public async Task DeleteCustomization_NotFound_TestAsync()
public async Task DeleteCustomization_NoContent_TestAsync()
{
//Arrange
int id = 1;
int id = 3;
var request = new HttpRequestMessage(HttpMethod.Delete, $"/codito/v1/customization/{id}");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);

//Assert
response.StatusCode.Should().Be(HttpStatusCode.NoContent);

request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
response = await _httpClient.SendAsync(request);
response = await fixture._httpClient.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

Expand All @@ -134,17 +131,17 @@ public async Task SellCustomization_Accepted_TestAsync()
int id = 1;

var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
var actualDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());

request = new HttpRequestMessage(HttpMethod.Post, $"/codito/v1/customization/{id}/sale");
//Act
response = await _httpClient.SendAsync(request);
response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.Accepted);
// (Inventory must be decremented, number of sales incremented.
request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
response = await _httpClient.SendAsync(request);
response = await fixture._httpClient.SendAsync(request);
var updatedDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());

updatedDto.InventoryLevel.Should().Be(actualDto.InventoryLevel - 1);
Expand All @@ -159,7 +156,7 @@ public async Task SellCustomization_NotFound_TestAsync()
int id = -1;
var request = new HttpRequestMessage(HttpMethod.Post, $"/codito/v1/customization/{id}/sale");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
Expand All @@ -176,7 +173,7 @@ public async Task SellCustomization_SoldOutBadRequest_TestAsync()
};

var request = TestExtensions.GetJsonRequest(newCustomization, "POST", "/codito/v1/customization/");
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.Created);
//(Get Id of this new customization)
var newDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
Expand All @@ -185,7 +182,7 @@ public async Task SellCustomization_SoldOutBadRequest_TestAsync()
request = new HttpRequestMessage(HttpMethod.Post, $"/codito/v1/customization/{id}/sale");

//Act
response = await _httpClient.SendAsync(request);
response = await fixture._httpClient.SendAsync(request);

//Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
Expand All @@ -204,7 +201,7 @@ public async Task UpdateCustomizationIncremental_NotFound_TestAsync()
var request = TestExtensions.GetJsonRequest(customization, "PATCH", $"/codito/v1/customization/{id}");

// Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);

//Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
Expand All @@ -221,19 +218,19 @@ public async Task UpdateCustomizationIncremental_NoContent_TestAsync()
};

var request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
var actualDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
request = TestExtensions.GetJsonRequest(customization, "PATCH", $"/codito/v1/customization/{id}");

//actualDto.Should().BeNull();

// Act
response = await _httpClient.SendAsync(request);
response = await fixture._httpClient.SendAsync(request);

//Assert
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
request = new HttpRequestMessage(HttpMethod.Get, $"/codito/v1/customization/{id}");
response = await _httpClient.SendAsync(request);
response = await fixture._httpClient.SendAsync(request);
var updatedDto = JsonConvert.DeserializeObject<CustomizationDto>(await response.Content.ReadAsStringAsync());
updatedDto.Id.Should().Be(actualDto.Id);

Expand Down
15 changes: 6 additions & 9 deletions maturity-level-two/tests/Codit.IntegrationTest/OpenApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@

namespace Codit.IntegrationTest
{
[Collection("TestServer")]
public class OpenApiTest
{
private readonly HttpClient _httpClient;
public OpenApiTest()
{
if (_httpClient != null) return;
var srv = new TestServer(new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>());
TestServerFixture fixture;

_httpClient = srv.CreateClient();
public OpenApiTest(TestServerFixture fixture)
{
this.fixture = fixture;
}

[Fact]
Expand All @@ -30,7 +27,7 @@ public async Task OpenApi_OperationId_Test()
//Arrange
var request = new HttpRequestMessage(new HttpMethod("GET"), "/swagger/v1/swagger.json");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);

Expand Down
25 changes: 11 additions & 14 deletions maturity-level-two/tests/Codit.IntegrationTest/ProblemJsonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@

namespace Codit.IntegrationTest
{
[Collection("TestServer")]
public class ProblemJsonTest
{
private readonly HttpClient _httpClient;
public ProblemJsonTest()
{
if (_httpClient != null) { return; }
var srv = new TestServer(new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>());
TestServerFixture fixture;

_httpClient = srv.CreateClient();
public ProblemJsonTest(TestServerFixture fixture)
{
this.fixture = fixture;
}

[Fact]
Expand All @@ -33,7 +30,7 @@ public async Task ProblemJson_RouteNotExists_404_Test()
//Arrange
var request = new HttpRequestMessage(new HttpMethod("GET"), "/codito/api/v1/car");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
Expand All @@ -50,7 +47,7 @@ public async Task ProblemJson_Validation_400_Test()
};
var request = TestExtensions.GetJsonRequest(customization, "POST", "/codito/v1/customization");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
Expand All @@ -62,9 +59,9 @@ public async Task ProblemJson_ContentNegotiationNotOk_406_Test()
{
//Arrange
var request = new HttpRequestMessage(new HttpMethod("GET"), "/codito/codito/car");
_httpClient.DefaultRequestHeaders.Add("Accept", "custom/content+type");
request.Headers.Add("Accept", "custom/content+type");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.NotAcceptable);
response.Content.Headers.Should().BeNullOrEmpty(); // With 406 the body is suppressed
Expand All @@ -81,7 +78,7 @@ public async Task ProblemJson_UnsupportedContentType_415_Test()
};
var request = TestExtensions.GetJsonRequest(customization, "POST", "/codito/v1/customization/", "application/pdf");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
Expand All @@ -100,7 +97,7 @@ public async Task ProblemJson_InternalServerError_500_Test()

var request = TestExtensions.GetJsonRequest(customization, "POST", "/codito/v1/customization");
//Act
var response = await _httpClient.SendAsync(request);
var response = await fixture._httpClient.SendAsync(request);
//Assert
response.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
response.Content.Headers.ContentLength.Should().BeGreaterThan(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Codit.LevelTwo;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Xunit;

namespace Codit.IntegrationTest
{
public class TestServerFixture
{
internal readonly HttpClient _httpClient;

public TestServerFixture()
{
if (_httpClient != null) return;
var srv = new TestServer(new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>());

_httpClient = srv.CreateClient();

}
}

[CollectionDefinition("TestServer")]
public class TestServerCollection : ICollectionFixture<TestServerFixture>
{
}
}