Skip to content

Commit

Permalink
Merge pull request #869 from cabinetoffice/refactoring/DP-730-more-sp…
Browse files Browse the repository at this point in the history
…ecific-exceptions

DP-730: Throw more specific exceptions in the UpdateOrganisationUseCase in preparation for better error codes
  • Loading branch information
jakzal authored Oct 29, 2024
2 parents cd1b577 + a1537be commit be7cfee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenUpdate
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Unknown organisation update type.");
.ThrowAsync<InvalidUpdateOrganisationCommand.UnknownOrganisationUpdateType>();
}

[Fact]
Expand All @@ -148,8 +147,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenAdditi
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Missing additional identifiers.");
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingAdditionalIdentifiers>();
}

[Fact]
Expand All @@ -165,8 +163,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenAddres
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Missing organisation address.");
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingOrganisationAddress>();
}

[Fact]
Expand Down Expand Up @@ -256,8 +253,7 @@ public async Task Execute_ShouldThrowException_WhenVatIdentifierIsOnlyIdentifier
Func<Task> act = async () => await UseCase.Execute((_organisationId, command));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("There are no identifiers remaining that can be set as the primary.");
.ThrowAsync<InvalidUpdateOrganisationCommand.NoPrimaryIdentifier>();
}

[Fact]
Expand Down Expand Up @@ -336,8 +332,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenOrgani
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Missing organisation name.");
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingOrganisationName>();
}

[Fact]
Expand Down Expand Up @@ -376,8 +371,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenOrgani
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Missing organisation email.");
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingOrganisationEmail>();
}

[Fact]
Expand All @@ -402,8 +396,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenOrgani
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("organisation email does not exists.");
.ThrowAsync<InvalidUpdateOrganisationCommand.OrganisationEmailDoesNotExist>();
}

[Fact]
Expand Down Expand Up @@ -442,8 +435,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenOrgani
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Missing organisation address.");
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingOrganisationAddress>();
}

[Fact]
Expand Down Expand Up @@ -474,8 +466,7 @@ public async Task Execute_ShouldThrowInvalidUpdateOrganisationCommand_WhenOrgani
Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Missing Organisation registered address.");
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingOrganisationRegisteredAddress>();
}

[Fact]
Expand Down Expand Up @@ -549,8 +540,26 @@ public async Task Execute_ShouldUpdateOrganisationRolesThrowsException_WhenRoles
Func<Task> act = async () => await UseCase.Execute((_organisationId, command));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>()
.WithMessage("Missing roles.");
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingRoles>();
}

[Fact]
public async Task Execute_ShouldThrowMissingContactPoint_WhenContactPointIsMissing()
{
var updateOrganisation = new UpdateOrganisation
{
Type = OrganisationUpdateType.ContactPoint,
Organisation = new OrganisationInfo()
};
var organisation = Organisation;

_organisationRepositoryMock.Setup(repo => repo.Find(_organisationId)).ReturnsAsync(organisation);

Func<Task> act = async () => await UseCase.Execute((_organisationId, updateOrganisation));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand.MissingContactPoint>()
.WithMessage("Missing contact point.");
}

