From e95ff65b88350c4c0cdfae70f96296b17b57dda8 Mon Sep 17 00:00:00 2001 From: philgei Date: Wed, 3 Jul 2024 11:18:35 +0200 Subject: [PATCH] Added tests for GetLearningElementUseCase --- .../GetLearningElementUseCaseTest.cs | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 AdLerBackend.Application.UnitTests/Common/InternalUseCases/GetLearningElementUseCaseTest.cs diff --git a/AdLerBackend.Application.UnitTests/Common/InternalUseCases/GetLearningElementUseCaseTest.cs b/AdLerBackend.Application.UnitTests/Common/InternalUseCases/GetLearningElementUseCaseTest.cs new file mode 100644 index 00000000..c408d810 --- /dev/null +++ b/AdLerBackend.Application.UnitTests/Common/InternalUseCases/GetLearningElementUseCaseTest.cs @@ -0,0 +1,149 @@ +using AdLerBackend.Application.Common.Exceptions; +using AdLerBackend.Application.Common.InternalUseCases.GetAllElementsFromLms; +using AdLerBackend.Application.Common.InternalUseCases.GetLearningElement; +using AdLerBackend.Application.Common.Responses.World; +using FluentAssertions; +using MediatR; +using NSubstitute; + +namespace AdLerBackend.Application.UnitTests.Common.InternalUseCases; + +public class GetLearningElementUseCaseTest +{ + private IMediator _mediatorMock; + + [SetUp] + public void Setup() + { + _mediatorMock = Substitute.For(); + } + + [Test] + public async Task GetLearningElement_Valid_ReturnsLearningElement() + { + // Arrange + _mediatorMock.Send(Arg.Any()).Returns( + new GetAllElementsFromLmsWithAdLerIdResponse() + { + ElementAggregations = new List() + { + new AdLerLmsElementAggregation() + { + AdLerElement = new BaseElement() + { + ElementId = 1 + } + }, + new AdLerLmsElementAggregation() + { + AdLerElement = new BaseElement() + { + ElementId = 2 + } + } + }, + LmsCourseId = 123, + AdLerWorldId = 1234 + } + + ); + var systemUnderTest = new GetLearningElementUseCase(_mediatorMock); + + // Act + var result = await systemUnderTest.Handle(new GetLearningElementCommand() + { + ElementId = 1, + CanBeLocked = false + }, CancellationToken.None); + + // Assert + result.Should().NotBeNull(); + result.AdLerElement.ElementId.Should().Be(1); + + } + + [Test] + public async Task GetLearningElement_ElementNotFound_ThrowsNotFoundException() + { + // Arrange + _mediatorMock.Send(Arg.Any()).Returns( + new GetAllElementsFromLmsWithAdLerIdResponse() + { + ElementAggregations = new List() + { + new AdLerLmsElementAggregation() + { + AdLerElement = new BaseElement() + { + ElementId = 1 + } + }, + new AdLerLmsElementAggregation() + { + AdLerElement = new BaseElement() + { + ElementId = 2 + } + } + }, + LmsCourseId = 123, + AdLerWorldId = 1234 + } + + ); + var systemUnderTest = new GetLearningElementUseCase(_mediatorMock); + + // Act + Func act = async () => await systemUnderTest.Handle(new GetLearningElementCommand() + { + ElementId = 3, + CanBeLocked = false + }, CancellationToken.None); + + // Assert + await act.Should().ThrowAsync(); + } + + [Test] + public async Task GetLearningElement_ElementIsLockedAndCannotBeAccessed_ThrowsForbiddenAccessException() + { + // Arrange + _mediatorMock.Send(Arg.Any()).Returns( + new GetAllElementsFromLmsWithAdLerIdResponse() + { + ElementAggregations = new List() + { + new AdLerLmsElementAggregation() + { + AdLerElement = new BaseElement() + { + ElementId = 1 + }, + IsLocked = true + }, + new AdLerLmsElementAggregation() + { + AdLerElement = new BaseElement() + { + ElementId = 2 + } + } + }, + LmsCourseId = 123, + AdLerWorldId = 1234 + } + + ); + var systemUnderTest = new GetLearningElementUseCase(_mediatorMock); + + // Act + Func act = async () => await systemUnderTest.Handle(new GetLearningElementCommand() + { + ElementId = 1, + CanBeLocked = false + }, CancellationToken.None); + + // Assert + await act.Should().ThrowAsync(); + } +} \ No newline at end of file