Skip to content

Commit

Permalink
Merge branch 'master' into feature/VIH-11088_update_royal_coat_of_arms
Browse files Browse the repository at this point in the history
  • Loading branch information
will-craig authored Jan 14, 2025
2 parents 2dc3c11 + fce53e9 commit ddf963d
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 328 deletions.
11 changes: 8 additions & 3 deletions VideoWeb/VideoWeb.Common/Caching/CachingHelper.cs
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
};
}
}
24 changes: 9 additions & 15 deletions VideoWeb/VideoWeb.Common/Caching/RedisCacheBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand All @@ -22,15 +22,15 @@ protected RedisCacheBase(
_logger = logger;
}

public virtual async Task WriteToCache(TKey key, TEntry toWrite, CancellationToken cancellationToken = default)
public virtual async Task WriteToCache(TKey key, TEntry toWrite, CancellationToken cancellationToken = default)
{
if(Equals(key, default(TKey)))
if (Equals(key, default(TKey)))
throw new ArgumentNullException(nameof(key));
if (CacheEntryOptions == null)
throw new InvalidOperationException($"Cannot write to cache without setting the {nameof(CacheEntryOptions)}");

var serialisedLayout = JsonConvert.SerializeObject(toWrite, CachingHelper.SerializerSettings);
var data = Encoding.UTF8.GetBytes(serialisedLayout);
var serializedLayout = JsonSerializer.Serialize(toWrite, CachingHelper.JsonSerializerOptions);
var data = Encoding.UTF8.GetBytes(serializedLayout);

try
{
Expand All @@ -57,16 +57,11 @@ private async Task<TResult> ReadFromCacheWithType<TResult>(TKey key, Cancellatio
try
{
var data = await _distributedCache.GetAsync(GetKey(key), cancellationToken);
if(data == null) return default;
if (data == null) return default;

var dataAsString = Encoding.UTF8.GetString(data);
var deserialisedObject =
JsonConvert.DeserializeObject<TResult>(dataAsString,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None, Formatting = Formatting.None
});
return deserialisedObject;
var deserializedObject = JsonSerializer.Deserialize<TResult>(dataAsString, CachingHelper.JsonSerializerOptions);
return deserializedObject;
}
catch (Exception ex)
{
Expand All @@ -85,7 +80,6 @@ protected virtual async Task RemoveFromCache(TKey key, CancellationToken cancell
{
_logger.LogError(ex, "Error removing from cache for key {Key}", GetKey(key));
}

}

protected abstract string GetKey(TKey key);
Expand Down
36 changes: 0 additions & 36 deletions VideoWeb/VideoWeb.Common/Helpers/ApiRequestHelper.cs

This file was deleted.

4 changes: 2 additions & 2 deletions VideoWeb/VideoWeb.UnitTests/Cache/CacheLockTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Text;
using System.Text.Json;
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;

Expand All @@ -18,7 +18,7 @@ public class CacheLockTests
private Mock<IDistributedCache> _distributedCacheMock;
private Mock<ILogger<CacheLock>> _loggerMock;
private const string LockKey = "cache_lock_key";
private readonly string _cacheLockValue = JsonConvert.SerializeObject("locked", CachingHelper.SerializerSettings);
private readonly string _cacheLockValue = JsonSerializer.Serialize("locked", CachingHelper.JsonSerializerOptions);

[SetUp]
public void Setup()
Expand Down
145 changes: 71 additions & 74 deletions VideoWeb/VideoWeb.UnitTests/Cache/DistributedConferenceCacheTests.cs
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extras.Moq;
Expand All @@ -9,7 +10,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using VideoWeb.Common.Caching;
using VideoWeb.Common.Models;
Expand All @@ -26,7 +26,7 @@ public class DistributedConsultationInvitationCacheTests

private async Task WriteToCache<T>(Guid key, T obj) where T : class
{
var serialisedObj = JsonConvert.SerializeObject(obj, CachingHelper.SerializerSettings);
var serialisedObj = JsonSerializer.Serialize(obj, CachingHelper.JsonSerializerOptions);
var data = Encoding.UTF8.GetBytes(serialisedObj);
await _distributedCache.SetAsync(key.ToString(), data,
new DistributedCacheEntryOptions
Expand All @@ -38,8 +38,8 @@ await _distributedCache.SetAsync(key.ToString(), data,
private async Task<T> ReadFromCache<T>(Guid key) where T : class
{
var data = await _distributedCache.GetAsync(key.ToString());
return data == null ? null : JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(data),
CachingHelper.SerializerSettings);
return data == null ? null : JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(data),
CachingHelper.JsonSerializerOptions);
}

[SetUp]
Expand Down
Loading

0 comments on commit ddf963d

Please sign in to comment.