-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/VIH-11088_update_royal_coat_of_arms
- Loading branch information
Showing
12 changed files
with
147 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
using Newtonsoft.Json; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace VideoWeb.Common.Caching | ||
{ | ||
public static class CachingHelper | ||
{ | ||
public static JsonSerializerSettings SerializerSettings => new JsonSerializerSettings | ||
public static JsonSerializerOptions JsonSerializerOptions => new() | ||
{ | ||
TypeNameHandling = TypeNameHandling.Objects, Formatting = Formatting.None | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
WriteIndented = false, | ||
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, | ||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)}, | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 71 additions & 74 deletions
145
VideoWeb/VideoWeb.UnitTests/Cache/DistributedConferenceCacheTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,90 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using VideoWeb.Common.Caching; | ||
using VideoWeb.Common.Configuration; | ||
|
||
namespace VideoWeb.UnitTests.Cache | ||
namespace VideoWeb.UnitTests.Cache; | ||
|
||
public class DistributedConferenceCacheTests : CacheTestBase | ||
{ | ||
public class DistributedConferenceCacheTests : CacheTestBase | ||
private Mock<IDistributedCache> _distributedCacheMock; | ||
private Mock<ILogger<DistributedConferenceCache>> _loggerMock; | ||
private CacheSettings _cacheSettings; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
private Mock<IDistributedCache> _distributedCacheMock; | ||
private Mock<ILogger<DistributedConferenceCache>> _loggerMock; | ||
private CacheSettings _cacheSettings; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_distributedCacheMock = new Mock<IDistributedCache>(); | ||
_loggerMock = new Mock<ILogger<DistributedConferenceCache>>(); | ||
_cacheSettings = new CacheSettings { CacheDuration = 1 }; | ||
} | ||
_distributedCacheMock = new Mock<IDistributedCache>(); | ||
_loggerMock = new Mock<ILogger<DistributedConferenceCache>>(); | ||
_cacheSettings = new CacheSettings { CacheDuration = 1 }; | ||
} | ||
|
||
[Test] | ||
public async Task GetOrAddConferenceAsync_should_return_conference_when_cache_contains_key() | ||
{ | ||
var conferenceResponse = CreateConferenceResponse(); | ||
var hearingDetails = CreateHearingResponse(conferenceResponse); | ||
var conference = ConferenceCacheMapper.MapConferenceToCacheModel(conferenceResponse, hearingDetails); | ||
var serialisedConference = JsonSerializer.Serialize(conference, CachingHelper.JsonSerializerOptions); | ||
var rawData = Encoding.UTF8.GetBytes(serialisedConference); | ||
_distributedCacheMock | ||
.Setup(x => x.GetAsync(conference.Id.ToString(), CancellationToken.None)) | ||
.ReturnsAsync(rawData); | ||
|
||
[Test] | ||
public async Task GetOrAddConferenceAsync_should_return_conference_when_cache_contains_key() | ||
{ | ||
var conferenceResponse = CreateConferenceResponse(); | ||
var hearingDetails = CreateHearingResponse(conferenceResponse); | ||
var conference = ConferenceCacheMapper.MapConferenceToCacheModel(conferenceResponse, hearingDetails); | ||
var serialisedConference = JsonConvert.SerializeObject(conference, SerializerSettings); | ||
var rawData = Encoding.UTF8.GetBytes(serialisedConference); | ||
_distributedCacheMock | ||
.Setup(x => x.GetAsync(conference.Id.ToString(), CancellationToken.None)) | ||
.ReturnsAsync(rawData); | ||
|
||
var cache = new DistributedConferenceCache(_distributedCacheMock.Object, _loggerMock.Object, _cacheSettings); | ||
|
||
var result = await cache.GetOrAddConferenceAsync(conference.Id, DummyInput); | ||
result.Should().BeEquivalentTo(conference); | ||
} | ||
var cache = new DistributedConferenceCache(_distributedCacheMock.Object, _loggerMock.Object, _cacheSettings); | ||
|
||
[Test] | ||
public async Task GetOrAddConferenceAsync_should_return_conference_when_cache_does_not_contains_key() | ||
{ | ||
var conferenceResponse = CreateConferenceResponse(); | ||
var hearingDetails = CreateHearingResponse(conferenceResponse); | ||
var conference = ConferenceCacheMapper.MapConferenceToCacheModel(conferenceResponse, hearingDetails); | ||
var serialisedConference = JsonConvert.SerializeObject(conference, SerializerSettings); | ||
var rawData = Encoding.UTF8.GetBytes(serialisedConference); | ||
_distributedCacheMock | ||
.SetupSequence(x => x.GetAsync(conference.Id.ToString(), CancellationToken.None)) | ||
.ReturnsAsync((byte[]) null) | ||
.ReturnsAsync(rawData); | ||
|
||
_distributedCacheMock | ||
.Setup(x => x.SetAsync(conference.Id.ToString(), rawData, It.IsAny<DistributedCacheEntryOptions>(), CancellationToken.None)); | ||
|
||
var cache = new DistributedConferenceCache(_distributedCacheMock.Object, _loggerMock.Object, _cacheSettings); | ||
|
||
var result = await cache.GetOrAddConferenceAsync(conference.Id, async () => await Task.FromResult((conferenceResponse, hearingDetails))); | ||
result.Should().BeEquivalentTo(conference); | ||
} | ||
var result = await cache.GetOrAddConferenceAsync(conference.Id, DummyInput); | ||
result.Should().BeEquivalentTo(conference); | ||
} | ||
|
||
[Test] | ||
public async Task GetOrAddConferenceAsync_should_return_conference_when_cache_does_not_contains_key() | ||
{ | ||
var conferenceResponse = CreateConferenceResponse(); | ||
var hearingDetails = CreateHearingResponse(conferenceResponse); | ||
var conference = ConferenceCacheMapper.MapConferenceToCacheModel(conferenceResponse, hearingDetails); | ||
var serialisedConference = JsonSerializer.Serialize(conference, CachingHelper.JsonSerializerOptions); | ||
var rawData = Encoding.UTF8.GetBytes(serialisedConference); | ||
_distributedCacheMock | ||
.SetupSequence(x => x.GetAsync(conference.Id.ToString(), CancellationToken.None)) | ||
.ReturnsAsync((byte[]) null) | ||
.ReturnsAsync(rawData); | ||
|
||
[Test] | ||
public async Task RemoveConferenceAsync_should_remove_conference_from_cache() | ||
{ | ||
// Arrange | ||
var conferenceResponse = CreateConferenceResponse(); | ||
var hearingDetails = CreateHearingResponse(conferenceResponse); | ||
var conference = ConferenceCacheMapper.MapConferenceToCacheModel(conferenceResponse, hearingDetails); | ||
var serialisedConference = JsonConvert.SerializeObject(conference, SerializerSettings); | ||
var rawData = Encoding.UTF8.GetBytes(serialisedConference); | ||
_distributedCacheMock | ||
.Setup(x => x.GetAsync(conference.Id.ToString(), CancellationToken.None)) | ||
.ReturnsAsync(rawData); | ||
|
||
var cache = new DistributedConferenceCache(_distributedCacheMock.Object, _loggerMock.Object, _cacheSettings); | ||
|
||
// Act | ||
await cache.RemoveConferenceAsync(conference); | ||
|
||
// Assert | ||
_distributedCacheMock.Verify(x => x.RemoveAsync(conference.Id.ToString(), default), Times.Once); | ||
} | ||
_distributedCacheMock | ||
.Setup(x => x.SetAsync(conference.Id.ToString(), rawData, It.IsAny<DistributedCacheEntryOptions>(), CancellationToken.None)); | ||
|
||
var cache = new DistributedConferenceCache(_distributedCacheMock.Object, _loggerMock.Object, _cacheSettings); | ||
|
||
var result = await cache.GetOrAddConferenceAsync(conference.Id, async () => await Task.FromResult((conferenceResponse, hearingDetails))); | ||
result.Should().BeEquivalentTo(conference); | ||
} | ||
|
||
[Test] | ||
public async Task RemoveConferenceAsync_should_remove_conference_from_cache() | ||
{ | ||
// Arrange | ||
var conferenceResponse = CreateConferenceResponse(); | ||
var hearingDetails = CreateHearingResponse(conferenceResponse); | ||
var conference = ConferenceCacheMapper.MapConferenceToCacheModel(conferenceResponse, hearingDetails); | ||
var serialisedConference = JsonSerializer.Serialize(conference, CachingHelper.JsonSerializerOptions); | ||
var rawData = Encoding.UTF8.GetBytes(serialisedConference); | ||
_distributedCacheMock | ||
.Setup(x => x.GetAsync(conference.Id.ToString(), CancellationToken.None)) | ||
.ReturnsAsync(rawData); | ||
|
||
var cache = new DistributedConferenceCache(_distributedCacheMock.Object, _loggerMock.Object, _cacheSettings); | ||
|
||
// Act | ||
await cache.RemoveConferenceAsync(conference); | ||
|
||
private static JsonSerializerSettings SerializerSettings => new () { TypeNameHandling = TypeNameHandling.Objects, Formatting = Formatting.None }; | ||
// Assert | ||
_distributedCacheMock.Verify(x => x.RemoveAsync(conference.Id.ToString(), default), Times.Once); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.