Skip to content

Commit 24fac9d

Browse files
authored
Merge pull request #68 from AmielCyber/dev
Dev
2 parents bd90c20 + 98ea4ed commit 24fac9d

30 files changed

+327
-360
lines changed

PetSearch.API.Tests/Clients/MapBoxClientTest.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using System.Globalization;
12
using System.Net;
23
using System.Net.Http.Json;
3-
using ErrorOr;
44
using JetBrains.Annotations;
5+
using Microsoft.AspNetCore.Http.HttpResults;
56
using Microsoft.Extensions.Options;
67
using Moq;
78
using PetSearch.API.Clients;
89
using PetSearch.API.Configurations;
10+
using PetSearch.API.Problems;
911
using PetSearch.API.Exceptions;
1012
using PetSearch.API.Models;
1113
using PetSearch.API.Tests.Data;
@@ -26,6 +28,7 @@ public class MapBoxClientTest
2628
private readonly Mock<IOptions<MapBoxConfiguration>> _mapBoxOptionsMock;
2729
private readonly string _accessTokenValue;
2830
private readonly LocationDto _expectedDefaultLocationDto;
31+
private readonly IExpectedProblems _expectedProblems;
2932

3033
public MapBoxClientTest()
3134
{
@@ -40,6 +43,7 @@ public MapBoxClientTest()
4043
};
4144
_mapBoxOptionsMock = new Mock<IOptions<MapBoxConfiguration>>();
4245
_mapBoxOptionsMock.Setup(options => options.Value).Returns(mapBoxConfiguration);
46+
_expectedProblems = new MapBoxProblems();
4347
}
4448

4549
[Fact]
@@ -51,12 +55,12 @@ public async Task GetLocationFromZipCode_ShouldReturnALocationDto_IfZipcodeIsVal
5155
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
5256
mockHttpClient.BaseAddress = _mapBoxUri;
5357

54-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
58+
59+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
5560

5661
// Act
5762
var result = await mapBoxClient.GetLocationFromZipCode(DefaultZipCode);
58-
var locationDto = result.Value;
59-
63+
LocationDto? locationDto = Assert.IsType<Ok<LocationDto>>(result.Result).Value;
6064
// Assert
6165
Assert.NotNull(locationDto);
6266
Assert.IsType<LocationDto>(locationDto);
@@ -73,7 +77,7 @@ public async Task GetLocationFromZipCode_ShouldCallMapBoxUrlFromMapBoxConfigUrl(
7377
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
7478
mockHttpClient.BaseAddress = _mapBoxUri;
7579

76-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
80+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
7781

7882
// Act
7983
await mapBoxClient.GetLocationFromZipCode(DefaultZipCode);
@@ -92,7 +96,7 @@ public async Task GetLocationFromZipCode_ShouldCallMapBoxUrlWithAccessToken()
9296
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
9397
mockHttpClient.BaseAddress = _mapBoxUri;
9498

95-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
99+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
96100

97101
// Act
98102
await mapBoxClient.GetLocationFromZipCode(DefaultZipCode);
@@ -112,7 +116,7 @@ public async Task GetLocationFromZipCode_ShouldCallMapBoxUrlWithPassedZipcode()
112116
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
113117
mockHttpClient.BaseAddress = _mapBoxUri;
114118

115-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
119+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
116120

117121
// Act
118122
await mapBoxClient.GetLocationFromZipCode(DefaultZipCode);
@@ -124,9 +128,9 @@ public async Task GetLocationFromZipCode_ShouldCallMapBoxUrlWithPassedZipcode()
124128
}
125129

126130
[Theory]
127-
[ClassData(typeof(MapBoxClientErrorsData))]
131+
[ClassData(typeof(MapBoxClientProblemsData))]
128132
public async Task GetLocationFromZipCode_ShouldReturnCorrectError_IfResultIsUnsuccessful(
129-
HttpStatusCode statusCode, Error expectedError
133+
HttpStatusCode statusCode
130134
)
131135
{
132136
// Arrange
@@ -135,14 +139,15 @@ public async Task GetLocationFromZipCode_ShouldReturnCorrectError_IfResultIsUnsu
135139
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
136140
mockHttpClient.BaseAddress = _mapBoxUri;
137141

138-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
142+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
139143

140144
// Act
141-
var result = await mapBoxClient.GetLocationFromZipCode(DefaultZipCode);
145+
Results<Ok<LocationDto>, ProblemHttpResult> result = await mapBoxClient.GetLocationFromZipCode(DefaultZipCode);
142146

147+
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result.Result);
143148
// Assert
144149
_mockHttp.Expect(MapBoxConfiguration.Url).Respond(statusCode);
145-
Assert.Equal(expectedError, result.FirstError);
150+
Assert.Equal((int)statusCode, problemResult.StatusCode);
146151
}
147152

