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

CONTROLLERS: Configurations - DeleteConfigurationByIdAsync #230

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using GitFyle.Core.Api.Models.Foundations.Configurations;
using GitFyle.Core.Api.Models.Foundations.Configurations.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Moq;
using RESTFulSense.Clients.Extensions;
using RESTFulSense.Models;
using Xeptions;

namespace GitFyle.Core.Api.Tests.Unit.Controllers.Configurations
{
public partial class ConfigurationsControllerTests
{
[Theory]
[MemberData(nameof(ValidationExceptions))]
public async Task ShouldReturnBadRequestOnDeleteIfValidationErrorOccursAsync(Xeption validationException)
{
// given
Guid someId = Guid.NewGuid();

BadRequestObjectResult expectedBadRequestObjectResult =
BadRequest(validationException.InnerException);

var expectedActionResult =
new ActionResult<Configuration>(expectedBadRequestObjectResult);

this.configurationServiceMock.Setup(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()))
.ThrowsAsync(validationException);

// when
ActionResult<Configuration> actualActionResult =
await this.configurationsController.DeleteConfigurationByIdAsync(someId);

// then
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);

this.configurationServiceMock.Verify(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()),
Times.Once);

this.configurationServiceMock.VerifyNoOtherCalls();
}

[Theory]
[MemberData(nameof(ServerExceptions))]
public async Task ShouldReturnInternalServerErrorOnDeleteIfServerErrorOccurredAsync(
Xeption validationException)
{
// given
Guid someId = Guid.NewGuid();

InternalServerErrorObjectResult expectedBadRequestObjectResult =
InternalServerError(validationException);

var expectedActionResult =
new ActionResult<Configuration>(expectedBadRequestObjectResult);

this.configurationServiceMock.Setup(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()))
.ThrowsAsync(validationException);

// when
ActionResult<Configuration> actualActionResult =
await this.configurationsController.DeleteConfigurationByIdAsync(someId);

// then
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);

this.configurationServiceMock.Verify(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()),
Times.Once);

this.configurationServiceMock.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldReturnNotFoundOnDeleteIfItemDoesNotExistAsync()
{
// given
Guid someId = Guid.NewGuid();
string someMessage = GetRandomString();

var notFoundConfigurationException =
new NotFoundConfigurationException(
message: someMessage);

var configurationValidationException =
new ConfigurationValidationException(
message: someMessage,
innerException: notFoundConfigurationException);

NotFoundObjectResult expectedNotFoundObjectResult =
NotFound(notFoundConfigurationException);

var expectedActionResult =
new ActionResult<Configuration>(expectedNotFoundObjectResult);

this.configurationServiceMock.Setup(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()))
.ThrowsAsync(configurationValidationException);

// when
ActionResult<Configuration> actualActionResult =
await this.configurationsController.DeleteConfigurationByIdAsync(someId);

// then
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);

this.configurationServiceMock.Verify(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()),
Times.Once);

this.configurationServiceMock.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldReturnLockedOnDeleteIfRecordIsLockedAsync()
{
// given
Guid someId = Guid.NewGuid();
var someInnerException = new Exception();
string someMessage = GetRandomString();

var lockedConfigurationException =
new LockedConfigurationException(
message: someMessage,
innerException: someInnerException,
data: someInnerException.Data);

var configurationDependencyValidationException =
new ConfigurationDependencyValidationException(
message: someMessage,
innerException: lockedConfigurationException);

LockedObjectResult expectedConflictObjectResult =
Locked(lockedConfigurationException);

var expectedActionResult =
new ActionResult<Configuration>(expectedConflictObjectResult);

this.configurationServiceMock.Setup(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()))
.ThrowsAsync(configurationDependencyValidationException);

// when
ActionResult<Configuration> actualActionResult =
await this.configurationsController.DeleteConfigurationByIdAsync(someId);

// then
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);

this.configurationServiceMock.Verify(service =>
service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()),
Times.Once);

this.configurationServiceMock.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Force.DeepCloner;
using GitFyle.Core.Api.Models.Foundations.Configurations;
using Microsoft.AspNetCore.Mvc;
using Moq;
using RESTFulSense.Clients.Extensions;

namespace GitFyle.Core.Api.Tests.Unit.Controllers.Configurations
{
public partial class ConfigurationsControllerTests
{
[Fact]
public async Task ShouldRemoveConfigurationOnDeleteByIdAsync()
{
// given
Configuration randomConfiguration = CreateRandomConfiguration();
Guid inputId = randomConfiguration.Id;
Configuration storageConfiguration = randomConfiguration;
Configuration expectedConfiguration = storageConfiguration.DeepClone();

var expectedObjectResult =
new OkObjectResult(expectedConfiguration);

var expectedActionResult =
new ActionResult<Configuration>(expectedObjectResult);

configurationServiceMock
.Setup(service => service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()))
.ReturnsAsync(storageConfiguration);

// when
ActionResult<Configuration> actualActionResult =
await configurationsController.DeleteConfigurationByIdAsync(inputId);

// then
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);

configurationServiceMock
.Verify(service => service.RemoveConfigurationByIdAsync(It.IsAny<Guid>()),
Times.Once);

configurationServiceMock.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public async Task ShouldThrowValidationExceptionOnModifyIfStorageUpdatedDateSame

invalidConfigurationException.AddData(
key: nameof(Configuration.UpdatedDate),
values: $"Date is same as {nameof(Source.UpdatedDate)}");
values: $"Date is same as {nameof(Configuration.UpdatedDate)}");

var expectedConfigurationValidationException =
new ConfigurationValidationException(
Expand Down Expand Up @@ -447,6 +447,5 @@ await Assert.ThrowsAsync<ConfigurationValidationException>(
this.datetimeBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
}

}
}
38 changes: 38 additions & 0 deletions GitFyle.Core.Api/Controllers/ConfigurationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,43 @@ public async ValueTask<ActionResult<Configuration>> PutConfigurationAsync(Config
return InternalServerError(configurationServiceException);
}
}

[HttpDelete("{configurationId}")]
public async ValueTask<ActionResult<Configuration>> DeleteConfigurationByIdAsync(Guid configurationId)
{
try
{
Configuration deleteConfiguration =
await this.configurationService.RemoveConfigurationByIdAsync(configurationId);

return Ok(deleteConfiguration);
}
catch (ConfigurationValidationException configurationValidationException)
when (configurationValidationException.InnerException is NotFoundConfigurationException)
{
return NotFound(configurationValidationException.InnerException);
}
catch (ConfigurationValidationException configurationValidationException)
{
return BadRequest(configurationValidationException.InnerException);
}
catch (ConfigurationDependencyValidationException configurationDependencyValidationException)
when (configurationDependencyValidationException.InnerException is LockedConfigurationException)
{
return Locked(configurationDependencyValidationException.InnerException);
}
catch (ConfigurationDependencyValidationException configurationDependencyValidationException)
{
return BadRequest(configurationDependencyValidationException.InnerException);
}
catch (ConfigurationDependencyException configurationDependencyException)
{
return InternalServerError(configurationDependencyException);
}
catch (ConfigurationServiceException configurationServiceException)
{
return InternalServerError(configurationServiceException);
}
}
}
}
Loading