Skip to content

Commit

Permalink
Versão com wiremock.net funcionando !
Browse files Browse the repository at this point in the history
  • Loading branch information
israelsantiago committed Aug 28, 2020
1 parent 0eafca3 commit 038c92c
Show file tree
Hide file tree
Showing 51 changed files with 472 additions and 66 deletions.
37 changes: 20 additions & 17 deletions PoC.TestesServicos.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,41 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29009.5
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoC.TestesServicos.API", "Poc.ServiceTest.API\PoC.TestesServicos.API.csproj", "{726F677C-FC88-495D-9504-A95A213A4FC2}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoC.TestesServicos.API", "src\Poc.ServiceTest.API\PoC.TestesServicos.API.csproj", "{D96D535B-3E42-492D-B889-AC01B6A88FA9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoC.TestesServicos.Data", "PoC.TestesServicos.Data\PoC.TestesServicos.Data.csproj", "{5D1D6D92-B546-4F2F-8B08-EF054A0D2FE8}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoC.TestesServicos.Core", "src\PoC.TestesServicos.Core\PoC.TestesServicos.Core.csproj", "{5767B573-5F6E-4AFB-B191-048B1E3A4139}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{4E42FBA5-4F43-460E-AE5C-4A69E15D64D1}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoC.TestesServicos.Data", "src\PoC.TestesServicos.Data\PoC.TestesServicos.Data.csproj", "{5A61E0B4-AD7B-4F09-B966-BB82C3D4F34D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoC.TestesServicos.Tests", "tests\PoC.TestesServicos.Tests\PoC.TestesServicos.Tests.csproj", "{D1B26BF0-3478-4A59-AA32-77E73B2D635A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoC.TestesServicos.Tests", "src\PoC.TestesServicos.Tests\PoC.TestesServicos.Tests.csproj", "{0E6D8BDD-2354-4D70-BF29-9C205DCF90D3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{726F677C-FC88-495D-9504-A95A213A4FC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{726F677C-FC88-495D-9504-A95A213A4FC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{726F677C-FC88-495D-9504-A95A213A4FC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{726F677C-FC88-495D-9504-A95A213A4FC2}.Release|Any CPU.Build.0 = Release|Any CPU
{5D1D6D92-B546-4F2F-8B08-EF054A0D2FE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D1D6D92-B546-4F2F-8B08-EF054A0D2FE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D1D6D92-B546-4F2F-8B08-EF054A0D2FE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D1D6D92-B546-4F2F-8B08-EF054A0D2FE8}.Release|Any CPU.Build.0 = Release|Any CPU
{D1B26BF0-3478-4A59-AA32-77E73B2D635A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1B26BF0-3478-4A59-AA32-77E73B2D635A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1B26BF0-3478-4A59-AA32-77E73B2D635A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1B26BF0-3478-4A59-AA32-77E73B2D635A}.Release|Any CPU.Build.0 = Release|Any CPU
{D96D535B-3E42-492D-B889-AC01B6A88FA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D96D535B-3E42-492D-B889-AC01B6A88FA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D96D535B-3E42-492D-B889-AC01B6A88FA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D96D535B-3E42-492D-B889-AC01B6A88FA9}.Release|Any CPU.Build.0 = Release|Any CPU
{5767B573-5F6E-4AFB-B191-048B1E3A4139}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5767B573-5F6E-4AFB-B191-048B1E3A4139}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5767B573-5F6E-4AFB-B191-048B1E3A4139}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5767B573-5F6E-4AFB-B191-048B1E3A4139}.Release|Any CPU.Build.0 = Release|Any CPU
{5A61E0B4-AD7B-4F09-B966-BB82C3D4F34D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A61E0B4-AD7B-4F09-B966-BB82C3D4F34D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A61E0B4-AD7B-4F09-B966-BB82C3D4F34D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A61E0B4-AD7B-4F09-B966-BB82C3D4F34D}.Release|Any CPU.Build.0 = Release|Any CPU
{0E6D8BDD-2354-4D70-BF29-9C205DCF90D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E6D8BDD-2354-4D70-BF29-9C205DCF90D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E6D8BDD-2354-4D70-BF29-9C205DCF90D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E6D8BDD-2354-4D70-BF29-9C205DCF90D3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D1B26BF0-3478-4A59-AA32-77E73B2D635A} = {4E42FBA5-4F43-460E-AE5C-4A69E15D64D1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6C11FC8B-DF97-4059-9E8A-86DD675C8622}
Expand Down
7 changes: 7 additions & 0 deletions src/PoC.TestesServicos.Core/Interfaces/CepApiOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PoC.TestesServicos.API.Configs
{
public class CepApiOptions
{
public string Url { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/PoC.TestesServicos.Core/Interfaces/ICepClientApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Refit;
using System.Threading.Tasks;
using PoC.TestesServicos.Data.Models;

namespace PoC.TestesServicos.Core.Interfaces
{
public interface ICepClientApiService
{
[Get("/ws/{cep}/json")]
Task<CepModel> GetAddressAsync(string cep);
}
}
10 changes: 10 additions & 0 deletions src/PoC.TestesServicos.Core/Interfaces/ICepService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using PoC.TestesServicos.Data.Models;

namespace PoC.TestesServicos.Core.Interfaces
{
public interface ICepService
{
Task<CepModel> GetCepDetails(string cep);
}
}
10 changes: 10 additions & 0 deletions src/PoC.TestesServicos.Core/Interfaces/ICustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using PoC.TestesServicos.Data.Models;

namespace PoC.TestesServicos.Core.Interfaces
{
public interface ICustomerService
{
Task<Customer> GetCustomerByCode(int code);
}
}
18 changes: 18 additions & 0 deletions src/PoC.TestesServicos.Core/PoC.TestesServicos.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.7" />
<PackageReference Include="Microsoft.OpenApi" Version="1.1.4" />
<PackageReference Include="refit" Version="5.1.67" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PoC.TestesServicos.Data\PoC.TestesServicos.Data.csproj" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions src/PoC.TestesServicos.Core/Services/CepService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using System.Configuration;
using PoC.TestesServicos.Core.Interfaces;
using PoC.TestesServicos.API.Configs;
using PoC.TestesServicos.Data.Models;
using Refit;


namespace PoC.TestesServicos.Core.Services
{
public class CepService : ICepService
{
private readonly CepApiOptions _cepApiOptions;

public CepService(IOptions<CepApiOptions> cepApiOptions)
{
_cepApiOptions = cepApiOptions.Value;
}

public async Task<CepModel> GetCepDetails(string cep)
{
if (_cepApiOptions == null)
throw new ConfigurationErrorsException("Cep API Url setting must be configured!");

ICepClientApiService client = RestService.For<ICepClientApiService>(_cepApiOptions.Url);

CepModel cepDetails = await client.GetAddressAsync(cep);

return cepDetails;
}
}
}
42 changes: 42 additions & 0 deletions src/PoC.TestesServicos.Core/Services/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PoC.TestesServicos.Core.Interfaces;
using PoC.TestesServicos.Data.Models;

namespace PoC.TestesServicos.Core.Services
{
public class CustomerService : ICustomerService
{
private readonly ICepService _cepService;

public CustomerService(ICepService cepService)
{
_cepService = cepService;
}

public async Task<Customer> GetCustomerByCode(int code)
{
Customer customerResponse = CustomerFakeRepository(c => c.Code == code)
.FirstOrDefault();

if(customerResponse != null && !string.IsNullOrEmpty(customerResponse.Cep))
customerResponse.CepDetails = await _cepService.GetCepDetails(customerResponse.Cep);

return customerResponse;
}

private IEnumerable<Customer> CustomerFakeRepository(Func<Customer, bool> predicate)
{
IList<Customer> customers = new List<Customer>
{
new Customer { Code = 1, Name = "Customer 01", Cep = "95600106" },
new Customer { Code = 2, Name = "Customer 02", Cep = "95600104" }
};

return customers.Where(predicate);
}
}

}
27 changes: 27 additions & 0 deletions src/PoC.TestesServicos.Data/Models/CepModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Newtonsoft.Json;

namespace PoC.TestesServicos.Data.Models
{
public class CepModel
{
[JsonProperty("cep")]
public string Cep { get; set; }
[JsonProperty("logradouro")]
public string Logradouro { get; set; }
[JsonProperty("complemento")]
public string Complemento { get; set; }
[JsonProperty("bairro")]
public string Bairro { get; set; }
[JsonProperty("localidade")]
public string Localidade { get; set; }
[JsonProperty("uf")]
public string Uf { get; set; }
[JsonProperty("unidade")]
public string Unidade { get; set; }
[JsonProperty("ibge")]
public string Ibge { get; set; }
[JsonProperty("gia")]
public string Gia { get; set; }

}
}
10 changes: 10 additions & 0 deletions src/PoC.TestesServicos.Data/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace PoC.TestesServicos.Data.Models
{
public class Customer
{
public int Code { get; set; }
public string Name { get; set; }
public string Cep { get; set; }
public CepModel CepDetails { get; set; }
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@ namespace PoC.TestesServicos.Tests
public abstract class ControllerTestsBase : IAsyncLifetime
{
private readonly Checkpoint _checkpoint;
private readonly string _connectionString;

private readonly string _connectionStringDB;
private readonly IServiceScope _scope;
protected HttpClient Client { get; }
protected UsersDataContext Context { get; }

public ControllerTestsBase(IntegrationContainersAppFactory integrationContainersFixture)
{
Client = integrationContainersFixture.Client;
_scope = integrationContainersFixture.Server.Host.Services.CreateScope();
_connectionString = integrationContainersFixture.ConnectionString;
_connectionStringDB = integrationContainersFixture.ConnectionStringDB;
_checkpoint = new Checkpoint();
Context = _scope.ServiceProvider.GetRequiredService<UsersDataContext>();
}

protected HttpClient Client { get; }
protected UsersDataContext Context { get; }

public Task InitializeAsync()
{
return _checkpoint.Reset(_connectionString);
return _checkpoint.Reset(_connectionStringDB);
}

public Task DisposeAsync()
Expand Down
39 changes: 39 additions & 0 deletions src/PoC.TestesServicos.Tests/CustomerClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using PoC.TestesServicos.Data.Models;
using PoC.TestesServicos.Tests.Fixtures;
using Xunit;

namespace PoC.TestesServicos.Tests
{
[Collection("Integration containers collection")]
public class CustomerClientTests : ControllerTestsBase
{
public CustomerClientTests(IntegrationContainersAppFactory integrationContainersFixture)
: base(integrationContainersFixture)
{
}

[Theory]
[Trait("Categoria", "Componente")]
[InlineData("1")]
[InlineData("2")]
public async Task Should_Get_A_Valid_Customer_With_CepDetails_For_Customer_Code(string customerCode)
{
string actualResponseContent;
HttpResponseMessage response;
Customer customerResponse;

response = await Client.GetAsync($"/api/Customer/{customerCode}");
response.EnsureSuccessStatusCode();

actualResponseContent = await response.Content.ReadAsStringAsync();
customerResponse = JsonConvert.DeserializeObject<Customer>(actualResponseContent);

Assert.Equal("Mocked", customerResponse.CepDetails.Localidade);
}


}
}
Loading

0 comments on commit 038c92c

Please sign in to comment.