148153
[Fact]
@@ -154,7 +159,7 @@ public async Task GetLocationFromZipCode_ShouldThrowForbiddenAccess_IfApiStatusC
154159
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
155160
mockHttpClient.BaseAddress = _mapBoxUri;
156161

157-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
162+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
158163

159164
// Act
160165
await Assert.ThrowsAsync<ForbiddenAccessException>(
@@ -171,11 +176,12 @@ public async Task GetLocationFromCoordinates_ShouldReturnALocationDto()
171176
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
172177
mockHttpClient.BaseAddress = _mapBoxUri;
173178

174-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
179+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
175180

176181
// Act
177-
var result = await mapBoxClient.GetLocationFromCoordinates(DefaultLongitude, DefaultLatitude);
178-
var locationDto = result.Value;
182+
Results<Ok<LocationDto>, ProblemHttpResult> result =
183+
await mapBoxClient.GetLocationFromCoordinates(DefaultLongitude, DefaultLatitude);
184+
LocationDto? locationDto = Assert.IsType<Ok<LocationDto>>(result.Result).Value;
179185

180186
// Assert
181187
Assert.NotNull(locationDto);
@@ -193,7 +199,7 @@ public async Task GetLocationFromCoordinates_ShouldCallMapBoxUrlFromMapBoxConfig
193199
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
194200
mockHttpClient.BaseAddress = _mapBoxUri;
195201

196-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
202+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
197203

198204
// Act
199205
await mapBoxClient.GetLocationFromCoordinates(DefaultLongitude, DefaultLatitude);
@@ -212,7 +218,7 @@ public async Task GetLocationFromCoordinates_ShouldCallMapBoxUrlWithAccessToken(
212218
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
213219
mockHttpClient.BaseAddress = _mapBoxUri;
214220

215-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
221+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
216222

217223
// Act
218224
await mapBoxClient.GetLocationFromCoordinates(DefaultLongitude, DefaultLatitude);
@@ -232,22 +238,22 @@ public async Task GetLocationFromCoordinates_ShouldCallMapBoxUrlWithPassedCoordi
232238
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
233239
mockHttpClient.BaseAddress = _mapBoxUri;
234240

235-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
241+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
236242

237243
// Act
238244
await mapBoxClient.GetLocationFromCoordinates(DefaultLongitude, DefaultLatitude);
239245

240246
// Assert
241247
Assert.Equal(1, _mockHttp.GetMatchCount(request));
242248
_mockHttp.Expect(MapBoxConfiguration.Url)
243-
.WithContent(DefaultLongitude.ToString())
244-
.WithContent(DefaultLatitude.ToString());
249+
.WithContent(DefaultLongitude.ToString(CultureInfo.InvariantCulture))
250+
.WithContent(DefaultLatitude.ToString(CultureInfo.InvariantCulture));
245251
}
246252

247253
[Theory]
248-
[ClassData(typeof(MapBoxClientErrorsData))]
254+
[ClassData(typeof(MapBoxClientProblemsData))]
249255
public async Task GetLocationFromCoordinates_ShouldReturnCorrectError_IfResultIsUnsuccessful(
250-
HttpStatusCode statusCode, Error expectedError
256+
HttpStatusCode statusCode
251257
)
252258
{
253259
// Arrange
@@ -256,14 +262,17 @@ public async Task GetLocationFromCoordinates_ShouldReturnCorrectError_IfResultIs
256262
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
257263
mockHttpClient.BaseAddress = _mapBoxUri;
258264

259-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
265+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
260266

261267
// Act
262-
var result = await mapBoxClient.GetLocationFromCoordinates(DefaultLongitude, DefaultLatitude);
268+
Results<Ok<LocationDto>, ProblemHttpResult> result =
269+
await mapBoxClient.GetLocationFromCoordinates(DefaultLongitude, DefaultLatitude);
270+
271+
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result.Result);
263272

264273
// Assert
265274
_mockHttp.Expect(MapBoxConfiguration.Url).Respond(statusCode);
266-
Assert.Equal(expectedError, result.FirstError);
275+
Assert.Equal((int)statusCode, problemResult.StatusCode);
267276
}
268277

