Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced validations from Exceptions to OneOf/FluentValidation #730

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Eraware_Dnn_Spa_Ef_Di_Stencil/Eraware_Dnn_Spa_Ef_Di_Stencil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<MinimumVisualStudioVersion>16.0</MinimumVisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<RuntimeIdentifier>win</RuntimeIdentifier>
<RuntimeIdentifier>win</RuntimeIdentifier>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
Expand Down Expand Up @@ -191,27 +191,29 @@
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="UnitTests\Services\Items\UpdateItemDtoValidatorTests.cs" />
<None Include="UnitTests\Services\Items\CreateItemDtoValidatorTests.cs" />
<None Include="module\Services\Items\UpdateItemDtoValidator.cs" />
<None Include="module\Services\Items\CreateItemDtoValidator.cs" />
<None Include="UnitTests\StartupTests.cs" />
<None Include="UnitTests\Providers\DateTimeProviderTests.cs" />
<None Include="module\Providers\IDateTimeProvider.cs" />
<None Include="module\Providers\DateTimeProvider.cs" />
<None Include="IntegrationTests\Startup.cs" />
<None Include="UnitTests\Startup.cs" />
<None Include="UnitTests\Common\Extensions\IQueryableExtensionsTests.cs" />
<None Include="module\ViewModels\LocalizationViewModel.cs" />
<None Include="module\Services\Localization\LocalizationViewModel.cs" />
<None Include="UnitTests\Controllers\LocalizationControllerTests.cs" />
<None Include="module\Services\LocalizationService.cs" />
<None Include="module\Services\ILocalizationService.cs" />
<None Include="UnitTests\Controllers\ModuleExceptionFilterAttributeTests.cs" />
<None Include="UnitTests\Controllers\ValidateModelAttributeTests.cs" />
<None Include="module\Services\Localization\LocalizationService.cs" />
<None Include="module\Services\Localization\ILocalizationService.cs" />
<None Include="UnitTests\Controllers\ServiceRouteMapperTests.cs" />
<None Include="UnitTests\Controllers\ModuleApiControllerTests.cs" />
<None Include="UnitTests\Controllers\ItemControllerTests.cs" />
<None Include="module\ViewModels\ItemsPageViewModel.cs" />
<None Include="module\ViewModels\ItemViewModel.cs" />
<None Include="UnitTests\Services\ItemServiceTests.cs" />
<None Include="module\Services\ItemService.cs" />
<None Include="module\Services\IItemService.cs" />
<None Include="module\Services\Items\ItemsPageViewModel.cs" />
<None Include="module\Services\Items\ItemViewModel.cs" />
<None Include="UnitTests\Services\Items\ItemServiceTests.cs" />
<None Include="module\Services\Items\ItemService.cs" />
<None Include="module\Services\Items\IItemService.cs" />
<None Include="UnitTests\Common\GlobalsTests.cs" />
<None Include="UnitTests\Data\Entities\ItemTests.cs" />
<None Include="UnitTests\Data\ModuleDbContextTests.cs" />
Expand All @@ -232,14 +234,12 @@
</Content>
<None Include="module\Data\Entities\BaseEntity.cs" />
<None Include="module\Data\Repositories\Repository.cs" />
<None Include="module\DTO\CreateItemDTO.cs" />
<None Include="module\Services\Items\CreateItemDTO.cs" />
<None Include="IntegrationTests\Controllers\ItemControllerTests.cs" />
<None Include="IntegrationTests\Controllers\ServiceRouteMapperTests.cs" />
<None Include="IntegrationTests\FakeDataContext.cs" />
<None Include="module\DTO\UpdateItemDTO.cs" />
<None Include="module\Controllers\ModuleExceptionFilterAttribute.cs" />
<None Include="module\Controllers\ValidateModelAttribute.cs" />
<None Include="module\DTO\GetItemsPageDTO.cs" />
<None Include="module\Services\Items\UpdateItemDTO.cs" />
<None Include="module\Services\Items\GetItemsPageDTO.cs" />
<None Include="module\Controllers\LocalizationController.cs" />
<None Include="module\Common\Extensions\IQueryableExtensions.cs" />
<None Include="module\Data\Repositories\PagedList{T}.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using $ext_rootnamespace$.Controllers;
using $ext_rootnamespace$.Data.Entities;
using $ext_rootnamespace$.Data.Repositories;
using $ext_rootnamespace$.DTO;
using $ext_rootnamespace$.Providers;
using $ext_rootnamespace$.Services;
using $ext_rootnamespace$.ViewModels;
using $ext_rootnamespace$.Services.Items;
using $ext_rootnamespace$.Services.Localization;
using DotNetNuke.Entities.Users;
using FluentValidation;
using Moq;
using System;
using System.Linq;
Expand All @@ -20,6 +20,9 @@ public class ItemControllerTests : FakeDataContext
private readonly Mock<IDateTimeProvider> dateTimeProvider;
private readonly IRepository<Item> itemRepository;
private readonly IItemService itemService;
private readonly IValidator<CreateItemDTO> createItemDtoValidator;
private readonly IValidator<UpdateItemDTO> updateItemDtoValidator;
private readonly Mock<ILocalizationService> localizationService;
private readonly ItemController itemController;


