Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Maksasj/hoo
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksasj committed Sep 29, 2024
2 parents e372147 + c39b118 commit a64dd1a
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 115 deletions.
28 changes: 15 additions & 13 deletions Hoo.Service/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Hoo.Service.Controllers
{
[ApiController]
public class FileController : ControllerBase
public class FileController
{
private readonly ILogger<FileController> _logger;

Expand All @@ -26,42 +26,44 @@ public FileController(ILogger<FileController> logger, IFileProviderService fileP

[HttpGet]
[Route("GetFiles")]
public async Task<FileItemPageResponseModel> GetFiles(int pageIndex = 0, int itemsPerPage = 100)
public async Task<IActionResult> GetFiles(int pageIndex = 0, int itemsPerPage = 100)
{
if (pageIndex < 0 || itemsPerPage < 0)
return new BadRequestObjectResult("Invalid page index or items per page");

var files = (await _fileProviderService.GetFilesAsync())
.Skip(pageIndex * itemsPerPage)
.Take(itemsPerPage)
.ToArray();
.Take(itemsPerPage);

return new FileItemPageResponseModel
return new OkObjectResult(new FileItemPageResponseModel
{
PageIndex = pageIndex,
ItemCount = files.Length,
ItemCount = files.Count(),
Files = files,
};
});
}

[HttpGet]
[Route("GetFileCount")]
public async Task<long> GetFileCount()
public async Task<IActionResult> GetFileCount()
{
return (await _fileProviderService.GetFilesAsync()).LongCount();
return new OkObjectResult((await _fileProviderService.GetFilesAsync()).LongCount());
}

[HttpGet]
[Route("GetFileThumbnail")]
public async Task<FileThumbnailResponseModel> GetFileThumbnail(Guid fileId)
public async Task<IActionResult> GetFileThumbnail(Guid fileId)
{
var item = await _fileThumbnailProviderService.GetFileThumbnailAsync(fileId);

if (item == null)
return null;
return new BadRequestObjectResult("File not found");

return new FileThumbnailResponseModel
return new OkObjectResult(new FileThumbnailResponseModel
{
FileId = item.FileId,
ThumbnailUrl = item.ThumbnailUrl
};
});
}
}
}
6 changes: 3 additions & 3 deletions Hoo.Service/Controllers/SourceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Hoo.Service.Controllers
{
[ApiController]
public class SourceController : ControllerBase
public class SourceController
{
private readonly IGoogleDriveService _googleDriveService;
private readonly IOneDriveService _oneDriveService;
Expand Down Expand Up @@ -36,7 +36,7 @@ public async Task<IActionResult> SyncSources()
// await _oneDriveService.SyncRemoteAsync();
// await _webFileService.SyncRemote();

return Ok();
return new OkResult();
}

[HttpDelete]
Expand All @@ -46,7 +46,7 @@ public async Task<IActionResult> ClearCache()
await _googleDriveService.ClearCacheAsync();
await _oneDriveService.ClearCacheAsync();

return Ok();
return new OkResult();
}
}
}
6 changes: 3 additions & 3 deletions Hoo.Service/Controllers/UtilsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Hoo.Service.Controllers
{
[ApiController]
public class UtilsController : ControllerBase
public class UtilsController
{
private readonly ILogger<UtilsController> _logger;

Expand All @@ -16,14 +16,14 @@ public UtilsController(ILogger<UtilsController> logger)
[Route("Ping")]
public async Task<IActionResult> Ping()

Check warning on line 17 in Hoo.Service/Controllers/UtilsController.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
return Ok();
return new OkResult();
}

[HttpGet]
[Route("Health")]
public async Task<IActionResult> GetHealth()

Check warning on line 24 in Hoo.Service/Controllers/UtilsController.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
return Ok();
return new OkResult();
}
}
}
4 changes: 2 additions & 2 deletions Hoo.Service/Controllers/WebFileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Hoo.Service.Controllers
{
[ApiController]
public class WebFileController : ControllerBase
public class WebFileController
{
private readonly ILogger<WebFileController> _logger;
private readonly IWebFileService _webFileService;
Expand All @@ -32,7 +32,7 @@ Uri fileUri
{
await _webFileService.AddFileAsync(fileUri);

return Ok();
return new OkResult();
}
}
}
2 changes: 1 addition & 1 deletion Hoo.Service/Hoo.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.7.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.7.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.7.3" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Hoo.Service/Models/FileItemPageResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class FileItemPageResponseModel

public int ItemCount { get; set; }