private Persistence.Organisation OrganisationWithOtherIdentifier =>
Expand Down
29 changes: 28 additions & 1 deletion Services/CO.CDP.Organisation.WebApi/Model/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,34 @@ public class UnknownOrganisationException(string message, Exception? cause = nul
public class UnknownPersonException(string message, Exception? cause = null) : Exception(message, cause);
public class UnknownInvitedPersonException(string message, Exception? cause = null) : Exception(message, cause);
public class EmptyPersonRoleException(string message, Exception? cause = null) : Exception(message, cause);
public class InvalidUpdateOrganisationCommand(string message, Exception? cause = null) : Exception(message, cause);

public abstract class InvalidUpdateOrganisationCommand(string message, Exception? cause = null)
: Exception(message, cause)
{
public class UnknownOrganisationUpdateType()
: InvalidUpdateOrganisationCommand("Unknown organisation update type.");

public class MissingOrganisationName() : InvalidUpdateOrganisationCommand("Missing organisation name.");

public class MissingRoles() : InvalidUpdateOrganisationCommand("Missing roles.");

public class MissingContactPoint() : InvalidUpdateOrganisationCommand("Missing contact point.");

public class NoPrimaryIdentifier()
: InvalidUpdateOrganisationCommand("There are no identifiers remaining that can be set as the primary.");

public class MissingOrganisationEmail() : InvalidUpdateOrganisationCommand("Missing organisation email.");

public class OrganisationEmailDoesNotExist()
: InvalidUpdateOrganisationCommand("organisation email does not exists.");

public class MissingOrganisationAddress() : InvalidUpdateOrganisationCommand("Missing organisation address.");

public class MissingOrganisationRegisteredAddress()
: InvalidUpdateOrganisationCommand("Missing Organisation registered address.");

public class MissingAdditionalIdentifiers() : InvalidUpdateOrganisationCommand("Missing additional identifiers.");
}
public class BuyerInfoNotExistException(string message, Exception? cause = null) : Exception(message, cause);
public class InvalidUpdateBuyerInformationCommand(string message, Exception? cause = null) : Exception(message, cause);
public class SupplierInfoNotExistException(string message, Exception? cause = null) : Exception(message, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,38 @@ public async Task<bool> Execute((Guid organisationId, UpdateOrganisation updateO
case OrganisationUpdateType.OrganisationName:
if (string.IsNullOrEmpty(updateObject.OrganisationName))
{
throw new InvalidUpdateOrganisationCommand("Missing organisation name.");
throw new InvalidUpdateOrganisationCommand.MissingOrganisationName();
}
organisation.Name = updateObject.OrganisationName;
break;

case OrganisationUpdateType.AddRoles:
if (updateObject.Roles == null || !updateObject.Roles.Any())
{
throw new InvalidUpdateOrganisationCommand("Missing roles.");
throw new InvalidUpdateOrganisationCommand.MissingRoles();
}

organisation.Roles.AddRange(updateObject.Roles);
break;

case OrganisationUpdateType.OrganisationEmail:
if (updateObject.ContactPoint == null || string.IsNullOrEmpty(updateObject.ContactPoint.Email))
throw new InvalidUpdateOrganisationCommand("Missing organisation email.");
throw new InvalidUpdateOrganisationCommand.MissingOrganisationEmail();

var organisationContact = organisation.ContactPoints.FirstOrDefault();
if (organisationContact == null)
throw new InvalidUpdateOrganisationCommand("organisation email does not exists.");
throw new InvalidUpdateOrganisationCommand.OrganisationEmailDoesNotExist();

organisationContact.Email = updateObject.ContactPoint.Email;
break;

case OrganisationUpdateType.RegisteredAddress:
if (updateObject.Addresses == null)
throw new InvalidUpdateOrganisationCommand("Missing organisation address.");
throw new InvalidUpdateOrganisationCommand.MissingOrganisationAddress();

var newAddress = updateObject.Addresses.FirstOrDefault(x => x.Type == AddressType.Registered);
if (newAddress == null)
throw new InvalidUpdateOrganisationCommand("Missing Organisation registered address.");
throw new InvalidUpdateOrganisationCommand.MissingOrganisationRegisteredAddress();

var existingAddress = organisation.Addresses.FirstOrDefault(i => i.Type == newAddress.Type);
if (existingAddress != null)
Expand Down Expand Up @@ -99,7 +99,7 @@ public async Task<bool> Execute((Guid organisationId, UpdateOrganisation updateO
case OrganisationUpdateType.AdditionalIdentifiers:
if (updateObject.AdditionalIdentifiers == null)
{
throw new InvalidUpdateOrganisationCommand("Missing additional identifiers.");
throw new InvalidUpdateOrganisationCommand.MissingAdditionalIdentifiers();
}

foreach (var identifier in updateObject.AdditionalIdentifiers)
Expand Down Expand Up @@ -129,7 +129,7 @@ public async Task<bool> Execute((Guid organisationId, UpdateOrganisation updateO
case OrganisationUpdateType.ContactPoint:
if (updateObject.ContactPoint == null)
{
throw new InvalidUpdateOrganisationCommand("Missing contact point.");
throw new InvalidUpdateOrganisationCommand.MissingContactPoint();
}

var existingContact = organisation.ContactPoints.FirstOrDefault();
Expand All @@ -150,7 +150,7 @@ public async Task<bool> Execute((Guid organisationId, UpdateOrganisation updateO
case OrganisationUpdateType.Address:
if (updateObject.Addresses == null)
{
throw new InvalidUpdateOrganisationCommand("Missing organisation address.");
throw new InvalidUpdateOrganisationCommand.MissingOrganisationAddress();
}
foreach (var address in updateObject.Addresses)
{
Expand Down Expand Up @@ -184,7 +184,7 @@ public async Task<bool> Execute((Guid organisationId, UpdateOrganisation updateO

break;
default:
throw new InvalidUpdateOrganisationCommand("Unknown organisation update type.");
throw new InvalidUpdateOrganisationCommand.UnknownOrganisationUpdateType();
}

organisation.UpdateSupplierInformation();
Expand Down Expand Up @@ -231,7 +231,7 @@ private void AllocateNextPrimaryIdentifier(OrganisationInformation.Persistence.O
}
else
{
throw new InvalidUpdateOrganisationCommand("There are no identifiers remaining that can be set as the primary.");
throw new InvalidUpdateOrganisationCommand.NoPrimaryIdentifier();
}
}
}

0 comments on commit be7cfee

Please sign in to comment.