Skip to content

Commit

Permalink
Adding unit tests + part of SLDisUnitTestsShared (Common.Testing)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichielOda committed Dec 28, 2023
1 parent 463cba3 commit 85f8286
Show file tree
Hide file tree
Showing 1,990 changed files with 175,860 additions and 8 deletions.
18 changes: 18 additions & 0 deletions Common.Testing/Common.Testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AssemblyName>Skyline.DataMiner.CICD.Validators.Common.Testing</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="QActionHelper">
<HintPath>..\DLLs\QActionHelper.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
71 changes: 71 additions & 0 deletions Common.Testing/ProtocolQActionHelperProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace SLDisUnitTestsShared
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

using Skyline.DataMiner.CICD.Validators.Common.Interfaces;
using Skyline.DataMiner.Scripting;

public class ProtocolQActionHelperProvider : IProtocolQActionHelperProvider
{
private const string AutoGeneratedCodeByDis = "// <auto-generated>This is auto-generated code by DIS. Do not modify.</auto-generated>";
private static readonly Mutex _mutex;

static ProtocolQActionHelperProvider()
{
string mutexId = $"Global\\{typeof(ProtocolQActionHelperProvider).GUID}";
_mutex = new Mutex(false, mutexId);
}

public string GetProtocolQActionHelper(string protocolCode, bool ignoreErrors = false)
{
var tempPath = Path.GetTempPath();
var tempName = Guid.NewGuid().ToString("N");
var tempFile = Path.Combine(tempPath, tempName + ".txt");

List<XMLParseError> result;

// prevent System.IO.IOException: The process cannot access the file 'C:\Skyline DataMiner\logging\SLQActionHelper.txt'
try
{
_mutex.WaitOne();
result = QActionHelper.CreateProtocolQActionHelperFromString(protocolCode, tempPath, tempName);
}
finally
{
_mutex.ReleaseMutex();
}

if (!ignoreErrors && result != null && result.Count > 0)
{
StringBuilder error = new StringBuilder();
error.Append("Couldn't generate QAction helper:");

foreach (var err in result)
{
error.Append("\n Line " + err.Line + ": " + err.Description);
}

throw new Exception(error.ToString());
}

var csContent = new StringBuilder();
csContent.AppendLine(AutoGeneratedCodeByDis);

if (File.Exists(tempFile))
{
csContent.Append(File.ReadAllText(tempFile));
File.Delete(tempFile);
}
else
{
csContent.Append("namespace Skyline.DataMiner.Scripting { public class SLProtocolExt : SLProtocol { } }");
}

return csContent.ToString();
}
}
}
80 changes: 80 additions & 0 deletions Common.Testing/ProtocolTestsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace SLDisUnitTestsShared
{
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;

using Skyline.DataMiner.CICD.Models.Common;
using Skyline.DataMiner.CICD.Models.Protocol;
using Skyline.DataMiner.CICD.Models.Protocol.Read;
using Skyline.DataMiner.CICD.Models.Protocol.Read.Interfaces;
using Skyline.DataMiner.CICD.Parsers.Common.Xml;
using Skyline.DataMiner.CICD.Validators.Common.Data;
using Skyline.DataMiner.CICD.Validators.Common.Interfaces;
using EditModel = Skyline.DataMiner.CICD.Models.Protocol.Edit;
using EditXml = Skyline.DataMiner.CICD.Parsers.Common.XmlEdit;

public static class ProtocolTestsHelper
{
public static (IProtocolModel model, XmlDocument document, string protocolCode) ReadProtocol(string fileName, [CallerFilePath] string pathToClassFile = "")
{
string filePath = Path.Combine(Path.GetDirectoryName(pathToClassFile), fileName);

string code;
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
using (var textReader = new StreamReader(fileStream))
{
code = textReader.ReadToEnd();
}

return ParseProtocol(code);
}

public static (IProtocolModel model, XmlDocument document, string protocolCode) ParseProtocol(string protocolCode)
{
Parser parser = new Parser(new StringBuilder(protocolCode));

return (new ProtocolModel(parser.Document), parser.Document, protocolCode);
}

public static QActionCompilationModel GetQActionCompilationModel(string xmlCode)
{
var document = new Parser(xmlCode).Document;
var model = new ProtocolModel(document);

return GetQActionCompilationModel(model, xmlCode);
}

public static QActionCompilationModel GetQActionCompilationModel(IProtocolModel model, string xmlCode)
{
var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
baseDir = Path.GetFullPath(Path.Combine(baseDir, @"..\..\..\..\DLLs"));

var dllImportResolver = new InternalFilesAssemblyResolver(baseDir);
var qactionHelperProvider = new ProtocolQActionHelperProvider();

string qactionHelperSourceCode = qactionHelperProvider.GetProtocolQActionHelper(xmlCode, ignoreErrors: true);
return new QActionCompilationModel(qactionHelperSourceCode, model, dllImportResolver);
}

public static IProtocolInputData GetProtocolInputData(string fileName, [CallerFilePath] string pathToClassFile = "")
{
(IProtocolModel model, XmlDocument document, string protocolCode) = ReadProtocol(fileName, pathToClassFile);

var qactionCompilationModel = GetQActionCompilationModel(model, protocolCode);

return new ProtocolInputData(model, document, qactionCompilationModel);
}

public static EditModel.Protocol GetEditProtocol(IProtocolModel model)
{
return new EditModel.Protocol(model.Protocol);
}

public static EditModel.Protocol GetEditProtocol(IProtocolModel model, EditXml.XmlElement xmlElement)
{
return new EditModel.Protocol(model.Protocol, xmlElement);
}
}
}
3 changes: 2 additions & 1 deletion Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Skyline.DataMiner.CICD.Models.Protocol" Version="1.0.7" />
<PackageReference Include="Skyline.DataMiner.CICD.Models.Protocol" Version="1.0.8-a" />
<PackageReference Include="Skyline.DataMiner.XmlSchemas.Protocol" Version="1.0.1-a" />
</ItemGroup>

Expand All @@ -35,6 +35,7 @@

<ItemGroup>
<InternalsVisibleTo Include="CommonTests" />
<InternalsVisibleTo Include="ProtocolTests" />
<InternalsVisibleTo Include="Skyline.DataMiner.CICD.Validators.Protocol" />
</ItemGroup>

Expand Down
16 changes: 15 additions & 1 deletion Common/Model/ValidatorSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Skyline.DataMiner.CICD.Validators.Common.Model
{
using System.Collections.Generic;
using System;
using System.Collections.Generic;

using Skyline.DataMiner.CICD.Common;
using Skyline.DataMiner.XmlSchemas.Protocol;
Expand All @@ -12,12 +13,25 @@ public class ValidatorSettings
{
private readonly List<(Category catergory, uint checkId)> testsToExecute;

/// <summary>
/// Initializes a new instance of the <see cref="ValidatorSettings"/> class.
/// Used for unit tests
/// </summary>
internal ValidatorSettings()
{
testsToExecute = new List<(Category catergory, uint checkId)>();
UnitList = new UnitList();
MinimumSupportedDataMinerVersion = new DataMinerVersion(new Version(10, 1, 0, 0), 9966);
}

/// <summary>
/// Initializes a new instance of the <see cref="ValidatorSettings"/> class.
/// </summary>
public ValidatorSettings(DataMinerVersion minimumSupportedDataMinerVersion)
{
testsToExecute = new List<(Category catergory, uint checkId)>();

// TODO-MOD: Probably don't initialize it here just in case an error happens? Maybe only on the get of the property if the field is null still?
UnitList = new UnitList();

MinimumSupportedDataMinerVersion = minimumSupportedDataMinerVersion;
Expand Down
2 changes: 1 addition & 1 deletion CommonTests/CommonTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>

Expand Down
Binary file added DLLs/Interop.SLDms.dll
Binary file not shown.
Binary file added DLLs/Newtonsoft.Json.dll
Binary file not shown.
Binary file added DLLs/QActionHelper.dll
Binary file not shown.
Binary file added DLLs/QActionHelperBaseClasses.dll
Binary file not shown.
Binary file added DLLs/SLLoggerUtil.dll
Binary file not shown.
Binary file added DLLs/SLManagedAutomation.dll
Binary file not shown.
Binary file added DLLs/SLManagedScripting.dll
Binary file not shown.
Binary file added DLLs/SLNetTypes.dll
Binary file not shown.
Binary file added DLLs/Skyline.DataMiner.Storage.Types.dll
Binary file not shown.
4 changes: 4 additions & 0 deletions DLLs/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This folder should contain files from latest main release:
S:\Public\DataMiner Software\DataMiner Files\Release\

These DLLs are only to be used for unit tests. They should never be included in the actual NuGet packages.
4 changes: 4 additions & 0 deletions Protocol.Features/Protocol.Features.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Protocol.FeaturesTests" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions Protocol.FeaturesTests/Features/10.0/DashboardTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace SLDisDmFeatureCheckUnitTests.Features
{
using System.Linq;

using FluentAssertions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Skyline.DataMiner.CICD.Validators.Common.Data;

using SLDisDmFeatureCheck.Common;
using SLDisDmFeatureCheck.Common.Results;
using SLDisDmFeatureCheck.Features;

[TestClass]
public class DashboardTests
{
private static Dashboard check;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
check = new Dashboard();
}

[TestMethod]
public void CheckIsUsed()
{
const string code = "<Protocol><Params><Param><Dashboard></Dashboard></Param></Params></Protocol>";
var input = new ProtocolInputData(code);

FeatureCheckContext context = new FeatureCheckContext(input);

var result = check.CheckIfUsed(context);
var expected = context.Model.Protocol.Params.Select(x => new FeatureCheckResultItem(x));

Assert.IsTrue(result.IsUsed);
result.FeatureItems.Should().BeEquivalentTo(expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace SLDisDmFeatureCheckUnitTests.Features
{
using System.Linq;

using FluentAssertions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Skyline.DataMiner.CICD.Validators.Common.Data;

using SLDisDmFeatureCheck.Common;
using SLDisDmFeatureCheck.Common.Results;
using SLDisDmFeatureCheck.Features;

[TestClass]
public class DynamicParameterReplicationTests
{
private static DynamicParameterReplication check;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
check = new DynamicParameterReplication();
}

[TestMethod]
public void CheckIsUsed()
{
const string code = "<Protocol><Params><Param><Replication><Element dynamic='1'/></Replication></Param></Params></Protocol>";
var input = new ProtocolInputData(code);

FeatureCheckContext context = new FeatureCheckContext(input);

var result = check.CheckIfUsed(context);
var expected = context.Model.Protocol.Params.Select(x => new FeatureCheckResultItem(x));

Assert.IsTrue(result.IsUsed);
result.FeatureItems.Should().BeEquivalentTo(expected);
}
}
}
41 changes: 41 additions & 0 deletions Protocol.FeaturesTests/Features/10.0/ExposerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace SLDisDmFeatureCheckUnitTests.Features
{
using System.Linq;

using FluentAssertions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Skyline.DataMiner.CICD.Validators.Common.Data;

using SLDisDmFeatureCheck.Common;
using SLDisDmFeatureCheck.Common.Results;
using SLDisDmFeatureCheck.Features;

[TestClass]
public class ExposerTests
{
private static Exposer check;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
check = new Exposer();
}

[TestMethod]
public void CheckIsUsed()
{
const string code = "<Protocol><Topologies><Topology><Cell><Exposer /></Cell></Topology></Topologies></Protocol>";

var input = new ProtocolInputData(code);
var context = new FeatureCheckContext(input);

var result = check.CheckIfUsed(context);
var expected = context.Model.Protocol.Topologies.Select(x => new FeatureCheckResultItem(x));

Assert.IsTrue(result.IsUsed);
result.FeatureItems.Should().BeEquivalentTo(expected);
}
}
}
Loading

0 comments on commit 85f8286

Please sign in to comment.