269278
[Fact]
@@ -275,7 +284,7 @@ public async Task GetLocationFromCoordinates_ShouldThrowForbiddenAccess_IfApiSta
275284
HttpClient mockHttpClient = _mockHttp.ToHttpClient();
276285
mockHttpClient.BaseAddress = _mapBoxUri;
277286

278-
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object);
287+
var mapBoxClient = new MapBoxClient(mockHttpClient, _mapBoxOptionsMock.Object, _expectedProblems);
279288

280289
// Act
281290
await Assert.ThrowsAsync<ForbiddenAccessException>(

PetSearch.API.Tests/Clients/PetFinderClientTest.cs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System.Net;
22
using System.Net.Http.Json;
3-
using ErrorOr;
43
using JetBrains.Annotations;
4+
using Microsoft.AspNetCore.Http.HttpResults;
55
using Moq;
66
using PetSearch.API.Clients;
77
using PetSearch.API.Configurations;
88
using PetSearch.API.Entities;
9+
using PetSearch.API.Problems;
910
using PetSearch.API.Exceptions;
1011
using PetSearch.API.Helpers;
1112
using PetSearch.API.Models;
@@ -31,6 +32,7 @@ public class PetFinderClientTest
3132
private readonly PaginationMetaDataProfile _paginationMetaDataProfile;
3233
private const int Id = 0;
3334
private readonly string _expectedPetUri;
35+
private readonly IExpectedProblems _expectedProblems;
3436

3537
public PetFinderClientTest()
3638
{
@@ -47,6 +49,7 @@ public PetFinderClientTest()
4749
_petProfile = new PetProfile();
4850
_paginationMetaDataProfile = new PaginationMetaDataProfile();
4951
_expectedPetUri = $"{_petFinderUri}/{Id}";
52+
_expectedProblems = new PetFinderProblems();
5053
}
5154

5255
[Theory]
@@ -61,37 +64,40 @@ public async Task GetPets_ShouldReturnA_PagedListOfPetDto(Pet pet)
6164
PaginatedPetList paginatedPetList = new PaginatedPetList(petList, pagination);
6265
var request = SetGetPetsRequest(HttpStatusCode.Accepted, paginatedPetList);
6366
var petFinderClient =
64-
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile);
67+
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile,
68+
_expectedProblems);
6569

6670
// Action
67-
ErrorOr<PagedList<PetDto>> response = await petFinderClient.GetPetsAsync(_petsParamsMock);
71+
Results<Ok<PagedList<PetDto>>, ProblemHttpResult> result = await petFinderClient.GetPetsAsync(_petsParamsMock);
6872

73+
var response = Assert.IsType<Ok<PagedList<PetDto>>>(result.Result);
6974
// Assert
7075
Assert.IsType<PagedList<PetDto>>(response.Value);
7176
Assert.Equal(1, _mockHttp.GetMatchCount(request));
7277
}
7378

7479
[Theory]
75-
[ClassData(typeof(PetFinderClientErrorsData))]
80+
[ClassData(typeof(PetFinderClientProblemsData))]
7681
public async Task GetPets_ShouldReturnCorrectError_IfResponseIsUnsuccessful(
77-
HttpStatusCode statusCode, Error expectedError
82+
HttpStatusCode statusCode
7883
)
7984
{
8085
// Arrange
8186
using HttpClient httpClient = _mockHttp.ToHttpClient();
8287
httpClient.BaseAddress = _petFinderUri;
8388
var request = SetGetPetsRequest(statusCode);
8489
var petFinderClient =
85-
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile);
90+
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile,
91+
_expectedProblems);
8692

8793
// Act
88-
ErrorOr<PagedList<PetDto>> response = await petFinderClient.GetPetsAsync(_petsParamsMock);
89-
94+
Results<Ok<PagedList<PetDto>>, ProblemHttpResult> result = await petFinderClient.GetPetsAsync(_petsParamsMock);
95+
ProblemHttpResult problemHttpResult = Assert.IsType<ProblemHttpResult>(result.Result);
9096

9197
// Assert
9298
Assert.Equal(1, _mockHttp.GetMatchCount(request));
9399
_mockHttp.Expect(MapBoxConfiguration.Url).Respond(statusCode);
94-
Assert.Equal(expectedError, response.FirstError);
100+
Assert.Equal((int)statusCode, problemHttpResult.StatusCode);
95101
}
96102

97103
[Fact]
@@ -105,7 +111,8 @@ public async Task GetPets_ShouldThrowForbiddenAccess_IfApiStatusCodeIs403()
105111
PaginatedPetList paginatedPetList = new PaginatedPetList(petList, pagination);
106112
SetGetPetsRequest(HttpStatusCode.Forbidden, paginatedPetList);
107113
var petFinderClient =
108-
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile);
114+
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile,
115+
_expectedProblems);
109116

110117
// Act and Assert
111118
await Assert.ThrowsAsync<ForbiddenAccessException>(async () =>
@@ -122,35 +129,38 @@ public async Task GetSinglePet_ShouldReturnA_PetDto(Pet pet)
122129
SetGetSinglePetRequest(Id, HttpStatusCode.Accepted, pet);
123130

124131
var petFinderClient =
125-
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile);
132+
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile,
133+
_expectedProblems);
126134

127-
ErrorOr<PetDto> result = await petFinderClient.GetSinglePetAsync(Id);
135+
Results<Ok<PetDto>, ProblemHttpResult> response = await petFinderClient.GetSinglePetAsync(Id);
136+
var result = Assert.IsType<Ok<PetDto>>(response.Result);
128137

129138
Assert.IsType<PetDto>(result.Value);
130139
_mockHttp.Expect(_expectedPetUri).Respond(HttpStatusCode.Accepted);
131140
}
132141

133142
[Theory]
134-
[ClassData(typeof(PetFinderClientErrorsData))]
143+
[ClassData(typeof(PetFinderClientProblemsData))]
135144
public async Task GetSinglePet_ShouldReturnCorrectError_IfResponseIsUnsuccessful(
136-
HttpStatusCode statusCode, Error expectedError
145+
HttpStatusCode statusCode
137146
)
138147
{
139148
using HttpClient httpClient = _mockHttp.ToHttpClient();
140149
httpClient.BaseAddress = _petFinderUri;
141150
SetGetSinglePetRequest(Id, statusCode, null);
142151

143152
var petFinderClient =
144-
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile);
153+
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile,
154+
_expectedProblems);
145155

146-
ErrorOr<PetDto> result = await petFinderClient.GetSinglePetAsync(0);
156+
Results<Ok<PetDto>, ProblemHttpResult> result = await petFinderClient.GetSinglePetAsync(0);
157+
ProblemHttpResult problemHttpResult = Assert.IsType<ProblemHttpResult>(result.Result);
147158

148159
// Assert
149160
_mockHttp.Expect(_expectedPetUri).Respond(statusCode);
150-
Assert.Equal(expectedError, result.FirstError);
161+
Assert.Equal((int)statusCode, problemHttpResult.StatusCode);
151162
}
152163

153-
154164
[Fact]
155165
public async Task GetSinglePet_ShouldThrowForbiddenAccess_IfApiStatusCodeIs403()
156166
{
@@ -159,7 +169,8 @@ public async Task GetSinglePet_ShouldThrowForbiddenAccess_IfApiStatusCodeIs403()
159169
httpClient.BaseAddress = _petFinderUri;
160170
SetGetSinglePetRequest(Id, HttpStatusCode.Forbidden, null);
161171
var petFinderClient =
162-
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile);
172+
new PetFinderClient(httpClient, _mockTokenService.Object, _petProfile, _paginationMetaDataProfile,
173+
_expectedProblems);
163174

164175
// Act and Assert
165176
await Assert.ThrowsAsync<ForbiddenAccessException>(async () =>

PetSearch.API.Tests/Data/BaseHandlerErrorData.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

PetSearch.API.Tests/Data/MapBoxClientErrorsData.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)