Included file-scope namespace catalog.functionaltests
This commit is contained in:
		
							parent
							
								
									b772a8363f
								
							
						
					
					
						commit
						d5c2342a05
					
				@ -1,93 +1,78 @@
 | 
			
		||||
using Catalog.API.Extensions;
 | 
			
		||||
using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
using Microsoft.AspNetCore.TestHost;
 | 
			
		||||
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
 | 
			
		||||
using Microsoft.eShopOnContainers.Services.Catalog.API;
 | 
			
		||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
 | 
			
		||||
using Microsoft.Extensions.Configuration;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using Microsoft.Extensions.Hosting;
 | 
			
		||||
using Microsoft.Extensions.Logging;
 | 
			
		||||
using Microsoft.Extensions.Options;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
namespace Catalog.FunctionalTests;
 | 
			
		||||
 | 
			
		||||
namespace Catalog.FunctionalTests
 | 
			
		||||
public class CatalogScenariosBase
 | 
			
		||||
{
 | 
			
		||||
    public class CatalogScenariosBase
 | 
			
		||||
    public TestServer CreateServer()
 | 
			
		||||
    {
 | 
			
		||||
        public TestServer CreateServer()
 | 
			
		||||
        var path = Assembly.GetAssembly(typeof(CatalogScenariosBase))
 | 
			
		||||
            .Location;
 | 
			
		||||
 | 
			
		||||
        var hostBuilder = new WebHostBuilder()
 | 
			
		||||
            .UseContentRoot(Path.GetDirectoryName(path))
 | 
			
		||||
            .ConfigureAppConfiguration(cb =>
 | 
			
		||||
            {
 | 
			
		||||
                cb.AddJsonFile("appsettings.json", optional: false)
 | 
			
		||||
                .AddEnvironmentVariables();
 | 
			
		||||
            })
 | 
			
		||||
            .UseStartup<Startup>();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        var testServer = new TestServer(hostBuilder);
 | 
			
		||||
 | 
			
		||||
        testServer.Host
 | 
			
		||||
            .MigrateDbContext<CatalogContext>((context, services) =>
 | 
			
		||||
            {
 | 
			
		||||
                var env = services.GetService<IWebHostEnvironment>();
 | 
			
		||||
                var settings = services.GetService<IOptions<CatalogSettings>>();
 | 
			
		||||
                var logger = services.GetService<ILogger<CatalogContextSeed>>();
 | 
			
		||||
 | 
			
		||||
                new CatalogContextSeed()
 | 
			
		||||
                .SeedAsync(context, env, settings, logger)
 | 
			
		||||
                .Wait();
 | 
			
		||||
            })
 | 
			
		||||
            .MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
 | 
			
		||||
 | 
			
		||||
        return testServer;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class Get
 | 
			
		||||
    {
 | 
			
		||||
        private const int PageIndex = 0;
 | 
			
		||||
        private const int PageCount = 4;
 | 
			
		||||
 | 
			
		||||
        public static string Items(bool paginated = false)
 | 
			
		||||
        {
 | 
			
		||||
            var path = Assembly.GetAssembly(typeof(CatalogScenariosBase))
 | 
			
		||||
              .Location;
 | 
			
		||||
 | 
			
		||||
            var hostBuilder = new WebHostBuilder()
 | 
			
		||||
                .UseContentRoot(Path.GetDirectoryName(path))
 | 
			
		||||
                .ConfigureAppConfiguration(cb =>
 | 
			
		||||
                {
 | 
			
		||||
                    cb.AddJsonFile("appsettings.json", optional: false)
 | 
			
		||||
                    .AddEnvironmentVariables();
 | 
			
		||||
                })
 | 
			
		||||
                .UseStartup<Startup>();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            var testServer = new TestServer(hostBuilder);
 | 
			
		||||
 | 
			
		||||
            testServer.Host
 | 
			
		||||
                .MigrateDbContext<CatalogContext>((context, services) =>
 | 
			
		||||
                {
 | 
			
		||||
                    var env = services.GetService<IWebHostEnvironment>();
 | 
			
		||||
                    var settings = services.GetService<IOptions<CatalogSettings>>();
 | 
			
		||||
                    var logger = services.GetService<ILogger<CatalogContextSeed>>();
 | 
			
		||||
 | 
			
		||||
                    new CatalogContextSeed()
 | 
			
		||||
                    .SeedAsync(context, env, settings, logger)
 | 
			
		||||
                    .Wait();
 | 
			
		||||
                })
 | 
			
		||||
                .MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
 | 
			
		||||
 | 
			
		||||
            return testServer;
 | 
			
		||||
            return paginated
 | 
			
		||||
                ? "api/v1/catalog/items" + Paginated(PageIndex, PageCount)
 | 
			
		||||
                : "api/v1/catalog/items";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static class Get
 | 
			
		||||
        public static string ItemById(int id)
 | 
			
		||||
        {
 | 
			
		||||
            private const int PageIndex = 0;
 | 
			
		||||
            private const int PageCount = 4;
 | 
			
		||||
            return $"api/v1/catalog/items/{id}";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            public static string Items(bool paginated = false)
 | 
			
		||||
            {
 | 
			
		||||
                return paginated
 | 
			
		||||
                    ? "api/v1/catalog/items" + Paginated(PageIndex, PageCount)
 | 
			
		||||
                    : "api/v1/catalog/items";
 | 
			
		||||
            }
 | 
			
		||||
        public static string ItemByName(string name, bool paginated = false)
 | 
			
		||||
        {
 | 
			
		||||
            return paginated
 | 
			
		||||
                ? $"api/v1/catalog/items/withname/{name}" + Paginated(PageIndex, PageCount)
 | 
			
		||||
                : $"api/v1/catalog/items/withname/{name}";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            public static string ItemById(int id)
 | 
			
		||||
            {
 | 
			
		||||
                return $"api/v1/catalog/items/{id}";
 | 
			
		||||
            }
 | 
			
		||||
        public static string Types = "api/v1/catalog/catalogtypes";
 | 
			
		||||
 | 
			
		||||
            public static string ItemByName(string name, bool paginated = false)
 | 
			
		||||
            {
 | 
			
		||||
                return paginated
 | 
			
		||||
                    ? $"api/v1/catalog/items/withname/{name}" + Paginated(PageIndex, PageCount)
 | 
			
		||||
                    : $"api/v1/catalog/items/withname/{name}";
 | 
			
		||||
            }
 | 
			
		||||
        public static string Brands = "api/v1/catalog/catalogbrands";
 | 
			
		||||
 | 
			
		||||
            public static string Types = "api/v1/catalog/catalogtypes";
 | 
			
		||||
        public static string Filtered(int catalogTypeId, int catalogBrandId, bool paginated = false)
 | 
			
		||||
        {
 | 
			
		||||
            return paginated
 | 
			
		||||
                ? $"api/v1/catalog/items/type/{catalogTypeId}/brand/{catalogBrandId}" + Paginated(PageIndex, PageCount)
 | 
			
		||||
                : $"api/v1/catalog/items/type/{catalogTypeId}/brand/{catalogBrandId}";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            public static string Brands = "api/v1/catalog/catalogbrands";
 | 
			
		||||
 | 
			
		||||
            public static string Filtered(int catalogTypeId, int catalogBrandId, bool paginated = false)
 | 
			
		||||
            {
 | 
			
		||||
                return paginated
 | 
			
		||||
                    ? $"api/v1/catalog/items/type/{catalogTypeId}/brand/{catalogBrandId}" + Paginated(PageIndex, PageCount)
 | 
			
		||||
                    : $"api/v1/catalog/items/type/{catalogTypeId}/brand/{catalogBrandId}";
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            private static string Paginated(int pageIndex, int pageCount)
 | 
			
		||||
            {
 | 
			
		||||
                return $"?pageIndex={pageIndex}&pageSize={pageCount}";
 | 
			
		||||
            }
 | 
			
		||||
        private static string Paginated(int pageIndex, int pageCount)
 | 
			
		||||
        {
 | 
			
		||||
            return $"?pageIndex={pageIndex}&pageSize={pageCount}";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,145 +1,140 @@
 | 
			
		||||
using System.Net;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Xunit;
 | 
			
		||||
namespace Catalog.FunctionalTests;
 | 
			
		||||
 | 
			
		||||
namespace Catalog.FunctionalTests
 | 
			
		||||
public class CatalogScenarios
 | 
			
		||||
    : CatalogScenariosBase
 | 
			
		||||
{
 | 
			
		||||
    public class CatalogScenarios
 | 
			
		||||
       : CatalogScenariosBase
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_all_catalogitems_and_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_all_catalogitems_and_response_ok_status_code()
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.Items());
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.Items());
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_catalogitem_by_id_and_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_catalogitem_by_id_and_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.ItemById(1));
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.ItemById(1));
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_catalogitem_by_id_and_response_bad_request_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_catalogitem_by_id_and_response_bad_request_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.ItemById(int.MinValue));
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.ItemById(int.MinValue));
 | 
			
		||||
 | 
			
		||||
                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
 | 
			
		||||
            }
 | 
			
		||||
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_catalogitem_by_id_and_response_not_found_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_catalogitem_by_id_and_response_not_found_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.ItemById(int.MaxValue));
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.ItemById(int.MaxValue));
 | 
			
		||||
 | 
			
		||||
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
 | 
			
		||||
            }
 | 
			
		||||
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_catalogitem_by_name_and_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_catalogitem_by_name_and_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.ItemByName(".NET"));
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.ItemByName(".NET"));
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_paginated_catalogitem_by_name_and_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_paginated_catalogitem_by_name_and_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                const bool paginated = true;
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.ItemByName(".NET", paginated));
 | 
			
		||||
            const bool paginated = true;
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.ItemByName(".NET", paginated));
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_paginated_catalog_items_and_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_paginated_catalog_items_and_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                const bool paginated = true;
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.Items(paginated));
 | 
			
		||||
            const bool paginated = true;
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.Items(paginated));
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_filtered_catalog_items_and_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_filtered_catalog_items_and_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.Filtered(1, 1));
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.Filtered(1, 1));
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_get_paginated_filtered_catalog_items_and_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_get_paginated_filtered_catalog_items_and_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                const bool paginated = true;
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.Filtered(1, 1, paginated));
 | 
			
		||||
            const bool paginated = true;
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.Filtered(1, 1, paginated));
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_catalog_types_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_catalog_types_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.Types);
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.Types);
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task Get_catalog_brands_response_ok_status_code()
 | 
			
		||||
    [Fact]
 | 
			
		||||
    public async Task Get_catalog_brands_response_ok_status_code()
 | 
			
		||||
    {
 | 
			
		||||
        using (var server = CreateServer())
 | 
			
		||||
        {
 | 
			
		||||
            using (var server = CreateServer())
 | 
			
		||||
            {
 | 
			
		||||
                var response = await server.CreateClient()
 | 
			
		||||
                    .GetAsync(Get.Brands);
 | 
			
		||||
            var response = await server.CreateClient()
 | 
			
		||||
                .GetAsync(Get.Brands);
 | 
			
		||||
 | 
			
		||||
                response.EnsureSuccessStatusCode();
 | 
			
		||||
            }
 | 
			
		||||
            response.EnsureSuccessStatusCode();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user