-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for GetLearningElementUseCase
- Loading branch information
Showing
1 changed file
with
149 additions
and
0 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
AdLerBackend.Application.UnitTests/Common/InternalUseCases/GetLearningElementUseCaseTest.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 |
---|---|---|
@@ -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<IMediator>(); | ||
} | ||
|
||
[Test] | ||
public async Task GetLearningElement_Valid_ReturnsLearningElement() | ||
{ | ||
// Arrange | ||
_mediatorMock.Send(Arg.Any<GetAllElementsFromLmsCommand>()).Returns( | ||
new GetAllElementsFromLmsWithAdLerIdResponse() | ||
{ | ||
ElementAggregations = new List<AdLerLmsElementAggregation>() | ||
{ | ||
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<GetAllElementsFromLmsCommand>()).Returns( | ||
new GetAllElementsFromLmsWithAdLerIdResponse() | ||
{ | ||
ElementAggregations = new List<AdLerLmsElementAggregation>() | ||
{ | ||
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<Task> act = async () => await systemUnderTest.Handle(new GetLearningElementCommand() | ||
{ | ||
ElementId = 3, | ||
CanBeLocked = false | ||
}, CancellationToken.None); | ||
|
||
// Assert | ||
await act.Should().ThrowAsync<NotFoundException>(); | ||
} | ||
|
||
[Test] | ||
public async Task GetLearningElement_ElementIsLockedAndCannotBeAccessed_ThrowsForbiddenAccessException() | ||
{ | ||
// Arrange | ||
_mediatorMock.Send(Arg.Any<GetAllElementsFromLmsCommand>()).Returns( | ||
new GetAllElementsFromLmsWithAdLerIdResponse() | ||
{ | ||
ElementAggregations = new List<AdLerLmsElementAggregation>() | ||
{ | ||
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<Task> act = async () => await systemUnderTest.Handle(new GetLearningElementCommand() | ||
{ | ||
ElementId = 1, | ||
CanBeLocked = false | ||
}, CancellationToken.None); | ||
|
||
// Assert | ||
await act.Should().ThrowAsync<ForbiddenAccessException>(); | ||
} | ||
} |