Skip to content

Commit

Permalink
Merge branch 'feature/Support_conditions_related_to_IO_(#561)' into d…
Browse files Browse the repository at this point in the history
…evelop
  • Loading branch information
Cédric L. Charlier committed Jan 6, 2020
2 parents eb8ca9a + bdacad0 commit ea21084
Show file tree
Hide file tree
Showing 20 changed files with 494 additions and 49 deletions.
1 change: 1 addition & 0 deletions NBi.Core/Decoration/DecorationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public IDecorationCondition Instantiate(IDecorationConditionArgs args)
switch (args)
{
case IProcessConditionArgs processArgs: return new ProcessConditionFactory().Instantiate(processArgs);
case IIoConditionArgs ioArgs: return new IoConditionFactory().Instantiate(ioArgs);
case ICustomConditionArgs customConditionArgs: return new CustomConditionFactory().Instantiate(customConditionArgs);
default: throw new ArgumentOutOfRangeException();
}
Expand Down
41 changes: 41 additions & 0 deletions NBi.Core/Decoration/IO/Conditions/FileExistsCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NBi.Core.Decoration.IO;
using System.Diagnostics;
using NBi.Extensibility;

namespace NBi.Core.Decoration.IO.Conditions
{
class FileExistsCondition : IDecorationCondition
{
private FileExistsConditionArgs Args { get; }

public string Message { get; private set; }

public FileExistsCondition(FileExistsConditionArgs args) => Args = args;

public bool Validate()
{
var fullPath = PathExtensions.CombineOrRoot(Args.BasePath, Args.FolderName.Execute(), Args.FileName.Execute());

var conditions = new List<Func<string, (bool, string)>>() { ExistsCondition };
if (Args.NotEmpty.Execute())
conditions.Add(IsNotEmptyCondition);

var result = true;
var enumerator = conditions.GetEnumerator();
while (result && enumerator.MoveNext())
(result, Message) = enumerator.Current.Invoke(fullPath);
return result;
}

protected (bool, string) ExistsCondition(string fullPath)
=> (File.Exists(fullPath), $"The file '{fullPath}' doesn't exists.");

protected (bool, string) IsNotEmptyCondition(string fullPath)
=> (new FileInfo(fullPath).Length > 0, $"The file '{fullPath}' has a size of 0 byte.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
using System.Diagnostics;
using NBi.Extensibility;

namespace NBi.Core.Decoration.IO.Commands
namespace NBi.Core.Decoration.IO.Conditions
{
class FolderExistsCondition : IDecorationCondition
{
private FolderExistsConditionArgs Args { get; }

public string Message => throw new NotImplementedException();
public string Message { get; private set; }

public FolderExistsCondition(FolderExistsConditionArgs args) => Args = args;

Expand All @@ -22,21 +22,21 @@ public bool Validate()
var path = PathExtensions.CombineOrRoot(Args.BasePath, Args.FolderPath.Execute());
var fullPath = PathExtensions.CombineOrRoot(path, Args.FolderName.Execute());

var conditions = new List<Func<string, bool>>() { ExistsCondition };
var conditions = new List<Func<string, (bool, string)>>() { ExistsCondition };
if (Args.NotEmpty.Execute())
conditions.Add(IsEmptyCondition);
conditions.Add(IsNotEmptyCondition);

var result = true;
var enumerator = conditions.GetEnumerator();
while (result && enumerator.MoveNext())
result = enumerator.Current.Invoke(fullPath);
(result, Message) = enumerator.Current.Invoke(fullPath);
return result;
}

protected bool ExistsCondition(string fullPath)
=> Directory.Exists(fullPath);
protected (bool, string) ExistsCondition(string fullPath)
=> (Directory.Exists(fullPath), $"The file '{fullPath}' doesn't exists.");

protected bool IsEmptyCondition(string fullPath)
=> !string.IsNullOrEmpty(Directory.EnumerateFiles(fullPath).FirstOrDefault());
protected (bool, string) IsNotEmptyCondition(string fullPath)
=> (!string.IsNullOrEmpty(Directory.EnumerateFiles(fullPath).FirstOrDefault()), $"The directory '{fullPath}' doesn't contain any file.");
}
}
19 changes: 19 additions & 0 deletions NBi.Core/Decoration/IO/FileExistsConditionArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NBi.Core.Scalar.Resolver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NBi.Core.Decoration.IO
{
public class FileExistsConditionArgs : IIoConditionArgs
{
public string BasePath { get; }
public IScalarResolver<string> FolderName { get; }
public IScalarResolver<string> FileName { get; }
public IScalarResolver<bool> NotEmpty { get; }

public FileExistsConditionArgs(string basePath, IScalarResolver<string> folderName, IScalarResolver<string> fileName, IScalarResolver<bool> notEmpty)
=> (BasePath, FolderName, FileName, NotEmpty) = (basePath, folderName, fileName, notEmpty);
}
}
22 changes: 22 additions & 0 deletions NBi.Core/Decoration/Process/IoConditionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NBi.Core.Decoration.IO;
using NBi.Core.Decoration.IO.Conditions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NBi.Core.Decoration.Process
{
class IoConditionFactory
{
public IDecorationCondition Instantiate(IIoConditionArgs args)
{
switch (args)
{
case FolderExistsConditionArgs folderExistsArgs: return new FolderExistsCondition(folderExistsArgs);
case FileExistsConditionArgs fileExistsArgs: return new FileExistsCondition(fileExistsArgs);
default: throw new ArgumentOutOfRangeException();
}
}
}
}
5 changes: 4 additions & 1 deletion NBi.Core/NBi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@
<Compile Include="Calculation\Grouping\ColumnBased\ColumnGroupByArgs.cs" />
<Compile Include="Calculation\Grouping\IGroupByArgs.cs" />
<Compile Include="Calculation\Grouping\NoneGroupByArgs.cs" />
<Compile Include="Decoration\IO\Commands\FolderExistsCondition.cs" />
<Compile Include="Decoration\IO\Conditions\FileExistsCondition.cs" />
<Compile Include="Decoration\IO\Conditions\FolderExistsCondition.cs" />
<Compile Include="Decoration\IO\FileExistsConditionArgs.cs" />
<Compile Include="Decoration\IO\FolderExistsConditionArgs.cs" />
<Compile Include="Decoration\IO\IIoConditionArgs.cs" />
<Compile Include="Decoration\Process\IoConditionFactory.cs" />
<Compile Include="ResultSet\Filtering\PredicationArgs.cs" />
<Compile Include="ResultSet\Filtering\IFilteringArgs.cs" />
<Compile Include="Calculation\Predication\AndCombinationPredication.cs" />
Expand Down
29 changes: 27 additions & 2 deletions NBi.NUnit/Builder/Helper/ConditionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NBi.Core;
using NBi.Core.Assemblies.Decoration;
using NBi.Core.Decoration;
using NBi.Core.Decoration.IO;
using NBi.Core.Decoration.Process;
using NBi.Core.Injection;
using NBi.Core.Scalar.Resolver;
Expand Down Expand Up @@ -31,7 +32,9 @@ public IDecorationConditionArgs Execute(object condition)
switch (condition)
{
case CustomConditionXml custom: return BuildCustomCondition(custom);
case ServiceRunningXml serviceRunning: return BuildServiceRunning(serviceRunning);
case ServiceRunningConditionXml serviceRunning: return BuildServiceRunning(serviceRunning);
case FileExistsConditionXml fileExists: return BuildFileExists(fileExists);
case FolderExistsConditionXml folderExists: return BuildFolderExists(folderExists);
default: throw new ArgumentOutOfRangeException();
}
}
Expand All @@ -47,7 +50,7 @@ private IDecorationConditionArgs BuildCustomCondition(CustomConditionXml custom)
);
}

