Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove on deserialized #3955

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
760e17b
Updated ISessionManager and SessionManager with timeout and cancellat…
luizbicalhoagl May 1, 2024
d54e552
`Refactor SessionManager and StateManager for improved session handling`
luizbicalhoagl May 1, 2024
911eb96
Refactor SessionManagerTests and remove certain test methods
luizbicalhoagl May 1, 2024
2b35219
Refactor SessionManager and update related tests
luizbicalhoagl May 2, 2024
d648d77
Merge branch 'main' into main
luizfbicalho May 3, 2024
33a0c17
`Convert SaveState to async in StateManager.cs`
luizbicalhoagl May 3, 2024
8e4617b
Merge branch 'main' of https://github.com/luizfbicalho/csla
luizbicalhoagl May 3, 2024
74ffc4c
`Update packages, refactor code, add tests, and remove project`
luizbicalhoagl May 3, 2024
ce84444
Updated SessionManager and its tests for async operations and better …
luizbicalhoagl May 4, 2024
73e9986
Subject: Refactored test methods and updated session retrieval
luizbicalhoagl May 4, 2024
5f59d4a
Refactor variable names to follow C# naming convention
luizbicalhoagl May 4, 2024
d9201aa
Merge branch 'main' into main
rockfordlhotka May 6, 2024
b6c01b9
Switch from Moq to NSubstitute in Csla.Blazor.WebAssembly.Tests
luizbicalhoagl May 7, 2024
76c6b75
Merge branch 'main' of https://github.com/luizfbicalho/csla
luizbicalhoagl May 7, 2024
57c3c5b
Merge branch 'main' into main
luizfbicalho May 7, 2024
98bc9e4
Merge branch 'MarimerLLC:main' into main
luizfbicalho May 11, 2024
0eb640f
Merge branch 'MarimerLLC:main' into main
luizfbicalho May 16, 2024
2b5ed27
Merge branch 'MarimerLLC:main' into main
luizfbicalho May 18, 2024
72454d8
Merge branch 'main' of https://github.com/luizfbicalho/csla
luizbicalhoagl May 19, 2024
97ddc29
Refactored code for readability and modified deserialization process
luizbicalhoagl May 19, 2024
410d2ce
Merge branch 'MarimerLLC:main' into main
luizfbicalho May 21, 2024
df149e3
Merge branch 'MarimerLLC:main' into main
luizfbicalho May 21, 2024
4db5841
merge from main
luizbicalhoagl May 21, 2024
712a854
Merge branch 'MarimerLLC:main' into main
luizfbicalho May 24, 2024
a926603
Merge branch 'MarimerLLC:main' into main
luizfbicalho May 25, 2024
26873f2
merge from main
luizbicalhoagl May 25, 2024
8e4822a
Merge branch 'main' into feature/RemoveOnDeserialized
luizfbicalho May 26, 2024
c792c48
Merge branch 'main' into feature/RemoveOnDeserialized
rockfordlhotka Jun 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSubstitute" Version="5.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
Expand Down
83 changes: 34 additions & 49 deletions Source/Csla.Blazor.WebAssembly.Tests/SessionManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
using System.Threading.Tasks;
using Csla.Blazor.WebAssembly.State;
using Csla.State;
using Moq;
using System.Net.Http;
using Csla.Blazor.WebAssembly.Configuration;
using Csla.Core;
using Csla.Runtime;
using Moq.Protected;
using System.Net;
using Csla.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Csla.Serialization.Mobile;
using Microsoft.AspNetCore.Components.Authorization;
using NSubstitute;
rockfordlhotka marked this conversation as resolved.
Show resolved Hide resolved

namespace Csla.Test.State
{
Expand All @@ -27,13 +26,13 @@ public class SessionManagerTests
[TestInitialize]
public void Initialize()
{
var mockServiceProvider = new Mock<IServiceProvider>();
var mockServiceProvider = Substitute.For<IServiceProvider>();

// Mock AuthenticationStateProvider
var mockAuthStateProvider = new Mock<AuthenticationStateProvider>();
var mockAuthStateProvider = Substitute.For<AuthenticationStateProvider>();

// Mock IServiceProvider
mockServiceProvider.Setup(x => x.GetService(typeof(AuthenticationStateProvider))).Returns(mockAuthStateProvider.Object);
mockServiceProvider.GetService(typeof(AuthenticationStateProvider)).Returns(mockAuthStateProvider);

_sessionValue = new SessionMessage
{
Expand All @@ -44,73 +43,59 @@ public void Initialize()
};

// Mock ISerializationFormatter
var mockFormatter = new Mock<ISerializationFormatter>();
mockFormatter.Setup(x => x.Serialize(It.IsAny<Stream>(), It.IsAny<object>()));
mockFormatter.Setup(x => x.Deserialize(It.IsAny<Stream>())).Returns(_sessionValue);
var mockFormatter = Substitute.For<ISerializationFormatter>();
mockFormatter.Serialize(Arg.Any<Stream>(), Arg.Any<object>());
mockFormatter.Deserialize(Arg.Any<Stream>()).Returns(_sessionValue);

// Mock IServiceProvider
mockServiceProvider.Setup(x => x.GetService(typeof(Csla.Serialization.Mobile.MobileFormatter))).Returns(mockFormatter.Object);
mockServiceProvider.GetService(typeof(Csla.Serialization.Mobile.MobileFormatter)).Returns(mockFormatter);

var mockActivator = new Mock<Csla.Server.IDataPortalActivator>();
mockActivator.Setup(x => x.CreateInstance(It.Is<Type>(t => t == typeof(Csla.Serialization.Mobile.MobileFormatter)))).Returns(mockFormatter.Object);
mockActivator.Setup(x => x.InitializeInstance(It.IsAny<object>()));
var mockActivator = Substitute.For<Csla.Server.IDataPortalActivator>();
mockActivator.CreateInstance(Arg.Is<Type>(t => t == typeof(Csla.Serialization.Mobile.MobileFormatter))).Returns(mockFormatter);
mockActivator.InitializeInstance(Arg.Any<object>());

// Mock IServiceProvider
mockServiceProvider.Setup(x => x.GetService(typeof(Csla.Server.IDataPortalActivator))).Returns(mockActivator.Object);
mockServiceProvider.GetService(typeof(Csla.Server.IDataPortalActivator)).Returns(mockActivator);

// Mock IContextManager
var mockContextManager = new Mock<IContextManager>();
mockContextManager.Setup(x => x.IsValid).Returns(true);
var mockContextManager = Substitute.For<IContextManager>();
mockContextManager.IsValid.Returns(true);

// Mock IContextManagerLocal
var mockLocalContextManager = new Mock<IContextManagerLocal>();
var mockLocalContextManager = Substitute.For<IContextManagerLocal>();

// Mock IServiceProvider
mockServiceProvider.Setup(x => x.GetService(typeof(IRuntimeInfo))).Returns(new RuntimeInfo());
mockServiceProvider.GetService(typeof(IRuntimeInfo)).Returns(new RuntimeInfo());

// Mock IEnumerable<IContextManager>
var mockContextManagerList = new List<IContextManager> { mockContextManager.Object };
var mockContextManagerList = new List<IContextManager> { mockContextManager };

// Mock ApplicationContextAccessor
var mockApplicationContextAccessor = new Mock<ApplicationContextAccessor>(mockContextManagerList, mockLocalContextManager.Object, mockServiceProvider.Object);

var _applicationContext = new ApplicationContext(mockApplicationContextAccessor.Object);
var mockApplicationContextAccessor = Substitute.For<ApplicationContextAccessor>(mockContextManagerList, mockLocalContextManager, mockServiceProvider);

var _applicationContext = new ApplicationContext(mockApplicationContextAccessor);

_sessionManager = new SessionManager(_applicationContext, GetHttpClient(_sessionValue, _applicationContext), new BlazorWebAssemblyConfigurationOptions { SyncContextWithServer = true });
}

public class TestHttpMessageHandler(SessionMessage session, ApplicationContext _applicationContext) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var response = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("{\"ResultStatus\":0, \"SessionData\":\"" + Convert.ToBase64String(GetSession(session, _applicationContext)) + "\"}"),
};
return Task.FromResult(response);
}
}
private static HttpClient GetHttpClient(SessionMessage session, ApplicationContext _applicationContext)
{
var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
handlerMock
.Protected()
// Setup the PROTECTED method to mock
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
// prepare the expected response of the mocked http call
.ReturnsAsync((HttpRequestMessage request, CancellationToken cancellationToken) =>
{
if (cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException(cancellationToken);
}
else
{
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("{\"ResultStatus\":0, \"SessionData\":\"" + Convert.ToBase64String(GetSession(session, _applicationContext)) + "\"}"),
};
}
})
.Verifiable();

var handlerMock = new TestHttpMessageHandler(session,_applicationContext);
// use real http client with mocked handler here
var httpClient = new HttpClient(handlerMock.Object)
var httpClient = new HttpClient(handlerMock)
{
BaseAddress = new Uri("http://test.com/"),
};
Expand Down