Skip to content

Commit

Permalink
Merge pull request #863 from ita-social-projects/fix-integrated-tests…
Browse files Browse the repository at this point in the history
…-valerii

Fix integrated tests valerii
  • Loading branch information
BohdanBybliv authored Jan 7, 2024
2 parents 62f6f3d + b0db67f commit 2b1a678
Show file tree
Hide file tree
Showing 47 changed files with 232 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public async Task<Result<SubtitleDTO>> Handle(GetSubtitleByIdQuery request, Canc
{
string errorMsg = _stringLocalizerCannotFind["CannotFindSubtitleWithCorrespondingId", request.Id].Value;
_logger.LogError(request, errorMsg);
return Result.Fail(new Error(errorMsg));
var returner = Result.Fail(new Error(errorMsg));
return returner;
}

return Result.Ok(_mapper.Map<SubtitleDTO>(subtitle));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<Result<IEnumerable<StreetcodeTagDTO>>> Handle(GetTagByStreetco
t => t.StreetcodeId == request.StreetcodeId,
include: q => q.Include(t => t.Tag));

if (tagIndexed is null)
if (tagIndexed is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind?["CannotFindAnyTagByTheStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<Result<IEnumerable<ArtDTO>>> Handle(GetArtsByStreetcodeIdQuery
include: scl => scl
.Include(sc => sc.Image) !);

if (arts is null)
if (arts is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind["CannotFindAnyArtWithCorrespondingStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<Result<IEnumerable<ImageDTO>>> Handle(GetImageByStreetcodeIdQu
f => f.Streetcodes.Any(s => s.Id == request.StreetcodeId),
include: q => q.Include(img => img.ImageDetails))).OrderBy(img => img.ImageDetails?.Alt);

if (images is null || images.Count() == 0)
if (images is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind["CannotFindAnImageWithTheCorrespondingStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<Result<IEnumerable<StreetcodeArtDTO>>> Handle(GetStreetcodeArt
.Include(a => a.Art)
.Include(i => i.Art.Image) !);

if (art is null)
if (art is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind["CannotFindAnyArtWithCorrespondingStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public string SaveFileInStorage(string base64, string name, string extension)

public void SaveFileInStorageBase64(string base64, string name, string extension)
{
byte[] imageBytes = Convert.FromBase64String(base64);
byte[] imageBytes = Convert.FromBase64String(base64.Trim());
Directory.CreateDirectory(_blobPath);
EncryptFile(imageBytes, extension, name);
}
Expand All @@ -87,26 +87,6 @@ public string UpdateFileInStorage(
return hashBlobStorageName;
}

public async Task CleanBlobStorage()
{
var base64Files = GetAllBlobNames();

var existingImagesInDatabase = await _repositoryWrapper.ImageRepository.GetAllAsync();
var existingAudiosInDatabase = await _repositoryWrapper.AudioRepository.GetAllAsync();

List<string> existingMedia = new ();
existingMedia.AddRange(existingImagesInDatabase.Select(img => img.BlobName));
existingMedia.AddRange(existingAudiosInDatabase.Select(img => img.BlobName));

var filesToRemove = base64Files.Except(existingMedia).ToList();

foreach (var file in filesToRemove)
{
Console.WriteLine($"Deleting {file}...");
DeleteFileInStorage(file);
}
}

private IEnumerable<string> GetAllBlobNames()
{
var paths = Directory.EnumerateFiles(_blobPath);
Expand Down
69 changes: 37 additions & 32 deletions Streetcode/Streetcode.BLL/Services/CacheService/CacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Streetcode.BLL.Interfaces.Cache;
using Streetcode.BLL.Interfaces.Logging;

Expand All @@ -14,54 +15,58 @@ namespace Streetcode.BLL.Services.CacheService
public class CacheService : ICacheService
{
private readonly IMemoryCache _cache;
private readonly ILoggerService _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ConcurrentDictionary<object, SemaphoreSlim> _locks = new ConcurrentDictionary<object, SemaphoreSlim>();

public CacheService(IMemoryCache cache, ILoggerService logger)
public CacheService(IMemoryCache cache, IServiceScopeFactory serviceScopeFactory )
{
_cache = cache;
_logger = logger;
_serviceScopeFactory = serviceScopeFactory;
}

public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> getItemCallback, TimeSpan cacheDuration)
{
_logger.LogInformation(key + "GetOrSetAsync function start!");
if (_cache.TryGetValue(key, out T cachedItem))
using (var scope = _serviceScopeFactory.CreateAsyncScope())
{
_logger.LogInformation(key + "TryGetValue function true");
return cachedItem;
}

_logger.LogInformation(key + "TryGetValue function false");
SemaphoreSlim mylock = _locks.GetOrAdd(key, k => new SemaphoreSlim(1, 1));

await mylock.WaitAsync();

try
{
if (_cache.TryGetValue(key, out cachedItem))
var logger = scope.ServiceProvider.GetRequiredService<ILoggerService>();
logger.LogInformation(key + "GetOrSetAsync function start!");
if (_cache.TryGetValue(key, out T cachedItem))
{
_logger.LogInformation(key + "TryGetValue function true");
logger.LogInformation(key + "TryGetValue function true");
return cachedItem;
}

_logger.LogInformation(key + "TryGetValue function false");
T item = await getItemCallback();
logger.LogInformation(key + "TryGetValue function false");
SemaphoreSlim mylock = _locks.GetOrAdd(key, k => new SemaphoreSlim(1, 1));

await mylock.WaitAsync();

var cacheEntryOptions = new MemoryCacheEntryOptions
try
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
SlidingExpiration = cacheDuration,
Priority = CacheItemPriority.Normal,
};
if (_cache.TryGetValue(key, out cachedItem))
{
logger.LogInformation(key + "TryGetValue function true");
return cachedItem;
}

_cache.Set(key, item, cacheEntryOptions);
_logger.LogInformation(key + "Set function true");
return item;
}
finally
{
mylock.Release();
logger.LogInformation(key + "TryGetValue function false");
T item = await getItemCallback();

var cacheEntryOptions = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
SlidingExpiration = cacheDuration,
Priority = CacheItemPriority.Normal,
};

_cache.Set(key, item, cacheEntryOptions);
logger.LogInformation(key + "Set function true");
return item;
}
finally
{
mylock.Release();
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions Streetcode/Streetcode.DAL/Streetcode.DAL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
<ItemGroup>
<PackageReference Include="EfCore.SchemaCompare" Version="6.0.0" />
<PackageReference Include="MailKit" Version="3.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.11">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.11">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
20 changes: 3 additions & 17 deletions Streetcode/Streetcode.WebApi/Controllers/BaseApiController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Resources;
using FluentResults;
using FluentResults;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Streetcode.BLL.MediatR.ResultVariations;

namespace Streetcode.WebApi.Controllers;
Expand All @@ -11,12 +9,7 @@ namespace Streetcode.WebApi.Controllers;
[Route("api/[controller]/[action]")]
public class BaseApiController : ControllerBase
{
private readonly IStringLocalizer _stringLocalizer;
private IMediator? _mediator;
public BaseApiController(IStringLocalizer<BaseApiController> stringLocalizer)
{
_stringLocalizer = stringLocalizer;
}

public BaseApiController()
{
Expand All @@ -28,21 +21,14 @@ protected ActionResult HandleResult<T>(Result<T> result)
{
if (result.IsSuccess)
{
if(result is NullResult<T>)
if (result is NullResult<T>)
{
return Ok(result.Value);
}

return (result.Value is null) ?
NotFound(_stringLocalizer?["NotFound"].Value) : Ok(result.Value);
}

foreach (var item in result.Reasons)
{
if (item.Message.Contains(_stringLocalizer?["NotFound"].Value))
{
return Ok();
}
NotFound("Not Found") : Ok(result.Value);
}

return BadRequest(result.Reasons);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task<IActionResult> GetById([FromRoute] int id)
}

[HttpGet("{streetcodeId:int}")]
public async Task<IActionResult> GetAllByStreetcodeId([FromRoute] int streetcodeId)
public async Task<IActionResult> GetArtsByStreetcodeId([FromRoute] int streetcodeId)
{
return HandleResult(await Mediator.Send(new GetArtsByStreetcodeIdQuery(streetcodeId)));
}
Expand Down
35 changes: 23 additions & 12 deletions Streetcode/Streetcode.WebApi/Extensions/RecurringJobExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hangfire;
using Microsoft.Extensions.DependencyInjection;
using Streetcode.BLL.Interfaces.Audio;
using Streetcode.BLL.Interfaces.Image;

Expand All @@ -8,23 +9,33 @@ public static class RecurringJobExtensions
{
public static void AddCleanAudiosJob(this WebApplication app)
{
var recurringJobManager = app.Services.GetService<IRecurringJobManager>();
var serviceScopeFactory = app.Services.GetService<IServiceScopeFactory>();
using (var scope = serviceScopeFactory.CreateScope())
{
var recurringJobManager = scope.ServiceProvider.GetService<IRecurringJobManager>();
var audioService = scope.ServiceProvider.GetService<IAudioService>();

recurringJobManager.AddOrUpdate(
"Clean audio that are not used in streetcodes",
() => app.Services.GetService<IAudioService>().CleanUnusedAudiosAsync(),
app.Configuration.GetSection("RecurringJobs")["AudioCleaningFrequency"],
TimeZoneInfo.Utc);
recurringJobManager.AddOrUpdate(
"Clean audio that are not used in streetcodes",
() => audioService.CleanUnusedAudiosAsync(),
app.Configuration.GetSection("RecurringJobs")["AudioCleaningFrequency"],
TimeZoneInfo.Utc);
}
}

public static void AddCleanImagesJob(this WebApplication app)
{
var recurringJobManager = app.Services.GetService<IRecurringJobManager>();
var serviceScopeFactory = app.Services.GetService<IServiceScopeFactory>();
using (var scope = serviceScopeFactory.CreateScope())
{
var recurringJobManager = scope.ServiceProvider.GetService<IRecurringJobManager>();
var imageService = scope.ServiceProvider.GetService<IImageService>();

recurringJobManager.AddOrUpdate(
"Clean images that are not used",
() => app.Services.GetService<IImageService>().CleanUnusedImagesAsync(),
app.Configuration.GetSection("RecurringJobs")["ImageCleaningFrequency"],
TimeZoneInfo.Utc);
recurringJobManager.AddOrUpdate(
"Clean images that are not used",
() => imageService.CleanUnusedImagesAsync(),
app.Configuration.GetSection("RecurringJobs")["ImageCleaningFrequency"],
TimeZoneInfo.Utc);
}
}
}
6 changes: 3 additions & 3 deletions Streetcode/Streetcode.WebApi/Streetcode.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
<PackageReference Include="MediatR" Version="11.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.15" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.11">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.13" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion Streetcode/Streetcode.WebApi/appsettings.Local.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=127.0.0.1;Database=StreetcodeDb;User Id=sa;Password=Admin@1234;MultipleActiveResultSets=true"
"DefaultConnection": "Server=127.0.0.1;Database=StreetcodeDb;User Id=sa;Password=Admin@1234;MultipleActiveResultSets=true"
},
"CORS": {
"AllowedOrigins": [ "http://localhost:3000" ],
Expand Down
2 changes: 1 addition & 1 deletion Streetcode/Streetcode.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=127.0.0.1;Database=StreetcodeDb;User Id=sa;Password=Admin@1234;MultipleActiveResultSets=true"
"DefaultConnection": "Server=127.0.0.1;Database=StreetcodeDb;User Id=sa;Password=Admin@1234;MultipleActiveResultSets=true"
},
"EmailConfiguration": {
"From": "[email protected]",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Streetcode.XIntegrationTest.ControllerTests.AdditionalContent
{
using Streetcode.BLL.DTO.AdditionalContent.Coordinates.Types;
using Streetcode.XIntegrationTest.ControllerTests.Utils;
using Streetcode.XIntegrationTest.ControllerTests.Utils.BeforeAndAfterTestAtribute.Streetcode;
using Xunit;
using Streetcode.BLL.DTO.AdditionalContent.Coordinates.Types;
using Streetcode.XIntegrationTest.ControllerTests.Utils;
using Streetcode.XIntegrationTest.ControllerTests.Utils.BeforeAndAfterTestAtribute.Streetcode;
using Xunit;

namespace Streetcode.XIntegrationTest.ControllerTests.AdditionalContent
{
public class CoordinateControllerTests : BaseControllerTests, IClassFixture<CustomWebApplicationFactory<Program>>
{
public CoordinateControllerTests(CustomWebApplicationFactory<Program> factory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public async Task GetById_Incorrect_ReturnBadRequest()
{
int incorrectId = -100;
var response = await this.client.GetByIdAsync(incorrectId);

Assert.Multiple(
() => Assert.False(response.IsSuccessStatusCode),
() => Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace Streetcode.XIntegrationTest.ControllerTests.AdditionalContent
using global::Streetcode.XIntegrationTest.ControllerTests.Utils;
using Streetcode.BLL.DTO.AdditionalContent;
using Streetcode.BLL.DTO.AdditionalContent.Tag;
using Streetcode.DAL.Entities.AdditionalContent;
using Streetcode.XIntegrationTest.ControllerTests.Utils.BeforeAndAfterTestAtribute.AdditionalContent.Tag;
using Streetcode.XIntegrationTest.ControllerTests.Utils.BeforeAndAfterTestAtribute.Streetcode;
using Xunit;

namespace Streetcode.XIntegrationTest.ControllerTests.AdditionalContent
{
using Streetcode.BLL.DTO.AdditionalContent;
using Streetcode.BLL.DTO.AdditionalContent.Tag;
using Streetcode.DAL.Entities.AdditionalContent;
using Streetcode.DAL.Entities.Streetcode.TextContent;
using Streetcode.XIntegrationTest.ControllerTests.Utils;
using Streetcode.XIntegrationTest.ControllerTests.Utils.BeforeAndAfterTestAtribute.AdditionalContent.Tag;
using Streetcode.XIntegrationTest.ControllerTests.Utils.BeforeAndAfterTestAtribute.Streetcode;
using Xunit;

public class TagControllerTests : BaseControllerTests, IClassFixture<CustomWebApplicationFactory<Program>>
{
public TagControllerTests(CustomWebApplicationFactory<Program> factory)
Expand Down Expand Up @@ -59,6 +58,7 @@ public async Task GetByIdIncorrect_ReturnBadRequest()
[ExtractTestTag]
public async Task GetByStreetcodeId_ReturnSuccessStatusCode()
{
StreetcodeTagIndexSetup.Setup(ExtractTestStreetcode.StreetcodeForTest, ExtractTestTag.TagForTest);
int streetcodeId = ExtractTestStreetcode.StreetcodeForTest.Id;
var response = await client.GetByStreetcodeId(streetcodeId);
var returnedValue = CaseIsensitiveJsonDeserializer.Deserialize<IEnumerable<StreetcodeTagDTO>>(response.Content);
Expand Down
Loading

0 comments on commit 2b1a678

Please sign in to comment.