private IDecorationConditionArgs BuildServiceRunning(ServiceRunningXml serviceRunning)
private IDecorationConditionArgs BuildServiceRunning(ServiceRunningConditionXml serviceRunning)
{
var scalarHelper = new ScalarHelper(serviceLocator, new Context(variables));
return new RunningArgs(
Expand All @@ -56,6 +59,28 @@ private IDecorationConditionArgs BuildServiceRunning(ServiceRunningXml serviceRu
);
}

private IDecorationConditionArgs BuildFileExists(FileExistsConditionXml fileExists)
{
var scalarHelper = new ScalarHelper(serviceLocator, new Context(variables));
return new FileExistsConditionArgs(
serviceLocator.BasePath
, scalarHelper.InstantiateResolver<string>(fileExists.Path)
, scalarHelper.InstantiateResolver<string>(fileExists.Name)
, scalarHelper.InstantiateResolver<bool>(fileExists.NotEmpty)
);
}

private IDecorationConditionArgs BuildFolderExists(FolderExistsConditionXml folderExists)
{
var scalarHelper = new ScalarHelper(serviceLocator, new Context(variables));
return new FolderExistsConditionArgs(
serviceLocator.BasePath
, scalarHelper.InstantiateResolver<string>(folderExists.Path)
, scalarHelper.InstantiateResolver<string>(folderExists.Name)
, scalarHelper.InstantiateResolver<bool>(folderExists.NotEmpty)
);
}

private class RunningArgs : IRunningConditionArgs
{
public RunningArgs(IScalarResolver<string> serviceName, IScalarResolver<int> timeOut)
Expand Down
6 changes: 6 additions & 0 deletions NBi.NUnit/Builder/Helper/ScalarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public IScalarResolver<T> InstantiateResolver<T>(string value)
return resolver;
}

public IScalarResolver<T> InstantiateResolver<T>(bool value)
=> InstantiateResolver<T>(value.ToString());

public IScalarResolver<T> InstantiateResolver<T>(int value)
=> InstantiateResolver<T>(value.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat));

