1- //// MIT-License
2- //// Copyright BridgingIT GmbH - All Rights Reserved
3- //// Use of this source code is governed by an MIT-style license that can be
4- //// found in the LICENSE file at https://github.com/bridgingit/bitdevkit/license
5-
6- //namespace BridgingIT.DevKit.Examples.GettingStarted.Modules.CoreModule.IntegrationTests.Presentation.Web;
7-
8- //using System.Text;
9- //using System.Text.Json;
10- //using Microsoft.VisualStudio.TestPlatform.TestHost;
11-
12- //[IntegrationTest("GettingStarted.Presentation.Web")]
13- //public class CustomerEndpointTests(ITestOutputHelper output, CustomWebApplicationFactoryFixture<Program> fixture) : IClassFixture<CustomWebApplicationFactoryFixture<Program>> // https://xunit.net/docs/shared-context#class-fixture
14- //{
15- // private readonly CustomWebApplicationFactoryFixture<Program> fixture = fixture.WithOutput(output);
16-
17- // [Theory]
18- // [InlineData("api/customers")]
19- // public async Task Get_SingleExisting_ReturnsOk(string route)
20- // {
21- // // Arrange
22- // this.fixture.Output.WriteLine($"Start Endpoint test for route: {route}");
23- // var model = await this.PostCustomerCreate(route);
24-
25- // // Act
26- // var response = await this.fixture.CreateClient()
27- // .GetAsync(route + $"/{model.Id}").AnyContext();
28- // this.fixture.Output.WriteLine($"Finish Endpoint test for route: {route} (status={(int)response.StatusCode})");
29-
30- // // Assert
31- // response.Should().Be200Ok(); // https://github.com/adrianiftode/FluentAssertions.Web
32- // response.Should().MatchInContent($"*{model.FirstName}*");
33- // response.Should().MatchInContent($"*{model.LastName}*");
34- // response.Should().MatchInContent($"*{model.Email}*");
35- // var responseModel = await response.Content.ReadAsAsync<CustomerModel>();
36- // responseModel.ShouldNotBeNull();
37- // this.fixture.Output.WriteLine($"ResponseModel: {responseModel.DumpText()}");
38- // }
39-
40- // [Theory]
41- // [InlineData("api/customers")]
42- // public async Task Get_SingleNotExisting_ReturnsNotFound(string route)
43- // {
44- // // Arrange
45- // this.fixture.Output.WriteLine($"Start Endpoint test for route: {route}");
46-
47- // // Act
48- // var response = await this.fixture.CreateClient()
49- // .GetAsync(route + $"/{Guid.NewGuid()}").AnyContext();
50- // this.fixture.Output.WriteLine($"Finish Endpoint test for route: {route} (status={(int)response.StatusCode})");
51-
52- // // Assert
53- // response.Should().Be404NotFound(); // https://github.com/adrianiftode/FluentAssertions.Web
54- // }
55-
56- // [Theory]
57- // [InlineData("api/customers")]
58- // public async Task Get_MultipleExisting_ReturnsOk(string route)
59- // {
60- // // Arrange & Act
61- // this.fixture.Output.WriteLine($"Start Endpoint test for route: {route}");
62- // var model = await this.PostCustomerCreate(route);
63-
64- // var response = await this.fixture.CreateClient()
65- // .GetAsync(route).AnyContext();
66- // this.fixture.Output.WriteLine($"Finish Endpoint test for route: {route} (status={(int)response.StatusCode})");
67-
68- // // Assert
69- // response.Should().Be200Ok(); // https://github.com/adrianiftode/FluentAssertions.Web
70- // response.Should().Satisfy<ICollection<CustomerModel>>(
71- // model =>
72- // {
73- // model.ShouldNotBeNull();
74- // });
75- // response.Should().MatchInContent($"*{model.FirstName}*");
76- // response.Should().MatchInContent($"*{model.LastName}*");
77- // response.Should().MatchInContent($"*{model.Email}*");
78- // var responseModel = await response.Content.ReadAsAsync<ICollection<CustomerModel>>();
79- // responseModel.ShouldNotBeNull();
80- // this.fixture.Output.WriteLine($"ResponseModel: {responseModel.DumpText()}");
81- // }
82-
83- // [Theory]
84- // [InlineData("api/customers")]
85- // public async Task Post_ValidModel_ReturnsCreated(string route)
86- // {
87- // // Arrange
88- // this.fixture.Output.WriteLine($"Start Endpoint test for route: {route}");
89- // var ticks = DateTime.UtcNow.Ticks;
90- // var model = new CustomerModel
91- // {
92- // FirstName = $"John{ticks}",
93- // LastName = $"Doe{ticks}",
94- // Email = $"john.doe{ticks}@example.com"
95- // };
96- // var content = new StringContent(
97- // JsonSerializer.Serialize(model, DefaultSystemTextJsonSerializerOptions.Create()), Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
98-
99- // // Act
100- // var response = await this.fixture.CreateClient()
101- // .PostAsync(route, content).AnyContext();
102- // this.fixture.Output.WriteLine($"Finish Endpoint test for route: {route} (status={(int)response.StatusCode})");
103-
104- // // Assert
105- // response.Should().Be201Created(); // https://github.com/adrianiftode/FluentAssertions.Web
106- // response.Headers.Location.Should().NotBeNull();
107- // var responseModel = await response.Content.ReadAsAsync<CustomerModel>();
108- // responseModel.ShouldNotBeNull();
109- // this.fixture.Output.WriteLine($"ResponseModel: {responseModel.DumpText()}");
110- // }
111-
112- // [Theory]
113- // [InlineData("api/customers")]
114- // public async Task Post_InvalidEntity_ReturnsBadRequest(string route)
115- // {
116- // // Arrange
117- // this.fixture.Output.WriteLine($"Start Endpoint test for route: {route}");
118- // var model = new CustomerModel
119- // {
120- // FirstName = string.Empty,
121- // LastName = string.Empty,
122- // Email = string.Empty
123- // };
124- // var content = new StringContent(
125- // JsonSerializer.Serialize(model, DefaultSystemTextJsonSerializerOptions.Create()), Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
126-
127- // // Act
128- // var response = await this.fixture.CreateClient()
129- // .PostAsync(route, content).AnyContext();
130- // this.fixture.Output.WriteLine($"Finish Endpoint test for route: {route} (status={(int)response.StatusCode})");
131-
132- // // Assert
133- // response.Should().Be400BadRequest(); // https://github.com/adrianiftode/FluentAssertions.Web
134- // response.Should().MatchInContent($"*[ValidationException]*");
135- // response.Should().MatchInContent($"*{nameof(model.FirstName)}*");
136- // response.Should().MatchInContent($"*{nameof(model.LastName)}*");
137- // response.Should().MatchInContent($"*{nameof(model.Email)}*");
138- // }
139-
140- // [Theory]
141- // [InlineData("api/customers")]
142- // public async Task Put_ValidModel_ReturnsOk(string route)
143- // {
144- // // Arrange
145- // this.fixture.Output.WriteLine($"Start Endpoint test for route: {route}");
146- // var model = await this.PostCustomerCreate(route);
147- // model.FirstName += "changed";
148- // model.LastName += "changed";
149- // var content = new StringContent(
150- // JsonSerializer.Serialize(model, DefaultSystemTextJsonSerializerOptions.Create()), Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
151-
152- // // Act
153- // var response = await this.fixture.CreateClient()
154- // .PutAsync(route + $"/{model.Id}", content).AnyContext();
155- // this.fixture.Output.WriteLine($"Finish Endpoint test for route: {route} (status={(int)response.StatusCode})");
156-
157- // // Assert
158- // response.Should().Be200Ok(); // https://github.com/adrianiftode/FluentAssertions.Web
159- // response.Headers.Location.Should().NotBeNull();
160- // response.Should().MatchInContent($"*{model.FirstName}*");
161- // response.Should().MatchInContent($"*{model.LastName}*");
162- // var responseModel = await response.Content.ReadAsAsync<CustomerModel>();
163- // responseModel.ShouldNotBeNull();
164- // this.fixture.Output.WriteLine($"ResponseModel: {responseModel.DumpText()}");
165- // }
166-
167- // private async Task<CustomerModel> PostCustomerCreate(string route)
168- // {
169- // var ticks = DateTime.UtcNow.Ticks;
170- // var model = new CustomerModel
171- // {
172- // FirstName = $"John{ticks}",
173- // LastName = $"Doe{ticks}",
174- // Email = $"john.doe{ticks}@example.com"
175- // };
176- // var content = new StringContent(
177- // JsonSerializer.Serialize(model, DefaultSystemTextJsonSerializerOptions.Create()), Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
178- // var response = await this.fixture.CreateClient()
179- // .PostAsync(route, content).AnyContext();
180- // response.EnsureSuccessStatusCode();
181-
182- // return await response.Content.ReadAsAsync<CustomerModel>();
183- // }
184- //}
1+ // MIT-License
2+ // Copyright BridgingIT GmbH - All Rights Reserved
3+ // Use of this source code is governed by an MIT-style license that can be
4+ // found in the LICENSE file at https://github.com/bridgingit/bitdevkit/license
5+
6+ namespace BridgingIT . DevKit . Examples . GettingStarted . Modules . CoreModule . IntegrationTests . Presentation . Web ;
7+
8+ using BridgingIT . DevKit . Examples . GettingStarted . Modules . CoreModule . Application ;
9+ using System . Text ;
10+ using System . Text . Json ;
11+
12+ [ IntegrationTest ( "GettingStarted.Presentation.Web" ) ]
13+ public class CustomerEndpointTests ( ITestOutputHelper output , CustomWebApplicationFactoryFixture < GettingStarted . Presentation . Web . Server . Program > fixture )
14+ : IClassFixture < CustomWebApplicationFactoryFixture < GettingStarted . Presentation . Web . Server . Program > > // https://xunit.net/docs/shared-context#class-fixture
15+ {
16+ private readonly CustomWebApplicationFactoryFixture < GettingStarted . Presentation . Web . Server . Program > fixture = fixture . WithOutput ( output ) ;
17+
18+ [ Theory ]
19+ [ InlineData ( "api/core/customers" ) ]
20+ public async Task Get_SingleExisting_ReturnsOk ( string route )
21+ {
22+ // Arrange
23+ this . fixture . Output . WriteLine ( $ "Start Endpoint test for route: { route } ") ;
24+ var model = await this . PostCustomerCreate ( route ) ;
25+
26+ // Act
27+ var response = await this . fixture . CreateClient ( )
28+ . GetAsync ( route + $ "/{ model . Id } ") . AnyContext ( ) ;
29+ this . fixture . Output . WriteLine ( $ "Finish Endpoint test for route: { route } (status={ ( int ) response . StatusCode } )") ;
30+
31+ // Assert
32+ response . Should ( ) . Be200Ok ( ) ; // https://github.com/adrianiftode/FluentAssertions.Web
33+ response . Should ( ) . MatchInContent ( $ "*{ model . FirstName } *") ;
34+ response . Should ( ) . MatchInContent ( $ "*{ model . LastName } *") ;
35+ response . Should ( ) . MatchInContent ( $ "*{ model . Email } *") ;
36+ var responseModel = await response . Content . ReadAsAsync < CustomerModel > ( ) ;
37+ responseModel . ShouldNotBeNull ( ) ;
38+ this . fixture . Output . WriteLine ( $ "ResponseModel: { responseModel . DumpText ( ) } ") ;
39+ }
40+
41+ [ Theory ]
42+ [ InlineData ( "api/core/customers" ) ]
43+ public async Task Get_SingleNotExisting_ReturnsNotFound ( string route )
44+ {
45+ // Arrange
46+ this . fixture . Output . WriteLine ( $ "Start Endpoint test for route: { route } ") ;
47+
48+ // Act
49+ var response = await this . fixture . CreateClient ( )
50+ . GetAsync ( route + $ "/{ Guid . NewGuid ( ) } ") . AnyContext ( ) ;
51+ this . fixture . Output . WriteLine ( $ "Finish Endpoint test for route: { route } (status={ ( int ) response . StatusCode } )") ;
52+
53+ // Assert
54+ response . Should ( ) . Be404NotFound ( ) ; // https://github.com/adrianiftode/FluentAssertions.Web
55+ }
56+
57+ [ Theory ]
58+ [ InlineData ( "api/core/customers" ) ]
59+ public async Task Get_MultipleExisting_ReturnsOk ( string route )
60+ {
61+ // Arrange & Act
62+ this . fixture . Output . WriteLine ( $ "Start Endpoint test for route: { route } ") ;
63+ var model = await this . PostCustomerCreate ( route ) ;
64+
65+ var response = await this . fixture . CreateClient ( )
66+ . GetAsync ( route ) . AnyContext ( ) ;
67+ this . fixture . Output . WriteLine ( $ "Finish Endpoint test for route: { route } (status={ ( int ) response . StatusCode } )") ;
68+
69+ // Assert
70+ response . Should ( ) . Be200Ok ( ) ; // https://github.com/adrianiftode/FluentAssertions.Web
71+ response . Should ( ) . Satisfy < ICollection < CustomerModel > > ( model => model . ShouldNotBeNull ( ) ) ;
72+ response . Should ( ) . MatchInContent ( $ "*{ model . FirstName } *") ;
73+ response . Should ( ) . MatchInContent ( $ "*{ model . LastName } *") ;
74+ response . Should ( ) . MatchInContent ( $ "*{ model . Email } *") ;
75+ var responseModel = await response . Content . ReadAsAsync < ICollection < CustomerModel > > ( ) ;
76+ responseModel . ShouldNotBeNull ( ) ;
77+ this . fixture . Output . WriteLine ( $ "ResponseModel: { responseModel . DumpText ( ) } ") ;
78+ }
79+
80+ [ Theory ]
81+ [ InlineData ( "api/core/customers" ) ]
82+ public async Task Post_ValidModel_ReturnsCreated ( string route )
83+ {
84+ // Arrange
85+ this . fixture . Output . WriteLine ( $ "Start Endpoint test for route: { route } ") ;
86+ var ticks = DateTime . UtcNow . Ticks ;
87+ var model = new CustomerModel
88+ {
89+ FirstName = $ "John{ ticks } ",
90+ LastName = $ "Doe{ ticks } ",
91+ Email = $ "john.doe{ ticks } @example.com"
92+ } ;
93+ var content = new StringContent (
94+ JsonSerializer . Serialize ( model , DefaultSystemTextJsonSerializerOptions . Create ( ) ) , Encoding . UTF8 , System . Net . Mime . MediaTypeNames . Application . Json ) ;
95+
96+ // Act
97+ var response = await this . fixture . CreateClient ( )
98+ . PostAsync ( route , content ) . AnyContext ( ) ;
99+ this . fixture . Output . WriteLine ( $ "Finish Endpoint test for route: { route } (status={ ( int ) response . StatusCode } )") ;
100+
101+ // Assert
102+ response . Should ( ) . Be201Created ( ) ; // https://github.com/adrianiftode/FluentAssertions.Web
103+ response . Headers . Location . Should ( ) . NotBeNull ( ) ;
104+ var responseModel = await response . Content . ReadAsAsync < CustomerModel > ( ) ;
105+ responseModel . ShouldNotBeNull ( ) ;
106+ this . fixture . Output . WriteLine ( $ "ResponseModel: { responseModel . DumpText ( ) } ") ;
107+ }
108+
109+ [ Theory ]
110+ [ InlineData ( "api/core/customers" ) ]
111+ public async Task Post_InvalidEntity_ReturnsBadRequest ( string route )
112+ {
113+ // Arrange
114+ this . fixture . Output . WriteLine ( $ "Start Endpoint test for route: { route } ") ;
115+ var model = new CustomerModel
116+ {
117+ FirstName = string . Empty ,
118+ LastName = string . Empty ,
119+ Email = string . Empty
120+ } ;
121+ var content = new StringContent (
122+ JsonSerializer . Serialize ( model , DefaultSystemTextJsonSerializerOptions . Create ( ) ) , Encoding . UTF8 , System . Net . Mime . MediaTypeNames . Application . Json ) ;
123+
124+ // Act
125+ var response = await this . fixture . CreateClient ( )
126+ . PostAsync ( route , content ) . AnyContext ( ) ;
127+ this . fixture . Output . WriteLine ( $ "Finish Endpoint test for route: { route } (status={ ( int ) response . StatusCode } )") ;
128+
129+ // Assert
130+ response . Should ( ) . Be400BadRequest ( ) ; // https://github.com/adrianiftode/FluentAssertions.Web
131+ response . Should ( ) . MatchInContent ( $ "*[ValidationException]*") ;
132+ response . Should ( ) . MatchInContent ( $ "*{ nameof ( model . FirstName ) } *") ;
133+ response . Should ( ) . MatchInContent ( $ "*{ nameof ( model . LastName ) } *") ;
134+ response . Should ( ) . MatchInContent ( $ "*{ nameof ( model . Email ) } *") ;
135+ }
136+
137+ [ Theory ]
138+ [ InlineData ( "api/core/customers" ) ]
139+ public async Task Put_ValidModel_ReturnsOk ( string route )
140+ {
141+ // Arrange
142+ this . fixture . Output . WriteLine ( $ "Start Endpoint test for route: { route } ") ;
143+ var model = await this . PostCustomerCreate ( route ) ;
144+ model . FirstName += "changed" ;
145+ model . LastName += "changed" ;
146+ var content = new StringContent (
147+ JsonSerializer . Serialize ( model , DefaultSystemTextJsonSerializerOptions . Create ( ) ) , Encoding . UTF8 , System . Net . Mime . MediaTypeNames . Application . Json ) ;
148+
149+ // Act
150+ var response = await this . fixture . CreateClient ( )
151+ . PutAsync ( route + $ "/{ model . Id } ", content ) . AnyContext ( ) ;
152+ this . fixture . Output . WriteLine ( $ "Finish Endpoint test for route: { route } (status={ ( int ) response . StatusCode } )") ;
153+
154+ // Assert
155+ response . Should ( ) . Be200Ok ( ) ; // https://github.com/adrianiftode/FluentAssertions.Web
156+ response . Headers . Location . Should ( ) . NotBeNull ( ) ;
157+ response . Should ( ) . MatchInContent ( $ "*{ model . FirstName } *") ;
158+ response . Should ( ) . MatchInContent ( $ "*{ model . LastName } *") ;
159+ var responseModel = await response . Content . ReadAsAsync < CustomerModel > ( ) ;
160+ responseModel . ShouldNotBeNull ( ) ;
161+ this . fixture . Output . WriteLine ( $ "ResponseModel: { responseModel . DumpText ( ) } ") ;
162+ }
163+
164+ private async Task < CustomerModel > PostCustomerCreate ( string route )
165+ {
166+ var ticks = DateTime . UtcNow . Ticks ;
167+ var model = new CustomerModel
168+ {
169+ FirstName = $ "John{ ticks } ",
170+ LastName = $ "Doe{ ticks } ",
171+ Email = $ "john.doe{ ticks } @example.com"
172+ } ;
173+ var content = new StringContent (
174+ JsonSerializer . Serialize ( model , DefaultSystemTextJsonSerializerOptions . Create ( ) ) , Encoding . UTF8 , System . Net . Mime . MediaTypeNames . Application . Json ) ;
175+ var response = await this . fixture . CreateClient ( )
176+ . PostAsync ( route , content ) . AnyContext ( ) ;
177+ response . EnsureSuccessStatusCode ( ) ;
178+
179+ return await response . Content . ReadAsAsync < CustomerModel > ( ) ;
180+ }
181+ }
0 commit comments