using System.Reflection; using ErsatzTV.Application.Graphics; using ErsatzTV.Controllers.Api; using ErsatzTV.Core.Api.Graphics; using MediatR; using Microsoft.AspNetCore.Mvc.Routing; using NSubstitute; using NUnit.Framework; using Shouldly; namespace ErsatzTV.Tests.Controllers; [TestFixture] public class GraphicsElementControllerTests { private GraphicsElementController _controller = null!; private IMediator _mediator = null!; [SetUp] public void SetUp() { _mediator = Substitute.For(); _controller = new GraphicsElementController(_mediator); } [Test] public void Controller_Should_Expose_Idiomatic_Rest_Route() { MethodInfo action = typeof(GraphicsElementController).GetMethod(nameof(GraphicsElementController.GetAll)) ?? throw new AssertionException("Missing action GetAll"); HttpMethodAttribute attribute = action.GetCustomAttributes(inherit: true).Single(); attribute.HttpMethods.ShouldContain("GET"); attribute.Template.ShouldBe("/api/graphics-elements"); } [Test] public async Task GetAll_Should_Return_GraphicsElements() { List models = [ new GraphicsElementResponseModel(1, "Lower Third (lower-third.png)"), new GraphicsElementResponseModel(2, "bug.png") ]; _mediator.Send(Arg.Any(), Arg.Any()) .Returns(models); List result = await _controller.GetAll(refresh: false, CancellationToken.None); result.ShouldBe(models); } [Test] public async Task GetAll_Should_Return_Empty_List_When_None_Exist() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns([]); List result = await _controller.GetAll(refresh: false, CancellationToken.None); result.ShouldBeEmpty(); } [Test] public async Task GetAll_Should_Not_Refresh_When_Refresh_Is_False() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns([]); await _controller.GetAll(refresh: false, CancellationToken.None); await _mediator.DidNotReceive().Send(Arg.Any(), Arg.Any()); } [Test] public async Task GetAll_Should_Refresh_Before_Listing_When_Refresh_Is_True() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns([]); await _controller.GetAll(refresh: true, CancellationToken.None); Received.InOrder(() => { _mediator.Send(Arg.Any(), Arg.Any()); _mediator.Send(Arg.Any(), Arg.Any()); }); } }