Skip to content

Commit 1d88de2

Browse files
authored
Merge pull request #27 from SMan0103/IntergrationTest
Intergration test
2 parents f41b61a + 778350d commit 1d88de2

18 files changed

+580
-219
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<coverage line-rate="0" branch-rate="0" version="1.9" timestamp="1763079159" lines-covered="0" lines-valid="0" branches-covered="0" branches-valid="0">
3+
<sources />
4+
<packages />
5+
</coverage>

backend/src/api/services/ClubService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public ClubService(HttpClient httpClient, IConfiguration config)
2525
_httpClient = httpClient;
2626
_config = config;
2727
_apiService = new APIService(_httpClient, _config);
28+
29+
// Initialize _url from configuration
30+
_url = config["API_URL"] ?? config["hightlyFootballURL"]
31+
?? throw new InvalidOperationException("Football API URL not found in configuration");
2832
}
2933

3034

@@ -367,7 +371,11 @@ public async Task<ClubModel> GetClubInfoById(string clubId)
367371
private bool IsYouthOrReserveTeam(string clubName)
368372
{
369373
string lowerName = clubName.ToLower();
370-
return lowerName.Contains(" b") ||
374+
// Check for B teams - but only if " B" appears at the end or is followed by a space
375+
// to avoid false positives like "FC Barcelona"
376+
bool isBTeam = lowerName.EndsWith(" b") || lowerName.Contains(" b ") || lowerName.EndsWith("-b") || lowerName.Contains("-b ");
377+
378+
return isBTeam ||
371379
lowerName.Contains("u17") ||
372380
lowerName.Contains("u19") ||
373381
lowerName.Contains("u21") ||

backend/src/api/services/FootballPlayerService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public FootballPlayerService(HttpClient httpClient, IConfiguration config)
3737
_config = config;
3838
_apiService = new APIService(_httpClient, _config);
3939
_helper = new JsonHelperService();
40+
41+
// Initialize _url from configuration
42+
_url = config["API_URL"] ?? config["hightlyFootballURL"]
43+
?? throw new InvalidOperationException("Football API URL not found in configuration");
4044
}
4145

4246

backend/src/api/services/LeagueService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public LeagueService(HttpClient httpClient, IConfiguration config)
2525
_config = config;
2626
_apiService = new APIService(_httpClient, _config);
2727
_helper = new JsonHelperService();
28+
29+
// Initialize _url from configuration
30+
_url = config["API_URL"] ?? config["hightlyFootballURL"]
31+
?? throw new InvalidOperationException("Football API URL not found in configuration");
2832
}
2933

