forked from aws-samples/serverless-test-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrationTest.cs
129 lines (101 loc) · 3.86 KB
/
IntegrationTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using ApiTests.IntegrationTest.Drivers;
using FluentAssertions;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Http.Json;
namespace ApiTests.IntegrationTest;
// use xunit extensions to control dependent test order
// REF: https://github.com/xunit/samples.xunit/blob/main/TestOrderExamples/TestCaseOrdering/PriorityOrderExamples.cs
[TestCaseOrderer("ApiTests.IntegrationTest.TestOrderer", "ApiTests.IntegrationTest")]
public class IntegrationTest : IClassFixture<Setup>, IDisposable
{
private readonly Setup _setup;
private readonly HttpClient _client;
private bool disposed;
public IntegrationTest(Setup setup)
{
_setup = setup;
_client = new HttpClient()
{
BaseAddress = new(setup.ApiUrl),
DefaultRequestHeaders = { { "INTEGRATION_TEST", "true" } },
};
}
public void Dispose()
{
if (disposed)
{
return;
}
disposed = true;
_client.Dispose();
}
[Fact, TestOrder(1)]
public async Task CreateProduct_ShouldReturnSuccess()
{
// arrange
var product = new Product(Guid.NewGuid().ToString(), "TestProduct", 10);
// act
var response = await _client.PutAsJsonAsync(product.Id, product);
// assert
response.StatusCode.Should().Be(HttpStatusCode.Created);
_setup.CreatedProductIds.Add(product.Id);
var content = await response.Content.ReadAsStringAsync();
content.Should().Be($"Created product with id {product.Id}");
response.Headers.Location.Should().NotBeNull();
}
[Fact, TestOrder(2)]
public async Task RetrieveACreatedProduct_ShouldReturnProduct()
{
// arrange
var id = _setup.CreatedProductIds[0];
// act
var response = await _client.GetAsync(id);
// assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var product = await response.Content.ReadFromJsonAsync<Product>();
product.Should().BeEquivalentTo(new Product(id, "TestProduct", 10));
}
[Fact, TestOrder(2)]
public async Task GetProducts_ShouldReturnSuccess()
{
// arrange
var id = _setup.CreatedProductIds[0];
// act
var response = await _client.GetAsync("/");
// assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var products = await response.Content.ReadFromJsonAsync<ProductWrapper>();
products.Should().BeEquivalentTo(new ProductWrapper(new() { new Product(id, "TestProduct", 10) }));
}
[Fact, TestOrder(3)]
public async Task UpdateProduct_CanUpdateProductData_ShouldReturnSuccess()
{
// arrange
var id = _setup.CreatedProductIds[0];
var expected = new Product(id, "Update name", 20);
// act
var response = await _client.PutAsJsonAsync(expected.Id, expected);
// assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
content.Should().Be($"Updated product with id {expected.Id}");
response = await _client.GetAsync(id);
var product = await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<Product>();
product.Should().BeEquivalentTo(expected);
}
[Fact, TestOrder(4)]
public async Task DeleteACreatedProduct_ShouldReturnSuccess()
{
// arrange
var id = _setup.CreatedProductIds[0];
// act
var response = await _client.DeleteAsync(id);
// assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
content.Should().Be($"Product with id {id} deleted");
(await _client.GetAsync(id)).StatusCode.Should().Be(HttpStatusCode.NotFound);
_setup.CreatedProductIds.Remove(id);
}
}