|
| 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 | +} |
0 commit comments