From 21e9ca05ccab69fe8c3e81401e22b17e8d882551 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Mon, 13 May 2024 13:27:31 +0200 Subject: [PATCH] #18: Synergy.Web.Api.Testing: Sample extended Added DateTime serialization --- .../Users/Domain/User.cs | 6 ++- .../Users/UserReadModel.cs | 12 +++--- .../Contracts/swagger-v1.json | 39 +++++++++++-------- .../Users/Patterns/S02_E01_CreateUser.json | 9 +++-- .../S02_E02_GetCreatedUserByLocation.json | 11 +++--- .../Users/Patterns/S03_E01_GetUser.json | 11 +++--- .../Patterns/S04_E01_GetListOfUsers.json | 7 ++-- .../Users/UserScenario.cs | 8 ++-- .../Users/Users.md | 35 +++++++++-------- .../Users/UsersClient.cs | 6 +-- 10 files changed, 80 insertions(+), 64 deletions(-) diff --git a/Web/Sample/Synergy.Sample.Web.API.Services/Users/Domain/User.cs b/Web/Sample/Synergy.Sample.Web.API.Services/Users/Domain/User.cs index 47d97c1..d1ef053 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Services/Users/Domain/User.cs +++ b/Web/Sample/Synergy.Sample.Web.API.Services/Users/Domain/User.cs @@ -1,14 +1,18 @@ -namespace Synergy.Sample.Web.API.Services.Users.Domain +using System; + +namespace Synergy.Sample.Web.API.Services.Users.Domain { public class User { public string Id { get; } public Login Login { get; } + public DateTime CreatedOn { get; } public User(string id, Login login) { this.Id = id; this.Login = login; + this.CreatedOn = DateTime.Now; } } } diff --git a/Web/Sample/Synergy.Sample.Web.API.Services/Users/UserReadModel.cs b/Web/Sample/Synergy.Sample.Web.API.Services/Users/UserReadModel.cs index 68ca9b1..a7215d7 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Services/Users/UserReadModel.cs +++ b/Web/Sample/Synergy.Sample.Web.API.Services/Users/UserReadModel.cs @@ -1,15 +1,14 @@ -using Newtonsoft.Json; +using System; +using Newtonsoft.Json; using Synergy.Sample.Web.API.Services.Users.Domain; namespace Synergy.Sample.Web.API.Services.Users { public class UserReadModel { - [JsonProperty("id")] - public string Id { get; } - - [JsonProperty("login")] - public Login Login { get; } + [JsonProperty("id")] public string Id { get; } + [JsonProperty("login")] public Login Login { get; } + [JsonProperty("created")] public DateTime CreatedOn { get; } public UserReadModel(string id, Login login) { @@ -21,6 +20,7 @@ public UserReadModel(User user) { this.Id = user.Id; this.Login = user.Login; + this.CreatedOn = user.CreatedOn; } } } \ No newline at end of file diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Contracts/swagger-v1.json b/Web/Sample/Synergy.Sample.Web.API.Tests/Contracts/swagger-v1.json index 2d174b0..69378c1 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Contracts/swagger-v1.json +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Contracts/swagger-v1.json @@ -16,23 +16,6 @@ }, "paths": { "/api/v1/users": { - "get": { - "tags": [ - "Users" - ], - "responses": { - "200": { - "description": "Success", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/GetUsersQueryResult" - } - } - } - } - } - }, "post": { "tags": [ "Users" @@ -83,6 +66,23 @@ } } } + }, + "get": { + "tags": [ + "Users" + ], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GetUsersQueryResult" + } + } + } + } + } } }, "/api/v1/users/{userId}": { @@ -238,6 +238,11 @@ }, "login": { "$ref": "#/components/schemas/Login" + }, + "createdOn": { + "type": "string", + "format": "date-time", + "readOnly": true } }, "additionalProperties": false diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E01_CreateUser.json b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E01_CreateUser.json index 0e4fcda..df26c03 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E01_CreateUser.json +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E01_CreateUser.json @@ -22,17 +22,18 @@ "response": { "status": "201 Created", "headers": { - "Location": "http://localhost/api/v1/users/102a023150d94b1b99dbfbd5d6a695d8", + "Location": "http://localhost/api/v1/users/0dd576b44c7a40a4884cc7512272e751", "api-supported-versions": "1.0", "Content-Type": "application/json; charset=utf-8", - "Content-Length": "129" + "Content-Length": "182" }, "body": { "user": { - "id": "102a023150d94b1b99dbfbd5d6a695d8", + "id": "0dd576b44c7a40a4884cc7512272e751", "login": { "value": "marcin@synergy.com" - } + }, + "created": "2024-05-13T13:19:15.8420304+02:00" } } } diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E02_GetCreatedUserByLocation.json b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E02_GetCreatedUserByLocation.json index 8ef3399..f821c69 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E02_GetCreatedUserByLocation.json +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S02_E02_GetCreatedUserByLocation.json @@ -6,8 +6,8 @@ "Manual: User details are returned" ], "request": { - "description": "Get user located at http://localhost/api/v1/users/102a023150d94b1b99dbfbd5d6a695d8", - "method": "GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8", + "description": "Get user located at http://localhost/api/v1/users/0dd576b44c7a40a4884cc7512272e751", + "method": "GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751", "headers": { "test": "header" } @@ -17,14 +17,15 @@ "headers": { "api-supported-versions": "1.0", "Content-Type": "application/json; charset=utf-8", - "Content-Length": "129" + "Content-Length": "182" }, "body": { "user": { - "id": "102a023150d94b1b99dbfbd5d6a695d8", + "id": "0dd576b44c7a40a4884cc7512272e751", "login": { "value": "marcin@synergy.com" - } + }, + "created": "2024-05-13T13:19:15.8420304+02:00" } } } diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S03_E01_GetUser.json b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S03_E01_GetUser.json index 6f6daa8..2b45e70 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S03_E01_GetUser.json +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S03_E01_GetUser.json @@ -6,8 +6,8 @@ "Manual: User details are returned" ], "request": { - "description": "Get user with id \"102a023150d94b1b99dbfbd5d6a695d8\"", - "method": "GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8", + "description": "Get user with id \"0dd576b44c7a40a4884cc7512272e751\"", + "method": "GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751", "headers": { "test": "header" } @@ -17,14 +17,15 @@ "headers": { "api-supported-versions": "1.0", "Content-Type": "application/json; charset=utf-8", - "Content-Length": "129" + "Content-Length": "182" }, "body": { "user": { - "id": "102a023150d94b1b99dbfbd5d6a695d8", + "id": "0dd576b44c7a40a4884cc7512272e751", "login": { "value": "marcin@synergy.com" - } + }, + "created": "2024-05-13T13:19:15.8420304+02:00" } } } diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S04_E01_GetListOfUsers.json b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S04_E01_GetListOfUsers.json index 783e172..679c2d1 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S04_E01_GetListOfUsers.json +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Patterns/S04_E01_GetListOfUsers.json @@ -17,15 +17,16 @@ "headers": { "api-supported-versions": "1.0", "Content-Type": "application/json; charset=utf-8", - "Content-Length": "152" + "Content-Length": "207" }, "body": { "users": [ { - "id": "102a023150d94b1b99dbfbd5d6a695d8", + "id": "0dd576b44c7a40a4884cc7512272e751", "login": { "value": "marcin@synergy.com" - } + }, + "created": "2024-05-13T13:19:15.8420304+02:00" } ] } diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UserScenario.cs b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UserScenario.cs index 2fb7514..0925f75 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UserScenario.cs +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UserScenario.cs @@ -70,7 +70,7 @@ private string CreateUser() this.EqualToPattern("/Patterns/S02_E01_CreateUser.json") .Ignore(Ignore.RequestBody("Login")) .Ignore(Ignore.ResponseLocationHeader()) - .Ignore(Ignore.ResponseBody("user.id")) + .Ignore(Ignore.ResponseBody("user.id", "user.created")) .Expected("Manual: User is created and its details are returned")) .ReadUserId(out var id) .ReadCreatedUserLocationUrl(out var location); @@ -81,7 +81,7 @@ private string CreateUser() .ShouldBe( this.EqualToPattern("/Patterns/S02_E02_GetCreatedUserByLocation.json") .Ignore(Ignore.RequestMethod()) - .Ignore(Ignore.ResponseBody("user.id")) + .Ignore(Ignore.ResponseBody("user.id", "user.created")) .Expected("Manual: User details are returned")); return id; @@ -97,7 +97,7 @@ private void GetUser(string userId) .ShouldBe( this.EqualToPattern("/Patterns/S03_E01_GetUser.json") .Ignore(Ignore.RequestMethod()) - .Ignore(Ignore.ResponseBody("user.id")) + .Ignore(Ignore.ResponseBody("user.id", "user.created")) .Expected("Manual: User details are returned")); this.users.GetUser("user-id-that-do-not-exist") @@ -119,7 +119,7 @@ private void GetListOfUsers() .ShouldBe(ApiConventionFor.GetListOfResources()) .ShouldBe( this.EqualToPattern("/Patterns/S04_E01_GetListOfUsers.json") - .Ignore(Ignore.ResponseBody("users[*].id")) + .Ignore(Ignore.ResponseBody("users[*].id", "users[*].created")) .Expected("Manual: Users list is returned") ); } diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Users.md b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Users.md index 530565b..1607584 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Users.md +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/Users.md @@ -95,11 +95,11 @@ Content-Length: 129 ### 2.2. Get created user pointed by "Location" header (1 request) -### 2.2.2. Request to [Get user located at http://localhost/api/v1/users/2fdc2f9a03534536b1b5532a383b7cc0] +### 2.2.2. Request to [Get created user] - Request ``` -GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8 +GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751 test: header ``` @@ -108,13 +108,14 @@ test: header HTTP/1.1 200 OK api-supported-versions: 1.0 Content-Type: application/json; charset=utf-8 -Content-Length: 129 +Content-Length: 182 { "user": { - "id": "102a023150d94b1b99dbfbd5d6a695d8", + "id": "0dd576b44c7a40a4884cc7512272e751", "login": { "value": "marcin@synergy.com" - } + }, + "created": "2024-05-13T13:19:15.8420304+02:00" } } ``` @@ -137,11 +138,11 @@ Content-Length: 129 ### 3.1. Get user by id (1 request) -### 3.1.1. Request to [Get user with id "2fdc2f9a03534536b1b5532a383b7cc0"] +### 3.1.1. Request to [Get user by id] - Request ``` -GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8 +GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751 test: header ``` @@ -150,13 +151,14 @@ test: header HTTP/1.1 200 OK api-supported-versions: 1.0 Content-Type: application/json; charset=utf-8 -Content-Length: 129 +Content-Length: 182 { "user": { - "id": "102a023150d94b1b99dbfbd5d6a695d8", + "id": "0dd576b44c7a40a4884cc7512272e751", "login": { "value": "marcin@synergy.com" - } + }, + "created": "2024-05-13T13:19:15.8420304+02:00" } } ``` @@ -171,7 +173,7 @@ Content-Length: 129 ### 3.2. Negative test: Try to get user that do not exist (1 request) -### 3.2.2. Request to [Get user with id "user-id-that-do-not-exist"] +### 3.2.2. Request to [Get user by id] - Request ``` @@ -222,14 +224,15 @@ test: header HTTP/1.1 200 OK api-supported-versions: 1.0 Content-Type: application/json; charset=utf-8 -Content-Length: 152 +Content-Length: 207 { "users": [ { - "id": "102a023150d94b1b99dbfbd5d6a695d8", + "id": "0dd576b44c7a40a4884cc7512272e751", "login": { "value": "marcin@synergy.com" - } + }, + "created": "2024-05-13T13:19:15.8420304+02:00" } ] } @@ -391,7 +394,7 @@ Content-Length: 351 ### 6.1. Delete user by id (1 request) -### 6.1.1. Request to [Delete user with id "2fdc2f9a03534536b1b5532a383b7cc0"] +### 6.1.1. Request to [Delete user by id] - Request ``` @@ -418,7 +421,7 @@ Content-Length: 2 ### 6.2. Try to get the deleted user (1 request) -### 6.2.2. Request to [Get user with id "2fdc2f9a03534536b1b5532a383b7cc0"] +### 6.2.2. Request to [Get user by id] - Request ``` diff --git a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UsersClient.cs b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UsersClient.cs index d22061a..2957540 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UsersClient.cs +++ b/Web/Sample/Synergy.Sample.Web.API.Tests/Users/UsersClient.cs @@ -23,11 +23,11 @@ public HttpOperation GetAll() public HttpOperation GetUserBy(Uri location) => this._testServer.Get(location.ToString()) - .Details($"Get user located at {location}"); + .Details($"Get created user"); public HttpOperation GetUser(string userId) => this._testServer.Get($"{UsersClient.Path}/{userId}") - .Details($"Get user with id {userId.QuoteOrNull()}"); + .Details($"Get user by id"); public CreateUserOperation Create(string login) => this._testServer.Post(UsersClient.Path, body: JObject.Parse($"{{login:{login.QuoteOrNull()}}}")) @@ -39,7 +39,7 @@ public CreateUserOperation Create(Login login) public HttpOperation DeleteUser(string userId) => this._testServer.Delete($"{UsersClient.Path}/{userId}") - .Details($"Delete user with id {userId.QuoteOrNull()}"); + .Details($"Delete user by id"); public class CreateUserOperation : HttpOperation {