|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Core.Testing; |
| 7 | +using FluentAssertions; |
| 8 | +using Microsoft.AspNetCore.Hosting; |
| 9 | +using Warehouse.Products.GettingProducts; |
| 10 | +using Warehouse.Products.RegisteringProduct; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Warehouse.Api.Tests.Products.GettingProducts |
| 14 | +{ |
| 15 | + public class GetProductsFixture: ApiFixture |
| 16 | + { |
| 17 | + protected override string ApiUrl => "/api/products"; |
| 18 | + |
| 19 | + protected override Func<IWebHostBuilder, IWebHostBuilder> SetupWebHostBuilder => |
| 20 | + whb => WarehouseTestWebHostBuilder.Configure(whb, nameof(GetProductsFixture)); |
| 21 | + |
| 22 | + public IList<ProductListItem> RegisteredProducts = new List<ProductListItem>(); |
| 23 | + |
| 24 | + public override async Task InitializeAsync() |
| 25 | + { |
| 26 | + var productsToRegister = new[] |
| 27 | + { |
| 28 | + new RegisterProductRequest("ZX1234", "ValidName", "ValidDescription"), |
| 29 | + new RegisterProductRequest("AD5678", "OtherValidName", "OtherValidDescription"), |
| 30 | + new RegisterProductRequest("BH90210", "AnotherValid", "AnotherValidDescription") |
| 31 | + }; |
| 32 | + |
| 33 | + foreach (var registerProduct in productsToRegister) |
| 34 | + { |
| 35 | + var registerResponse = await Post(registerProduct); |
| 36 | + registerResponse.EnsureSuccessStatusCode() |
| 37 | + .StatusCode.Should().Be(HttpStatusCode.Created); |
| 38 | + |
| 39 | + var createdId = await registerResponse.GetResultFromJson<Guid>(); |
| 40 | + |
| 41 | + var (sku, name, _) = registerProduct; |
| 42 | + RegisteredProducts.Add(new ProductListItem(createdId, sku!, name!)); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public class GetProductsTests: IClassFixture<GetProductsFixture> |
| 48 | + { |
| 49 | + private readonly GetProductsFixture fixture; |
| 50 | + |
| 51 | + public GetProductsTests(GetProductsFixture fixture) |
| 52 | + { |
| 53 | + this.fixture = fixture; |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task ValidRequest_With_NoParams_ShouldReturn_200() |
| 58 | + { |
| 59 | + // Given |
| 60 | + |
| 61 | + // When |
| 62 | + var response = await fixture.Get(); |
| 63 | + |
| 64 | + // Then |
| 65 | + response.EnsureSuccessStatusCode() |
| 66 | + .StatusCode.Should().Be(HttpStatusCode.OK); |
| 67 | + |
| 68 | + var products = await response.GetResultFromJson<IReadOnlyList<ProductListItem>>(); |
| 69 | + products.Should().NotBeEmpty(); |
| 70 | + products.Should().BeEquivalentTo(fixture.RegisteredProducts); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public async Task ValidRequest_With_Filter_ShouldReturn_SubsetOfRecords() |
| 75 | + { |
| 76 | + // Given |
| 77 | + var filteredRecord = fixture.RegisteredProducts.First(); |
| 78 | + var filter = fixture.RegisteredProducts.First().Sku.Substring(1); |
| 79 | + |
| 80 | + // When |
| 81 | + var response = await fixture.Get($"?filter={filter}"); |
| 82 | + |
| 83 | + // Then |
| 84 | + response.EnsureSuccessStatusCode() |
| 85 | + .StatusCode.Should().Be(HttpStatusCode.OK); |
| 86 | + |
| 87 | + var products = await response.GetResultFromJson<IReadOnlyList<ProductListItem>>(); |
| 88 | + products.Should().NotBeEmpty(); |
| 89 | + products.Should().BeEquivalentTo(new List<ProductListItem>{filteredRecord}); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task ValidRequest_With_Paging_ShouldReturn_PageOfRecords() |
| 96 | + { |
| 97 | + // Given |
| 98 | + const int page = 2; |
| 99 | + const int pageSize = 1; |
| 100 | + var filteredRecords = fixture.RegisteredProducts |
| 101 | + .Skip(page - 1) |
| 102 | + .Take(pageSize) |
| 103 | + .ToList(); |
| 104 | + |
| 105 | + // When |
| 106 | + var response = await fixture.Get($"?page={page}&pageSize={pageSize}"); |
| 107 | + |
| 108 | + // Then |
| 109 | + response.EnsureSuccessStatusCode() |
| 110 | + .StatusCode.Should().Be(HttpStatusCode.OK); |
| 111 | + |
| 112 | + var products = await response.GetResultFromJson<IReadOnlyList<ProductListItem>>(); |
| 113 | + products.Should().NotBeEmpty(); |
| 114 | + products.Should().BeEquivalentTo(filteredRecords); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public async Task NegativePage_ShouldReturn_400() |
| 119 | + { |
| 120 | + // Given |
| 121 | + var pageSize = -20; |
| 122 | + |
| 123 | + // When |
| 124 | + var response = await fixture.Get($"?page={pageSize}"); |
| 125 | + |
| 126 | + // Then |
| 127 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 128 | + } |
| 129 | + |
| 130 | + [Theory] |
| 131 | + [InlineData(0)] |
| 132 | + [InlineData(-20)] |
| 133 | + public async Task NegativeOrZeroPageSize_ShouldReturn_400(int pageSize) |
| 134 | + { |
| 135 | + // Given |
| 136 | + |
| 137 | + // When |
| 138 | + var response = await fixture.Get($"?page={pageSize}"); |
| 139 | + |
| 140 | + // Then |
| 141 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments