Skip to content

Commit 31002ef

Browse files
Merge pull request #54 from DilmurodDeveloper/users/DilmurodDeveloper/foundations-reader-retrieve
FOUNDATIONS: Retrieve Reader
2 parents 3e5f0e1 + 63645f7 commit 31002ef

File tree

11 files changed

+457
-0
lines changed

11 files changed

+457
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//-----------------------------------------------------------
2+
// Copyright (c) Coalition of Good-Hearted Engineers
3+
// Free To Use To Build Reliable Library Management Solutions
4+
//-----------------------------------------------------------
5+
6+
using FluentAssertions;
7+
using LibraryManagement.Api.Models.Foundations.Readers.Exceptions;
8+
using Microsoft.Data.SqlClient;
9+
using Moq;
10+
11+
namespace LibraryManagement.Api.Tests.Unit.Services.Foundations.Readers
12+
{
13+
public partial class ReaderServiceTests
14+
{
15+
[Fact]
16+
public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
17+
{
18+
// given
19+
SqlException sqlException = GetSqlError();
20+
21+
var failedReaderStorageException =
22+
new FailedReaderStorageException(sqlException);
23+
24+
var expectedReaderDependencyException =
25+
new ReaderDependencyException(failedReaderStorageException);
26+
27+
this.storageBrokerMock.Setup(broker =>
28+
broker.SelectAllReaders()).Throws(sqlException);
29+
30+
// when
31+
Action retrieveAllReadersAction = () =>
32+
this.readerService.RetrieveAllReaders();
33+
34+
ReaderDependencyException actualReaderDependencyException =
35+
Assert.Throws<ReaderDependencyException>(retrieveAllReadersAction);
36+
37+
// then
38+
actualReaderDependencyException.Should()
39+
.BeEquivalentTo(expectedReaderDependencyException);
40+
41+
this.storageBrokerMock.Verify(broker =>
42+
broker.SelectAllReaders(),
43+
Times.Once());
44+
45+
this.loggingBrokerMock.Verify(broker =>
46+
broker.LogCritical(It.Is(SameExceptionAs(
47+
expectedReaderDependencyException))),
48+
Times.Once);
49+
50+
this.storageBrokerMock.VerifyNoOtherCalls();
51+
this.loggingBrokerMock.VerifyNoOtherCalls();
52+
}
53+
54+
[Fact]
55+
public void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
56+
{
57+
// given
58+
string exceptionMessage = GetRandomString();
59+
var serverException = new Exception(exceptionMessage);
60+
61+
var failedReaderServiceException =
62+
new FailedReaderServiceException(serverException);
63+
64+
var expectedReaderServiceException =
65+
new ReaderServiceException(failedReaderServiceException);
66+
67+
this.storageBrokerMock.Setup(broker =>
68+
broker.SelectAllReaders()).Throws(serverException);
69+
70+
// when
71+
Action retrieveAllReaderActions = () =>
72+
this.readerService.RetrieveAllReaders();
73+
74+
ReaderServiceException actualReaderServiceException =
75+
Assert.Throws<ReaderServiceException>(retrieveAllReaderActions);
76+
77+
// then
78+
actualReaderServiceException.Should()
79+
.BeEquivalentTo(expectedReaderServiceException);
80+
81+
this.storageBrokerMock.Verify(broker =>
82+
broker.SelectAllReaders(),
83+
Times.Once());
84+
85+
this.loggingBrokerMock.Verify(broker =>
86+
broker.LogError(It.Is(SameExceptionAs(
87+
expectedReaderServiceException))),
88+
Times.Once);
89+
90+
this.storageBrokerMock.VerifyNoOtherCalls();
91+
this.loggingBrokerMock.VerifyNoOtherCalls();
92+
}
93+
}
94+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//-----------------------------------------------------------
2+
// Copyright (c) Coalition of Good-Hearted Engineers
3+
// Free To Use To Build Reliable Library Management Solutions
4+
//-----------------------------------------------------------
5+
6+
using FluentAssertions;
7+
using LibraryManagement.Api.Models.Foundations.Readers;
8+
using LibraryManagement.Api.Models.Foundations.Readers.Exceptions;
9+
using Microsoft.Data.SqlClient;
10+
using Moq;
11+
12+
namespace LibraryManagement.Api.Tests.Unit.Services.Foundations.Readers
13+
{
14+
public partial class ReaderServiceTests
15+
{
16+
[Fact]
17+
public async Task ShouldThrowCriticalDependencyExceptionOnRetrieveByIdIfSqlErrorOccursAndLogItAsync()
18+
{
19+
// given
20+
Guid someId = Guid.NewGuid();
21+
SqlException sqlException = GetSqlError();
22+
23+
var failedReaderStorageException =
24+
new FailedReaderStorageException(sqlException);
25+
26+
var expectedReaderDependencyException =
27+
new ReaderDependencyException(failedReaderStorageException);
28+
29+
this.storageBrokerMock.Setup(broker =>
30+
broker.SelectReaderByIdAsync(It.IsAny<Guid>()))
31+
.ThrowsAsync(sqlException);
32+
33+
// when
34+
ValueTask<Reader> retrieveReaderById =
35+
this.readerService.RetrieveReaderByIdAsync(someId);
36+
37+
ReaderDependencyException actualReaderDependencyException =
38+
await Assert.ThrowsAsync<ReaderDependencyException>(() =>
39+
retrieveReaderById.AsTask());
40+
41+
// then
42+
actualReaderDependencyException.Should()
43+
.BeEquivalentTo(expectedReaderDependencyException);
44+
45+
this.storageBrokerMock.Verify(broker =>
46+
broker.SelectReaderByIdAsync(someId),
47+
Times.Once());
48+
49+
this.loggingBrokerMock.Verify(broker =>
50+
broker.LogCritical(It.Is(SameExceptionAs(
51+
expectedReaderDependencyException))),
52+
Times.Once);
53+
54+
this.storageBrokerMock.VerifyNoOtherCalls();
55+
this.loggingBrokerMock.VerifyNoOtherCalls();
56+
}
57+
58+
[Fact]
59+
public async Task ShouldThrowServiceExceptionOnRetrieveByIdAsyncIfServiceErrorOccursAndLogItAsync()
60+
{
61+
// given
62+
Guid someId = Guid.NewGuid();
63+
var serverException = new Exception();
64+
65+
var failedReaderServiceException =
66+
new FailedReaderServiceException(serverException);
67+
68+
var expectedReaderServiceException =
69+
new ReaderServiceException(failedReaderServiceException);
70+
71+
this.storageBrokerMock.Setup(broker =>
72+
broker.SelectReaderByIdAsync(It.IsAny<Guid>()))
73+
.ThrowsAsync(serverException);
74+
75+
// when
76+
ValueTask<Reader> retrieveReaderById =
77+
this.readerService.RetrieveReaderByIdAsync(someId);
78+
79+
ReaderServiceException actualReaderServiceException =
80+
await Assert.ThrowsAsync<ReaderServiceException>(() =>
81+
retrieveReaderById.AsTask());
82+
83+
// then
84+
actualReaderServiceException.Should()
85+
.BeEquivalentTo(expectedReaderServiceException);
86+
87+
this.storageBrokerMock.Verify(broker =>
88+
broker.SelectReaderByIdAsync(someId),
89+
Times.Once);
90+
91+
this.loggingBrokerMock.Verify(broker =>
92+
broker.LogError(It.Is(SameExceptionAs(
93+
expectedReaderServiceException))),
94+
Times.Once);
95+
96+
this.storageBrokerMock.VerifyNoOtherCalls();
97+
this.loggingBrokerMock.VerifyNoOtherCalls();
98+
}
99+
}
100+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//-----------------------------------------------------------
2+
// Copyright (c) Coalition of Good-Hearted Engineers
3+
// Free To Use To Build Reliable Library Management Solutions
4+
//-----------------------------------------------------------
5+
6+
using FluentAssertions;
7+
using Force.DeepCloner;
8+
using LibraryManagement.Api.Models.Foundations.Readers;
9+
using Moq;
10+
11+
namespace LibraryManagement.Api.Tests.Unit.Services.Foundations.Readers
12+
{
13+
public partial class ReaderServiceTests
14+
{
15+
[Fact]
16+
public void ShouldRetrieveAllReaders()
17+
{
18+
// given
19+
IQueryable<Reader> randomReader = CreateRandomReaders();
20+
IQueryable<Reader> storageReader = randomReader;
21+
IQueryable<Reader> expectedReader = storageReader.DeepClone();
22+
23+
this.storageBrokerMock.Setup(broker =>
24+
broker.SelectAllReaders())
25+
.Returns(storageReader);
26+
27+
// when
28+
IQueryable<Reader> actualReader =
29+
this.readerService.RetrieveAllReaders();
30+
31+
// then
32+
actualReader.Should().BeEquivalentTo(expectedReader);
33+
34+
this.storageBrokerMock.Verify(broker =>
35+
broker.SelectAllReaders(),
36+
Times.Once);
37+
38+
this.storageBrokerMock.VerifyNoOtherCalls();
39+
this.loggingBrokerMock.VerifyNoOtherCalls();
40+
}
41+
}
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//-----------------------------------------------------------
2+
// Copyright (c) Coalition of Good-Hearted Engineers
3+
// Free To Use To Build Reliable Library Management Solutions
4+
//-----------------------------------------------------------
5+
6+
using FluentAssertions;
7+
using Force.DeepCloner;
8+
using LibraryManagement.Api.Models.Foundations.Readers;
9+
using Moq;
10+
11+
namespace LibraryManagement.Api.Tests.Unit.Services.Foundations.Readers
12+
{
13+
public partial class ReaderServiceTests
14+
{
15+
[Fact]
16+
public async Task ShouldRetrieveReaderByIdAsync()
17+
{
18+
// given
19+
Guid randomReaderId = Guid.NewGuid();
20+
Guid inputReaderId = randomReaderId;
21+
Reader randomReader = CreateRandomReader();
22+
Reader storageReader = randomReader;
23+
Reader expectedReader = storageReader.DeepClone();
24+
25+
this.storageBrokerMock.Setup(broker =>
26+
broker.SelectReaderByIdAsync(inputReaderId))
27+
.ReturnsAsync(storageReader);
28+
29+
// when
30+
Reader actualReader = await this
31+
.readerService.RetrieveReaderByIdAsync(inputReaderId);
32+
33+
// then
34+
actualReader.Should().BeEquivalentTo(expectedReader);
35+
36+
this.storageBrokerMock.Verify(broker =>
37+
broker.SelectReaderByIdAsync(inputReaderId),
38+
Times.Once);
39+
40+
this.storageBrokerMock.VerifyNoOtherCalls();
41+
this.loggingBrokerMock.VerifyNoOtherCalls();
42+
}
43+
}
44+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//-----------------------------------------------------------
2+
// Copyright (c) Coalition of Good-Hearted Engineers
3+
// Free To Use To Build Reliable Library Management Solutions
4+
//-----------------------------------------------------------
5+
6+
using FluentAssertions;
7+
using LibraryManagement.Api.Models.Foundations.Readers;
8+
using LibraryManagement.Api.Models.Foundations.Readers.Exceptions;
9+
using Moq;
10+
11+
namespace LibraryManagement.Api.Tests.Unit.Services.Foundations.Readers
12+
{
13+
public partial class ReaderServiceTests
14+
{
15+
[Fact]
16+
public async Task ShouldThrowValidationExceptionOnRetrieveByIdIfIdIsInvalidAndLogItAsync()
17+
{
18+
// given
19+
Guid invalidReaderId = Guid.Empty;
20+
var invalidReaderException = new InvalidReaderException();
21+
22+
invalidReaderException.AddData(
23+
key: nameof(Reader.ReaderId),
24+
values: "Id is required");
25+
26+
var expectedReaderValidationException =
27+
new ReaderValidationException(invalidReaderException);
28+
29+
// when
30+
ValueTask<Reader> retrieveReaderById =
31+
this.readerService.RetrieveReaderByIdAsync(invalidReaderId);
32+
33+
ReaderValidationException actualReaderValidationException =
34+
await Assert.ThrowsAsync<ReaderValidationException>(() =>
35+
retrieveReaderById.AsTask());
36+
37+
// then
38+
actualReaderValidationException.Should()
39+
.BeEquivalentTo(expectedReaderValidationException);
40+
41+
this.loggingBrokerMock.Verify(broker =>
42+
broker.LogError(It.Is(SameExceptionAs(
43+
expectedReaderValidationException))),
44+
Times.Once);
45+
46+
this.storageBrokerMock.Verify(broker =>
47+
broker.SelectReaderByIdAsync(It.IsAny<Guid>()),
48+
Times.Never);
49+
50+
this.loggingBrokerMock.VerifyNoOtherCalls();
51+
this.storageBrokerMock.VerifyNoOtherCalls();
52+
}
53+
54+
[Fact]
55+
public async Task ShouldThrowValidationExceptionOnRetrieveByIdIfReaderNotFoundAndLogItAsync()
56+
{
57+
// given
58+
Guid someReaderId = Guid.NewGuid();
59+
Reader noReader = null;
60+
61+
var notFoundReaderException =
62+
new NotFoundReaderException(someReaderId);
63+
64+
var expectedReaderValidationException =
65+
new ReaderValidationException(notFoundReaderException);
66+
67+
this.storageBrokerMock.Setup(broker =>
68+
broker.SelectReaderByIdAsync(It.IsAny<Guid>()))
69+
.ReturnsAsync(noReader);
70+
71+
// when
72+
ValueTask<Reader> retriveByIdReaderTask =
73+
this.readerService.RetrieveReaderByIdAsync(someReaderId);
74+
75+
var actualReaderValidationException =
76+
await Assert.ThrowsAsync<ReaderValidationException>(() =>
77+
retriveByIdReaderTask.AsTask());
78+
79+
// then
80+
actualReaderValidationException.Should()
81+
.BeEquivalentTo(expectedReaderValidationException);
82+
83+
this.storageBrokerMock.Verify(broker =>
84+
broker.SelectReaderByIdAsync(someReaderId),
85+
Times.Once);
86+
87+
this.loggingBrokerMock.Verify(broker =>
88+
broker.LogError(It.Is(SameExceptionAs(
89+
expectedReaderValidationException))),
90+
Times.Once);
91+
92+
this.storageBrokerMock.VerifyNoOtherCalls();
93+
this.loggingBrokerMock.VerifyNoOtherCalls();
94+
}
95+
}
96+
}

LibraryManagement.Api.Tests.Unit/Services/Foundations/Readers/ReaderServiceTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ private static Reader CreateRandomReader() =>
3737
private static DateTimeOffset GetRandomDateTimeOffset() =>
3838
new DateTimeRange(earliestDate: new DateTime()).GetValue();
3939

40+
private IQueryable<Reader> CreateRandomReaders()
41+
{
42+
return CreateReaderFiller(GetRandomDateTimeOffset())
43+
.Create(count: GetRandomNumber()).AsQueryable();
44+
}
45+
46+
private static int GetRandomNumber() =>
47+
new IntRange(2, 9).GetValue();
48+
4049
private static SqlException GetSqlError() =>
4150
(SqlException)RuntimeHelpers.GetUninitializedObject(typeof(SqlException));
4251

0 commit comments

Comments
 (0)