Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
beeond-dev committed Feb 9, 2020
2 parents 12ff1c9 + 3fb2989 commit d5e4d7b
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 2 deletions.
187 changes: 187 additions & 0 deletions UaClient.UnitTests/UnitTests/ServiceExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Workstation.ServiceModel.Ua;
using Xunit;

namespace Workstation.UaClient.UnitTests
{
public class ServiceExtensionsTests
{
private static Task Never() => Task.Delay(-1);

private static async Task<T> Never<T>(T value)
{
await Never();
return value;
}

[Fact]
public void ToVariantArray()
{
var input = new object[] { 1, 2, 3, 70 };
var expected = new Variant[] { 1, 2, 3, 70 };

input.ToVariantArray()
.Should().BeEquivalentTo(expected);
}

[Fact]
public void ToVariantArrayEmpty()
{
var input = new object[] { };
var expected = new Variant[] { };

input.ToVariantArray()
.Should().BeEquivalentTo(expected);
}

[Fact]
public void ToVariantArrayNull()
{
var input = default(object[]);

input.Invoking(i => i.ToVariantArray())
.Should().Throw<ArgumentNullException>();
}

[Fact]
public void ToObjectArray()
{
var input = new Variant[] { 1, 2, 3, 70 };
var expected = new object[] { 1, 2, 3, 70 };

input.ToObjectArray()
.Should().BeEquivalentTo(expected);
}

[Fact]
public void ToObjectArrayEmpty()
{
var input = new Variant[] { };
var expected = new object[] { };

input.ToObjectArray()
.Should().BeEquivalentTo(expected);
}

[Fact]
public void ToObjectArrayNull()
{
var input = default(Variant[]);

input.Invoking(i => i.ToObjectArray())
.Should().Throw<ArgumentNullException>();
}

[Fact]
public void WithCancellationCompleted()
{
var task = Task.CompletedTask;

task.WithCancellation(default).IsCompleted
.Should().BeTrue();
}

[Fact]
public async Task WithCancellationFastTask()
{
using (var tcs = new CancellationTokenSource())
{
var fast = Task.Delay(1);
var task = fast.WithCancellation(tcs.Token);

await task.Invoking(t => t)
.Should().NotThrowAsync();
}
}

[Fact]
public async Task WithCancellation()
{
using (var tcs = new CancellationTokenSource())
{
var never = Never();
var task = never.WithCancellation(tcs.Token);
tcs.Cancel();

await task.Invoking(t => t)
.Should().ThrowAsync<OperationCanceledException>();
}
}

[Fact]
public void ValueWithCancellationCompleted()
{
var task = Task.FromResult(10);

task.WithCancellation(default).IsCompleted
.Should().BeTrue();
}

[Fact]
public async Task ValueWithCancellation()
{
using (var tcs = new CancellationTokenSource())
{
var never = Never(10);
var task = never.WithCancellation(tcs.Token);
tcs.Cancel();

await task.Invoking(t => t)
.Should().ThrowAsync<OperationCanceledException>();
}
}

[Fact]
public void WithTimeoutAfterCompleted()
{
var task = Task.CompletedTask;

task.WithTimeoutAfter(-1).IsCompleted
.Should().BeTrue();
}

[Fact]
public async Task WithTimeoutAfterFastTask()
{
var fast = Task.Delay(1);
var task = fast.WithTimeoutAfter(-1);

await task.Invoking(t => t)
.Should().NotThrowAsync();
}

[Fact]
public async Task WithTimeoutAfter()
{
var never = Never();
var task = never.WithTimeoutAfter(0);

await task.Invoking(t => t)
.Should().ThrowAsync<TimeoutException>();
}

[Fact]
public void ValueWithTimeoutAfterCompleted()
{
var task = Task.FromResult(10);

task.WithTimeoutAfter(-1).IsCompleted
.Should().BeTrue();
}

[Fact]
public async Task ValueWithTimeoutAfter()
{
var never = Never(10);
var task = never.WithTimeoutAfter(0);

await task.Invoking(t => t)
.Should().ThrowAsync<TimeoutException>();
}
}
}
133 changes: 133 additions & 0 deletions UaClient.UnitTests/UnitTests/ServiceSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,5 +730,138 @@ public async Task UnregisterNodesAsync()
channel.Request
.Should().BeSameAs(request);
}

