From 90756eb3c07ce6f604085f54366eb02136f27987 Mon Sep 17 00:00:00 2001 From: Wederson Santos Date: Sat, 19 Oct 2024 17:26:16 -0300 Subject: [PATCH 1/7] Update appsettings.json --- Api/appsettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Api/appsettings.json b/Api/appsettings.json index e47fe81..101f574 100644 --- a/Api/appsettings.json +++ b/Api/appsettings.json @@ -7,7 +7,7 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "MySql": "Server=localhost;Database=minimal_api;Uid=root;Pwd=root;" + "MySql": "Server=localhost;Database=minimal_api;Uid=root;Pwd=password;" }, "Jwt": "minimal-api-alunos-vamos_lá" } From 7c1cef94c3791a49a4fc538857f582a439d3f3bd Mon Sep 17 00:00:00 2001 From: Wederson Santos Date: Sat, 19 Oct 2024 17:28:49 -0300 Subject: [PATCH 2/7] Update appsettings.json --- Test/appsettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/appsettings.json b/Test/appsettings.json index 3173660..101f574 100644 --- a/Test/appsettings.json +++ b/Test/appsettings.json @@ -7,7 +7,7 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "MySql": "Server=localhost;Database=minimal_apitest;Uid=root;Pwd=root;" + "MySql": "Server=localhost;Database=minimal_api;Uid=root;Pwd=password;" }, "Jwt": "minimal-api-alunos-vamos_lá" } From 0c89b50f1e6c6067a3173c6bbd3a3e4df2aefc58 Mon Sep 17 00:00:00 2001 From: Wederson Santos Date: Sat, 19 Oct 2024 17:37:13 -0300 Subject: [PATCH 3/7] Add files via upload --- Test/Requests/VeiculoRequestTest.cs | 185 ++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 Test/Requests/VeiculoRequestTest.cs diff --git a/Test/Requests/VeiculoRequestTest.cs b/Test/Requests/VeiculoRequestTest.cs new file mode 100644 index 0000000..22fdcfe --- /dev/null +++ b/Test/Requests/VeiculoRequestTest.cs @@ -0,0 +1,185 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using MinimalApi.Dominio.Entidades; +using MinimalApi.Dominio.ModelViews; +using MinimalApi.DTOs; +using Test.Helpers; + +namespace Test.Requests; + +[TestClass] +public class VeiculoRequestTest +{ + [ClassInitialize] + public static void ClassInit(TestContext testContext) + { + Setup.ClassInit(testContext); + } + + [ClassCleanup] + public static void ClassCleanup() + { + Setup.ClassCleanup(); + } + + [TestMethod] + public async Task TestIncluirVeiculo() + { + // Arrange + VeiculoDTO veiculoDTO = new() + { + Ano = 2024, + Marca = "Honda", + Nome = "Civic", + }; + var loginDTO = new LoginDTO{ + Email = "adm@teste.com", + Senha = "123456" + }; + var contentLogin = new StringContent(JsonSerializer.Serialize(loginDTO), Encoding.UTF8, "Application/json"); + var responseLogin = await Setup.client.PostAsync("/administradores/login", contentLogin); + var result = await responseLogin.Content.ReadAsStringAsync(); + var admLogado = JsonSerializer.Deserialize(result, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + var request = new HttpRequestMessage(HttpMethod.Post, "/veiculos") + { + Content = new StringContent(JsonSerializer.Serialize(veiculoDTO), Encoding.UTF8, "application/json") + }; + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", admLogado?.Token); + // Act + var response = await Setup.client.SendAsync(request); + // Assert + Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); + + } + [TestMethod] + public async Task TestGetVeiculoPorId(){ + var loginDTO = new LoginDTO{ + Email = "adm@teste.com", + Senha = "123456" + }; + var contentLogin = new StringContent(JsonSerializer.Serialize(loginDTO), Encoding.UTF8, "Application/json"); + var responseLogin = await Setup.client.PostAsync("/administradores/login", contentLogin); + var result = await responseLogin.Content.ReadAsStringAsync(); + var admLogado = JsonSerializer.Deserialize(result, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + var request = new HttpRequestMessage(HttpMethod.Get, $"/veiculos/{1}"); + + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", admLogado?.Token); + + // Act + var response = await Setup.client.SendAsync(request); + // Assert + if (response.StatusCode == HttpStatusCode.NotFound) + { + Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); + } + else + { + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + var veiculo = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + Assert.IsNotNull(veiculo); + Assert.AreEqual(1, veiculo.Id); + } + } + + [TestMethod] + public async Task TestAtualizarVeiculo(){ + var loginDTO = new LoginDTO{ + Email = "adm@teste.com", + Senha = "123456" + }; + VeiculoDTO veiculoDTO = new() + { + Ano = 2024, + Marca = "Honda", + Nome = "Civic", + }; + var contentLogin = new StringContent(JsonSerializer.Serialize(loginDTO), Encoding.UTF8, "Application/json"); + var responseLogin = await Setup.client.PostAsync("/administradores/login", contentLogin); + var result = await responseLogin.Content.ReadAsStringAsync(); + var admLogado = JsonSerializer.Deserialize(result, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + var request = new HttpRequestMessage(HttpMethod.Put, $"/veiculos/{1}") + { + Content = new StringContent(JsonSerializer.Serialize(veiculoDTO), Encoding.UTF8, "application/json") + }; + + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", admLogado?.Token); + + // Act + var response = await Setup.client.SendAsync(request); + // Assert + if (response.StatusCode == HttpStatusCode.NotFound) + { + Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); + } + + else + { + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + var veiculoAtualizado = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + Assert.IsNotNull(veiculoAtualizado); + Assert.AreEqual(veiculoDTO.Nome, veiculoAtualizado.Nome); + Assert.AreEqual(veiculoDTO.Marca, veiculoAtualizado.Marca); + Assert.AreEqual(veiculoDTO.Ano, veiculoAtualizado.Ano); + } + } + + [TestMethod] + public async Task TestApagarVeiculo() + { + // Arrange + int veiculoId = 1; // Substitua pelo ID do veículo que deseja testar + + var loginDTO = new LoginDTO + { + Email = "adm@teste.com", + Senha = "123456" + }; + + var contentLogin = new StringContent(JsonSerializer.Serialize(loginDTO), Encoding.UTF8, "application/json"); + + var responseLogin = await Setup.client.PostAsync("/administradores/login", contentLogin); + var result = await responseLogin.Content.ReadAsStringAsync(); + var admLogado = JsonSerializer.Deserialize(result, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + // Adicione o token ao cabeçalho da requisição + var request = new HttpRequestMessage(HttpMethod.Delete, $"/veiculos/{veiculoId}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", admLogado?.Token); + + // Act + var response = await Setup.client.SendAsync(request); + + // Assert + if (response.StatusCode == HttpStatusCode.NotFound) + { + Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); + } + else + { + Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); + } + } + + + + +} \ No newline at end of file From b4231eeff64e64d4ae8f678904977a8abd4f4a14 Mon Sep 17 00:00:00 2001 From: Wederson Santos Date: Sat, 19 Oct 2024 17:39:27 -0300 Subject: [PATCH 4/7] Add files via upload --- Test/Mocks/VeiculServicoMock.cs | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Test/Mocks/VeiculServicoMock.cs diff --git a/Test/Mocks/VeiculServicoMock.cs b/Test/Mocks/VeiculServicoMock.cs new file mode 100644 index 0000000..92d9e72 --- /dev/null +++ b/Test/Mocks/VeiculServicoMock.cs @@ -0,0 +1,53 @@ +using MinimalApi.Dominio.Entidades; +using MinimalApi.Dominio.Interfaces; + +namespace Test.Mocks +{ + public class VeiculoServicoMock : IVeiculoServico + { + private static readonly List veiculos = new(){ + new Veiculo{ + Id = 1, + Ano = 2024, + Marca = "Honda", + Nome = "Civic Type R" + }, + new Veiculo{ + Id = 2, + Ano = 2001, + Marca = "Toyota", + Nome = "Corola" + } + }; + + + + public void Apagar(Veiculo veiculo) + { + veiculos.Remove(veiculo); + } + + public void Atualizar(Veiculo veiculo) + { + veiculos[veiculo.Id] = veiculo; + + } + + public Veiculo? BuscaPorId(int id) + { + return veiculos.Find(veiculo => veiculo.Id == id); + } + + public void Incluir(Veiculo veiculo) + { + veiculo.Id = veiculos.Count + 1; + veiculos.Add(veiculo); + + } + + public List Todos(int? pagina = 1, string? nome = null, string? marca = null) + { + return veiculos; + } + } +} \ No newline at end of file From 6bb8ad7cd7b9ee0f81244bce3b36fbeff07f420f Mon Sep 17 00:00:00 2001 From: Wederson Santos Date: Sat, 19 Oct 2024 17:40:48 -0300 Subject: [PATCH 5/7] Update Setup.cs --- Test/Helpers/Setup.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Test/Helpers/Setup.cs b/Test/Helpers/Setup.cs index b2301fb..86e954e 100644 --- a/Test/Helpers/Setup.cs +++ b/Test/Helpers/Setup.cs @@ -25,6 +25,7 @@ public static void ClassInit(TestContext testContext) builder.ConfigureServices(services => { services.AddScoped(); + services.AddScoped(); }); }); @@ -36,4 +37,4 @@ public static void ClassCleanup() { Setup.http.Dispose(); } -} \ No newline at end of file +} From 9f97093efc4a94dcb8053e91628019acd661cae6 Mon Sep 17 00:00:00 2001 From: Wederson Santos Date: Sat, 19 Oct 2024 17:43:11 -0300 Subject: [PATCH 6/7] Add files via upload --- Test/Domain/Entidades/VeiculosTest.cs | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Test/Domain/Entidades/VeiculosTest.cs diff --git a/Test/Domain/Entidades/VeiculosTest.cs b/Test/Domain/Entidades/VeiculosTest.cs new file mode 100644 index 0000000..c5677cd --- /dev/null +++ b/Test/Domain/Entidades/VeiculosTest.cs @@ -0,0 +1,28 @@ +using MinimalApi.Dominio.Entidades; +namespace Test.Domain.Entidades +{ + [TestClass] + public class VeiculosTest + { + [TestMethod] + public void TestMethod1() + { + // Arrange + + Veiculo veiculo = new() + { + // Act + Ano = 2001, + Id = 1, + Marca = "Toyota", + Nome = "Corola" + }; + + // Assert + Assert.AreEqual(1, veiculo.Id); + Assert.AreEqual("Toyota", veiculo.Marca); + Assert.AreEqual("Corola", veiculo.Nome); + Assert.AreEqual(2001, veiculo.Ano); + } + } +} \ No newline at end of file From 0fb971f082cfbdd7de33bf0ba154f162566fcf41 Mon Sep 17 00:00:00 2001 From: Wederson Santos Date: Sat, 19 Oct 2024 17:44:33 -0300 Subject: [PATCH 7/7] Add files via upload --- Test/Domain/Servicos/VeiculoServicoTest.cs | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Test/Domain/Servicos/VeiculoServicoTest.cs diff --git a/Test/Domain/Servicos/VeiculoServicoTest.cs b/Test/Domain/Servicos/VeiculoServicoTest.cs new file mode 100644 index 0000000..ab67644 --- /dev/null +++ b/Test/Domain/Servicos/VeiculoServicoTest.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using MinimalApi.Dominio.Entidades; +using MinimalApi.Dominio.Servicos; +using MinimalApi.Infraestrutura.Db; + +namespace Test.Domain.Servicos +{ + [TestClass] + public class VeiculoServicoTest + { + // Arrange + private static DbContexto CriarContextoDeTeste() + { + var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + var path = Path.GetFullPath(Path.Combine(assemblyPath ?? "", "..", "..", "..")); + + var builder = new ConfigurationBuilder() + .SetBasePath(path ?? Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddEnvironmentVariables(); + + var configuration = builder.Build(); + + return new DbContexto(configuration); + } + + + private VeiculoServico servico = new(CriarContextoDeTeste()); + + + [TestMethod] + public void TestIncluir(){ + // Arrange + Veiculo veiculo = new() + { + Ano = 2001, + Id = 1, + Marca = "Toyota", + Nome = "Corola" + }; + // Act + servico.Incluir(veiculo); + + // Assert + Assert.AreEqual(1, servico.Todos(1).Count); + servico.Apagar(veiculo); + } + + + [TestMethod] + public void TestApagar(){ + // Arrange + Veiculo veiculo = new() + { + Ano = 2001, + Id = 2, + Marca = "Toyota", + Nome = "Corola" + }; + // Act + servico.Incluir(veiculo); + servico.Apagar(veiculo); + + // Assert + Assert.AreEqual(1, servico.Todos(1).Count); + + } + + [TestMethod] + public void TestAtualizar(){ + // Arrange + Veiculo veiculo = new() + { + Ano = 2024, + Id = 1, + Marca = "Renaut", + Nome = "Qwid" + }; + // Act + servico.Atualizar(veiculo); + + // Assert + Assert.AreEqual(2024, veiculo.Ano); + Assert.AreEqual("Renaut", veiculo.Marca); + Assert.AreEqual("Qwid", veiculo.Nome); + + + } + + [TestMethod] + public void TestBuscaPorId(){ + // Arrange + Veiculo veiculo = new() + { + Ano = 2024, + Id = 4, + Marca = "GTR", + Nome = "Porshe" + }; + // Act + servico.Incluir(veiculo); + Veiculo? veiculoDoBanco = servico.BuscaPorId(veiculo.Id); + + // Assert + Assert.AreEqual(4, veiculoDoBanco?.Id); + } + } +} \ No newline at end of file