Skip to content

Commit 8eb4cef

Browse files
authored
Added example to display error of AppendHttpCallStub when there are query params with curly brackets
* Added complex parameter logic as example in controller, service and proxy. * Re-added previously overwritten test * Separated tests to different files
1 parent 6fe5798 commit 8eb4cef

8 files changed

Lines changed: 190 additions & 3 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace MovieProject.Logic.DTO
2+
{
3+
using System.Text.Json.Serialization;
4+
5+
namespace MovieProject.Logic.Proxy.DTO
6+
{
7+
8+
public class UserSearchModel
9+
{
10+
[JsonPropertyName("name")]
11+
public string Name { get; set; }
12+
13+
[JsonPropertyName("username")]
14+
public string Username { get; set; }
15+
16+
[JsonPropertyName("email")]
17+
public string Email { get; set; }
18+
19+
[JsonPropertyName("phone")]
20+
public string Phone { get; set; }
21+
22+
[JsonPropertyName("website")]
23+
public string Website { get; set; }
24+
}
25+
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using MovieProject.Logic.Proxy.DTO;
2+
3+
namespace MovieProject.Logic.Extensions
4+
{
5+
public static class UserMapperExtensions
6+
{
7+
public static UserSearchModel MapUserSearchModelDtoToProxyDto(
8+
this DTO.MovieProject.Logic.Proxy.DTO.UserSearchModel searchModel)
9+
{
10+
return new UserSearchModel
11+
{
12+
Name = searchModel.Name,
13+
Username = searchModel.Username,
14+
Email = searchModel.Email,
15+
Phone = searchModel.Phone,
16+
Website = searchModel.Website
17+
};
18+
}
19+
}
20+
}

Examples/MovieProject/MovieProject.Logic/MovieProject.Logic.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
<WCFMetadata Include="Connected Services" />
2323
</ItemGroup>
2424

25+
<ItemGroup>
26+
<Folder Include="Models\" />
27+
</ItemGroup>
28+
2529
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace MovieProject.Logic.Proxy.DTO
4+
{
5+
6+
public class UserSearchModel
7+
{
8+
[JsonPropertyName("name")]
9+
public string Name { get; set; }
10+
11+
[JsonPropertyName("username")]
12+
public string Username { get; set; }
13+
14+
[JsonPropertyName("email")]
15+
public string Email { get; set; }
16+
17+
[JsonPropertyName("phone")]
18+
public string Phone { get; set; }
19+
20+
[JsonPropertyName("website")]
21+
public string Website { get; set; }
22+
}
23+
24+
}

Examples/MovieProject/MovieProject.Logic/Proxy/UserProxy.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
using System;
33
using System.Net;
44
using System.Net.Http;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
57
using System.Threading.Tasks;
8+
using MovieProject.Logic.Proxy.DTO;
69

710
namespace MovieProject.Logic.Proxy
811
{
912
public interface IUserProxy
1013
{
1114
Task<DTO.User[]> GetUsers();
15+
Task<DTO.User[]> GetSearchUsers(UserSearchModel searchModel);
1216
}
1317

1418
public class UserProxy : BaseProxy, IUserProxy
@@ -36,5 +40,21 @@ public UserProxy(HttpClient client,
3640

3741
return result;
3842
}
43+
44+
public async Task<DTO.User[]> GetSearchUsers(UserSearchModel searchModel)
45+
{
46+
string serialisedQueryParameter = JsonSerializer.Serialize(searchModel);
47+
string route = $"searchUsers?userSearchModel={serialisedQueryParameter}";
48+
49+
var result = await Send(HttpMethod.Get, route, (DTO.User[] users, HttpStatusCode status) =>
50+
{
51+
if(status != HttpStatusCode.OK || users == null)
52+
throw new Exception("Unexpected response");
53+
54+
return users;
55+
});
56+
57+
return result;
58+
}
3959
}
4060
}

Examples/MovieProject/MovieProject.Logic/Service/UserService.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
using MovieProject.Logic.Proxy;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using System.Threading.Tasks;
4+
using MovieProject.Logic.Proxy;
5+
using MovieProject.Logic.Proxy.DTO;
56

6-
namespace MovieProject.Logic
7+
namespace MovieProject.Logic.Service
78
{
89
public interface IUserService
910
{
1011
Task<List<string>> GetUsers();
12+
Task<List<string>> GetSearchUsers(UserSearchModel searchModel);
1113
}
1214

1315
public class UserService : IUserService
@@ -25,5 +27,12 @@ public async Task<List<string>> GetUsers()
2527

2628
return users.Select(c=> c.Name).OrderBy(c=> c).ToList();
2729
}
30+
31+
public async Task<List<string>> GetSearchUsers(UserSearchModel searchModel)
32+
{
33+
var users = await _userProxy.GetSearchUsers(searchModel);
34+
35+
return users.Select(c=> c.Name).OrderBy(c=> c).ToList();
36+
}
2837
}
2938
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Collections.Generic;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using FluentAssertions;
6+
using FluentAssertions.Execution;
7+
using IsolatedTests.ComponentTestings;
8+
using MovieProject.Logic.DTO.MovieProject.Logic.Proxy.DTO;
9+
using Newtonsoft.Json;
10+
using SystemTestingTools;
11+
using Xunit;
12+
13+
namespace MovieProject.IsolatedTests.ComponentTesting
14+
{
15+
[Collection("SharedServer collection")]
16+
[Trait("Project", "User Component Tests (Happy)")]
17+
public class SearchUserHappyTests
18+
{
19+
private readonly TestServerFixture Fixture;
20+
21+
private static string Url = "https://jsonplaceholder.typicode.com/searchUsers";
22+
23+
public SearchUserHappyTests(TestServerFixture fixture)
24+
{
25+
Fixture = fixture;
26+
}
27+
28+
[Fact]
29+
public async Task When_UserSearchesWithValidParameters_Then_ReturnListProperly()
30+
{
31+
// arrange
32+
var complexParameter = new UserSearchModel
33+
{
34+
Username = "Bret",
35+
};
36+
var serialisedComplexParameters = JsonConvert.SerializeObject(complexParameter);
37+
var expectedResponse = new List<string>()
38+
{
39+
"Leanne Graham"
40+
};
41+
42+
var client = Fixture.Server.CreateClient();
43+
client.CreateSession();
44+
var response = ResponseFactory.FromFiddlerLikeResponseFile($"{Fixture.StubsFolder}/UserApi/Real_Responses/Happy/200_SearchListUsers.txt");
45+
46+
client.AppendHttpCallStub(HttpMethod.Get, new System.Uri(@$"{Url}?userSearchModel={serialisedComplexParameters}"), response);
47+
48+
// act
49+
var httpResponse = await client.GetAsync("/api/users");
50+
51+
using (new AssertionScope())
52+
{
53+
// assert logs
54+
var logs = client.GetSessionLogs();
55+
logs.Should().BeEmpty();
56+
57+
// assert outgoing
58+
var outgoingRequests = client.GetSessionOutgoingRequests();
59+
outgoingRequests.Count.Should().Be(1);
60+
outgoingRequests[0].GetEndpoint().Should().Be($"GET {Url}");
61+
outgoingRequests[0].GetHeaderValue("Referer").Should().Be(MovieProject.Logic.Constants.Website);
62+
63+
// assert return
64+
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);
65+
66+
var list = await httpResponse.ReadJsonBody<List<string>>();
67+
list.Count.Should().Be(1);
68+
list.Should().BeEquivalentTo(expectedResponse);
69+
}
70+
}
71+
}
72+
}

Examples/MovieProject/MovieProject.Web/Controllers/UserController.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using MovieProject.Logic;
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
5+
using MovieProject.Logic.DTO.MovieProject.Logic.Proxy.DTO;
6+
using MovieProject.Logic.Extensions;
7+
using MovieProject.Logic.Service;
58

69
namespace MovieProject.Web.Controllers
710
{
@@ -22,5 +25,13 @@ public async Task<List<string>> GetUsers()
2225
// second controller with separate dependency used for testing SystemTestingTools
2326
return await _userService.GetUsers();
2427
}
28+
29+
[HttpPost("searchUsers")]
30+
public async Task<List<string>> GetSearchUsers([FromBody] UserSearchModel searchModel)
31+
{
32+
var proxyDtoSearchModel = searchModel.MapUserSearchModelDtoToProxyDto();
33+
// second controller with separate dependency used for testing SystemTestingTools
34+
return await _userService.GetSearchUsers(proxyDtoSearchModel);
35+
}
2536
}
2637
}

0 commit comments

Comments
 (0)