Skip to content

Commit

Permalink
[Add] ParameterValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
samatstariongroup committed Dec 8, 2024
1 parent e6ed504 commit 85845a2
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 16 deletions.
6 changes: 3 additions & 3 deletions DEH-CSV.Tests/Services/DataSourceSelectorTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Verify_that_when_a_file_uri_is_provided_a_file_dal_is_returned()

var dal = this.dataSourceSelector.Select(uri);

Assert.That(dal, Is.InstanceOf(typeof(CDP4JsonFileDal.JsonFileDal)));
Assert.That(dal, Is.InstanceOf<CDP4JsonFileDal.JsonFileDal>());
}

[Test]
Expand All @@ -68,11 +68,11 @@ public void Verify_that_when_a_http_uri_is_provided_a_file_dal_is_returned()

var dal = this.dataSourceSelector.Select(httpUri);

Assert.That(dal, Is.InstanceOf(typeof(CDP4ServicesDal.CdpServicesDal)));
Assert.That(dal, Is.InstanceOf<CDP4ServicesDal.CdpServicesDal>());

dal = this.dataSourceSelector.Select(httpsUri);

Assert.That(dal, Is.InstanceOf(typeof(CDP4ServicesDal.CdpServicesDal)));
Assert.That(dal, Is.InstanceOf<CDP4ServicesDal.CdpServicesDal>());
}

[Test]
Expand Down
27 changes: 21 additions & 6 deletions DEH-CSV/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public CsvWriter(ILoggerFactory loggerFactory = null)
/// </param>
public void Write(Iteration iteration, bool includeNestedElements, IEnumerable<TypeMap> maps, DirectoryInfo target, object options)
{
if (iteration == null)
{
throw new ArgumentNullException(nameof(iteration));
}

var things = iteration.Cache.Select(item => item.Value.Value).ToList();

if (includeNestedElements)
Expand All @@ -105,15 +110,15 @@ public void Write(Iteration iteration, bool includeNestedElements, IEnumerable<T
}
}

this.logger.LogDebug("A total of {count} are available in the data set", things.Count);
this.logger.LogDebug("A total of {Count} are available in the data set", things.Count);

foreach (var typeMap in maps)

Check warning on line 115 in DEH-CSV/CsvWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to add validation of parameter 'maps' before using it. (https://rules.sonarsource.com/csharp/RSPEC-3900)
{
var targetThings = things.Where(x => x.ClassKind == typeMap.ClassKind).ToList();

this.Write(targetThings, typeMap, target, options);

this.logger.LogInformation("Writing CSV file for {classKind}", typeMap.ClassKind);
this.logger.LogInformation("Writing CSV file for {ClassKind}", typeMap.ClassKind);
}
}

