Skip to content

Commit

Permalink
Merge branch 'main' into eduardoworrel-feature-setup-adapter-structure
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoworrel committed Jul 19, 2023
2 parents e26f3e8 + c257955 commit 00e2cf8
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI_CD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ jobs:
- name: Build and push Docker image
run: |
docker-compose -f devops/docker/docker-compose.yml build
docker-compose -f devops/docker/docker-compose.yml push
docker-compose -f devops/docker/docker-compose.yml push
10 changes: 10 additions & 0 deletions Apps/Core.BocaSuja/Domain/Interfaces/IContentSafetyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Core.BocaSuja.Domain.Entities;

namespace Core.BocaSuja.Domain.Interfaces;

public interface IContentSafetyService
{
public Task<bool> Validate(Guid id, string text);
public Task<List<Incidencia>> Rank();
public Task<List<Incidencia>> Rank(Guid id);
}
22 changes: 22 additions & 0 deletions Apps/Core.BocaSuja/Domain/Services/AzureContentSafetyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Core.BocaSuja.Domain.Entities;
using Core.BocaSuja.Domain.Interfaces;

namespace Core.BocaSuja.Domain.Services;

public class AzureContentSafetyService : IContentSafetyService
{
public Task<List<Incidencia>> Rank()
{
throw new NotImplementedException();
}

public Task<List<Incidencia>> Rank(Guid id)
{
throw new NotImplementedException();
}

public Task<bool> Validate(Guid id, string text)
{
throw new NotImplementedException();
}
}
58 changes: 57 additions & 1 deletion Apps/Web.Api.BocaSuja/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Core.BocaSuja.Domain.Interfaces;
using Core.BocaSuja.Factories;
using Core.BocaSuja.Models;
using Core.BocaSuja.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Web.Api.BocaSuja.Context;
using Web.Api.BocaSuja.HealthCheck;
Expand All @@ -24,6 +24,8 @@
);
builder.Services.AddSingleton<TempService>();

builder.Services.AddScoped<IContentSafetyService, AzureContentSafetyService>();

var app = builder.Build();

dbHealth.Check(app.Services);
Expand All @@ -39,4 +41,58 @@
}
);

app.MapGet(
"/api/v1/validate",
async (Guid? id, string? text, [FromServices] IContentSafetyService safetyService) =>
{
if (id.HasValue && !string.IsNullOrEmpty(text))
{
try
{
return Results.Ok(await safetyService.Validate(id.Value, text));
}
catch (Exception ex)
{
return Results.Problem(detail: ex.Message, statusCode: 501);
}
}
else
{
return Results.BadRequest(new BadHttpRequestException("'id' or 'text' parameter"));
}
}
);

app.MapGet(
"/api/v1/rank",
async ([FromServices] IContentSafetyService safetyService) =>
{
try
{
return Results.Ok(await safetyService.Rank());
}
catch (Exception ex)
{
return Results.Problem(detail: ex.Message, statusCode: 501);
}
}
);

app.MapGet(
"/api/v1/rank/{id}",
async (Guid id, [FromServices] IContentSafetyService safetyService) =>
{
try
{
return Results.Ok(await safetyService.Rank(id));
}
catch (Exception ex)
{
return Results.Problem(detail: ex.Message, statusCode: 501);
}
}
);

app.Run();

public partial class Program { }
2 changes: 1 addition & 1 deletion Apps/Web.Api.BocaSuja/Web.Api.BocaSuja.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.5.23280.1" />
Expand Down
90 changes: 90 additions & 0 deletions Tests/IntegrationTests/Web.Api.BocaSuja/RoutingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net;

namespace Tests.IntegrationTests.Web.Api.BocaSuja;

[TestFixture]
public class IntegrationTests
{
private WebApplicationFactory<Program> _factory;
private HttpClient _client;

[OneTimeSetUp]
public void Setup()
{
_factory = new WebApplicationFactory<Program>();
_client = _factory.CreateClient();
}

[OneTimeTearDown]
public void TearDown()
{
_client.Dispose();
_factory.Dispose();
}

[Test, Category("WebApi - Routing - Health")]
[Description("Verifica endpoint de health")]
public async Task HealthEndpoint_ShouldReturnOk()
{
var endpoint = "/health";

var response = await _client.GetAsync(endpoint);

var content = await response.Content.ReadAsStringAsync();
Assert.That(content, Is.EqualTo("OK"));
}

[Test, Category("WebApi - Routing - Validate")]
[Description("Verifica endpoint de validação")]
public async Task ValidateEndpoint_WithValidIdAndText_ShouldReturnOk()
{
var textExample = "Example Text";
var endpoint = "/api/v1/validate?id=" + Guid.NewGuid() + "&text=" + textExample;

var response = await _client.GetAsync(endpoint);

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotImplemented));
}

[Test, Category("WebApi - Routing - Validate")]
[TestCase("")]
[TestCase("texto teste")]
[Description("Verifica endpoint de validação caso falte algum parametro")]
public async Task ValidateEndpoint_WithMissingText_ShouldReturnBadRequest(string text)
{
var endpoint = $"/api/v1/validate?";

if (string.IsNullOrEmpty(text))
endpoint += "id=" + Guid.NewGuid();
else
endpoint += "text=" + text;

var response = await _client.GetAsync(endpoint);

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}

[Test, Category("WebApi - Routing - Rank")]
[Description("Verifica endpoint de rank sem guid")]
public async Task RankEndpoint_ShouldReturnOk()
{
var endpoint = "/api/v1/rank";

var response = await _client.GetAsync(endpoint);

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotImplemented));
}

[Test, Category("WebApi - Routing - Rank")]
[Description("Verifica endpoint de rank com guid")]
public async Task RankByIdEndpoint_WithValidId_ShouldReturnOk()
{
var id = Guid.NewGuid();
var endpoint = $"/api/v1/rank/{id}";

var response = await _client.GetAsync(endpoint);

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotImplemented));
}
}
13 changes: 9 additions & 4 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -10,21 +10,26 @@
<CollectCoverage>true</CollectCoverage>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0-preview.5.23280.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0-preview.5.23280.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0-preview-23364-03" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Core.BocaSuja.Domain.Interfaces;
using Core.BocaSuja.Domain.Services;

namespace Tests.UnitTests.Core.BocaSuja.Domain.Services;

[TestFixture]
public class AzureContentSafetyServiceTest
{
private AzureContentSafetyService _contentSafetyService;

[SetUp]
public void SetUp()
{
_contentSafetyService = new AzureContentSafetyService();
}

[Test, Category("Core - Domain - Services - AzureContentSafetyService")]
[Description("Não implementado")]
public void Rank_ThrowsNotImplementedException()
{
async Task action() => await _contentSafetyService.Rank();

Assert.ThrowsAsync<NotImplementedException>(action);
}

[Test, Category("Core - Domain - Services - AzureContentSafetyService")]
[Description("Não implementado")]
public void Rank_WithId_ThrowsNotImplementedException()
{
var id = Guid.NewGuid();

async Task action() => await _contentSafetyService.Rank(id);

Assert.ThrowsAsync<NotImplementedException>(action);
}

[Test, Category("Core - Domain - Services - AzureContentSafetyService")]
[Description("Não implementado")]
public void Validate_ThrowsNotImplementedException()
{
var id = Guid.NewGuid();
var text = "Test text";

async Task action() => await _contentSafetyService.Validate(id, text);

Assert.ThrowsAsync<NotImplementedException>(action);
}
}

0 comments on commit 00e2cf8

Please sign in to comment.