/*
* ServiceExtensions
* Tests for: ServiceExtensions.css
*/
[Fact]
public async Task ConditionRefreshAsync()
{
var response = new CallResponse
{
Results = new CallMethodResult[]
{
new CallMethodResult
{
StatusCode = StatusCodes.BadAttributeIdInvalid
}
}
};
var channel = new TestRequestChannel(response);

var ret = await channel.ConditionRefreshAsync(subscriptionId: 100);

ret
.Should().Be((StatusCode)StatusCodes.BadAttributeIdInvalid);

channel.Request
.Should().BeEquivalentTo(new
{
MethodsToCall = new[]
{
new
{
ObjectId = NodeId.Parse(ObjectTypeIds.ConditionType),
MethodId = NodeId.Parse(MethodIds.ConditionType_ConditionRefresh),
InputArguments = new Variant[] { 100u }
}
}
});
}

[Fact]
public async Task AcknowledgeAsync()
{
var condition = new AcknowledgeableCondition();
var comment = new LocalizedText("Comment");
var response = new CallResponse
{
Results = new CallMethodResult[]
{
new CallMethodResult
{
StatusCode = StatusCodes.BadAttributeIdInvalid
}
}
};
var channel = new TestRequestChannel(response);

var ret = await channel.AcknowledgeAsync(condition, comment);

ret
.Should().Be((StatusCode)StatusCodes.BadAttributeIdInvalid);

channel.Request
.Should().BeEquivalentTo(new
{
MethodsToCall = new[]
{
new
{
ObjectId = condition.ConditionId,
MethodId = NodeId.Parse(MethodIds.AcknowledgeableConditionType_Acknowledge),
InputArguments = new Variant[] { condition.EventId, comment }
}
}
});
}

[Fact]
public void AcknowledgeAsyncNull()
{
var response = new CallResponse();
var channel = new TestRequestChannel(response);

channel.Invoking(c => c.AcknowledgeAsync(null, "Comment"))
.Should().Throw<ArgumentNullException>();
}

[Fact]
public async Task ConfirmAsync()
{
var condition = new AcknowledgeableCondition();
var comment = new LocalizedText("Comment");
var response = new CallResponse
{
Results = new CallMethodResult[]
{
new CallMethodResult
{
StatusCode = StatusCodes.BadAttributeIdInvalid
}
}
};
var channel = new TestRequestChannel(response);

var ret = await channel.ConfirmAsync(condition, comment);

ret
.Should().Be((StatusCode)StatusCodes.BadAttributeIdInvalid);

channel.Request
.Should().BeEquivalentTo(new
{
MethodsToCall = new[]
{
new
{
ObjectId = condition.ConditionId,
MethodId = NodeId.Parse(MethodIds.AcknowledgeableConditionType_Confirm),
InputArguments = new Variant[] { condition.EventId, comment }
}
}
});
}

[Fact]
public void ConfirmAsyncNull()
{
var response = new CallResponse();
var channel = new TestRequestChannel(response);

channel.Invoking(c => c.ConfirmAsync(null, "Comment"))
.Should().Throw<ArgumentNullException>();
}
}
}
2 changes: 1 addition & 1 deletion UaClient/ServiceModel/Ua/StatusCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Workstation.ServiceModel.Ua
{
public struct StatusCode
public readonly struct StatusCode
{
private const uint SeverityMask = 0xC0000000u;
private const uint SeverityGood = 0x00000000u;
Expand Down
2 changes: 1 addition & 1 deletion UaClient/ServiceModel/Ua/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public enum VariantType
DiagnosticInfo = 25,
}

public struct Variant
public readonly struct Variant
{
public static readonly Variant Null = default(Variant);

Expand Down

0 comments on commit d5e4d7b

Please sign in to comment.