public FileItemModel[] Files { get; set; }
public IEnumerable<FileItemModel> Files { get; set; }
}
}
12 changes: 6 additions & 6 deletions Hoo.Service/Services/GoogleDrive/Client/GoogleDriveClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ namespace Hoo.Service.Services.GoogleDrive.Client
{
public class GoogleDriveClient : IGoogleDriveClient
{
private readonly ILogger<SourceController> _logger;
private DriveService _driveService { get; }
private readonly ILogger<SourceController> _logger;

public GoogleDriveClient(ILogger<SourceController> logger, IConfiguration configuration)
{
_logger = logger;
_driveService = InitializeDriveService(configuration);
}

public IGoogleDriveFileCollection GetFiles()
{
return new GoogleDriveFileCollection(_driveService);
}

private DriveService InitializeDriveService(IConfiguration configuration)
{
Expand Down Expand Up @@ -56,5 +51,10 @@ private DriveService InitializeDriveService(IConfiguration configuration)
ApplicationName = applicationName
});
}

public IGoogleDriveFileCollection GetFiles()
{
return new GoogleDriveFileCollection(_driveService);
}
}
}
1 change: 0 additions & 1 deletion Hoo.Service/Services/HooFileProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public async Task<IEnumerable<FileItemModel>> GetFilesAsync()
Name = file.AccessUri.ToString()
}));