Expand All @@ -28,7 +31,36 @@ public ItemControllerTests()
this.dateTimeProvider = new Mock<IDateTimeProvider>();
this.dateTimeProvider.Setup(p => p.GetUtcNow()).Returns(new DateTime(2022, 1, 1));
this.itemRepository = new Repository<Item>(this.dataContext, this.dateTimeProvider.Object);
this.itemService = new ItemService(this.itemRepository);
this.localizationService = new Mock<ILocalizationService>();
var resx = new LocalizationViewModel
{
ModelValidation = new LocalizationViewModel.ModelValidationInfo
{
NameRequired = "Name is required.",
IdGreaterThanZero = "Id must be greater than zero.",
},
UI = new LocalizationViewModel.UIInfo
{
AddItem = "Add Item",
Cancel = "Cancel",
Create = "Create",
Delete = "Delete",
DeleteItemConfirm = "Are you sure you want to delete this item?",
Description = "Description",
Edit = "Edit",
LoadMore = "Load more",
Name = "Name",
No = "No",
Save = "Save",
Yes = "Yes",
SearchPlaceholder = "Search...",
ShownItems = "Shown items",
},
};
this.localizationService.SetupGet(s => s.ViewModel).Returns(resx);
this.createItemDtoValidator = new CreateItemDtoValidator(localizationService.Object);
this.updateItemDtoValidator = new UpdateItemDtoValidator(localizationService.Object);
this.itemService = new ItemService(this.itemRepository, this.createItemDtoValidator, this.updateItemDtoValidator);
this.itemController = new FakeItemController(this.itemService);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using $ext_rootnamespace$.Controllers;
using $ext_rootnamespace$.DTO;
using $ext_rootnamespace$.Services;
using $ext_rootnamespace$.ViewModels;
using $ext_rootnamespace$.Services.Items;
using DotNetNuke.Entities.Users;
using Moq;
using OneOf;
using OneOf.Types;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Results;
using Xunit;
Expand All @@ -14,12 +15,13 @@ namespace UnitTests.Controllers
{
public class ItemControllerTests
{
private CancellationToken token;
private readonly Mock<IItemService> itemService;
private readonly ItemController itemController;


public ItemControllerTests()
{
this.token = new CancellationToken();
this.itemService = new Mock<IItemService>();
this.itemController = new FakeItemController(this.itemService.Object);
}
Expand All @@ -29,14 +31,16 @@ public async Task CreateItem_Creates()
{
var name = "Name";
var description = "Description";
var userId = 123;
var dto = new CreateItemDTO()
{
Name = name,
Description = description,
};
var viewModel = new ItemViewModel() { Id = 1, Name = name, Description = description };
this.itemService.Setup(i => i.CreateItemAsync(It.IsAny<CreateItemDTO>(), It.IsAny<int>()))
.Returns(Task.FromResult(new ItemViewModel() { Id = 1, Name = name, Description = description }));
this.itemService.Setup(i => i.CreateItemAsync(It.IsAny<CreateItemDTO>(), userId, this.token))
.Returns(Task.FromResult<OneOf<Success<ItemViewModel>, Error<List<FluentValidation.Results.ValidationFailure>>>>(
new Success<ItemViewModel>(new ItemViewModel() { Id = 1, Name = name, Description = description })));

var result = await this.itemController.CreateItem(dto);

Expand Down Expand Up @@ -111,7 +115,7 @@ public async Task UpdateItem_Updates()

await this.itemController.UpdateItem(item);

this.itemService.Verify(s => s.UpdateItemAsync(item, It.IsAny<int>()), Times.Once);
this.itemService.Verify(s => s.UpdateItemAsync(item, It.IsAny<int>(), this.token), Times.Once);
}

public class FakeItemController : ItemController
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using DotNetNuke.Entities.Users;
using $ext_rootnamespace$.Controllers;
using $ext_rootnamespace$.Services;
using $ext_rootnamespace$.ViewModels;
using $ext_rootnamespace$.Services.Localization;
using Moq;
using System.Web.Http.Results;
using Xunit;
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading