Skip to content

Commit

Permalink
ImportObjectTest - fix UT error #681
Browse files Browse the repository at this point in the history
fixes #681
  • Loading branch information
mpostol committed Sep 8, 2022
1 parent c8f103f commit 4213bfd
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,9 @@ public class AddressSpaceContextUnitTest
public void AddressSpaceContextConstructorTest()
{
List<IUANodeBase> _invalidNodes = new List<IUANodeBase>();
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext();
_asp.UTAddressSpaceCheckConsistency(x => _invalidNodes.Add(x));
_asp.TestConsistency(0);
}

[TestMethod]
[TestCategory("AddressSpaceContext")]
public void ReferencesCheckConsistencyTest()
{
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
_asp.UTReferencesCheckConsistency((x, y, z, v) => Assert.Fail());
_asp.TestConsistency(0);
}
Expand All @@ -47,7 +40,7 @@ public void ReferencesCheckConsistencyTest()
[TestCategory("AddressSpaceContext")]
public void AddressSpaceContextContentCheck()
{
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext();
List<IUANodeContext> _content = new List<IUANodeContext>();
_asp.UTTryGetUANodeContext(VariableTypes.PropertyType, x => _content.Add(x));
Assert.AreEqual<int>(1, _content.Count);
Expand All @@ -70,8 +63,9 @@ public void AddressSpaceContextContentCheck()
[TestCategory("AddressSpaceContext")]
public void AddressSpaceReferencesContentCheck()
{
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext();
List<UAReferenceContext> _content = new List<UAReferenceContext>();
_content.Clear();
_asp.UTGetReferences(ObjectIds.RootFolder, x => _content.Add(x));
Assert.AreEqual<int>(4, _content.Count);
//RootFolder
Expand Down Expand Up @@ -125,12 +119,11 @@ public void ImportUANodeSetTest()
}
}
};
TracedAddressSpaceContext tracedAddressSpace = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext tracedAddressSpace = new TracedAddressSpaceContext(newNodeSet);
List<UAReferenceContext> references = new List<UAReferenceContext>();
tracedAddressSpace.UTGetReferences(NodeId.Parse(newNodeSet.Items[0].NodeId), x => references.Add(x));
Assert.AreEqual<int>(1, references.Count);
Assert.AreEqual<ReferenceKindEnum>(ReferenceKindEnum.HasProperty, references[0].ReferenceKind);
Assert.AreEqual<ReferenceKindEnum>(ReferenceKindEnum.HasProperty, references[0].ReferenceKind);
references.Clear();
tracedAddressSpace.UTGetReferences(NodeId.Parse(newNodeSet.Items[1].NodeId), x => references.Add(x));
Assert.AreEqual<int>(2, references.Count);
Expand All @@ -142,7 +135,7 @@ public void ImportUANodeSetTest()
[TestCategory("AddressSpaceContext")]
public void AddressSpaceContextValidateAndExportModelOpcUa()
{
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext();
_asp.ValidateAndExportModel(new Uri(UAInformationModel.Namespaces.OpcUa));
_asp.TestConsistency(0);
}
Expand All @@ -152,15 +145,15 @@ public void AddressSpaceContextValidateAndExportModelOpcUa()
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void AddressSpaceContextValidateAndExportModelWrongNamespace()
{
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext();
_asp.ValidateAndExportModel(new Uri("http://www.example.com/afterthought/box"));
}

[TestMethod]
[TestCategory("AddressSpaceContext")]
public void AddressSpaceContextValidateAndExportIndex0()
{
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext();
IEnumerable<IUANodeContext> _returnValue = null;
_asp.UTValidateAndExportModel(0, x => _returnValue = x);
Assert.AreEqual<int>(4071, (_returnValue.Count<IUANodeContext>()));
Expand All @@ -174,9 +167,8 @@ public void AddressSpaceContextValidateAndExportIndex0()
[TestCategory("AddressSpaceContext")]
public void ImportObjectTest()
{
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(null);
UANodeSet _newNodeSet = TestData.CreateNodeSetModel();
_asp.TestConsistency(1);
TracedAddressSpaceContext _asp = new TracedAddressSpaceContext(_newNodeSet);
_asp.UTAddressSpaceCheckConsistency(x => { Assert.Fail(); });
_asp.TestConsistency(1);
List<UAReferenceContext> _content = new List<UAReferenceContext>();
Expand Down Expand Up @@ -220,8 +212,7 @@ public void GetMyReferencesTest()
}
}
};
TracedAddressSpaceContext asp = new TracedAddressSpaceContext(null);
((IAddressSpaceContext)asp.AddressSpace).ImportUANodeSet(newNodeSet);
TracedAddressSpaceContext asp = new TracedAddressSpaceContext(newNodeSet);
IUANodeContext uaObjectType = asp.GetOrCreateNodeContext(NodeId.Parse(newNodeSet.Items[0].NodeId), x => { Assert.Fail(); return null; });
Assert.IsNotNull(uaObjectType);
IEnumerable<UAReferenceContext> myReferences = ((IAddressSpaceBuildContext)asp.AddressSpace).GetMyReferences(uaObjectType);
Expand All @@ -236,7 +227,7 @@ public void GetMyReferencesTest()
[TestMethod]
public void GetBaseTypesTest()
{
TracedAddressSpaceContext tasp = new TracedAddressSpaceContext(null);
TracedAddressSpaceContext tasp = new TracedAddressSpaceContext();
List<IUANodeContext> inheritanceChain = new List<IUANodeContext>();
IUANodeContext hasPropertyNode = tasp.GetOrCreateNodeContext(ReferenceTypeIds.HasProperty, x => { Assert.Fail(); return null; });
tasp.GetBaseTypes(hasPropertyNode, inheritanceChain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UAOOI.SemanticData.AddressSpace.Abstractions;
using UAOOI.SemanticData.BuildingErrorsHandling;
using UAOOI.SemanticData.InformationModelFactory;
Expand All @@ -24,30 +25,89 @@ namespace UAOOI.SemanticData.UANodeSetValidation.Helpers
/// Implements the <seealso cref="IBuildErrorsHandling"/> interface
/// </summary>
///
internal class TracedAddressSpaceContext : IBuildErrorsHandling
internal class TracedAddressSpaceContext
{
/// <summary>
/// Setup new instance
/// - creates <see cref="IAddressSpaceContext"/>
/// - reads standard model
/// - reads custom model if not null
/// - reads custom model
/// </summary>
/// <param name="testDataFileInfo">Custom model for testing purpose</param>
internal TracedAddressSpaceContext(FileInfo testDataFileInfo)
/// <param name="iUANodeSet">Custom model for testing purpose</param>
internal TracedAddressSpaceContext(IUANodeSet iUANodeSet) : this()
{
AddressSpace = AddressSpaceFactory.AddressSpace(this);
IUANodeSet iUANodeSet = UANodeSet.ReadUADefinedTypes();
Assert.IsNotNull(iUANodeSet);
AddressSpace.ImportUANodeSet(iUANodeSet);
if (testDataFileInfo != null)
}

/// <summary>
/// Setup new instance
/// - creates <see cref="IAddressSpaceContext"/>
/// - reads standard model
/// - reads custom model
/// </summary>
/// <param name="path">Custom model for testing purpose</param>
internal TracedAddressSpaceContext(string path) : this()
{
FileInfo testDataFileInfo = new FileInfo(path);
Assert.IsTrue(testDataFileInfo.Exists);
IUANodeSet iUANodeSet = UANodeSet.ReadModelFile(testDataFileInfo);
AddressSpace.ImportUANodeSet(iUANodeSet);
}

/// <summary>
/// Setup new instance
/// - creates <see cref="IAddressSpaceContext"/>
/// - reads standard model
/// </summary>
internal TracedAddressSpaceContext()
{
Log.Clear();
AddressSpace = addressSpaceContext.Value;
}

private class BuildErrorsHandling : IBuildErrorsHandling
{
internal void Clear()
{
Assert.IsTrue(testDataFileInfo.Exists);
iUANodeSet = UANodeSet.ReadModelFile(testDataFileInfo);
AddressSpace.ImportUANodeSet(iUANodeSet);
Errors = 0;
TraceList.Clear();
}

internal List<TraceMessage> TraceList = new List<TraceMessage>();

#region IBuildErrorsHandling

public int Errors { get; set; } = 0;

public void TraceData(TraceEventType eventType, int id, object data)
{
throw new NotImplementedException($"{nameof(TraceData)} must not be used");
}

public void WriteTraceMessage(TraceMessage traceMessage)
{
Console.WriteLine(traceMessage.ToString());
if (traceMessage.BuildError.Focus == Focus.Diagnostic)
return;
Errors++;
TraceList.Add(traceMessage);
}

#endregion IBuildErrorsHandling
}

private static BuildErrorsHandling Log = new BuildErrorsHandling();

private static Lazy<IAddressSpaceContext> addressSpaceContext = new Lazy<IAddressSpaceContext>(() =>
{
var x = AddressSpaceFactory.AddressSpace(Log);
x.ImportUANodeSet(UANodeSet.ReadUADefinedTypes());
return x;
});

internal TraceMessage this[int i] => Log.TraceList[i];
internal IAddressSpaceContext AddressSpace = null;
internal readonly List<TraceMessage> TraceList = new List<TraceMessage>();

internal void UTAddressSpaceCheckConsistency(Action<IUANodeContext> action)
{
Expand Down Expand Up @@ -89,35 +149,19 @@ internal void GetBaseTypes(IUANodeContext hasPropertyNode, List<IUANodeContext>
((AddressSpaceContext)AddressSpace).GetBaseTypes(hasPropertyNode, inheritanceChain);
}

internal void TestConsistency(int errorsCounter)
{
Assert.AreEqual<int>(errorsCounter, TraceList.Count);
}

internal void Clear()
internal int Where(Func<TraceMessage, bool> predicate)
{
Errors = 0;
TraceList.Clear();
return Log.TraceList.Where<TraceMessage>(predicate).Count<TraceMessage>();
}

#region IBuildErrorsHandling

public int Errors { get; set; } = 0;

public void TraceData(TraceEventType eventType, int id, object data)
internal void TestConsistency(int errorsCounter)
{
throw new NotImplementedException($"{nameof(TraceData)} must not be used");
Assert.AreEqual<int>(errorsCounter, Log.TraceList.Count);
}

public void WriteTraceMessage(TraceMessage traceMessage)
internal void Clear()
{
Console.WriteLine(traceMessage.ToString());
if (traceMessage.BuildError.Focus == Focus.Diagnostic)
return;
Errors++;
TraceList.Add(traceMessage);
Log.Clear();
}

#endregion IBuildErrorsHandling
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public void WrongFileNFormatTestMethod()
[TestCategory("Correct Model")]
public void UAReferenceTestMethod()
{
FileInfo _testDataFileInfo = new FileInfo(@"CorrectModels\ReferenceTest\ReferenceTest.NodeSet2.xml");
Assert.IsTrue(_testDataFileInfo.Exists);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(_testDataFileInfo, 1, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/ReferenceTest").Uri);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(@"CorrectModels\ReferenceTest\ReferenceTest.NodeSet2.xml", 1, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/ReferenceTest").Uri);
Assert.IsFalse(_nodes.Where<IUANodeContext>(x => x.UANode == null).Any<IUANodeContext>());
Assert.AreEqual<int>(1, _nodes.Where<IUANodeContext>(x => x.UANode is UAType).Count<IUANodeContext>());
}
Expand All @@ -55,9 +53,7 @@ public void UAObjectTypeTestMethod()
{
//TODO NetworkIdentifier is missing in generated Model Design for DI model #629
//TODO The exported model doesn't contain all nodes #653
FileInfo _testDataFileInfo = new FileInfo(@"CorrectModels\ObjectTypeTest\ObjectTypeTest.NodeSet2.xml");
Assert.IsTrue(_testDataFileInfo.Exists);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(_testDataFileInfo, 85, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/ObjectTypeTest").Uri);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(@"CorrectModels\ObjectTypeTest\ObjectTypeTest.NodeSet2.xml", 85, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/ObjectTypeTest").Uri);
Assert.IsFalse(_nodes.Where<IUANodeContext>(x => x.UANode == null).Any<IUANodeContext>());
Assert.AreEqual<int>(3, _nodes.Where<IUANodeContext>(x => x.UANode is UAType).Count<IUANodeContext>());
Assert.AreEqual<int>(1, _nodes.Where<IUANodeContext>(x => x.NodeIdContext == new DataSerialization.NodeId(413, 1)).Count<IUANodeContext>());
Expand All @@ -67,9 +63,7 @@ public void UAObjectTypeTestMethod()
[TestCategory("Correct Model")]
public void UAVariableTypeTestMethod()
{
FileInfo _testDataFileInfo = new FileInfo(@"CorrectModels\VariableTypeTest\VariableTypeTest.NodeSet2.xml");
Assert.IsTrue(_testDataFileInfo.Exists);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(_testDataFileInfo, 5, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/VariableTypeTest").Uri);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(@"CorrectModels\VariableTypeTest\VariableTypeTest.NodeSet2.xml", 5, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/VariableTypeTest").Uri);
Assert.IsFalse(_nodes.Where<IUANodeContext>(x => x.UANode == null).Any<IUANodeContext>());
Assert.AreEqual<int>(3, _nodes.Where<IUANodeContext>(x => x.UANode is UAType).Count<IUANodeContext>());
}
Expand All @@ -78,9 +72,7 @@ public void UAVariableTypeTestMethod()
[TestCategory("Correct Model")]
public void UADataTypeTestMethod()
{
FileInfo _testDataFileInfo = new FileInfo(@"CorrectModels\DataTypeTest\DataTypeTest.NodeSet2.xml");
Assert.IsTrue(_testDataFileInfo.Exists);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(_testDataFileInfo, 22, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/DataTypeTest").Uri);
List<IUANodeContext> _nodes = ValidateAndExportModelUnitTest(@"CorrectModels\DataTypeTest\DataTypeTest.NodeSet2.xml", 22, new UriBuilder("http://cas.eu/UA/CommServer/UnitTests/DataTypeTest").Uri);
Assert.IsFalse(_nodes.Where<IUANodeContext>(x => x.UANode == null).Any<IUANodeContext>());
Assert.AreEqual<int>(4, _nodes.Where<IUANodeContext>(x => x.UANode is UAType).Count<IUANodeContext>());
}
Expand All @@ -89,18 +81,18 @@ public void UADataTypeTestMethod()

#region private

private List<IUANodeContext> ValidateAndExportModelUnitTest(FileInfo testDataFileInfo, int numberOfNodes, Uri model)
private List<IUANodeContext> ValidateAndExportModelUnitTest(string testDataFileInfo, int numberOfNodes, Uri model)
{
TracedAddressSpaceContext tracedAddressSpaceContext = new TracedAddressSpaceContext(testDataFileInfo);
tracedAddressSpaceContext.Clear();
Assert.AreEqual<int>(0, tracedAddressSpaceContext.TraceList.Count);
tracedAddressSpaceContext.TestConsistency(0);
tracedAddressSpaceContext.UTAddressSpaceCheckConsistency(x => { Assert.Fail(); });
tracedAddressSpaceContext.UTReferencesCheckConsistency((x, y, z, v) => Assert.Fail());
IEnumerable<IUANodeContext> _nodes = null;
tracedAddressSpaceContext.UTValidateAndExportModel(1, x => _nodes = x);
Assert.AreEqual<int>(numberOfNodes, _nodes.Count<IUANodeContext>());
tracedAddressSpaceContext.ValidateAndExportModel(model);
Assert.AreEqual<int>(0, tracedAddressSpaceContext.TraceList.Count);
tracedAddressSpaceContext.TestConsistency(0);
return _nodes.ToList<IUANodeContext>();
}

Expand Down
Loading

0 comments on commit 4213bfd

Please sign in to comment.