|
1 | | -using System.Net; |
2 | | -using Core.Api.Testing; |
3 | 1 | using Core.Testing; |
4 | 2 | using FluentAssertions; |
5 | 3 | using Tickets.Api.Requests; |
6 | 4 | using Tickets.Api.Responses; |
7 | | -using Tickets.Api.Tests.Config; |
8 | 5 | using Tickets.Reservations; |
9 | | -using Tickets.Reservations.CreatingTentativeReservation; |
10 | 6 | using Tickets.Reservations.GettingReservationById; |
11 | 7 | using Tickets.Reservations.GettingReservationHistory; |
12 | 8 | using Tickets.Reservations.GettingReservations; |
| 9 | +using Ogooreck.API; |
13 | 10 | using Xunit; |
| 11 | +using static Ogooreck.API.ApiSpecification; |
14 | 12 |
|
15 | 13 | namespace Tickets.Api.Tests.Reservations.CreatingTentativeReservation; |
16 | 14 |
|
17 | | -public class CreateTentativeReservationFixture: ApiWithEventsFixture<Startup> |
| 15 | +public class CreateTentativeReservationTests: IClassFixture<TestWebApplicationFactory<Program>> |
18 | 16 | { |
19 | | - protected override string ApiUrl => "/api/Reservations"; |
20 | | - |
21 | | - protected virtual Dictionary<string, string> GetConfiguration(string fixtureName) => |
22 | | - TestConfiguration.Get(fixtureName); |
23 | | - |
24 | | - public readonly Guid SeatId = Guid.NewGuid(); |
25 | | - |
26 | | - public HttpResponseMessage CommandResponse = default!; |
27 | | - |
28 | | - public override async Task InitializeAsync() |
29 | | - { |
30 | | - // send create command |
31 | | - CommandResponse = await Post(new CreateTentativeReservationRequest { SeatId = SeatId }); |
32 | | - } |
33 | | -} |
34 | | - |
35 | | -public class CreateTentativeReservationTests: IClassFixture<CreateTentativeReservationFixture> |
36 | | -{ |
37 | | - private readonly CreateTentativeReservationFixture fixture; |
38 | | - |
39 | | - public CreateTentativeReservationTests(CreateTentativeReservationFixture fixture) |
40 | | - { |
41 | | - this.fixture = fixture; |
42 | | - } |
43 | | - |
44 | | - [Fact] |
45 | | - [Trait("Category", "Acceptance")] |
46 | | - public async Task CreateCommand_ShouldReturn_CreatedStatus_With_ReservationId() |
47 | | - { |
48 | | - var commandResponse = fixture.CommandResponse; |
49 | | - commandResponse.EnsureSuccessStatusCode(); |
50 | | - commandResponse.StatusCode.Should().Be(HttpStatusCode.Created); |
51 | | - |
52 | | - // get created record id |
53 | | - var createdId = await commandResponse.GetResultFromJson<Guid>(); |
54 | | - createdId.Should().NotBeEmpty(); |
55 | | - } |
56 | | - |
57 | 17 | [Fact] |
58 | | - [Trait("Category", "Acceptance")] |
59 | | - public async Task CreateCommand_ShouldPublish_TentativeReservationCreated() |
| 18 | + public async Task Post_ShouldReturn_CreatedStatus_With_CartId() |
60 | 19 | { |
61 | | - var createdReservationId = await fixture.CommandResponse.GetResultFromJson<Guid>(); |
62 | | - |
63 | | - await fixture.ShouldPublishInternalEventOfType<TentativeReservationCreated>( |
64 | | - @event => |
65 | | - @event.ReservationId == createdReservationId |
66 | | - && @event.SeatId == fixture.SeatId |
67 | | - && !string.IsNullOrEmpty(@event.Number) |
| 20 | + var createdReservationId = Guid.Empty; |
| 21 | + |
| 22 | + await API.Scenario( |
| 23 | + // Create Reservations |
| 24 | + API.Given( |
| 25 | + URI("/api/Reservations/"), |
| 26 | + BODY(new CreateTentativeReservationRequest { SeatId = SeatId }) |
| 27 | + ) |
| 28 | + .When(POST) |
| 29 | + .Then(CREATED, |
| 30 | + response => |
| 31 | + { |
| 32 | + createdReservationId = response.GetCreatedId<Guid>(); |
| 33 | + return ValueTask.CompletedTask; |
| 34 | + }), |
| 35 | + |
| 36 | + // Get reservation details |
| 37 | + _ => API.Given( |
| 38 | + URI($"/api/Reservations/{createdReservationId}") |
| 39 | + ) |
| 40 | + .When(GET) |
| 41 | + .Then( |
| 42 | + OK, |
| 43 | + RESPONSE_BODY<ReservationDetails>(reservation => |
| 44 | + { |
| 45 | + reservation.Id.Should().Be(createdReservationId); |
| 46 | + reservation.Status.Should().Be(ReservationStatus.Tentative); |
| 47 | + reservation.SeatId.Should().Be(SeatId); |
| 48 | + reservation.Number.Should().NotBeEmpty(); |
| 49 | + reservation.Version.Should().Be(1); |
| 50 | + })), |
| 51 | + |
| 52 | + // Get reservations list |
| 53 | + _ => API.Given( |
| 54 | + URI("/api/Reservations/") |
| 55 | + ) |
| 56 | + .When(GET) |
| 57 | + .Then( |
| 58 | + OK, |
| 59 | + RESPONSE_BODY<PagedListResponse<ReservationShortInfo>>(reservations => |
| 60 | + { |
| 61 | + reservations.Should().NotBeNull(); |
| 62 | + reservations.Items.Should().NotBeNull(); |
| 63 | + |
| 64 | + reservations.Items.Should().HaveCount(1); |
| 65 | + reservations.TotalItemCount.Should().Be(1); |
| 66 | + reservations.HasNextPage.Should().Be(false); |
| 67 | + |
| 68 | + var reservationInfo = reservations.Items.Single(); |
| 69 | + |
| 70 | + reservationInfo.Id.Should().Be(createdReservationId); |
| 71 | + reservationInfo.Number.Should().NotBeNull().And.NotBeEmpty(); |
| 72 | + reservationInfo.Status.Should().Be(ReservationStatus.Tentative); |
| 73 | + })), |
| 74 | + |
| 75 | + // Get reservation history |
| 76 | + _ => API.Given( |
| 77 | + URI($"/api/Reservations/{createdReservationId}/history") |
| 78 | + ) |
| 79 | + .When(GET) |
| 80 | + .Then( |
| 81 | + OK, |
| 82 | + RESPONSE_BODY<PagedListResponse<ReservationHistory>>(reservations => |
| 83 | + { |
| 84 | + reservations.Should().NotBeNull(); |
| 85 | + reservations.Items.Should().NotBeNull(); |
| 86 | + |
| 87 | + reservations.Items.Should().HaveCount(1); |
| 88 | + reservations.TotalItemCount.Should().Be(1); |
| 89 | + reservations.HasNextPage.Should().Be(false); |
| 90 | + |
| 91 | + var reservationInfo = reservations.Items.Single(); |
| 92 | + |
| 93 | + reservationInfo.ReservationId.Should().Be(createdReservationId); |
| 94 | + reservationInfo.Description.Should().StartWith("Created tentative reservation with number"); |
| 95 | + })) |
68 | 96 | ); |
69 | 97 | } |
70 | 98 |
|
71 | | - [Fact] |
72 | | - [Trait("Category", "Acceptance")] |
73 | | - public async Task CreateCommand_ShouldCreate_ReservationDetailsReadModel() |
74 | | - { |
75 | | - var createdReservationId = await fixture.CommandResponse.GetResultFromJson<Guid>(); |
| 99 | + private readonly Guid SeatId = Guid.NewGuid(); |
76 | 100 |
|
77 | | - // prepare query |
78 | | - var query = $"{createdReservationId}"; |
| 101 | + private readonly ApiSpecification<Program> API; |
79 | 102 |
|
80 | | - //send query |
81 | | - var queryResponse = await fixture.Get(query); |
82 | | - queryResponse.EnsureSuccessStatusCode(); |
83 | | - |
84 | | - var reservationDetails = await queryResponse.GetResultFromJson<ReservationDetails>(); |
85 | | - reservationDetails.Id.Should().Be(createdReservationId); |
86 | | - reservationDetails.Number.Should().NotBeNull().And.NotBeEmpty(); |
87 | | - reservationDetails.Status.Should().Be(ReservationStatus.Tentative); |
88 | | - } |
89 | | - |
90 | | - [Fact] |
91 | | - [Trait("Category", "Acceptance")] |
92 | | - public async Task CreateCommand_ShouldCreate_ReservationList() |
93 | | - { |
94 | | - var createdReservationId = await fixture.CommandResponse.GetResultFromJson<Guid>(); |
95 | | - |
96 | | - //send query |
97 | | - var queryResponse = await fixture.Get(); |
98 | | - queryResponse.EnsureSuccessStatusCode(); |
99 | | - |
100 | | - var reservationPagedList = await queryResponse.GetResultFromJson<PagedListResponse<ReservationShortInfo>>(); |
101 | | - |
102 | | - reservationPagedList.Should().NotBeNull(); |
103 | | - reservationPagedList.Items.Should().NotBeNull(); |
104 | | - |
105 | | - reservationPagedList.Items.Should().HaveCount(1); |
106 | | - reservationPagedList.TotalItemCount.Should().Be(1); |
107 | | - reservationPagedList.HasNextPage.Should().Be(false); |
108 | | - |
109 | | - var reservationInfo = reservationPagedList.Items.Single(); |
110 | | - |
111 | | - reservationInfo.Id.Should().Be(createdReservationId); |
112 | | - reservationInfo.Number.Should().NotBeNull().And.NotBeEmpty(); |
113 | | - reservationInfo.Status.Should().Be(ReservationStatus.Tentative); |
114 | | - } |
115 | | - |
116 | | - |
117 | | - [Fact] |
118 | | - [Trait("Category", "Acceptance")] |
119 | | - public async Task CreateCommand_ShouldCreate_ReservationHistory() |
120 | | - { |
121 | | - var createdReservationId = await fixture.CommandResponse.GetResultFromJson<Guid>(); |
122 | | - |
123 | | - // prepare query |
124 | | - var query = $"{createdReservationId}/history"; |
125 | | - |
126 | | - //send query |
127 | | - var queryResponse = await fixture.Get(query); |
128 | | - queryResponse.EnsureSuccessStatusCode(); |
129 | | - |
130 | | - var reservationPagedList = await queryResponse.GetResultFromJson<PagedListResponse<ReservationHistory>>(); |
131 | | - |
132 | | - reservationPagedList.Should().NotBeNull(); |
133 | | - reservationPagedList.Items.Should().NotBeNull(); |
134 | | - |
135 | | - reservationPagedList.Items.Should().HaveCount(1); |
136 | | - reservationPagedList.TotalItemCount.Should().Be(1); |
137 | | - reservationPagedList.HasNextPage.Should().Be(false); |
138 | | - |
139 | | - var reservationInfo = reservationPagedList.Items.Single(); |
140 | | - |
141 | | - reservationInfo.ReservationId.Should().Be(createdReservationId); |
142 | | - reservationInfo.Description.Should().StartWith("Created tentative reservation with number"); |
143 | | - } |
| 103 | + public CreateTentativeReservationTests(TestWebApplicationFactory<Program> fixture) => |
| 104 | + API = ApiSpecification<Program>.Setup(fixture); |
144 | 105 | } |
0 commit comments