From 40687a91e9cb3b1ae5b417a18f469698bfc06c5f Mon Sep 17 00:00:00 2001 From: Abdulsamad Osunlana Date: Sun, 14 Apr 2024 14:11:45 +0100 Subject: [PATCH 1/2] CODE RUB: Namespace Cleanup, Upgrade Test to Standard 2.10.1 --- .../FailedFileSerializationException.cs | 5 -- .../Exceptions/FailedFileServiceException.cs | 5 -- .../Exceptions/FileDependencyException.cs | 5 -- .../FileDependencyValidationException.cs | 5 -- .../Files/Exceptions/FileServiceException.cs | 5 -- .../Exceptions/FileValidationException.cs | 5 -- .../Exceptions/InvalidFileContentException.cs | 4 -- .../InvalidFileDependencyException.cs | 5 -- .../Exceptions/InvalidFilePathException.cs | 4 -- .../Exceptions/AdoDependencyException.cs | 7 +- .../AdoDependencyValidationException.cs | 7 +- .../Exceptions/AdoValidationException.cs | 7 +- .../Exceptions/BuildServiceException.cs | 7 +- .../Exceptions/NullPathException.cs | 10 +-- .../Exceptions/NullPipelineException.cs | 9 +-- .../Builds/BuildService.Exceptions.cs | 53 ++++++++++---- .../Builds/BuildService.Validations.cs | 7 +- .../Files/FileService.Exceptions.cs | 71 ++++++++++++++----- .../Files/FileService.Validations.cs | 6 +- .../Builds/BuildServiceTests.Exceptions.cs | 28 +++++--- ...ts.logic.cs => BuildServiceTests.Logic.cs} | 2 +- .../Builds/BuildServiceTests.Validations.cs | 22 ++++-- .../Services/Builds/BuildServiceTests.cs | 2 +- .../FileServiceTests.Exceptions.Write.cs | 25 +++++-- .../Files/FileServiceTests.Logic.Write.cs | 2 +- .../FileServiceTests.Validations.Write.cs | 17 +++-- .../Foundations/Files/FileServiceTests.cs | 2 +- 27 files changed, 180 insertions(+), 147 deletions(-) rename AdoNet.Tests.Unit/Services/Builds/{BuildServiceTests.logic.cs => BuildServiceTests.Logic.cs} (97%) diff --git a/ADotNet/Models/Foundations/Files/Exceptions/FailedFileSerializationException.cs b/ADotNet/Models/Foundations/Files/Exceptions/FailedFileSerializationException.cs index ed31d75..a523e16 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/FailedFileSerializationException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/FailedFileSerializationException.cs @@ -11,11 +11,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class FailedFileSerializationException : Xeption { - public FailedFileSerializationException(Exception innerException) - : base(message: "Failed file serialization error occurred, contact support.", - innerException: innerException) - { } - public FailedFileSerializationException(string message, Exception innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/FailedFileServiceException.cs b/ADotNet/Models/Foundations/Files/Exceptions/FailedFileServiceException.cs index 6345b71..eb3dad3 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/FailedFileServiceException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/FailedFileServiceException.cs @@ -11,11 +11,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class FailedFileServiceException : Xeption { - public FailedFileServiceException(Exception innerException) - : base(message: "Failed file service error occurred, contact support.", - innerException: innerException) - { } - public FailedFileServiceException(string message, Exception innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyException.cs b/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyException.cs index 9ad3cfd..7c05b69 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyException.cs @@ -10,11 +10,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class FileDependencyException : Xeption { - public FileDependencyException(Xeption innerException) - : base(message: "File dependency error occurred, contact support.", - innerException: innerException) - { } - public FileDependencyException(string message, Xeption innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs b/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs index c7a0e7d..e1803ca 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs @@ -10,11 +10,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class FileDependencyValidationException : Xeption { - public FileDependencyValidationException(Xeption innerException) - : base(message: "File dependency validation error occurred, fix the errors and try again.", - innerException: innerException) - { } - public FileDependencyValidationException(string message, Xeption innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/FileServiceException.cs b/ADotNet/Models/Foundations/Files/Exceptions/FileServiceException.cs index 35a821f..a769954 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/FileServiceException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/FileServiceException.cs @@ -10,11 +10,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class FileServiceException : Xeption { - public FileServiceException(Xeption innerException) - : base(message: "File service error occurred, contact support.", - innerException: innerException) - { } - public FileServiceException(string message, Xeption innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/FileValidationException.cs b/ADotNet/Models/Foundations/Files/Exceptions/FileValidationException.cs index 0ec00b6..c7ccd19 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/FileValidationException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/FileValidationException.cs @@ -10,11 +10,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class FileValidationException : Xeption { - public FileValidationException(Xeption innerException) - : base(message: "File validation error occurred, fix the errors and try again.", - innerException: innerException) - { } - public FileValidationException(string message, Xeption innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileContentException.cs b/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileContentException.cs index 8823963..e805dda 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileContentException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileContentException.cs @@ -10,10 +10,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class InvalidFileContentException : Xeption { - public InvalidFileContentException() - : base(message: "Invalid file content, fix errors and try again.") - { } - public InvalidFileContentException(string message) : base(message) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileDependencyException.cs b/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileDependencyException.cs index 7290f8b..05c7b11 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileDependencyException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/InvalidFileDependencyException.cs @@ -11,11 +11,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class InvalidFileException : Xeption { - public InvalidFileException(Exception innerException) - : base(message: "Invalid file error occurred.", - innerException: innerException) - { } - public InvalidFileException(string message, Exception innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Foundations/Files/Exceptions/InvalidFilePathException.cs b/ADotNet/Models/Foundations/Files/Exceptions/InvalidFilePathException.cs index 8bc2103..c10da65 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/InvalidFilePathException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/InvalidFilePathException.cs @@ -10,10 +10,6 @@ namespace ADotNet.Models.Foundations.Files.Exceptions { public class InvalidFilePathException : Xeption { - public InvalidFilePathException() - : base(message: "Invalid file path, fix the errors and try again.") - { } - public InvalidFilePathException(string message) : base(message) { } diff --git a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyException.cs b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyException.cs index b6a5837..d3c0383 100644 --- a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyException.cs +++ b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyException.cs @@ -6,15 +6,10 @@ using System; -namespace ADotNet.Models.Pipelines.Exceptions +namespace ADotNet.Models.Pipelines.AdoPipelines.Exceptions { public class AdoDependencyException : Exception { - public AdoDependencyException(Exception innerException) - : base(message: "Ado dependency error occured, contact support.", - innerException: innerException) - { } - public AdoDependencyException(string message, Exception innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyValidationException.cs b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyValidationException.cs index 72bc931..c65277b 100644 --- a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyValidationException.cs +++ b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoDependencyValidationException.cs @@ -6,15 +6,10 @@ using System; -namespace ADotNet.Models.Pipelines.Exceptions +namespace ADotNet.Models.Pipelines.AdoPipelines.Exceptions { public class AdoDependencyValidationException : Exception { - public AdoDependencyValidationException(Exception innerException) - : base(message: "Ado dependency validation error occured, try again.", - innerException: innerException) - { } - public AdoDependencyValidationException(string message, Exception innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoValidationException.cs b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoValidationException.cs index 4f7c16a..da38713 100644 --- a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoValidationException.cs +++ b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/AdoValidationException.cs @@ -6,15 +6,10 @@ using System; -namespace ADotNet.Models.Pipelines.Exceptions +namespace ADotNet.Models.Pipelines.AdoPipelines.Exceptions { public class AdoValidationException : Exception { - public AdoValidationException(Exception innerException) - : base(message: "Ado validation exception occurred, try again", - innerException: innerException) - { } - public AdoValidationException(string message,Exception innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/BuildServiceException.cs b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/BuildServiceException.cs index d710c73..dde4f1f 100644 --- a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/BuildServiceException.cs +++ b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/BuildServiceException.cs @@ -6,15 +6,10 @@ using System; -namespace ADotNet.Models.Pipelines.Exceptions +namespace ADotNet.Models.Pipelines.AdoPipelines.Exceptions { public class BuildServiceException : Exception { - public BuildServiceException(Exception innerException) - : base(message: "Build service exception occured, contact support.", - innerException: innerException) - { } - public BuildServiceException(string message, Exception innerException) : base(message, innerException) { } diff --git a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPathException.cs b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPathException.cs index ce68bbc..f54aa96 100644 --- a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPathException.cs +++ b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPathException.cs @@ -4,16 +4,12 @@ // See License.txt in the project root for license information. // --------------------------------------------------------------------------- -using System; +using Xeptions; -namespace ADotNet.Models.Pipelines.Exceptions +namespace ADotNet.Models.Pipelines.AdoPipelines.Exceptions { - public class NullPathException : Exception + public class NullPathException : Xeption { - public NullPathException() - : base(message: "Path is null") - { } - public NullPathException(string message) : base(message) { } diff --git a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPipelineException.cs b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPipelineException.cs index 2981d08..c6b614f 100644 --- a/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPipelineException.cs +++ b/ADotNet/Models/Pipelines/AdoPipelines/Exceptions/NullPipelineException.cs @@ -5,15 +5,12 @@ // --------------------------------------------------------------------------- using System; +using Xeptions; -namespace ADotNet.Models.Pipelines.Exceptions +namespace ADotNet.Models.Pipelines.AdoPipelines.Exceptions { - public class NullPipelineException : Exception + public class NullPipelineException : Xeption { - public NullPipelineException() - : base(message: "Pipeline is null") - { } - public NullPipelineException(string message) : base(message) { } diff --git a/ADotNet/Services/Builds/BuildService.Exceptions.cs b/ADotNet/Services/Builds/BuildService.Exceptions.cs index ad764b6..965b027 100644 --- a/ADotNet/Services/Builds/BuildService.Exceptions.cs +++ b/ADotNet/Services/Builds/BuildService.Exceptions.cs @@ -7,7 +7,8 @@ using System; using System.IO; using System.Security; -using ADotNet.Models.Pipelines.Exceptions; +using ADotNet.Models.Pipelines.AdoPipelines.Exceptions; +using Xeptions; namespace ADotNet.Services.Builds { @@ -23,49 +24,75 @@ private static void TryCatch(ReturningNothingFunction returningNothingFunction) } catch (NullPipelineException nullPipelineException) { - throw new AdoValidationException(nullPipelineException); + throw CreateAdoValidationException(nullPipelineException); } catch (NullPathException nullPathException) { - throw new AdoValidationException(nullPathException); + throw CreateAdoValidationException(nullPathException); } catch (ArgumentNullException argumentNullException) { - throw new AdoDependencyValidationException(argumentNullException); + throw CreateAdoDependencyValidationException(argumentNullException); } catch (ArgumentException argumentException) { - throw new AdoDependencyValidationException(argumentException); + throw CreateAdoDependencyValidationException(argumentException); } catch (PathTooLongException pathTooLongException) { - throw new AdoDependencyValidationException(pathTooLongException); + throw CreateAdoDependencyValidationException(pathTooLongException); } catch (DirectoryNotFoundException directoryNotFoundException) { - throw new AdoDependencyValidationException( - directoryNotFoundException); + throw CreateAdoDependencyValidationException(directoryNotFoundException); } catch (IOException ioException) { - throw new AdoDependencyException(ioException); + throw CreateAdoDependencyException(ioException); } catch (SecurityException securityException) { - throw new AdoDependencyException(securityException); + throw CreateAdoDependencyException(securityException); } catch (UnauthorizedAccessException unauthorizedAccessException) { - throw new AdoDependencyException(unauthorizedAccessException); + throw CreateAdoDependencyException(unauthorizedAccessException); } catch (NotSupportedException notSupportedException) { - throw new AdoDependencyException(notSupportedException); + throw CreateAdoDependencyException(notSupportedException); } catch (Exception exception) { - throw new BuildServiceException(exception); + throw CreateBuildServiceException(exception); } } + + private static AdoValidationException CreateAdoValidationException(Xeption innerException) + { + return new AdoValidationException( + "Ado validation exception occurred, try again", innerException); + } + + private static AdoDependencyValidationException CreateAdoDependencyValidationException( + Exception innerException) + { + return new AdoDependencyValidationException( + "Ado dependency validation error occurs, try again.", + innerException); + } + + private static AdoDependencyException CreateAdoDependencyException( + Exception innerException) => + new AdoDependencyException( + "Ado dependency error occured, contact support.", + innerException); + + private static Exception CreateBuildServiceException(Exception innerException) + { + return new BuildServiceException( + "Build service exception occured, contact support.", + innerException); + } } } diff --git a/ADotNet/Services/Builds/BuildService.Validations.cs b/ADotNet/Services/Builds/BuildService.Validations.cs index e2c1e2e..3b06229 100644 --- a/ADotNet/Services/Builds/BuildService.Validations.cs +++ b/ADotNet/Services/Builds/BuildService.Validations.cs @@ -4,7 +4,7 @@ // See License.txt in the project root for license information. // --------------------------------------------------------------------------- -using ADotNet.Models.Pipelines.Exceptions; +using ADotNet.Models.Pipelines.AdoPipelines.Exceptions; namespace ADotNet.Services.Builds { @@ -15,10 +15,11 @@ public static void ValidateInputs(string path, object pipeline) switch (path, pipeline) { case (_, null): - throw new NullPipelineException(); + throw new NullPipelineException( + message: "Pipeline is null"); case (null, _): - throw new NullPathException(); + throw new NullPathException(message: "Path is null"); } } } diff --git a/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs b/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs index 0081ee9..101a94e 100644 --- a/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs +++ b/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs @@ -7,6 +7,7 @@ using System; using System.Runtime.Serialization; using ADotNet.Models.Foundations.Files.Exceptions; +using Xeptions; namespace ADotNet.Services.Foundations.Files { @@ -22,40 +23,74 @@ private static void TryCatch(ReturningNothingFunction returningNothingFunction) } catch (InvalidFilePathException invalidFilePathException) { - throw new FileValidationException(invalidFilePathException); + throw CreateFileValidationException(invalidFilePathException); } catch (InvalidFileContentException invalidFileContentException) { - throw new FileValidationException(invalidFileContentException); + throw CreateFileValidationException(invalidFileContentException); } catch (ArgumentNullException argumentNullException) { - var invalidFileException = - new InvalidFileException(argumentNullException); - - throw new FileDependencyValidationException(invalidFileException); + throw CreateFileDependencyValidationException(argumentNullException); } catch (ArgumentException argumentException) { - var invalidFileException = - new InvalidFileException(argumentException); - - throw new FileDependencyValidationException(invalidFileException); + throw CreateFileDependencyValidationException(argumentException); } catch (SerializationException serializationException) { - var failedFileSerializationException = - new FailedFileSerializationException(serializationException); - - throw new FileDependencyException(failedFileSerializationException); + throw CreateFileDependencyException(serializationException); } catch (Exception exception) { - var failedFileServiceException = - new FailedFileServiceException(exception); - - throw new FileServiceException(failedFileServiceException); + throw CreateFileServiceException(exception); } } + + private static FileValidationException CreateFileValidationException( + Xeption innerException) + { + return new FileValidationException( + "File validation error occurred, fix the errors and try again.", + innerException); + } + + private static FileDependencyValidationException CreateFileDependencyValidationException( + Exception innerException) + { + var invalidFileException = + new InvalidFileException( + "Invalid file error occurred.", innerException); + + throw new FileDependencyValidationException( + "File dependency validation error occurred, fix the errors and try again.", + invalidFileException); + } + + private static FileDependencyException CreateFileDependencyException( + Exception innerException) + { + var failedFileSerializationException = + new FailedFileSerializationException( + "Failed file serialization error occurred, contact support.", + innerException); + + throw new FileDependencyException( + "File dependency error occurred, contact support.", + failedFileSerializationException); + } + + private static FileServiceException CreateFileServiceException( + Exception innerException) + { + var failedFileServiceException = + new FailedFileServiceException( + "Failed file service error occurred, contact support.", + innerException); + + throw new FileServiceException( + "File service error occurred, contact support.", + failedFileServiceException); + } } } diff --git a/ADotNet/Services/Foundations/Files/FileService.Validations.cs b/ADotNet/Services/Foundations/Files/FileService.Validations.cs index 4e5b096..1d3c88d 100644 --- a/ADotNet/Services/Foundations/Files/FileService.Validations.cs +++ b/ADotNet/Services/Foundations/Files/FileService.Validations.cs @@ -16,10 +16,12 @@ private static void ValidateInputs(string path, string content) switch (path, content) { case { } when IsInvalid(path): - throw new InvalidFilePathException(); + throw new InvalidFilePathException( + message: "Invalid file path, fix the errors and try again."); case { } when IsInvalid(content): - throw new InvalidFileContentException(); + throw new InvalidFileContentException( + message: "Invalid file content, fix errors and try again."); } } diff --git a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Exceptions.cs b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Exceptions.cs index 9b6a8c2..431d9c5 100644 --- a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Exceptions.cs +++ b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Exceptions.cs @@ -6,12 +6,12 @@ using System; using ADotNet.Models.Pipelines.AdoPipelines.AspNets; -using ADotNet.Models.Pipelines.Exceptions; +using ADotNet.Models.Pipelines.AdoPipelines.Exceptions; using FluentAssertions; using Moq; using Xunit; -namespace ADotNet.Tests.Unit.Services +namespace ADotNet.Tests.Unit.Services.Builds { public partial class BuildServiceTests { @@ -24,6 +24,10 @@ private void ShouldThrowDependencyValidationOnSerializeIfDependencyValidationErr AspNetPipeline somePipeline = CreateRandomAspNetPipeline(); string somePath = GetRandomFilePath(); + var expectedAdoDependencyValidationException = new AdoDependencyValidationException( + message: "Ado dependency validation error occurs, try again.", + innerException: dependencyValidationException); + this.yamlBrokerMock.Setup(broker => broker.SerializeToYaml(It.IsAny())) .Throws(dependencyValidationException); @@ -39,8 +43,8 @@ private void ShouldThrowDependencyValidationOnSerializeIfDependencyValidationErr Assert.Throws( serializeAndWriteToFileAction); - actualAdoDependencyValidationException.InnerException.Message.Should() - .BeEquivalentTo(dependencyValidationException.Message); + actualAdoDependencyValidationException.Should() + .BeEquivalentTo(expectedAdoDependencyValidationException); this.yamlBrokerMock.Verify(broker => broker.SerializeToYaml(It.IsAny()), @@ -63,6 +67,10 @@ private void ShouldThrowDependencyExceptionOnSerializeIfDependencyValidationErro AspNetPipeline somePipeline = CreateRandomAspNetPipeline(); string somePath = GetRandomFilePath(); + var expectedAdoDependencyException = new AdoDependencyException( + message: "Ado dependency error occured, contact support.", + innerException: dependencyException); + this.yamlBrokerMock.Setup(broker => broker.SerializeToYaml(It.IsAny())) .Throws(dependencyException); @@ -78,8 +86,8 @@ private void ShouldThrowDependencyExceptionOnSerializeIfDependencyValidationErro Assert.Throws( serializeAndWriteToFileAction); - actualAdoDependencyException.InnerException.Message.Should() - .BeEquivalentTo(dependencyException.Message); + actualAdoDependencyException.Should() + .BeEquivalentTo(expectedAdoDependencyException); this.yamlBrokerMock.Verify(broker => broker.SerializeToYaml(It.IsAny()), @@ -102,6 +110,10 @@ private void ShouldThrowServiceExceptionOnSerializeIfSystemErrorOccurs() string innerExceptionMessage = GetRandomString(); var serviceException = new Exception(innerExceptionMessage); + var expectedBuildServiceException = new BuildServiceException( + message: "Build service exception occured, contact support.", + innerException: serviceException); + this.yamlBrokerMock.Setup(broker => broker.SerializeToYaml(It.IsAny())) .Throws(serviceException); @@ -117,8 +129,8 @@ private void ShouldThrowServiceExceptionOnSerializeIfSystemErrorOccurs() Assert.Throws( serializeAndWriteToFileAction); - actualBuildServiceException.InnerException.Message.Should() - .BeEquivalentTo(serviceException.Message); + actualBuildServiceException.Should() + .BeEquivalentTo(expectedBuildServiceException); this.yamlBrokerMock.Verify(broker => broker.SerializeToYaml(It.IsAny()), diff --git a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.logic.cs b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Logic.cs similarity index 97% rename from AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.logic.cs rename to AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Logic.cs index e68e49f..033f571 100644 --- a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.logic.cs +++ b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Logic.cs @@ -8,7 +8,7 @@ using Moq; using Xunit; -namespace ADotNet.Tests.Unit.Services +namespace ADotNet.Tests.Unit.Services.Builds { public partial class BuildServiceTests { diff --git a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Validations.cs b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Validations.cs index 6ced8a4..e55a1aa 100644 --- a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Validations.cs +++ b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.Validations.cs @@ -6,12 +6,12 @@ using System; using ADotNet.Models.Pipelines.AdoPipelines.AspNets; -using ADotNet.Models.Pipelines.Exceptions; +using ADotNet.Models.Pipelines.AdoPipelines.Exceptions; using FluentAssertions; using Moq; using Xunit; -namespace ADotNet.Tests.Unit.Services +namespace ADotNet.Tests.Unit.Services.Builds { public partial class BuildServiceTests { @@ -26,6 +26,10 @@ private void ShouldThrowValidationExceptionOnSerializeIfPipelineIsNull() new NullPipelineException( message: "Pipeline is null"); + var expectedAdoValidationException = new AdoValidationException( + message: "Ado validation exception occurred, try again", + innerException: nullPiplineException); + // when Action serializeAndWriteToFileAction = () => this.buildService.SerializeAndWriteToFile(somePath, invalidPipeline); @@ -35,9 +39,9 @@ private void ShouldThrowValidationExceptionOnSerializeIfPipelineIsNull() Assert.Throws( serializeAndWriteToFileAction); - actualAdoValidationException.InnerException.Message.Should() - .BeEquivalentTo(nullPiplineException.Message); - + actualAdoValidationException.Should().BeEquivalentTo( + expectedAdoValidationException); + this.yamlBrokerMock.Verify(broker => broker.SerializeToYaml(It.IsAny()), Times.Never); @@ -63,6 +67,10 @@ private void ShouldThrowValidationExceptionOnSerializeIfPathIsNull() new NullPathException( message: "Path is null"); + var expectedAdoValidation = new AdoValidationException( + message: "Ado validation exception occurred, try again", + innerException: nullPathException); + // when Action serializeAndWriteToFileAction = () => this.buildService.SerializeAndWriteToFile( @@ -74,8 +82,8 @@ private void ShouldThrowValidationExceptionOnSerializeIfPathIsNull() Assert.Throws( serializeAndWriteToFileAction); - actualAdoValidationException.InnerException.Message.Should() - .BeEquivalentTo(nullPathException.Message); + actualAdoValidationException.Should().BeEquivalentTo( + expectedAdoValidation); this.yamlBrokerMock.Verify(broker => broker.SerializeToYaml(It.IsAny()), diff --git a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.cs b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.cs index 5a2c019..36e9775 100644 --- a/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.cs +++ b/AdoNet.Tests.Unit/Services/Builds/BuildServiceTests.cs @@ -15,7 +15,7 @@ using Tynamix.ObjectFiller; using Xunit; -namespace ADotNet.Tests.Unit.Services +namespace ADotNet.Tests.Unit.Services.Builds { public partial class BuildServiceTests { diff --git a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Exceptions.Write.cs b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Exceptions.Write.cs index d289f87..4f6c8cd 100644 --- a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Exceptions.Write.cs +++ b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Exceptions.Write.cs @@ -7,10 +7,11 @@ using System; using System.Runtime.Serialization; using ADotNet.Models.Foundations.Files.Exceptions; +using FluentAssertions; using Moq; using Xunit; -namespace AdoNet.Tests.Unit.Services.Foundations.Files +namespace ADotNet.Tests.Unit.Services.Foundations.Files { public partial class FileServiceTests { @@ -28,7 +29,7 @@ private void ShouldThrowDependencyValidationExceptionOnWriteIfDependencyValidati message: "Invalid file error occurred.", innerException: dependencyValidationException); - var fileDependencyValidationException = + var expectedFileDependencyValidationException = new FileDependencyValidationException( message: "File dependency validation error occurred, fix the errors and try again.", innerException: invalidFileException); @@ -42,8 +43,11 @@ private void ShouldThrowDependencyValidationExceptionOnWriteIfDependencyValidati this.fileService.WriteToFile(somePath, someContent); // then - Assert.Throws( - writeToFileAction); + FileDependencyValidationException actualFileDependencyValidationException = + Assert.Throws(writeToFileAction); + + actualFileDependencyValidationException.Should().BeEquivalentTo( + expectedFileDependencyValidationException); this.filesBrokerMock.Verify(broker => broker.WriteToFile(It.IsAny(), It.IsAny()), @@ -81,8 +85,11 @@ private void ShouldThrowDependencyExceptionOnWriteIfSerializationErrorOccurs() this.fileService.WriteToFile(somePath, someContent); // then - Assert.Throws( - writeToFileAction); + FileDependencyException actualFileDependencyException = + Assert.Throws(writeToFileAction); + + actualFileDependencyException.Should().BeEquivalentTo( + fileDependencyException); this.filesBrokerMock.Verify(broker => broker.WriteToFile(It.IsAny(), It.IsAny()), @@ -118,7 +125,11 @@ private void ShoudThrowServiceExceptionOnWriteIfServiceErrorOccurs() this.fileService.WriteToFile(somePath, someContent); // then - Assert.Throws(writeToFileAction); + FileServiceException actualFileServiceException = + Assert.Throws(writeToFileAction); + + actualFileServiceException.Should().BeEquivalentTo( + fileServiceException); this.filesBrokerMock.Verify(broker => broker.WriteToFile(It.IsAny(), It.IsAny()), diff --git a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Logic.Write.cs b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Logic.Write.cs index e8ed34a..c996401 100644 --- a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Logic.Write.cs +++ b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Logic.Write.cs @@ -7,7 +7,7 @@ using Moq; using Xunit; -namespace AdoNet.Tests.Unit.Services.Foundations.Files +namespace ADotNet.Tests.Unit.Services.Foundations.Files { public partial class FileServiceTests { diff --git a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Validations.Write.cs b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Validations.Write.cs index 51e18ae..ae0d342 100644 --- a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Validations.Write.cs +++ b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.Validations.Write.cs @@ -6,10 +6,11 @@ using System; using ADotNet.Models.Foundations.Files.Exceptions; +using FluentAssertions; using Moq; using Xunit; -namespace AdoNet.Tests.Unit.Services.Foundations.Files +namespace ADotNet.Tests.Unit.Services.Foundations.Files { public partial class FileServiceTests { @@ -39,8 +40,11 @@ private void ShouldThrowValidationExceptionOnWriteIfPathIsInvalid( someContent); // then - Assert.Throws( - writeToFileAction); + FileValidationException actualFileValidationException = + Assert.Throws(writeToFileAction); + + actualFileValidationException.Should().BeEquivalentTo( + expectedFileValidationException); this.filesBrokerMock.Verify(broker => broker.WriteToFile( @@ -77,8 +81,11 @@ private void ShouldThrowValidationExceptionOnWriteIfContentIsInvalid( invalidContent); // then - Assert.Throws( - writeToFileAction); + FileValidationException actualFileValidationException = + Assert.Throws(writeToFileAction); + + actualFileValidationException.Should().BeEquivalentTo( + expectedFileValidationException); this.filesBrokerMock.Verify(broker => broker.WriteToFile( diff --git a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.cs b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.cs index 1b56ec6..058d7a9 100644 --- a/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.cs +++ b/AdoNet.Tests.Unit/Services/Foundations/Files/FileServiceTests.cs @@ -11,7 +11,7 @@ using Tynamix.ObjectFiller; using Xunit; -namespace AdoNet.Tests.Unit.Services.Foundations.Files +namespace ADotNet.Tests.Unit.Services.Foundations.Files { public partial class FileServiceTests { From 1995e99d78a885f540d81210725fc76d992febdc Mon Sep 17 00:00:00 2001 From: Abdulsamad Osunlana Date: Sun, 14 Apr 2024 15:39:47 +0100 Subject: [PATCH 2/2] CODE RUB: CODE Cleanup, Upgrade Test to Standard 2.10.1 --- .../FileDependencyValidationException.cs | 3 +- .../Builds/BuildService.Exceptions.cs | 29 ++++---- .../Files/FileService.Exceptions.cs | 72 ++++++++++--------- 3 files changed, 57 insertions(+), 47 deletions(-) diff --git a/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs b/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs index e1803ca..2e7711d 100644 --- a/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs +++ b/ADotNet/Models/Foundations/Files/Exceptions/FileDependencyValidationException.cs @@ -4,13 +4,14 @@ // See License.txt in the project root for license information. // --------------------------------------------------------------- +using System; using Xeptions; namespace ADotNet.Models.Foundations.Files.Exceptions { public class FileDependencyValidationException : Xeption { - public FileDependencyValidationException(string message, Xeption innerException) + public FileDependencyValidationException(string message, Exception innerException) : base(message, innerException) { } } diff --git a/ADotNet/Services/Builds/BuildService.Exceptions.cs b/ADotNet/Services/Builds/BuildService.Exceptions.cs index 965b027..84fb075 100644 --- a/ADotNet/Services/Builds/BuildService.Exceptions.cs +++ b/ADotNet/Services/Builds/BuildService.Exceptions.cs @@ -67,32 +67,35 @@ private static void TryCatch(ReturningNothingFunction returningNothingFunction) throw CreateBuildServiceException(exception); } } - + private static AdoValidationException CreateAdoValidationException(Xeption innerException) { return new AdoValidationException( - "Ado validation exception occurred, try again", innerException); + message: "Ado validation exception occurred, try again", + innerException: innerException); } - + private static AdoDependencyValidationException CreateAdoDependencyValidationException( Exception innerException) { return new AdoDependencyValidationException( - "Ado dependency validation error occurs, try again.", - innerException); + message: "Ado dependency validation error occurs, try again.", + innerException: innerException); } private static AdoDependencyException CreateAdoDependencyException( - Exception innerException) => - new AdoDependencyException( - "Ado dependency error occured, contact support.", - innerException); - + Exception innerException) + { + return new AdoDependencyException( + message: "Ado dependency error occured, contact support.", + innerException: innerException); + } + private static Exception CreateBuildServiceException(Exception innerException) { return new BuildServiceException( - "Build service exception occured, contact support.", - innerException); + message: "Build service exception occured, contact support.", + innerException: innerException); } } -} +} \ No newline at end of file diff --git a/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs b/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs index 101a94e..42c90c2 100644 --- a/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs +++ b/ADotNet/Services/Foundations/Files/FileService.Exceptions.cs @@ -31,19 +31,39 @@ private static void TryCatch(ReturningNothingFunction returningNothingFunction) } catch (ArgumentNullException argumentNullException) { - throw CreateFileDependencyValidationException(argumentNullException); + var invalidFileException = + new InvalidFileException( + message: "Invalid file error occurred.", + innerException: argumentNullException); + + throw CreateFileDependencyValidationException(invalidFileException); } catch (ArgumentException argumentException) { - throw CreateFileDependencyValidationException(argumentException); + var invalidFileException = + new InvalidFileException( + message: "Invalid file error occurred.", + innerException: argumentException); + + throw CreateFileDependencyValidationException(invalidFileException); } catch (SerializationException serializationException) { - throw CreateFileDependencyException(serializationException); + var failedFileSerializationException = + new FailedFileSerializationException( + message: "Failed file serialization error occurred, contact support.", + innerException: serializationException); + + throw CreateFileDependencyException(failedFileSerializationException); } catch (Exception exception) { - throw CreateFileServiceException(exception); + var failedFileServiceException = + new FailedFileServiceException( + message: "Failed file service error occurred, contact support.", + innerException: exception); + + throw CreateFileServiceException(failedFileServiceException); } } @@ -51,46 +71,32 @@ private static FileValidationException CreateFileValidationException( Xeption innerException) { return new FileValidationException( - "File validation error occurred, fix the errors and try again.", - innerException); + message: "File validation error occurred, fix the errors and try again.", + innerException: innerException); } - + private static FileDependencyValidationException CreateFileDependencyValidationException( Exception innerException) { - var invalidFileException = - new InvalidFileException( - "Invalid file error occurred.", innerException); - - throw new FileDependencyValidationException( - "File dependency validation error occurred, fix the errors and try again.", - invalidFileException); + return new FileDependencyValidationException( + message: "File dependency validation error occurred, fix the errors and try again.", + innerException: innerException); } private static FileDependencyException CreateFileDependencyException( - Exception innerException) + Xeption innerException) { - var failedFileSerializationException = - new FailedFileSerializationException( - "Failed file serialization error occurred, contact support.", - innerException); - - throw new FileDependencyException( - "File dependency error occurred, contact support.", - failedFileSerializationException); + return new FileDependencyException( + message: "File dependency error occurred, contact support.", + innerException); } private static FileServiceException CreateFileServiceException( - Exception innerException) + Xeption innerException) { - var failedFileServiceException = - new FailedFileServiceException( - "Failed file service error occurred, contact support.", - innerException); - - throw new FileServiceException( - "File service error occurred, contact support.", - failedFileServiceException); + return new FileServiceException( + message: "File service error occurred, contact support.", + innerException: innerException); } } -} +} \ No newline at end of file