Skip to content

Commit 1900ee2

Browse files
committed
Edit the integration test
1 parent 99685b3 commit 1900ee2

6 files changed

Lines changed: 175 additions & 60 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace IntegrationTests.Services.Marketing
2+
{
3+
public class CampaignScenarioBase : MarketingScenarioBase
4+
{
5+
public static class Get
6+
{
7+
public static string Campaigns = CampaignsUrlBase;
8+
9+
public static string CampaignBy(int id)
10+
=> $"{CampaignsUrlBase}/{id}";
11+
}
12+
13+
public static class Post
14+
{
15+
public static string AddNewCampaign = CampaignsUrlBase;
16+
}
17+
18+
public static class Put
19+
{
20+
public static string CampaignBy(int id)
21+
=> $"{CampaignsUrlBase}/{id}";
22+
}
23+
24+
public static class Delete
25+
{
26+
public static string CampaignBy(int id)
27+
=> $"{CampaignsUrlBase}/{id}";
28+
}
29+
}
30+
}

test/Services/IntegrationTests/Services/Marketing/MarketingScenarios.cs renamed to test/Services/IntegrationTests/Services/Marketing/CampaignScenarios.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
using Xunit;
77
using System;
88
using Newtonsoft.Json;
9-
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
10-
using System.Collections.Generic;
119
using System.Net;
1210
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
1311

14-
public class MarketingScenarios
15-
: MarketingScenarioBase
12+
public class CampaignScenarios
13+
: CampaignScenarioBase
1614
{
1715
[Fact]
1816
public async Task Get_get_all_campaigns_and_response_ok_status_code()
@@ -29,10 +27,11 @@ public async Task Get_get_all_campaigns_and_response_ok_status_code()
2927
[Fact]
3028
public async Task Get_get_campaign_by_id_and_response_ok_status_code()
3129
{
30+
var campaignId = 1;
3231
using (var server = CreateServer())
3332
{
3433
var response = await server.CreateClient()
35-
.GetAsync(Get.CampaignBy(1));
34+
.GetAsync(Get.CampaignBy(campaignId));
3635

3736
response.EnsureSuccessStatusCode();
3837
}
@@ -44,7 +43,7 @@ public async Task Get_get_campaign_by_id_and_response_not_found_status_code()
4443
using (var server = CreateServer())
4544
{
4645
var response = await server.CreateClient()
47-
.GetAsync(Get.CampaignBy(9999999));
46+
.GetAsync(Get.CampaignBy(int.MaxValue));
4847

4948
Assert.True(response.StatusCode == HttpStatusCode.NotFound);
5049
}
@@ -122,15 +121,6 @@ private static CampaignDTO GetFakeCampaignDto()
122121
From = DateTime.Now,
123122
To = DateTime.Now.AddDays(7),
124123
Url = "http://CampaignUrl.test/fdaf91ad0cef5419719f50198",
125-
Rules = new List<RuleDTO>
126-
{
127-
new RuleDTO
128-
{
129-
LocationId = 1,
130-
Description = "testDescription",
131-
RuleTypeId = 3,
132-
}
133-
}
134124
};
135125
}
136126
}