Expand Down Expand Up @@ -182,7 +187,7 @@ public virtual void Write(IEnumerable<Thing> things, TypeMap typeMap, DirectoryI
{
if (queryResult != null)
{
var csvValue = propertyMap.ValuePrefix + queryResult.ToString();
var csvValue = propertyMap.ValuePrefix + queryResult;

csvWriter.WriteField(csvValue, true);
}
Expand All @@ -198,7 +203,7 @@ public virtual void Write(IEnumerable<Thing> things, TypeMap typeMap, DirectoryI

csvWriter.Flush();

this.logger.LogDebug("A total of {records} records was written to {filepath} in {time} [ms]",
this.logger.LogDebug("A total of {Records} records was written to {Filepath} in {Time} [ms]",
things.Count(), path, sw.ElapsedMilliseconds);
}

Expand Down Expand Up @@ -228,6 +233,16 @@ public virtual void Write(IEnumerable<Thing> things, TypeMap typeMap, DirectoryI
/// </remarks>
protected virtual object QueryValue(Thing thing, PropertyMap propertyMap, object options)
{
if (thing == null)
{
throw new ArgumentNullException(nameof(thing));
}

if (propertyMap == null)
{
throw new ArgumentNullException(nameof(propertyMap));
}

return thing.QueryValue(propertyMap.Source);
}

Expand All @@ -242,7 +257,7 @@ protected virtual object QueryValue(Thing thing, PropertyMap propertyMap, object
/// </param>
protected void WriteHeader(CsvHelper.CsvWriter csvWriter, TypeMap typeMap)
{
this.logger.LogDebug("Writing Header for {typeMap}", typeMap.ClassKind.ToString());
this.logger.LogDebug("Writing Header for {TypeMap}", typeMap.ClassKind.ToString());

Check warning on line 260 in DEH-CSV/CsvWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to add validation of parameter 'typeMap' before using it. (https://rules.sonarsource.com/csharp/RSPEC-3900)

foreach (var propertyMap in typeMap.Properties)
{
Expand Down Expand Up @@ -281,4 +296,4 @@ protected internal string QueryFileName(TypeMap typeMap)
return string.Join("-", result);
}
}
}
}
5 changes: 5 additions & 0 deletions DEH-CSV/CustomProperties/ThingTimeStampPropertyEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public string QueryValue(Thing thing, PropertyMap propertyMap, object options)
// ThingTimeStamp or ThingTimeStampPropertyEvaluator and override the
// CsvWriter.QueryValue method

if (thing == null)
{
throw new ArgumentNullException(nameof(thing));
}

return $"{thing.ClassKind}:{DateTime.UtcNow}";

Check warning on line 69 in DEH-CSV/CustomProperties/ThingTimeStampPropertyEvaluator.cs

View workflow job for this annotation

GitHub Actions / Build

Prefer using "DateTimeOffset" instead of "DateTime" (https://rules.sonarsource.com/csharp/RSPEC-6566)

Check warning on line 69 in DEH-CSV/CustomProperties/ThingTimeStampPropertyEvaluator.cs

View workflow job for this annotation

GitHub Actions / Build

Use a testable (date) time provider instead. (https://rules.sonarsource.com/csharp/RSPEC-6354)
}
}
Expand Down
11 changes: 11 additions & 0 deletions DEH-CSV/CustomProperties/ThingTimeStampedCSVWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace STARIONGROUP.DEHCSV.CustomProperties
using CDP4Common.CommonData;

using STARIONGROUP.DEHCSV.Mapping;
using System;

/// <summary>
/// Custom CSV Writer that used the <see cref="ThingTimeStampPropertyEvaluator"/>
Expand Down Expand Up @@ -56,6 +57,16 @@ public class ThingTimeStampedCSVWriter : CsvWriter
/// </remarks>
protected override object QueryValue(Thing thing, PropertyMap propertyMap, object options)
{
if (thing == null)
{
throw new ArgumentNullException(nameof(thing));
}

if (propertyMap == null)
{
throw new ArgumentNullException(nameof(propertyMap));
}

switch (propertyMap.Source)
{
case "ThingTimeStamp":
Expand Down
2 changes: 1 addition & 1 deletion DEH-CSV/ICsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ public interface ICsvWriter
/// </param>
public void Write(Iteration iteration, bool includeNestedElements, IEnumerable<TypeMap> maps, DirectoryInfo target, object options);
}
}
}
4 changes: 2 additions & 2 deletions DEH-CSV/Mapping/MappingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public MappingProvider(ILoggerFactory loggerFactory = null)
/// </returns>
public IEnumerable<TypeMap> QueryMappings(string path)
{
this.logger.LogDebug("Reading the mapping from {path}", path);
this.logger.LogDebug("Reading the mapping from {Path}", path);

var json = File.ReadAllText(path);

Expand All @@ -74,7 +74,7 @@ public IEnumerable<TypeMap> QueryMappings(string path)

var mappings = JsonSerializer.Deserialize<List<TypeMap>>(json, options);

this.logger.LogDebug("A total of {count} mappings have been found", mappings.Count);
this.logger.LogDebug("A total of {Count} mappings have been found", mappings.Count);

return mappings;
}
Expand Down
2 changes: 1 addition & 1 deletion DEH-CSV/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
// -------------------------------------------------------------------------------------------------
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("DEH-CSV.Tests")]
[assembly: InternalsVisibleTo("DEH-CSV.Tests")]
8 changes: 7 additions & 1 deletion DEH-CSV/Services/DataSourceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace STARIONGROUP.DEHCSV.Services
{
using System;
using System.Globalization;

using CDP4Dal.DAL;

Expand Down Expand Up @@ -61,7 +62,12 @@ public DataSourceSelector(ILoggerFactory loggerFactory = null)
/// </returns>
public IDal Select(Uri uri)
{
switch (uri.Scheme.ToLower())
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}

switch (uri.Scheme.ToLower(CultureInfo.InvariantCulture))
{
case "http":
case "https":
Expand Down
10 changes: 8 additions & 2 deletions DEH-CSV/Services/IterationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace STARIONGROUP.DEHCSV.Services

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;

/// <summary>
/// The purpose of the <see cref="IIterationReader"/> is to read an <see cref="Iteration"/>
Expand Down Expand Up @@ -75,7 +76,12 @@ public IterationReader(ILoggerFactory loggerFactory = null)
/// </returns>
public async Task<Iteration> ReadAsync(ISession session, string modelShortName, int iterationNumber, string domainOfExpertiseShortName)

Check warning on line 77 in DEH-CSV/Services/IterationReader.cs

View workflow job for this annotation

GitHub Actions / Build

Split this method into two, one handling parameters check and the other handling the asynchronous code. (https://rules.sonarsource.com/csharp/RSPEC-4457)
{
this.logger.LogDebug("Setting up the read request for {modelShortName}:{iterationNumber}", modelShortName, iterationNumber);
if (session == null)
{
throw new ArgumentNullException(nameof(session));
}

this.logger.LogDebug("Setting up the read request for {ModelShortName}:{IterationNumber}", modelShortName, iterationNumber);

var siteDirectory = session.RetrieveSiteDirectory();
var engineeringModelSetup = siteDirectory.Model.SingleOrDefault(x => x.ShortName == modelShortName);
Expand Down Expand Up @@ -107,7 +113,7 @@ public async Task<Iteration> ReadAsync(ISession session, string modelShortName,

engineeringModel.Iteration.Add(iteration);

this.logger.LogDebug("Reading iteration data from the data-source {dataSource}", session.DataSourceUri);
this.logger.LogDebug("Reading iteration data from the data-source {DataSource}", session.DataSourceUri);
await session.Read(iteration, domainOfExpertise);

this.logger.LogDebug("Reading iteration from cache");
Expand Down

0 comments on commit 85845a2

Please sign in to comment.