From 8a9492e5733a68a07cc4cfb2c9791bd1da9ff4e3 Mon Sep 17 00:00:00 2001 From: "DESKTOP-V1VLQ15\\dsanz" Date: Tue, 14 Mar 2017 16:52:33 +0100 Subject: [PATCH] Add catalog test. --- .../FunctionalTests/FunctionalTests.csproj | 1 + .../Services/Catalog/CatalogScenarios.cs | 55 +++++++++++++++++++ .../Services/Catalog/CatalogScenariosBase.cs | 38 +++++++++++++ .../Services/Catalog/CatalogTestsStartup.cs | 15 +++++ test/Services/FunctionalTests/settings.json | 3 +- 5 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test/Services/FunctionalTests/Services/Catalog/CatalogScenarios.cs create mode 100644 test/Services/FunctionalTests/Services/Catalog/CatalogScenariosBase.cs create mode 100644 test/Services/FunctionalTests/Services/Catalog/CatalogTestsStartup.cs diff --git a/test/Services/FunctionalTests/FunctionalTests.csproj b/test/Services/FunctionalTests/FunctionalTests.csproj index 039a734a2..0e24f233d 100644 --- a/test/Services/FunctionalTests/FunctionalTests.csproj +++ b/test/Services/FunctionalTests/FunctionalTests.csproj @@ -13,6 +13,7 @@ + diff --git a/test/Services/FunctionalTests/Services/Catalog/CatalogScenarios.cs b/test/Services/FunctionalTests/Services/Catalog/CatalogScenarios.cs new file mode 100644 index 000000000..ec4643ce7 --- /dev/null +++ b/test/Services/FunctionalTests/Services/Catalog/CatalogScenarios.cs @@ -0,0 +1,55 @@ +using Microsoft.eShopOnContainers.Services.Catalog.API.Model; +using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace FunctionalTests.Services.Catalog +{ + public class CatalogScenarios : CatalogScenariosBase + { + [Fact] + public async Task Post_update_a_catalogitem_price_and_catalogitem_is_returned_modified() + { + using (var server = CreateServer()) + { + var client = server.CreateClient(); + + // Arrange + var itemToModify = GetCatalogItem(); + var newPrice = new Random().Next(1, 200); + itemToModify.Price = newPrice; + + // Act + var postRes = await client.PostAsync(Post.UpdateCatalogProduct, + new StringContent(JsonConvert.SerializeObject(itemToModify), + UTF8Encoding.UTF8, "application/json")); + var response = await client.GetAsync(Get.ProductByName(itemToModify.Name)); + var result = JsonConvert.DeserializeObject>(await response.Content.ReadAsStringAsync()); + var item = result.Data.First(); + + // Assert + Assert.Equal(result.Count, 1); + Assert.Equal(itemToModify.Id, item.Id); + Assert.Equal(newPrice, item.Price); + } + + } + + private CatalogItem GetCatalogItem() + { + return new CatalogItem() + { + Id = 1, + Price = 12.5M, + Name = ".NET Bot Black Sweatshirt" + }; + } + + } +} diff --git a/test/Services/FunctionalTests/Services/Catalog/CatalogScenariosBase.cs b/test/Services/FunctionalTests/Services/Catalog/CatalogScenariosBase.cs new file mode 100644 index 000000000..b4107b3bc --- /dev/null +++ b/test/Services/FunctionalTests/Services/Catalog/CatalogScenariosBase.cs @@ -0,0 +1,38 @@ +using FunctionalTests.Middleware; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.eShopOnContainers.Services.Catalog.API; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace FunctionalTests.Services.Catalog +{ + public class CatalogScenariosBase + { + public TestServer CreateServer() + { + var webHostBuilder = new WebHostBuilder(); + webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory()); + webHostBuilder.UseStartup(); + + return new TestServer(webHostBuilder); + } + + public static class Get + { + public static string Orders = "api/v1/orders"; + + public static string ProductByName(string name) + { + return $"api/v1/catalog/items/withname/{name}"; + } + } + + public static class Post + { + public static string UpdateCatalogProduct = "api/v1/catalog"; + } + } +} diff --git a/test/Services/FunctionalTests/Services/Catalog/CatalogTestsStartup.cs b/test/Services/FunctionalTests/Services/Catalog/CatalogTestsStartup.cs new file mode 100644 index 000000000..f06f9b248 --- /dev/null +++ b/test/Services/FunctionalTests/Services/Catalog/CatalogTestsStartup.cs @@ -0,0 +1,15 @@ +using Microsoft.eShopOnContainers.Services.Catalog.API; +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.AspNetCore.Hosting; + +namespace FunctionalTests.Services.Catalog +{ + public class CatalogTestsStartup : Startup + { + public CatalogTestsStartup(IHostingEnvironment env) : base(env) + { + } + } +} diff --git a/test/Services/FunctionalTests/settings.json b/test/Services/FunctionalTests/settings.json index 3b6023956..7091ec819 100644 --- a/test/Services/FunctionalTests/settings.json +++ b/test/Services/FunctionalTests/settings.json @@ -1,5 +1,6 @@ { "ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;", "IdentityUrl": "http://localhost:5105", - "isTest": "true" + "isTest": "true", + "EventBusConnection": "localhost" }