3034

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using backend.api.services.helper.FootballPlayer;
2+
using backend.db.Models.Player;
3+
using backend.db.Models.TransferHistoryResponse;
4+
using Microsoft.Extensions.Configuration;
5+
6+
public class HightlyFootballServiceTest
7+
{
8+
private readonly HttpClient _httpClient;
9+
private readonly string _apiKey;
10+
private readonly string _url;
11+
12+
public HightlyFootballServiceTest(HttpClient httpClient, IConfiguration config)
13+
{
14+
_httpClient = httpClient;
15+
// Prefer environment-specific keys, fall back to alternate names, and throw if none found
16+
_apiKey = config["API_FOOTBALL_KEY"] ?? config["FootballApiKey"]
17+
?? throw new InvalidOperationException("Football API key not found in configuration");
18+
19+
_url = config["API_URL"] ?? config["hightlyFootballURL"]
20+
?? throw new InvalidOperationException("Football API URL not found in configuration");
21+
}
22+
23+
public async Task TestPlayerStats()
24+
{
25+
26+
// Setup configuration
27+
IConfigurationRoot configuration = new ConfigurationBuilder()
28+
.AddEnvironmentVariables()
29+
.Build();
30+
31+
// Create service
32+
HttpClient httpClient = new();
33+
FootballPlayerService service = new FootballPlayerService(httpClient, configuration);
34+
35+
try
36+
{
37+
int playerID = 33481;
38+
Console.WriteLine($"� Testing API connection with player id: {playerID}");
39+
Console.WriteLine("� Making API call...");
40+
41+
// Get player statistics which includes league info
42+
PlayerModel player = await service.GetPlayerStatistics(playerID);
43+
44+
Console.WriteLine("\n✅ Player Information Retrieved:");
45+
Console.WriteLine($"📝 Name: {player.Name}");
46+
Console.WriteLine($"🎂 Age: {player.Age}");
47+
Console.WriteLine($"📏 Height: {player.Height}");
48+
Console.WriteLine($"🌍 Nationality: {player.Nationality}");
49+
Console.WriteLine($"💰 Market Value: {player.MarketValue}");
50+
Console.WriteLine($"⚽ Position: {player.Position}");
51+
52+
Console.WriteLine("\n🏢 Club Information:");
53+
Console.WriteLine($" Name: {player.Club.clubName}");
54+
Console.WriteLine($" ID: {player.Club.clubID}");
55+
Console.WriteLine($" Logo URL: {player.Club.clubLogo}");
56+
57+
Console.WriteLine("\n🏆 League Information:");
58+
Console.WriteLine($" Name: {player.League.leagueName}");
59+
Console.WriteLine($" ID: {player.League.leagueID}");
60+
Console.WriteLine($" Logo URL: {player.League.leagueLogo}");
61+
62+
Console.WriteLine("Transfer History:");
63+
TransferHistoryResponseModel transferHistory = await service.GetPlayerTransferHistory(playerID);
64+
foreach (var transfer in transferHistory.Transfers)
65+
{
66+
Console.WriteLine($"{transfer.ToClub.Name}) {transfer.ToClub.Logo}");
67+
}
68+
69+
Console.WriteLine("\n================================================");
70+
Console.WriteLine("✅ Test completed successfully!");
71+
}
72+
catch (Exception ex)
73+
{
74+
Console.WriteLine($"❌ Test Failed: {ex.Message}");
75+
Console.WriteLine($"🔍 Stack Trace: {ex.StackTrace}");
76+
77+
// Check if API key is being loaded
78+
string? apiKey = configuration["API_FOOTBALL_KEY"];
79+
if (string.IsNullOrEmpty(apiKey))
80+
{
81+
Console.WriteLine("⚠️ API key not found in environment variables!");
82+
}
83+
else
84+
{
85+
Console.WriteLine($"✅ API key loaded: {apiKey.Substring(0, Math.Min(8, apiKey.Length))}...");
86+
}
87+
}
88+
}
89+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Xunit;
2+
using System.Net;
3+
using backend.db.Models.Club;
4+
5+
namespace Backend.Tests.Integration
6+
{
7+
public class ClubServiceIntegrationTests
8+
{
9+
[Fact]
10+
public async Task GetClubInfo_ValidClubName_ReturnsClubData()
11+
{
12+
// Arrange
13+
string mockApiResponse = @"[{
14+
""id"": ""33"",
15+
""name"": ""FC Barcelona"",
16+
""logo"": ""https://example.com/logo.png""
17+
}]";
18+
19+
var service = IntegrationTestHelper.CreateClubService(mockApiResponse);
20+
21+
// Act
22+
ClubModel result = await service.GetClubInfo("FC Barcelona");
23+
24+
// Assert
25+
Assert.NotNull(result);
26+
Assert.Equal("33", result.clubID);
27+
Assert.Equal("FC Barcelona", result.clubName);
28+
Assert.Equal("https://example.com/logo.png", result.clubLogo);
29+
}
30+
[Fact]
31+
public async Task GetClubInfoById_ValidClubId_ReturnsClubData()
32+
{
33+
// Arrange
34+
string mockApiResponse = @"[{
35+
""id"": ""33"",
36+
""name"": ""FC Barcelona"",
37+
""logo"": ""https://example.com/logo.png""
38+
}]";
39+
40+
var service = IntegrationTestHelper.CreateClubService(mockApiResponse);
41+
42+
// Act
43+
ClubModel result = await service.GetClubInfoById("33");
44+
45+
// Assert
46+
Assert.NotNull(result);
47+
Assert.Equal("33", result.clubID);
48+
Assert.Equal("FC Barcelona", result.clubName);
49+
Assert.Equal("https://example.com/logo.png", result.clubLogo);
50+
}
51+
}
52+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using Xunit;
2+
using System.Net;
3+
using backend.db.Models.Player;
4+
using backend.db.Models.TransferHistoryResponse;
5+
6+
namespace Backend.Tests.Integration
7+
{
8+
public class FootballPlayerServiceIntegrationTests
9+
{
10+
[Fact]
11+
public async Task GetPlayerStats_ValidPlayerId_ReturnsPlayerData()
12+
{
13+
// Arrange
14+
string mockApiResponse = @"[{
15+
""id"": 123,
16+
""name"": ""Test Player"",
17+
""profile"": {
18+
""birthDate"": ""1995-01-15"",
19+
""height"": ""180cm"",
20+
""citizenship"": ""England"",
21+
""position"": { ""main"": ""Forward"" },
22+
""club"": { ""current"": ""Test FC"" }
23+
},
24+
""marketValue"": [{
25+
""value"": 50000000,
26+
""currency"": ""€""
27+
}],
28+
""transfers"": []
29+
}]";
30+
31+
var service = IntegrationTestHelper.CreateFootballPlayerService(mockApiResponse);
32+
33+
// Act
34+
PlayerModel result = await service.GetPlayerStats(123);
35+
36+
// Assert
37+
Assert.NotNull(result);
38+
Assert.Equal("Test Player", result.Name);
39+
Assert.Equal(30, result.Age); // Age calculated from 1995-01-15 (born Jan 15, 1995, now Nov 14, 2025 = 30 years old)
40+
Assert.Equal("€50.0M", result.MarketValue);
41+
}
42+
43+
[Fact]
44+
public async Task GetPlayerStats_ApiError_ThrowsException()
45+
{
46+
// Arrange
47+
var service = IntegrationTestHelper.CreateFootballPlayerService("", HttpStatusCode.InternalServerError);
48+
49+
// Act & Assert
50+
await Assert.ThrowsAsync<Exception>(() => service.GetPlayerStats(123));
51+
}
52+
[Fact]
53+
public async Task GetPlayerWithClubLogo()
54+
{
55+
// Arrange
56+
string mockApiResponse = @"[{
57+
""id"": 123,
58+
""name"": ""Test Player"",
59+
""profile"": {
60+
""birthDate"": ""1995-01-15"",
61+
""height"": ""180cm"",
62+
""citizenship"": ""England"",
63+
""position"": { ""main"": ""Forward"" },
64+
""club"": { ""current"": ""Test FC"" }
65+
},
66+
""marketValue"": [{
67+
""value"": 50000000,
68+
""currency"": ""€""
69+
}],
70+
""transfers"": []
71+
}]";
72+
var service = IntegrationTestHelper.CreateFootballPlayerService(mockApiResponse);
73+
// Act
74+
PlayerModel result = await service.GetPlayerWithClubLogo(123);
75+
// Assert
76+
Assert.NotNull(result);
77+
Assert.Equal("Test Player", result.Name);
78+
Assert.Equal("Test FC", result.Club.clubName);
79+
Assert.Equal("Test FC", result.Club.clubName);
80+
}
81+
[Fact]
82+
public async Task GetPlayerWithStatistics()
83+
{
84+
// Arrange
85+
string mockApiResponse = @"[{
86+
""id"": 123,
87+
""name"": ""Test Player"",
88+
""profile"": {
89+
""birthDate"": ""1995-01-15"",
90+
""height"": ""180cm"",
91+
""citizenship"": ""England"",
92+
""position"": { ""main"": ""Forward"" },
93+
""club"": { ""current"": ""Test FC"" }
94+
},
95+
""marketValue"": [{
96+
""value"": 50000000,
97+
""currency"": ""€""
98+
}],
99+
""transfers"": []
100+
}]";
101+
var service = IntegrationTestHelper.CreateFootballPlayerService(mockApiResponse);
102+
// Act
103+
PlayerModel result = await service.GetPlayerStatistics(123);
104+
// Assert
105+
Assert.NotNull(result);
106+
Assert.Equal("Test Player", result.Name);
107+
Assert.Equal("England", result.Nationality);
108+
Assert.Equal("Forward", result.Position);
109+
Assert.Equal("180cm", result.Height);
110+
Assert.Equal(30, result.Age);
111+
Assert.Equal("Test FC", result.Club.clubName);
112+
Assert.Equal("€50.0M", result.MarketValue);
113+
Assert.Equal("Test FC", result.Club.clubName);
114+
}
115+
[Fact]
116+
public async Task GetPlayerTransferHistory()
117+
{
118+
// Arrange
119+
string mockApiResponse = @"[{
120+
""id"": 123,
121+
""name"": ""Test Player"",
122+
""profile"": {
123+
""birthDate"": ""1995-01-15"",
124+
""height"": ""180cm"",
125+
""citizenship"": ""England"",
126+
""position"": { ""main"": ""Forward"" },
127+
""club"": { ""current"": ""Test FC"" }
128+
},
129+
""marketValue"": [{
130+
""value"": 50000000,
131+
""currency"": ""€""
132+
}],
133+
""transfers"": [
134+
{
135+
""to"": ""Club A"",
136+
""from"": ""Club X"",
137+
""fee"": ""10M"",
138+
""type"": ""Transfer"",
139+
""season"": ""2023"",
140+
""marketValue"": ""10000000"",
141+
""transferDate"": ""2023-01-15""
142+
},
143+
{
144+
""to"": ""Club B"",
145+
""from"": ""Club A"",
146+
""fee"": ""15M"",
147+
""type"": ""Transfer"",
148+
""season"": ""2024"",
149+
""marketValue"": ""15000000"",
150+
""transferDate"": ""2024-06-01""
151+
}
152+
]
153+
}]";
154+
var service = IntegrationTestHelper.CreateFootballPlayerService(mockApiResponse);
155+
// Act
156+
TransferHistoryResponseModel result = await service.GetPlayerTransferHistory(123);
157+
// Assert
158+
Assert.NotNull(result);
159+
Assert.Equal(2, result.Transfers.Count);
160+
Assert.Equal("Club A", result.Transfers[0].ToClub.Name);
161+
Assert.Equal("Club B", result.Transfers[1].ToClub.Name);
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)