|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using System; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Csla.Blazor.WebAssembly.State; |
| 6 | +using Csla.State; |
| 7 | +using System.Net.Http; |
| 8 | +using Csla.Blazor.WebAssembly.Configuration; |
| 9 | +using Csla.Core; |
| 10 | +using Csla.Runtime; |
| 11 | +using System.Net; |
| 12 | +using Csla.Serialization; |
| 13 | +using System.Runtime.Serialization.Formatters.Binary; |
| 14 | +using Csla.Serialization.Mobile; |
| 15 | +using Microsoft.AspNetCore.Components.Authorization; |
| 16 | +using NSubstitute; |
| 17 | + |
| 18 | +namespace Csla.Test.State |
| 19 | +{ |
| 20 | + [TestClass] |
| 21 | + public class SessionManagerTests |
| 22 | + { |
| 23 | + private SessionManager _sessionManager; |
| 24 | + private SessionMessage _sessionValue; |
| 25 | + |
| 26 | + [TestInitialize] |
| 27 | + public void Initialize() |
| 28 | + { |
| 29 | + var mockServiceProvider = Substitute.For<IServiceProvider>(); |
| 30 | + |
| 31 | + // Mock AuthenticationStateProvider |
| 32 | + var mockAuthStateProvider = Substitute.For<AuthenticationStateProvider>(); |
| 33 | + |
| 34 | + // Mock IServiceProvider |
| 35 | + mockServiceProvider.GetService(typeof(AuthenticationStateProvider)).Returns(mockAuthStateProvider); |
| 36 | + |
| 37 | + _sessionValue = new SessionMessage |
| 38 | + { |
| 39 | + // Set properties here |
| 40 | + // For example: |
| 41 | + Principal = new Security.CslaClaimsPrincipal() { }, |
| 42 | + Session = [] |
| 43 | + }; |
| 44 | + |
| 45 | + // Mock ISerializationFormatter |
| 46 | + var mockFormatter = Substitute.For<ISerializationFormatter>(); |
| 47 | + mockFormatter.Serialize(Arg.Any<Stream>(), Arg.Any<object>()); |
| 48 | + mockFormatter.Deserialize(Arg.Any<Stream>()).Returns(_sessionValue); |
| 49 | + |
| 50 | + // Mock IServiceProvider |
| 51 | + mockServiceProvider.GetService(typeof(Csla.Serialization.Mobile.MobileFormatter)).Returns(mockFormatter); |
| 52 | + |
| 53 | + var mockActivator = Substitute.For<Csla.Server.IDataPortalActivator>(); |
| 54 | + mockActivator.CreateInstance(Arg.Is<Type>(t => t == typeof(Csla.Serialization.Mobile.MobileFormatter))).Returns(mockFormatter); |
| 55 | + mockActivator.InitializeInstance(Arg.Any<object>()); |
| 56 | + |
| 57 | + // Mock IServiceProvider |
| 58 | + mockServiceProvider.GetService(typeof(Csla.Server.IDataPortalActivator)).Returns(mockActivator); |
| 59 | + |
| 60 | + // Mock IContextManager |
| 61 | + var mockContextManager = Substitute.For<IContextManager>(); |
| 62 | + mockContextManager.IsValid.Returns(true); |
| 63 | + |
| 64 | + // Mock IContextManagerLocal |
| 65 | + var mockLocalContextManager = Substitute.For<IContextManagerLocal>(); |
| 66 | + |
| 67 | + // Mock IServiceProvider |
| 68 | + mockServiceProvider.GetService(typeof(IRuntimeInfo)).Returns(new RuntimeInfo()); |
| 69 | + |
| 70 | + // Mock IEnumerable<IContextManager> |
| 71 | + var mockContextManagerList = new List<IContextManager> { mockContextManager }; |
| 72 | + |
| 73 | + // Mock ApplicationContextAccessor |
| 74 | + var mockApplicationContextAccessor = Substitute.For<ApplicationContextAccessor>(mockContextManagerList, mockLocalContextManager, mockServiceProvider); |
| 75 | + |
| 76 | + var _applicationContext = new ApplicationContext(mockApplicationContextAccessor); |
| 77 | + |
| 78 | + _sessionManager = new SessionManager(_applicationContext, GetHttpClient(_sessionValue, _applicationContext), new BlazorWebAssemblyConfigurationOptions { SyncContextWithServer = true }); |
| 79 | + } |
| 80 | + |
| 81 | + public class TestHttpMessageHandler(SessionMessage session, ApplicationContext _applicationContext) : HttpMessageHandler |
| 82 | + { |
| 83 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 84 | + { |
| 85 | + cancellationToken.ThrowIfCancellationRequested(); |
| 86 | + var response = new HttpResponseMessage() |
| 87 | + { |
| 88 | + StatusCode = HttpStatusCode.OK, |
| 89 | + Content = new StringContent("{\"ResultStatus\":0, \"SessionData\":\"" + Convert.ToBase64String(GetSession(session, _applicationContext)) + "\"}"), |
| 90 | + }; |
| 91 | + return Task.FromResult(response); |
| 92 | + } |
| 93 | + } |
| 94 | + private static HttpClient GetHttpClient(SessionMessage session, ApplicationContext _applicationContext) |
| 95 | + { |
| 96 | + var handlerMock = new TestHttpMessageHandler(session,_applicationContext); |
| 97 | + // use real http client with mocked handler here |
| 98 | + var httpClient = new HttpClient(handlerMock) |
| 99 | + { |
| 100 | + BaseAddress = new Uri("http://test.com/"), |
| 101 | + }; |
| 102 | + return httpClient; |
| 103 | + } |
| 104 | + |
| 105 | + private static byte[] GetSession(SessionMessage session, ApplicationContext _applicationContext) |
| 106 | + { |
| 107 | + var info = new MobileFormatter(_applicationContext).SerializeToDTO(session); |
| 108 | + var ms = new MemoryStream(); |
| 109 | + new CslaBinaryWriter(_applicationContext).Write(ms, info); |
| 110 | + ms.Position = 0; |
| 111 | + var array = ms.ToArray(); |
| 112 | + return array; |
| 113 | + } |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public async Task RetrieveSession_WithTimeoutValue_ShouldNotThrowException() |
| 117 | + { |
| 118 | + var timeout = TimeSpan.FromHours(1); |
| 119 | + var session = await _sessionManager.RetrieveSession(timeout); |
| 120 | + Assert.AreEqual(session, _sessionValue.Session); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public async Task RetrieveSession_WithZeroTimeout_ShouldThrowTimeoutException() |
| 125 | + { |
| 126 | + var timeout = TimeSpan.Zero; |
| 127 | + await Assert.ThrowsExceptionAsync<TimeoutException>(() => _sessionManager.RetrieveSession(timeout)); |
| 128 | + } |
| 129 | + |
| 130 | + [TestMethod] |
| 131 | + public async Task RetrieveSession_WithValidCancellationToken_ShouldNotThrowException() |
| 132 | + { |
| 133 | + var cts = new CancellationTokenSource(); |
| 134 | + var session = await _sessionManager.RetrieveSession(cts.Token); |
| 135 | + Assert.AreEqual(session, _sessionValue.Session); |
| 136 | + } |
| 137 | + |
| 138 | + [TestMethod] |
| 139 | + public async Task RetrieveSession_WithCancelledCancellationToken_ShouldThrowTaskCanceledException() |
| 140 | + { |
| 141 | + var cts = new CancellationTokenSource(); |
| 142 | + cts.Cancel(); |
| 143 | + await Assert.ThrowsExceptionAsync<TaskCanceledException>(() => _sessionManager.RetrieveSession(cts.Token)); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public async Task SendSession_WithTimeoutValue_ShouldNotThrowException() |
| 148 | + { |
| 149 | + await _sessionManager.RetrieveSession(TimeSpan.FromHours(1)); |
| 150 | + |
| 151 | + var timeout = TimeSpan.FromHours(1); |
| 152 | + await _sessionManager.SendSession(timeout); |
| 153 | + Assert.IsTrue(true); |
| 154 | + } |
| 155 | + |
| 156 | + [TestMethod] |
| 157 | + public async Task SendSession_WithZeroTimeout_ShouldThrowTimeoutException() |
| 158 | + { |
| 159 | + await _sessionManager.RetrieveSession(TimeSpan.FromHours(1)); |
| 160 | + |
| 161 | + var timeout = TimeSpan.Zero; |
| 162 | + await Assert.ThrowsExceptionAsync<TimeoutException>(() => _sessionManager.SendSession(timeout)); |
| 163 | + } |
| 164 | + |
| 165 | + [TestMethod] |
| 166 | + public async Task SendSession_WithValidCancellationToken_ShouldNotThrowException() |
| 167 | + { |
| 168 | + await _sessionManager.RetrieveSession(TimeSpan.FromHours(1)); |
| 169 | + |
| 170 | + var cts = new CancellationTokenSource(); |
| 171 | + await _sessionManager.SendSession(cts.Token); |
| 172 | + Assert.IsTrue(true); |
| 173 | + } |
| 174 | + |
| 175 | + [TestMethod] |
| 176 | + public async Task SendSession_WithCancelledCancellationToken_ShouldThrowTaskCanceledException() |
| 177 | + { |
| 178 | + await _sessionManager.RetrieveSession(TimeSpan.FromHours(1)); |
| 179 | + |
| 180 | + var cts = new CancellationTokenSource(); |
| 181 | + cts.Cancel(); |
| 182 | + await Assert.ThrowsExceptionAsync<TaskCanceledException>(() => _sessionManager.SendSession(cts.Token)); |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + [TestMethod] |
| 187 | + public async Task RetrieveCachedSessionSession() |
| 188 | + { |
| 189 | + await _sessionManager.RetrieveSession(TimeSpan.FromHours(1)); |
| 190 | + |
| 191 | + var session = _sessionManager.GetCachedSession(); |
| 192 | + Assert.IsNotNull(session); |
| 193 | + } |
| 194 | + [TestMethod] |
| 195 | + public void RetrieveNullCachedSessionSession() |
| 196 | + { |
| 197 | + var session = _sessionManager.GetCachedSession(); |
| 198 | + Assert.IsNull(session); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments