From a166898583335ee4107b44c80d24187e715ced8c Mon Sep 17 00:00:00 2001 From: samatstarion Date: Sun, 8 Dec 2024 13:35:00 +0100 Subject: [PATCH] [Add] Parameter Validation --- DEH-CSV/CsvReader.cs | 74 ++++++++++++++--------------- DEH-CSV/CsvWriter.cs | 27 +++++++++++ DEH-CSV/Mapping/PropertyMap.cs | 2 +- DEH-CSV/Mapping/TypeMap.cs | 2 +- DEH-CSV/Services/IterationReader.cs | 26 +++++++++- 5 files changed, 91 insertions(+), 40 deletions(-) diff --git a/DEH-CSV/CsvReader.cs b/DEH-CSV/CsvReader.cs index 4410d27..d646ef4 100644 --- a/DEH-CSV/CsvReader.cs +++ b/DEH-CSV/CsvReader.cs @@ -73,8 +73,44 @@ public CsvReader(ILogger logger) /// A that returns a collection of mapped s public async Task> ReadAsync(Stream stream, IReadOnlyCollection typeMaps, ISession session) { - ValidateReadParameters(stream, typeMaps, session); + if (stream == null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (typeMaps == null) + { + throw new ArgumentNullException(nameof(typeMaps)); + } + + if (session == null) + { + throw new ArgumentNullException(nameof(session)); + } + + if (typeMaps.Count == 0) + { + throw new ArgumentException("The provided collection of TypeMap is empty", nameof(typeMaps)); + } + + if (typeMaps.SelectMany(x => x.Properties).Any(x => x.IsIdentifierProperty && !x.ClassKind.HasValue)) + { + throw new InvalidDataException("One of the IdentifierProperty do not specify the ClassKind to query"); + } + return await this.ReadInternalAsync(stream, typeMaps, session); + } + + /// + /// Reads the CSV content of the and maps it to s based on the provided collection of + /// s + /// + /// The that contains CSV content + /// The collection of s + /// The that helps to retrieve + /// A that returns a collection of mapped s + private async Task> ReadInternalAsync(Stream stream, IReadOnlyCollection typeMaps, ISession session) + { var things = new List(); var accessibleThings = session.Assembler.Cache @@ -455,42 +491,6 @@ private async Task ReadHeaderAsync(IReader reader, IEnumerable typeMaps } } - /// - /// Validates all parameters provided for the method - /// - /// The provided - /// The provided collection of s - /// The provided - /// - /// - private static void ValidateReadParameters(Stream stream, IReadOnlyCollection typeMaps, ISession session) - { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } - - if (typeMaps == null) - { - throw new ArgumentNullException(nameof(typeMaps)); - } - - if (session == null) - { - throw new ArgumentNullException(nameof(session)); - } - - if (typeMaps.Count == 0) - { - throw new ArgumentException("The provided collection of TypeMap is empty", nameof(typeMaps)); - } - - if (typeMaps.SelectMany(x => x.Properties).Any(x => x.IsIdentifierProperty && !x.ClassKind.HasValue)) - { - throw new InvalidDataException("One of the IdentifierProperty do not specify the ClassKind to query"); - } - } - /// /// Queries the value to use for a cell inside a CSV row based on a /// diff --git a/DEH-CSV/CsvWriter.cs b/DEH-CSV/CsvWriter.cs index 110d706..8d39072 100644 --- a/DEH-CSV/CsvWriter.cs +++ b/DEH-CSV/CsvWriter.cs @@ -94,6 +94,11 @@ public void Write(Iteration iteration, bool includeNestedElements, IEnumerable item.Value.Value).ToList(); if (includeNestedElements) @@ -135,6 +140,13 @@ public void Write(Iteration iteration, bool includeNestedElements, IEnumerable /// The target to which the CSV file is to be written /// + /// + /// an object that may contain any kind of configuration that is required + /// for the evaluation of the custom property. This may be a value property + /// such as a string or int or a complex object. It is the responsibility + /// of the interface implementation to verify that the options argument is + /// of the correct type + /// public virtual void Write(IEnumerable things, TypeMap typeMap, DirectoryInfo target, object options) { if (things == null) @@ -257,6 +269,16 @@ protected virtual object QueryValue(Thing thing, PropertyMap propertyMap, object /// protected void WriteHeader(CsvHelper.CsvWriter csvWriter, TypeMap typeMap) { + if (csvWriter == null) + { + throw new ArgumentNullException(nameof(csvWriter)); + } + + if (typeMap == null) + { + throw new ArgumentNullException(nameof(typeMap)); + } + this.logger.LogDebug("Writing Header for {TypeMap}", typeMap.ClassKind.ToString()); foreach (var propertyMap in typeMap.Properties) @@ -280,6 +302,11 @@ protected void WriteHeader(CsvHelper.CsvWriter csvWriter, TypeMap typeMap) /// protected internal string QueryFileName(TypeMap typeMap) { + if (typeMap == null) + { + throw new ArgumentNullException(nameof(typeMap)); + } + if (!string.IsNullOrEmpty(typeMap.FileName)) { return typeMap.FileName; diff --git a/DEH-CSV/Mapping/PropertyMap.cs b/DEH-CSV/Mapping/PropertyMap.cs index ecfe618..6e297f5 100644 --- a/DEH-CSV/Mapping/PropertyMap.cs +++ b/DEH-CSV/Mapping/PropertyMap.cs @@ -28,7 +28,7 @@ namespace STARIONGROUP.DEHCSV.Mapping public class PropertyMap { /// - /// Gets or sets the source property name on the ECSS-E-TM-10-25 class in case of the mapping from ECSS-E-TM10-25 to CSV. + /// Gets or sets the source property name on the ECSS-E-TM-10-25 class in case of the mapping from ECSS-E-TM-10-25 to CSV. /// Gets or sets the column name of the CSV to use to retrieve a value /// public string Source { get; set; } diff --git a/DEH-CSV/Mapping/TypeMap.cs b/DEH-CSV/Mapping/TypeMap.cs index d24c4ff..413dbd0 100644 --- a/DEH-CSV/Mapping/TypeMap.cs +++ b/DEH-CSV/Mapping/TypeMap.cs @@ -42,6 +42,6 @@ public class TypeMap /// /// Gets or sets the s /// - public List Properties { get; set; } = new List(); + public List Properties { get; set; } = []; } } diff --git a/DEH-CSV/Services/IterationReader.cs b/DEH-CSV/Services/IterationReader.cs index 9de184b..f91b767 100644 --- a/DEH-CSV/Services/IterationReader.cs +++ b/DEH-CSV/Services/IterationReader.cs @@ -81,6 +81,30 @@ public async Task ReadAsync(ISession session, string modelShortName, throw new ArgumentNullException(nameof(session)); } + return await ReadInternalAsync(session, modelShortName, iterationNumber, domainOfExpertiseShortName); + } + + /// + /// ReadAsync the iteration from the + /// + /// + /// The object used to read the data + /// + /// + /// The shortName of the that is to be opened + /// + /// + /// The number of hte that is to be read + /// + /// + /// The shortName of the used to open the with + /// + /// + /// An instance of + /// + private async Task ReadInternalAsync(ISession session, string modelShortName, int iterationNumber, + string domainOfExpertiseShortName) + { this.logger.LogDebug("Setting up the read request for {ModelShortName}:{IterationNumber}", modelShortName, iterationNumber); var siteDirectory = session.RetrieveSiteDirectory(); @@ -104,7 +128,7 @@ public async Task ReadAsync(ISession session, string modelShortName, { throw new InstanceNotFoundException($"The DomainOfExpertise with shortName {domainOfExpertiseShortName} could not be found"); } - + var engineeringModel = new EngineeringModel(engineeringModelSetup.EngineeringModelIid, session.Assembler.Cache, session.Credentials.Uri); engineeringModel.EngineeringModelSetup = engineeringModelSetup;