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