Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

Commit a58d29b

Browse files
committed
feat: adds unit tests for LightWorker
1 parent 6517313 commit a58d29b

File tree

4 files changed

+244
-6
lines changed

4 files changed

+244
-6
lines changed

Liquid.All.sln

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.Domain.Tests", "test
4949
EndProject
5050
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.Tests", "test\Liquid.Tests\Liquid.Tests.csproj", "{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}"
5151
EndProject
52-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.OnAzure.Tests", "test\Liquid.OnAzure.Tests\Liquid.OnAzure.Tests.csproj", "{9DEA4109-7542-40D5-9CCF-A673C9787E6D}"
52+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.Activation.Tests", "test\Liquid.Activation.Tests\Liquid.Activation.Tests.csproj", "{E5FCB486-0C76-4714-8AC3-3E468FC1DB61}"
53+
EndProject
54+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.OnAzure.Tests", "test\Liquid.OnAzure.Tests\Liquid.OnAzure.Tests.csproj", "{CE80BBAE-350E-444B-A4FD-D9B58C5214E2}"
5355
EndProject
5456
Global
5557
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -113,10 +115,14 @@ Global
113115
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
114116
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
115117
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}.Release|Any CPU.Build.0 = Release|Any CPU
116-
{9DEA4109-7542-40D5-9CCF-A673C9787E6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
117-
{9DEA4109-7542-40D5-9CCF-A673C9787E6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
118-
{9DEA4109-7542-40D5-9CCF-A673C9787E6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
119-
{9DEA4109-7542-40D5-9CCF-A673C9787E6D}.Release|Any CPU.Build.0 = Release|Any CPU
118+
{E5FCB486-0C76-4714-8AC3-3E468FC1DB61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119+
{E5FCB486-0C76-4714-8AC3-3E468FC1DB61}.Debug|Any CPU.Build.0 = Debug|Any CPU
120+
{E5FCB486-0C76-4714-8AC3-3E468FC1DB61}.Release|Any CPU.ActiveCfg = Release|Any CPU
121+
{E5FCB486-0C76-4714-8AC3-3E468FC1DB61}.Release|Any CPU.Build.0 = Release|Any CPU
122+
{CE80BBAE-350E-444B-A4FD-D9B58C5214E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123+
{CE80BBAE-350E-444B-A4FD-D9B58C5214E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
124+
{CE80BBAE-350E-444B-A4FD-D9B58C5214E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
125+
{CE80BBAE-350E-444B-A4FD-D9B58C5214E2}.Release|Any CPU.Build.0 = Release|Any CPU
120126
EndGlobalSection
121127
GlobalSection(SolutionProperties) = preSolution
122128
HideSolutionNode = FALSE
@@ -136,7 +142,8 @@ Global
136142
{5279F425-0A2C-4889-968D-FFEB25975453} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
137143
{90CF0830-CD3C-4DD7-8C90-584772885BE0} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
138144
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
139-
{9DEA4109-7542-40D5-9CCF-A673C9787E6D} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
145+
{E5FCB486-0C76-4714-8AC3-3E468FC1DB61} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
146+
{CE80BBAE-350E-444B-A4FD-D9B58C5214E2} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
140147
EndGlobalSection
141148
GlobalSection(ExtensibilityGlobals) = postSolution
142149
SolutionGuid = {1E0953B7-A30F-4440-9719-22CA017677B5}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright (c) Avanade Inc. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Liquid.Base;
9+
using Liquid.Interfaces;
10+
using Newtonsoft.Json;
11+
using NSubstitute;
12+
using Xunit;
13+
14+
namespace Liquid.Activation.Tests
15+
{
16+
public class LightWorkerTests
17+
{
18+
public LightWorkerTests()
19+
{
20+
Workbench.Instance.Reset();
21+
Workbench.Instance.AddToCache(WorkbenchServiceType.Telemetry, Substitute.For<ILightTelemetry>());
22+
}
23+
24+
[Fact]
25+
public void InitializeWhenMockLightWorkerPresentThenQueueAndTopicsAreDiscovered()
26+
{
27+
var sut = new MockLightWorker();
28+
sut.Initialize();
29+
30+
Assert.Contains(
31+
MockLightWorker.TopicList,
32+
_ => _.MethodInfo.ReflectedType == typeof(MockLightWorker)
33+
&& _.MethodInfo.Name == nameof(MockLightWorker.TopicMethod));
34+
35+
Assert.Contains(
36+
MockLightWorker.QueueList,
37+
_ => _.MethodInfo.ReflectedType == typeof(MockLightWorker)
38+
&& _.MethodInfo.Name == nameof(MockLightWorker.QueueMethod));
39+
40+
// Given the static nature of LightWorker, we couldn't make this an isolated assertion
41+
// TODO: Refactor LightWorker and then make this isolated
42+
Assert.Throws<LightException>(() => new MockLightWorker().Initialize());
43+
}
44+
45+
[Fact]
46+
public void InvokeProcessWhenMessageIsntValidJsonThrows()
47+
{
48+
var actual = LightWorker.InvokeProcess(null, null);
49+
Assert.Null(actual);
50+
}
51+
52+
[Fact]
53+
public void InvokeProcessWhenMessageIsValidJsonParsesItCorrectly()
54+
{
55+
// ARRANGE
56+
var anonymous = new Foobar { Foo = "Bar" };
57+
var anonymousAsByteStream = ToJsonByteStream(anonymous);
58+
59+
var method = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.FoobarMethod));
60+
61+
// ACT
62+
var actual = (Foobar)LightWorker.InvokeProcess(method, anonymousAsByteStream);
63+
64+
// ASSERT
65+
Assert.Equal(anonymous.Foo, actual.Foo);
66+
}
67+
68+
[Fact]
69+
public void InvokeProcessWhenMethodHasZeroParametersDoesntParseMessage()
70+
{
71+
// ARRANGE
72+
var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.ConstantMethod));
73+
74+
// ACT
75+
var actual = (string)LightWorker.InvokeProcess(mi, null);
76+
77+
// ASSERT
78+
Assert.Equal(MethodsCollection.Value, actual);
79+
}
80+
81+
[Fact]
82+
public void InvokeProcessWhenMethodThrowsAsyncThrows()
83+
{
84+
// ARRANGE
85+
var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.ThrowsAsync));
86+
87+
var anonymous = new Foobar { Foo = "Bar" };
88+
var anonymousAsByteStream = ToJsonByteStream(anonymous);
89+
90+
// ACT & ASSERT
91+
Assert.ThrowsAsync<MethodsCollection.TestException>(() => (Task)LightWorker.InvokeProcess(mi, anonymousAsByteStream));
92+
}
93+
94+
/// <summary>
95+
/// Serialize any object to a JSON string and then convert it to a bytestream.
96+
/// </summary>
97+
/// <param name="obj">The object to serialize.</param>
98+
/// <returns>A byestream containing the object as UTF8 bytes.</returns>
99+
private byte[] ToJsonByteStream(object obj)
100+
{
101+
var anonymousAsString = JsonConvert.SerializeObject(obj);
102+
var anonymousAsByteStream = Encoding.UTF8.GetBytes(anonymousAsString);
103+
104+
return anonymousAsByteStream;
105+
}
106+
107+
[SuppressMessage(
108+
"Design",
109+
"CA1034:Nested types should not be visible",
110+
Justification = "Must be public so LightWorker access the class")]
111+
public class Foobar
112+
{
113+
public string Foo { get; set; } = "Bar";
114+
}
115+
116+
private class MethodsCollection
117+
{
118+
public const string Value = "string";
119+
120+
public string ConstantMethod()
121+
{
122+
return Value;
123+
}
124+
125+
public Foobar FoobarMethod(Foobar foobar)
126+
{
127+
return foobar;
128+
}
129+
130+
public Task ThrowsAsync(Foobar foobar)
131+
{
132+
return Task.FromException(new TestException(string.Empty));
133+
}
134+
135+
// Used to test throwing from a method
136+
public class TestException : Exception
137+
{
138+
public TestException(string message)
139+
: base(message)
140+
{
141+
}
142+
143+
public TestException(string message, Exception innerException)
144+
: base(message, innerException)
145+
{
146+
}
147+
148+
public TestException()
149+
{
150+
}
151+
}
152+
}
153+
}
154+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
8+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<Compile Remove="LightWorkerTests\**" />
13+
<EmbeddedResource Remove="LightWorkerTests\**" />
14+
<None Remove="LightWorkerTests\**" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<AdditionalFiles Include="..\..\stylecop.json" Link="stylecop.json" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Include="AutoFixture" Version="4.11.0" />
23+
<PackageReference Include="AutoFixture.Xunit2" Version="4.11.0" />
24+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.6">
25+
<PrivateAssets>all</PrivateAssets>
26+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
27+
</PackageReference>
28+
<PackageReference Include="NSubstitute" Version="4.2.1" />
29+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.113">
30+
<PrivateAssets>all</PrivateAssets>
31+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
32+
</PackageReference>
33+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
34+
<PackageReference Include="xunit" Version="2.4.0" />
35+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
36+
<PackageReference Include="coverlet.collector" Version="1.0.1" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<ProjectReference Include="..\..\src\Liquid.Activation\Liquid.Activation.csproj" />
41+
</ItemGroup>
42+
43+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Avanade Inc. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Linq;
8+
using System.Reflection;
9+
using Xunit;
10+
11+
namespace Liquid.Activation.Tests
12+
{
13+
[MessageBus("asd")]
14+
public class MockLightWorker : LightWorker
15+
{
16+
public static List<(MethodInfo MethodInfo, TopicAttribute TopicAttribute)> TopicList => _topics
17+
.Select(kvp => (kvp.Key, kvp.Value))
18+
.ToList();
19+
20+
public static List<(MethodInfo MethodInfo, QueueAttribute QueueAttribute)> QueueList => _queues
21+
.Select(kvp => (kvp.Key, kvp.Value))
22+
.ToList();
23+
24+
[Topic("name", "subscriptionName", 10, true)]
25+
public static void TopicMethod()
26+
{
27+
}
28+
29+
[Queue("name")]
30+
public static void QueueMethod()
31+
{
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)