diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e7e755a..e45a0d6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -44,5 +44,5 @@ jobs: run: | .\.sonar\scanner\dotnet-sonarscanner begin /k:"draialexis_cat_cafe" /o:"draialexis" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml dotnet build cat_cafe\cat_cafe.sln - dotnet test cat_cafe\cat_cafe.Tests\cat_cafe.Tests.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./coverage.xml + dotnet test Tests\Tests.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./coverage.xml .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}" \ No newline at end of file diff --git a/ApiGateway/Properties/launchSettings.json b/ApiGateway/Properties/launchSettings.json index 936506d..7f204a4 100644 --- a/ApiGateway/Properties/launchSettings.json +++ b/ApiGateway/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "$schema": "https://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, @@ -11,7 +11,6 @@ "profiles": { "ApiGateway": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://localhost:5003;http://localhost:5197", @@ -28,4 +27,4 @@ } } } -} +} \ No newline at end of file diff --git a/Tests/Controllers/CustomersControllerTest.cs b/Tests/Controllers/CustomersControllerTest.cs new file mode 100644 index 0000000..2a29ee6 --- /dev/null +++ b/Tests/Controllers/CustomersControllerTest.cs @@ -0,0 +1,151 @@ +using AutoMapper; +using cat_cafe.Dto; +using cat_cafe.Entities; +using cat_cafe.Mappers; +using cat_cafe.Repositories; +using cat_cafe.WeSo; +using FluentAssertions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace cat_cafe.Controllers.Tests +{ + + [TestClass()] + public class CustomersControllerTest + { + + private readonly ILogger logger = new NullLogger(); + + private readonly MapperConfiguration mapperConf = new(mapper => mapper.AddProfile(typeof(CustomerMapper))); + + private readonly DbContextOptions options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "CatCafeTests") + .Options; + + private readonly Customer alice = new() + { + Id = 1, + FullName = "Alice ", + Age = 5 + }; + + private readonly Customer bob = new() + { + Id = 2, + FullName = "Bob", + Age = 7 + }; + + + private readonly CustomerDto aliceDto; + + private readonly CustomerDto bobDto; + + private readonly IMapper mapper; + + private readonly CatCafeContext context; + + private readonly CustomersController controller; + + public CustomersControllerTest() + { + mapper = mapperConf.CreateMapper(); + context = new CatCafeContext(options); + controller = new CustomersController(context, mapper, logger); + aliceDto = mapper.Map(alice); + bobDto = mapper.Map(bob); + } + + + [TestInitialize] + public void BeforeEach() + { + context.Database.EnsureCreated(); + context.Customers.AddRange(alice, bob); + context.SaveChanges(); + } + + [TestCleanup] + public void AfterEach() + { + context.Database.EnsureDeleted(); + } + + [TestMethod()] + public async Task GetCustomersTest() + { + // control response type + var actual = await controller.GetCustomers(); + actual.Result.Should().BeOfType(); + + // control response object + var actualResult = actual.Result as OkObjectResult; + actualResult.Should().NotBeNull(); + actualResult!.Value.Should() + .BeEquivalentTo(new List() { aliceDto, bobDto }.AsEnumerable()); + } + + [TestMethod()] + public async Task GetCustomerTest() + { + // control response type + var actual = await controller.GetCustomer(1); + actual.Result.Should().BeOfType(); + + // control response object + var actualResult = actual.Result as OkObjectResult; + actualResult.Should().NotBeNull(); + actualResult!.Value.Should().BeEquivalentTo(aliceDto); + } + + + [TestMethod()] + public async Task PutCustomerTest() + { + // Arrange + CustomerDto jhone = new() { Id = 2, FullName = "bob" }; + + // Act + var responseType = await controller.PutCustomer(bob.Id, jhone); + + // Assert + responseType.Should().BeOfType(); + var actual = await controller.GetCustomer(bob.Id); + var actualResult = actual.Result as OkObjectResult; + actualResult!.Value.Should().BeEquivalentTo(jhone); + } + + + [TestMethod()] + public async Task PostCustomerTest() + { + // Arrange + CustomerDto clyde = new() { Id = 3, FullName = "Clyde" }; + + // Act + var responseType = await controller.PostCustomer(clyde); + + // Assert + responseType.Result.Should().BeOfType(); + var actual = await controller.GetCustomer(clyde.Id); + var actualResult = actual.Result as OkObjectResult; + actualResult!.Value.Should().BeEquivalentTo(clyde); + } + + [TestMethod()] + public async Task DeleteCustomerTest() + { + // Act + var responseType = await controller.DeleteCustomer(alice.Id); + + // Assert + responseType.Should().BeOfType(); + + var actual = await controller.GetCustomer(alice.Id); + actual.Result.Should().BeOfType(); + } + } +} \ No newline at end of file