// Google Drive files
result.AddRange((await _googleDriveService.GetFilesAsync()).Select(file => new FileItemModel
{
Expand Down
179 changes: 172 additions & 7 deletions Hoo.UnitTests/Controllers/FileControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,189 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.AutoMoq;
using AutoFixture.Kernel;
using Hoo.Service.Controllers;
using HooService.Common;

Check failure on line 6 in Hoo.UnitTests/Controllers/FileControllerTests.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HooService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 6 in Hoo.UnitTests/Controllers/FileControllerTests.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HooService' could not be found (are you missing a using directive or an assembly reference?)
using Moq;
using NUnit.Framework.Internal;
using Hoo.Service.Services;
using Microsoft.Extensions.Logging;
using Hoo.Service.Models;
using NUnit.Framework.Legacy;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.AspNetCore.Mvc;

namespace Hoo.UnitTests.Controllers
{
public class FileControllerTests
{
private IFixture _fixture;
private Mock<IFileProviderService> _fileProviderServiceStub;

Check failure on line 21 in Hoo.UnitTests/Controllers/FileControllerTests.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IFileProviderService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 21 in Hoo.UnitTests/Controllers/FileControllerTests.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IFileProviderService' could not be found (are you missing a using directive or an assembly reference?)
private Mock<IFileThumbnailProviderService> _fileThumbnailProviderServiceStub;

private FileThumbnailModel _fileThumbnailModelStub;
private Guid _fileIdStub;

[SetUp]
public void SetUp()
{
_fixture = new Fixture();
_fixture.Customize(new AutoMoqCustomization());

_fileProviderServiceStub = _fixture.Freeze<Mock<IFileProviderService>>();
_fileThumbnailProviderServiceStub = _fixture.Freeze<Mock<IFileThumbnailProviderService>>();

_fileThumbnailModelStub = _fixture.Create<FileThumbnailModel>();
_fileIdStub = _fixture.Create<Guid>();
}

[Theory]
public async Task GetFiles_FileProviderService_ExecutedOnce()
{
// Arrange
var fileControllerSut = _fixture.Create<FileController>();

// Act
var _ = await fileControllerSut.GetFiles();

// Assert
_fileProviderServiceStub.Verify(mock => mock.GetFilesAsync(), Times.Once);
}

[Theory]
public async Task GetFileCount_FileProviderService_ExecutedOnce()
{
// Arrange
var fileControllerSut = _fixture.Create<FileController>();

// Act
var _ = await fileControllerSut.GetFileCount();

// Assert
_fileProviderServiceStub.Verify(mock => mock.GetFilesAsync(), Times.Once);
}

[Theory]
public async Task GetFiles_GetFirstPage_ReturnsValidPage()
{
// Arrange
var files = Enumerable.Repeat(_fixture.Create<FileItemModel>(), 100).ToArray();

_fileProviderServiceStub.Setup(mock => mock.GetFilesAsync()).ReturnsAsync(files);

var fileControllerSut = _fixture.Create<FileController>();

// Act
var result = await fileControllerSut.GetFiles();

// Assert
ClassicAssert.IsInstanceOf<OkObjectResult>(result);
var actualResult = ((result as OkObjectResult).Value) as FileItemPageResponseModel;

ClassicAssert.AreEqual(0, actualResult.PageIndex);
ClassicAssert.AreEqual(100, actualResult.ItemCount);
ClassicAssert.AreEqual(files, actualResult.Files);
}

[Test]
public void Test1()
[Theory]
public async Task GetFiles_GetFirstPage_ReturnsOnlyFirstPage()
{
// Arrange
var files = Enumerable.Repeat(_fixture.Create<FileItemModel>(), 10000).ToArray();

_fileProviderServiceStub.Setup(mock => mock.GetFilesAsync()).ReturnsAsync(files);

var fileControllerSut = _fixture.Create<FileController>();

// Act
var result = await fileControllerSut.GetFiles();

// Assert
ClassicAssert.IsInstanceOf<OkObjectResult>(result);
var actualResult = ((result as OkObjectResult).Value) as FileItemPageResponseModel;

ClassicAssert.AreEqual(0, actualResult.PageIndex);
ClassicAssert.AreEqual(100, actualResult.ItemCount);
ClassicAssert.AreEqual(files.Take(100), actualResult.Files);
}

[Theory]
[TestCase(1, 100)]
[TestCase(3, 25)]
[TestCase(99, 100)]
[TestCase(999, 100)]
public async Task GetFiles_GetAnyPage_ReturnsOnlyRequestedPage(int pageIndex, int itemsPerPage)
{
// Arrange
var files = Enumerable.Repeat(_fixture.Create<FileItemModel>(), 10000).ToArray();
var expected = files.Skip(pageIndex * itemsPerPage).Take(itemsPerPage);

_fileProviderServiceStub.Setup(mock => mock.GetFilesAsync()).ReturnsAsync(files);

var fileControllerSut = _fixture.Create<FileController>();

// Act
var result = await fileControllerSut.GetFiles(pageIndex, itemsPerPage);

// Assert
ClassicAssert.IsInstanceOf<OkObjectResult>(result);
var actualResult = ((result as OkObjectResult).Value) as FileItemPageResponseModel;

ClassicAssert.AreEqual(pageIndex, actualResult.PageIndex);
ClassicAssert.AreEqual(expected.Count(), actualResult.ItemCount);
ClassicAssert.AreEqual(expected, actualResult.Files);
}

[Theory]
public async Task GetFileThumbnail_FileThumbnailProviderService_ExecutedOnce()
{
// Arrange
var fileControllerSut = _fixture.Create<FileController>();

_fileThumbnailProviderServiceStub.Setup(mock => mock.GetFileThumbnailAsync(It.IsAny<Guid>()))
.ReturnsAsync(_fileThumbnailModelStub);

// Act
var _ = await fileControllerSut.GetFileThumbnail(_fileIdStub);

// Assert
_fileThumbnailProviderServiceStub.Verify(mock => mock.GetFileThumbnailAsync(It.IsAny<Guid>()), Times.Once);
}

[Theory]
public async Task GetFileThumbnail_ReturnsExpectedValue()
{
// Arrange
var fileControllerSut = _fixture.Create<FileController>();

_fileThumbnailProviderServiceStub.Setup(mock => mock.GetFileThumbnailAsync(_fileIdStub))
.ReturnsAsync(_fileThumbnailModelStub);

// Act
var result = await fileControllerSut.GetFileThumbnail(_fileIdStub);

// Assert
ClassicAssert.IsInstanceOf<OkObjectResult>(result);
var actualResult = ((result as OkObjectResult).Value) as FileThumbnailResponseModel;

ClassicAssert.AreEqual(_fileThumbnailModelStub.FileId, actualResult.FileId);
ClassicAssert.AreEqual(_fileThumbnailModelStub.ThumbnailUrl, actualResult.ThumbnailUrl);
}

[Theory]
public async Task GetFileThumbnail_FileDoesNotExist_ReturnsNull()
{
// Arrange
var fileControllerSut = _fixture.Create<FileController>();

_fileThumbnailProviderServiceStub.Setup(mock => mock.GetFileThumbnailAsync(It.IsAny<Guid>()))
.Returns(Task.FromResult<FileThumbnailModel>(null));

// Act
var result = await fileControllerSut.GetFileThumbnail(_fileIdStub);

// Assert
ClassicAssert.IsInstanceOf<BadRequestObjectResult>(result);
}
}
}
Loading

0 comments on commit a64dd1a

Please sign in to comment.