mirror of https://github.com/abpframework/abp.git
14 changed files with 228 additions and 60 deletions
@ -1,30 +0,0 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.Mvc; |
|||
|
|||
namespace Volo.Abp.AspNetCore.App |
|||
{ |
|||
[Route("api/unitofwork-test")] |
|||
public class UnitOfWorkTestController : AbpController |
|||
{ |
|||
[HttpGet] |
|||
[Route("ActionRequiresUow")] |
|||
public ActionResult ActionRequiresUow() |
|||
{ |
|||
CurrentUnitOfWork.ShouldNotBeNull(); |
|||
CurrentUnitOfWork.Options.IsTransactional.ShouldBeFalse(); |
|||
|
|||
return Content("OK"); |
|||
} |
|||
|
|||
[HttpPost] |
|||
[Route("ActionRequiresUowPost")] |
|||
public ActionResult ActionRequiresUowPost() |
|||
{ |
|||
CurrentUnitOfWork.ShouldNotBeNull(); |
|||
CurrentUnitOfWork.Options.IsTransactional.ShouldBeTrue(); |
|||
|
|||
return Content("OK"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,8 +1,7 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Volo.Abp.AspNetCore.Mvc; |
|||
using Volo.Abp.Ui; |
|||
|
|||
namespace Volo.Abp.AspNetCore.App |
|||
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling |
|||
{ |
|||
[Route("api/exception-test")] |
|||
public class ExceptionTestController : AbpController |
|||
@ -0,0 +1,42 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Ui; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Uow |
|||
{ |
|||
[Dependency(ReplaceServices = true)] |
|||
public class TestUnitOfWork : UnitOfWork |
|||
{ |
|||
private readonly TestUnitOfWorkConfig _config; |
|||
|
|||
public TestUnitOfWork(IServiceProvider serviceProvider, IOptions<UnitOfWorkDefaultOptions> options, TestUnitOfWorkConfig config) |
|||
: base(serviceProvider, options) |
|||
{ |
|||
_config = config; |
|||
} |
|||
|
|||
public override void Complete() |
|||
{ |
|||
ThrowExceptionIfRequested(); |
|||
base.Complete(); |
|||
} |
|||
|
|||
public override Task CompleteAsync(CancellationToken cancellationToken = default(CancellationToken)) |
|||
{ |
|||
ThrowExceptionIfRequested(); |
|||
return base.CompleteAsync(cancellationToken); |
|||
} |
|||
|
|||
private void ThrowExceptionIfRequested() |
|||
{ |
|||
if (_config.ThrowExceptionOnComplete) |
|||
{ |
|||
throw new UserFriendlyException(TestUnitOfWorkConfig.ExceptionOnCompleteMessage); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Uow |
|||
{ |
|||
public class TestUnitOfWorkConfig : ISingletonDependency |
|||
{ |
|||
public const string ExceptionOnCompleteMessage = "TestUnitOfWork configured for exception"; |
|||
|
|||
public bool ThrowExceptionOnComplete { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using System.Net; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.Http; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Uow |
|||
{ |
|||
public class UnitOfWorkMiddleware_Exception_Rollback_Tests : AspNetCoreMvcTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Rollback_Transaction_For_Handled_Exceptions() |
|||
{ |
|||
var result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/unitofwork-test/HandledException", HttpStatusCode.InternalServerError); |
|||
result.Error.ShouldNotBeNull(); |
|||
result.Error.Message.ShouldBe("This is a sample exception!"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Gracefully_Handle_Exceptions_On_Complete() |
|||
{ |
|||
var result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/unitofwork-test/ExceptionOnComplete", HttpStatusCode.InternalServerError); |
|||
result.Error.ShouldNotBeNull(); |
|||
result.Error.Message.ShouldBe(TestUnitOfWorkConfig.ExceptionOnCompleteMessage); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Shouldly; |
|||
using Volo.Abp.Ui; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Uow |
|||
{ |
|||
[Route("api/unitofwork-test")] |
|||
public class UnitOfWorkTestController : AbpController |
|||
{ |
|||
private readonly TestUnitOfWorkConfig _testUnitOfWorkConfig; |
|||
|
|||
public UnitOfWorkTestController(TestUnitOfWorkConfig testUnitOfWorkConfig) |
|||
{ |
|||
_testUnitOfWorkConfig = testUnitOfWorkConfig; |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("ActionRequiresUow")] |
|||
public ActionResult ActionRequiresUow() |
|||
{ |
|||
CurrentUnitOfWork.ShouldNotBeNull(); |
|||
CurrentUnitOfWork.Options.IsTransactional.ShouldBeFalse(); |
|||
|
|||
return Content("OK"); |
|||
} |
|||
|
|||
[HttpPost] |
|||
[Route("ActionRequiresUowPost")] |
|||
public ActionResult ActionRequiresUowPost() |
|||
{ |
|||
CurrentUnitOfWork.ShouldNotBeNull(); |
|||
CurrentUnitOfWork.Options.IsTransactional.ShouldBeTrue(); |
|||
|
|||
return Content("OK"); |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("HandledException")] |
|||
[UnitOfWork(isTransactional: true)] |
|||
public void HandledException() |
|||
{ |
|||
CurrentUnitOfWork.ShouldNotBeNull(); |
|||
CurrentUnitOfWork.Options.IsTransactional.ShouldBeTrue(); |
|||
|
|||
throw new UserFriendlyException("This is a sample exception!"); |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("ExceptionOnComplete")] |
|||
public void ExceptionOnComplete() |
|||
{ |
|||
CurrentUnitOfWork.ShouldNotBeNull(); |
|||
CurrentUnitOfWork.Options.IsTransactional.ShouldBeFalse(); |
|||
|
|||
_testUnitOfWorkConfig.ThrowExceptionOnComplete = true; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue