Skip to content

Commit

Permalink
adding engine client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLewis-digirati committed May 29, 2024
1 parent d0a9404 commit 8fb0f95
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using DLCS.AWS.ElasticTranscoder.Models;
using DLCS.AWS.SQS;
using DLCS.Core.Caching;
using DLCS.Core.Types;
using DLCS.Model.Assets;
using DLCS.Model.Messaging;
using DLCS.Repository.Messaging;
using FakeItEasy;
using LazyCache.Mocks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Test.Helpers.Http;

namespace DLCS.Repository.Tests.Messaging;
Expand All @@ -33,8 +38,9 @@ public EngineClientTests()

queueLookup = A.Fake<IQueueLookup>();
queueSender = A.Fake<IQueueSender>();

sut = new EngineClient(queueLookup, queueSender, httpClient, new NullLogger<EngineClient>());

sut = new EngineClient(queueLookup, queueSender, httpClient, new MockCachingService(), Options.Create(new CacheSettings()),
new NullLogger<EngineClient>());
}

[Fact]
Expand Down Expand Up @@ -139,4 +145,47 @@ public async Task GetAllowedAvOptions_ReturnsNull_IfEngineAvPolicyEndpointUnreac
message.Method.Should().Be(HttpMethod.Get);
returnedAvPolicyOptions.Should().BeNull();
}

[Fact]
public async Task GetAvPresets_RetrievesAllowedAvPresets()
{
// Arrange
HttpRequestMessage message = null;
httpHandler.RegisterCallback(r => message = r);

var response = JsonSerializer.Serialize(new Dictionary<string, TranscoderPreset>()
{
{ "webm-policy", new("webm-policy", "some-webm-preset", "oga") },
{ "oga-policy", new("oga-policy", "some-oga-preset", "webm") }
});

httpHandler.GetResponseMessage(response, HttpStatusCode.OK);

// Act
var returnedAvPresets = await sut.GetAvPresets();

// Assert
httpHandler.CallsMade.Should().ContainSingle().Which.Should().Be("http://engine.dlcs/av-presets");
message.Method.Should().Be(HttpMethod.Get);
returnedAvPresets!.Count.Should().Be(2);
returnedAvPresets!.Keys.Should().BeEquivalentTo("webm-policy", "oga-policy");
returnedAvPresets!.Values.Should().Contain(new TranscoderPreset("webm-policy", "some-webm-preset", "oga"));
}

[Fact]
public async Task GetAvPresets_ReturnsEmpty_IfEngineAvPolicyEndpointUnreachable()
{
// Arrange
HttpRequestMessage message = null;
httpHandler.RegisterCallback(r => message = r);
httpHandler.GetResponseMessage("Not found", HttpStatusCode.NotFound);

// Act
var returnedAvPresets = await sut.GetAvPresets();

// Assert
httpHandler.CallsMade.Should().ContainSingle().Which.Should().Be("http://engine.dlcs/av-presets");
message.Method.Should().Be(HttpMethod.Get);
returnedAvPresets.Should().BeNull();
}
}
2 changes: 1 addition & 1 deletion src/protagonist/DLCS.Repository/Messaging/EngineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async Task<int> AsynchronousIngestBatch(IReadOnlyCollection<Asset> assets
public async Task<IReadOnlyDictionary<string, TranscoderPreset>?> GetAvPresets(CancellationToken cancellationToken = default)
{
const string key = "avPresetList";
return await appCache.GetOrAddAsync(key, async entry =>
return await appCache.GetOrAddAsync(key, async () =>
{
try
{
Expand Down

0 comments on commit 8fb0f95

Please sign in to comment.