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

Commit 0b8c6e7

Browse files
committed
feat: adds unit tests for LightWorker
1 parent f732504 commit 0b8c6e7

File tree

7 files changed

+238
-1
lines changed

7 files changed

+238
-1
lines changed

Liquid.All.sln

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.Base.Tests", "test\L
4747
EndProject
4848
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.Domain.Tests", "test\Liquid.Domain.Tests\Liquid.Domain.Tests.csproj", "{90CF0830-CD3C-4DD7-8C90-584772885BE0}"
4949
EndProject
50-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Liquid.Tests", "test\Liquid.Tests\Liquid.Tests.csproj", "{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}"
50+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Liquid.Tests", "test\Liquid.Tests\Liquid.Tests.csproj", "{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}"
51+
EndProject
52+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Liquid.Activation.Tests", "test\Liquid.Activation.Tests\Liquid.Activation.Tests.csproj", "{C0DF4C34-EF2E-4A2B-BF2D-3ED5CDCE3A3E}"
5153
EndProject
5254
Global
5355
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -111,6 +113,10 @@ Global
111113
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
112114
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
113115
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5}.Release|Any CPU.Build.0 = Release|Any CPU
116+
{C0DF4C34-EF2E-4A2B-BF2D-3ED5CDCE3A3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
117+
{C0DF4C34-EF2E-4A2B-BF2D-3ED5CDCE3A3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
118+
{C0DF4C34-EF2E-4A2B-BF2D-3ED5CDCE3A3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
119+
{C0DF4C34-EF2E-4A2B-BF2D-3ED5CDCE3A3E}.Release|Any CPU.Build.0 = Release|Any CPU
114120
EndGlobalSection
115121
GlobalSection(SolutionProperties) = preSolution
116122
HideSolutionNode = FALSE
@@ -130,6 +136,7 @@ Global
130136
{5279F425-0A2C-4889-968D-FFEB25975453} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
131137
{90CF0830-CD3C-4DD7-8C90-584772885BE0} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
132138
{6BA7DC4B-0F66-415A-AE74-5DEA5D3943D5} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
139+
{C0DF4C34-EF2E-4A2B-BF2D-3ED5CDCE3A3E} = {6A0B6B3D-15FE-4C0B-97A1-7897E31C0C4E}
133140
EndGlobalSection
134141
GlobalSection(ExtensibilityGlobals) = postSolution
135142
SolutionGuid = {1E0953B7-A30F-4440-9719-22CA017677B5}

src/Liquid.Activation/Liquid.Activation.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@
1515
<DebugType>full</DebugType>
1616
<DebugSymbols>true</DebugSymbols>
1717
</PropertyGroup>
18+
<ItemGroup>
19+
<AdditionalFiles Include="..\..\stylecop.json" Link="stylecop.json" />
20+
</ItemGroup>
1821
<ItemGroup>
1922
<Compile Remove="ServiceBus\**" />
2023
<EmbeddedResource Remove="ServiceBus\**" />
2124
<None Remove="ServiceBus\**" />
2225
</ItemGroup>
2326
<ItemGroup>
2427
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.3" />
28+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.113">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31+
</PackageReference>
2532
</ItemGroup>
2633
<ItemGroup>
2734
<ProjectReference Include="..\Liquid.Base\Liquid.Base.csproj" />

src/Liquid.Activation/Worker/LightWorker.cs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections;
1010
using System.Collections.Generic;
11+
using System.Dynamic;
1112
using System.Linq;
1213
using System.Reflection;
1314
using System.Text;
@@ -149,6 +150,7 @@ public static object InvokeProcess(MethodInfo method, byte[] message)
149150
//Check if it needs authorization, unless that there isn't AuthorizeAttribute
150151
foreach (AuthorizeAttribute authorize in method.GetCustomAttributes(typeof(AuthorizeAttribute), false))
151152
{
153+
var t = (ILightMessage)lightMessage;
152154
if ((lightMessage.Context == null) || ((lightMessage.Context != null) && (lightMessage.Context.User == null)))
153155
{
154156
//If there isn't Context, will be throw exception.

src/Liquid.Base/Workbench.cs

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.IO;
78
using Liquid.Base.Interfaces.Polly;
89
using Liquid.Interfaces;
@@ -14,6 +15,10 @@ namespace Liquid
1415
/// Provides a global way to configure a Liquid application.
1516
/// </summary>
1617
[Obsolete("Please use the correct spelled class, Liquid.Base.Workbench")]
18+
[SuppressMessage(
19+
"StyleCop.CSharp.MaintainabilityRules",
20+
"SA1402:File may only contain a single type",
21+
Justification = "Obsolete class will be removed.")]
1722
public static class WorkBench
1823
{
1924
public static ILightRepository Repository => Workbench.Instance.Repository;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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.Text;
6+
using Liquid.Base;
7+
using Liquid.Interfaces;
8+
using Microsoft.AspNetCore.Authorization;
9+
using Newtonsoft.Json;
10+
using NSubstitute;
11+
using Xunit;
12+
13+
namespace Liquid.Activation.Tests
14+
{
15+
[TestCaseOrderer("Liquid.Activation.Tests.DeclarationOrder", "Liquid.Activation.Tests")]
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 InvokeProcessWhenMethodHasAuthorizeAndMessageHasNoContextThrows()
83+
{
84+
// ARRANGE
85+
var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.AuthorizedMethod));
86+
var data = ToJsonByteStream(new Foobar());
87+
88+
// ACT
89+
var actual = (string)LightWorker.InvokeProcess(mi, data);
90+
91+
// ASSERT
92+
Assert.Equal(MethodsCollection.Value, actual);
93+
}
94+
95+
/// <summary>
96+
/// Serialize any object to a JSON string and then convert it to a bytestream.
97+
/// </summary>
98+
/// <param name="anonymous">The object to serialize.</param>
99+
/// <returns>A byestream containing the object as UTF8 bytes.</returns>
100+
private byte[] ToJsonByteStream(object anonymous)
101+
{
102+
var anonymousAsString = JsonConvert.SerializeObject(anonymous);
103+
var anonymousAsByteStream = Encoding.UTF8.GetBytes(anonymousAsString);
104+
105+
return anonymousAsByteStream;
106+
}
107+
108+
private class Foobar : ILightMessage
109+
{
110+
public string Foo { get; set; } = "Bar";
111+
112+
public string TokenJwt { get; set; } = "asd";
113+
114+
public ILightContext Context { get; set; } = new LightContext
115+
{
116+
User = new System.Security.Claims.ClaimsPrincipal(),
117+
};
118+
}
119+
120+
private class MethodsCollection
121+
{
122+
public const string Value = "string";
123+
124+
public string ConstantMethod()
125+
{
126+
return Value;
127+
}
128+
129+
public Foobar FoobarMethod(Foobar _)
130+
{
131+
return _;
132+
}
133+
134+
[Authorize]
135+
public string AuthorizedMethod(Foobar value)
136+
{
137+
return value.Foo;
138+
}
139+
}
140+
}
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
<AdditionalFiles Include="..\..\stylecop.json" Link="stylecop.json" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="AutoFixture" Version="4.11.0" />
17+
<PackageReference Include="AutoFixture.Xunit2" Version="4.11.0" />
18+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.6">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="NSubstitute" Version="4.2.1" />
23+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.113">
24+
<PrivateAssets>all</PrivateAssets>
25+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
26+
</PackageReference>
27+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
28+
<PackageReference Include="xunit" Version="2.4.0" />
29+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
30+
<PackageReference Include="coverlet.collector" Version="1.0.1" />
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<ProjectReference Include="..\..\src\Liquid.Activation\Liquid.Activation.csproj" />
35+
</ItemGroup>
36+
37+
<ItemGroup>
38+
<Folder Include="LightWorkerTests\" />
39+
</ItemGroup>
40+
41+
</Project>
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)