public IScalarResolver InstantiateResolver(ColumnType columnType, string value)
{
var argsBuilder = new ScalarResolverArgsBuilder(ServiceLocator, Context);
Expand Down
5 changes: 5 additions & 0 deletions NBi.Testing.Core/Decoration/DecorationFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Reflection;
using NBi.Extensibility.Decoration;
using NBi.Extensibility.Decoration.DataEngineering;
using NBi.Core.Decoration.IO.Conditions;

namespace NBi.Testing.Core.Decoration.DataEngineering
{
Expand Down Expand Up @@ -119,6 +120,8 @@ private IDecorationConditionArgs GetConditionArgsMock(Type type)
switch (type)
{
case Type x when x == typeof(IRunningConditionArgs): return Mock.Of<IRunningConditionArgs>();
case Type x when x == typeof(FolderExistsConditionArgs): return new FolderExistsConditionArgs(string.Empty, null, null, null);
case Type x when x == typeof(FileExistsConditionArgs): return new FileExistsConditionArgs (string.Empty, null, null, null);
case Type x when x == typeof(ICustomConditionArgs): return Mock.Of<ICustomConditionArgs>
(
y => y.AssemblyPath == new LiteralScalarResolver<string>($@"{FileOnDisk.GetDirectoryPath()}\NBi.Testing.Core.dll")
Expand All @@ -130,6 +133,8 @@ private IDecorationConditionArgs GetConditionArgsMock(Type type)

[Test]
[TestCase(typeof(IRunningConditionArgs), typeof(RunningCondition))]
[TestCase(typeof(FolderExistsConditionArgs), typeof(FolderExistsCondition))]
[TestCase(typeof(FileExistsConditionArgs), typeof(FileExistsCondition))]
[TestCase(typeof(ICustomConditionArgs), typeof(CustomCondition))]
public void Get_IDecorationConditionArgs_CorrectCondition(Type argsType, Type conditionType)
{
Expand Down
90 changes: 86 additions & 4 deletions NBi.Testing.Xml/Decoration/ConditionXmlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public void Deserialize_SampleFile_RunningService()
TestSuiteXml ts = DeserializeSample();

// Check the properties of the object.
Assert.That(ts.Tests[testNr].Condition.Predicates[0], Is.TypeOf<ServiceRunningXml>());
var check = ts.Tests[testNr].Condition.Predicates[0] as ServiceRunningXml;
Assert.That(ts.Tests[testNr].Condition.Predicates[0], Is.TypeOf<ServiceRunningConditionXml>());
var check = ts.Tests[testNr].Condition.Predicates[0] as ServiceRunningConditionXml;
Assert.That(check.TimeOut, Is.EqualTo("5000")); //Default value
Assert.That(check.ServiceName, Is.EqualTo("MyService"));

// Check the properties of the object.
Assert.That(ts.Tests[testNr].Condition.Predicates[1], Is.TypeOf<ServiceRunningXml>());
var check2 = ts.Tests[testNr].Condition.Predicates[1] as ServiceRunningXml;
Assert.That(ts.Tests[testNr].Condition.Predicates[1], Is.TypeOf<ServiceRunningConditionXml>());
var check2 = ts.Tests[testNr].Condition.Predicates[1] as ServiceRunningConditionXml;
Assert.That(check2.TimeOut, Is.EqualTo("1000")); //Value Specified
Assert.That(check2.ServiceName, Is.EqualTo("MyService2"));
}
Expand Down Expand Up @@ -127,5 +127,87 @@ public void Serialize_CustomWithParameters_Correct()
Console.WriteLine(xml);
Assert.That(xml, Does.Contain("<parameter name=\"firstParam\">myValue</parameter>"));
}

[Test]
public void Deserialize_SampleFile_FolderExists()
{
int testNr = 2;

// Create an instance of the XmlSerializer specifying type and namespace.
var ts = DeserializeSample();

// Check the properties of the object.
Assert.That(ts.Tests[testNr].Condition.Predicates[0], Is.TypeOf<FolderExistsConditionXml>());
var condition = ts.Tests[testNr].Condition.Predicates[0] as FolderExistsConditionXml;
Assert.That(condition.Path, Is.EqualTo(@"..\"));
Assert.That(condition.Name, Is.EqualTo("MyFolder"));
Assert.That(condition.NotEmpty, Is.False);
}

[Test]
public void Serialize_FolderExists_Correct()
{
var root = new ConditionXml()
{
Predicates = new List<DecorationConditionXml>()
{
new FolderExistsConditionXml()
{
Path = ".",
Name = "myFolderName",
NotEmpty = false
}
}
};

var manager = new XmlManager();
var xml = manager.XmlSerializeFrom(root);
Console.WriteLine(xml);
Assert.That(xml, Does.Contain("<folder-exists "));
Assert.That(xml, Does.Contain("path=\".\""));
Assert.That(xml, Does.Contain("name=\"myFolderName\""));
Assert.That(xml, Does.Not.Contain("not-empty"));
}

[Test]
public void Deserialize_SampleFile_FileExists()
{
int testNr = 3;

// Create an instance of the XmlSerializer specifying type and namespace.
var ts = DeserializeSample();

// Check the properties of the object.
Assert.That(ts.Tests[testNr].Condition.Predicates[0], Is.TypeOf<FileExistsConditionXml>());
var condition = ts.Tests[testNr].Condition.Predicates[0] as FileExistsConditionXml;
Assert.That(condition.Path, Is.EqualTo(@"..\"));
Assert.That(condition.Name, Is.EqualTo("MyFile.txt"));
Assert.That(condition.NotEmpty, Is.True);
}

[Test]
public void Serialize_FileExists_Correct()
{
var root = new ConditionXml()
{
Predicates = new List<DecorationConditionXml>()
{
new FileExistsConditionXml()
{
Path = "Folder\\",
Name = "myFileName.txt",
NotEmpty = true
}
}
};

var manager = new XmlManager();
var xml = manager.XmlSerializeFrom(root);
Console.WriteLine(xml);
Assert.That(xml, Does.Contain("<file-exists "));
Assert.That(xml, Does.Contain("path=\"Folder\\\""));
Assert.That(xml, Does.Contain("name=\"myFileName.txt\""));
Assert.That(xml, Does.Contain("not-empty=\"true\""));
}
}
}
Loading

0 comments on commit ea21084

Please sign in to comment.