test/Services/IntegrationTests/Services/Marketing/MarketingScenarioBase.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace IntegrationTests.Services.Marketing
2+
{
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.TestHost;
5+
using System.IO;
6+
7+
public class MarketingScenarioBase
8+
{
9+
public static string CampaignsUrlBase => "api/v1/campaigns";
10+
11+
public TestServer CreateServer()
12+
{
13+
var webHostBuilder = new WebHostBuilder();
14+
webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory() + "\\Services\\Marketing");
15+
webHostBuilder.UseStartup<MarketingTestsStartup>();
16+
17+
return new TestServer(webHostBuilder);
18+
}
19+
}
20+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
namespace IntegrationTests.Services.Marketing
2+
{
3+
using System.Net.Http;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using System;
8+
using Newtonsoft.Json;
9+
using System.Net;
10+
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
11+
12+
public class UserLocationRoleScenarios
13+
: UserLocationRoleScenariosBase
14+
{
15+
[Fact]
16+
public async Task Get_get_all_user_location_rules_by_campaignId_and_response_ok_status_code()
17+
{
18+
var campaignId = 1;
19+
20+
using (var server = CreateServer())
21+
{
22+
var response = await server.CreateClient()
23+
.GetAsync(Get.UserLocationRulesByCampaignId(campaignId));
24+
25+
response.EnsureSuccessStatusCode();
26+
}
27+
}
28+
29+
[Fact]
30+
public async Task Post_add_new_user_location_rule_and_response_ok_status_code()
31+
{
32+
var campaignId = 1;
33+
34+
using (var server = CreateServer())
35+
{
36+
var fakeCampaignDto = GetFakeUserLocationRuleDto();
37+
var content = new StringContent(JsonConvert.SerializeObject(fakeCampaignDto), Encoding.UTF8, "application/json");
38+
var response = await server.CreateClient()
39+
.PostAsync(Post.AddNewuserLocationRule(campaignId), content);
40+
41+
response.EnsureSuccessStatusCode();
42+
}
43+
}
44+
45+
[Fact]
46+
public async Task Delete_delete_user_location_role_and_response_not_content_status_code()
47+
{
48+
var campaignId = 1;
49+
50+
using (var server = CreateServer())
51+
{
52+
var fakeCampaignDto = GetFakeUserLocationRuleDto();
53+
var content = new StringContent(JsonConvert.SerializeObject(fakeCampaignDto), Encoding.UTF8, "application/json");
54+
55+
//add user location role
56+
var campaignResponse = await server.CreateClient()
57+
.PostAsync(Post.AddNewuserLocationRule(campaignId), content);
58+
59+
if (int.TryParse(campaignResponse.Headers.Location.Segments[6], out int userLocationRuleId))
60+
{
61+
var response = await server.CreateClient()
62+
.DeleteAsync(Delete.UserLocationRoleBy(campaignId, userLocationRuleId));
63+
64+
Assert.True(response.StatusCode == HttpStatusCode.NoContent);
65+
}
66+
67+
campaignResponse.EnsureSuccessStatusCode();
68+
}
69+
}
70+
71+
private static UserLocationRuleDTO GetFakeUserLocationRuleDto()
72+
{
73+
return new UserLocationRuleDTO
74+
{
75+
LocationId = 20,
76+
Description = "FakeUserLocationRuleDescription"
77+
};
78+
}
79+
}
80+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace IntegrationTests.Services.Marketing
2+
{
3+
public class UserLocationRoleScenariosBase : MarketingScenarioBase
4+
{
5+
private const string EndpointLocationName = "locations";
6+
public static class Get
7+
{
8+
public static string UserLocationRulesByCampaignId(int campaignId)
9+
=> GetUserLocationRolesUrlBase(campaignId);
10+
11+
public static string UserLocationRuleByCampaignAndUserLocationRuleId(int campaignId,
12+
int userLocationRuleId)
13+
=> $"{GetUserLocationRolesUrlBase(campaignId)}/{userLocationRuleId}";
14+
}
15+
16+
public static class Post
17+
{
18+
public static string AddNewuserLocationRule(int campaignId)
19+
=> GetUserLocationRolesUrlBase(campaignId);
20+
}
21+
22+
public static class Put
23+
{
24+
public static string UserLocationRoleBy(int campaignId,
25+
int userLocationRuleId)
26+
=> $"{GetUserLocationRolesUrlBase(campaignId)}/{userLocationRuleId}";
27+
}
28+
29+
public static class Delete
30+
{
31+
public static string UserLocationRoleBy(int campaignId,
32+
int userLocationRuleId)
33+
=> $"{GetUserLocationRolesUrlBase(campaignId)}/{userLocationRuleId}";
34+
}
35+
36+
37+
private static string GetUserLocationRolesUrlBase(int campaignId)
38+
=> $"{CampaignsUrlBase}/{campaignId}/{EndpointLocationName}";
39+
}
40+
}

0 commit comments

Comments
 (0)