< Summary - Core.Tests

Information
Class: Common.Core.Classes.DataFactoryBase
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Classes\DataFactoryBase.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
SerializeJson(...)100%11100%
DeserializeJson(...)100%22100%
GetFileResource(...)100%11100%
ReturnItems(...)100%44100%
GetStartIndex(...)100%44100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Classes\DataFactoryBase.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.Json;
 3
 4namespace Common.Core.Classes;
 5
 6/// <summary>Base class for Data Factory classes.</summary>
 7public abstract class DataFactoryBase
 8{
 9  /// <summary>Default application settings file name.</summary>
 10  public const string cConfigFile = "appsettings.json";
 11
 12  /// <summary>Represents a pseudo-random number generator.</summary>
 113  protected static readonly Random sRandom = new();
 14
 15  /// <summary>Serializes the Json data to a disk file.</summary>
 16  /// <param name="obj">Object containing the data.</param>
 17  /// <param name="path">Location for the Json data file.</param>
 18  /// <param name="file">Name of the file. If not supplied the default name is used.</param>
 19  /// <param name="options">Serialization options.</param>
 20  /// <returns><see langword="true"/> if the data was serialized, otherwise <see langword="false"/> is returned.</return
 21  protected static bool SerializeJson( object obj, string path, string file, JsonSerializerOptions options )
 122  {
 123    return JsonHelper.Serialize( obj, Path.Combine( path, file ), options );
 124  }
 25
 26  /// <summary>Reads a Json disk file and populates a factory object.</summary>
 27  /// <typeparam name="T">Type of factory to populate.</typeparam>
 28  /// <param name="path">Location of the data file.</param>
 29  /// <param name="file">Name of the file.</param>
 30  /// <param name="options">Json serializer options.</param>
 31  /// <returns><see langword="null"/> is returned if the factory could not be populated.</returns>
 32  protected static T? DeserializeJson<T>( string path, string file, JsonSerializerOptions options ) where T : DataFactor
 233  {
 234    var json = GetFileResource( path, file );
 435    if( json is null ) { return null; }
 136    return JsonHelper.DeserializeJson<T>( ref json, options );
 237  }
 38
 39  /// <summary>Returns the Json from a disk file.</summary>
 40  /// <param name="path">Location of the file.</param>
 41  /// <param name="file">Name of the file.</param>
 42  /// <returns><see langword="null"/> is returned if the resource could not be loaded.</returns>
 43  protected static string? GetFileResource( string path, string file )
 444  {
 45    try
 446    {
 447      return File.ReadAllText( Path.Combine( path, file ), Encoding.ASCII );
 48    }
 649    catch( Exception ) { }
 250    return null;
 451  }
 52
 53  /// <summary>Returns the requested number of data objects from a list.</summary>
 54  /// <typeparam name="T">Data object class or interface.</typeparam>
 55  /// <param name="data">Collection of data objects.</param>
 56  /// <param name="count">Number of data objects to return.</param>
 57  /// <returns>IList collection of data objects.</returns>
 58  protected static IList<T> ReturnItems<T>( List<T> data, int count )
 259  {
 260    int max = data.Count - count;
 261    if( max > 0 && count < data.Count )
 162    {
 163      var idx = sRandom.Next( 1, max + 1 );
 164      return data.GetRange( idx, count );
 65    }
 166    return data;
 267  }
 68
 69  /// <summary>Returns the start index for a requested number of data objects.</summary>
 70  /// <param name="total">Total count of data objects.</param>
 71  /// <param name="count">Number of data objects to return.</param>
 72  /// <returns>The starting index.</returns>
 73  protected static int GetStartIndex( int total, int count )
 374  {
 375    int max = total - count;
 376    if( count > 0 && count <= total )
 177    {
 178      return sRandom.Next( 1, max + 1 );
 79    }
 280    return 0;
 381  }
 82}