Skip to content

Commit

Permalink
Fix #248 : L2 injured players fail (maligue2 website)
Browse files Browse the repository at this point in the history
  • Loading branch information
ALIX LOURME - U171755 authored and axel3rd committed Aug 2, 2023
1 parent a8cfd49 commit ec5435b
Show file tree
Hide file tree
Showing 3 changed files with 1,992 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class InjuredSuspendedMaLigue2Client extends AbstractClient {
/*
* Team name "maligue2 -> MPG" wrapper
*/
TEAM_NAME_WRAPPER.put("QRM", "Quevilly Rouen");
TEAM_NAME_WRAPPER.put("AS Saint-Etienne", "St Etienne");
TEAM_NAME_WRAPPER.put("AS Saint-Etienne", "AS Saint-Étienne");
TEAM_NAME_WRAPPER.put("FC Sochaux-Montbéliard", "FCSM");
TEAM_NAME_WRAPPER.put("Stade Lavallois", "Laval MFC");
TEAM_NAME_WRAPPER.put("Valenciennes FC", "VAFC");
}

private List<Player> cache = null;
Expand Down Expand Up @@ -61,7 +63,7 @@ static String getMpgTeamName(String name) {
public Player getPlayer(String playerName, String teamName) {
// For composed lastName (highest priority than firstName composed), we replace space by '-'
String lastName = playerName;
if (!playerName.startsWith("De ")) {
if (!playerName.matches("(De |Ben |D').*")) {
int spaceIndex = lastName.lastIndexOf(' ');
if (spaceIndex > 0) {
lastName = lastName.substring(0, spaceIndex);
Expand All @@ -88,7 +90,7 @@ protected List<Player> getPlayers(String content) {
List<Player> players = new ArrayList<>();
Document doc = Jsoup.parse(content);
for (Element item : doc.select("tr")) {
if (item.selectFirst("th.column-1") != null && "Club".equals(item.selectFirst("th.column-1").text())) {
if ((item.selectFirst("th.column-1") != null && "Club".equals(item.selectFirst("th.column-1").text())) || item.selectFirst("td.column-1") == null) {
continue;
}
String team = item.selectFirst("td.column-1").text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,67 @@

public class InjuredSuspendedMaLigue2ClientTest extends AbstractMockTestClient {

@Test
public void testCheckTeams2023L2() throws Exception {
List<String> mpgTeams = Arrays.asList("AC Ajaccio", "AJ Auxerre", "AS Saint-Étienne", "Amiens SC", "Angers SCO", "Bordeaux", "Concarneau", "Dunkerque", "EA Guingamp", "FCSM", "Grenoble",
"Laval MFC", "Paris FC", "Pau FC", "QRM", "Rodez AF", "SC Bastia", "SM Caen", "Troyes", "VAFC");
Document doc = Jsoup.parse(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20230802.html"), Charset.forName("UTF-8")));
List<String> maLigue2Teams = new ArrayList<>();
for (Element item : doc.select("tr")) {
if ((item.selectFirst("th.column-1") != null && "Club".equals(item.selectFirst("th.column-1").text())) || item.selectFirst("td.column-1") == null) {
continue;
}
maLigue2Teams.add(InjuredSuspendedMaLigue2Client.getMpgTeamName(item.selectFirst("td.column-1").text()));
}
for (String mpgTeam : mpgTeams) {
boolean contains = false;
for (String maLigue2Team : maLigue2Teams) {
if (maLigue2Team.contains(mpgTeam)) {
contains = true;
}
}
Assert.assertTrue(mpgTeam, contains);
}
}

@Test
public void testSeasons2023() throws Exception {
InjuredSuspendedMaLigue2Client client = spy(InjuredSuspendedMaLigue2Client.build(null));
doReturn(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20230802.html"), Charset.forName("UTF-8"))).when(client).getHtmlContent();

Assert.assertNotNull("SC Bastia Guidi Blesse", client.getPlayer("Guidi", "Bastia"));
Assert.assertEquals("SC Bastia Guidi Blesse", OutType.INJURY_RED, client.getPlayer("Guidi", "Bastia").getOutType());

Assert.assertNotNull("Caen Ben Youssef Blesse", client.getPlayer("Ben Youssef", "SM Caen"));
Assert.assertEquals("Caen Ben Youssef Blesse", OutType.INJURY_RED, client.getPlayer("Ben Youssef", "SM Caen").getOutType());

Assert.assertNotNull("Dunkerque Gnanduillet Blesse", client.getPlayer("Gnanduillet", "Dunkerque"));
Assert.assertEquals("Dunkerque Gnanduillet Blesse", OutType.INJURY_RED, client.getPlayer("Gnanduillet", "Dunkerque").getOutType());

Assert.assertNotNull("EA Guingamp Vallier Blesse", client.getPlayer("Vallier", "EA Guingamp"));
Assert.assertEquals("EA Guingamp Vallier Blesse", OutType.INJURY_RED, client.getPlayer("Vallier", "EA Guingamp").getOutType());

Assert.assertNotNull("Laval MFC Ferhaoui Blesse", client.getPlayer("Ferhaoui", "Laval MFC"));
Assert.assertEquals("Laval MFC Ferhaoui Blesse", OutType.INJURY_RED, client.getPlayer("Ferhaoui", "Laval MFC").getOutType());

Assert.assertNotNull("Grenoble Foot Mendy Suspendu", client.getPlayer("Mendy", "Grenoble"));
Assert.assertEquals("Grenoble Foot Mendy Suspendu", OutType.SUSPENDED, client.getPlayer("Mendy", "Grenoble").getOutType());

Assert.assertNotNull("Pau FC Batisse Suspendu", client.getPlayer("Batisse", "Pau FC"));
Assert.assertEquals("Pau FC Batisse Suspendu", OutType.SUSPENDED, client.getPlayer("Batisse", "Pau FC").getOutType());

Assert.assertNotNull("Pau FC Abzi Suspendu", client.getPlayer("Abzi", "Pau FC"));
Assert.assertEquals("Pau FC Abzi Suspendu", OutType.SUSPENDED, client.getPlayer("Abzi", "Pau FC").getOutType());

Assert.assertNotNull("Pau FC D'Almeida Suspendu", client.getPlayer("D'Almeida", "Pau FC"));
Assert.assertEquals("Pau FC D'Almeida Suspendu", OutType.SUSPENDED, client.getPlayer("D'Almeida", "Pau FC").getOutType());
}

@Test
public void testCheckTeams2022L2() throws Exception {
List<String> mpgTeams = Arrays.asList("Amiens", "Annecy", "Bastia", "Bordeaux", "Caen", "Dijon", "Grenoble", "Guingamp", "Laval", "Le Havre",
"Metz", "Nîmes", "Niort", "Paris FC", "Pau", "Quevilly Rouen", "Rodez", "St Etienne", "Sochaux", "Valenciennes");
Document doc = Jsoup.parse(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20220724.html"),
Charset.forName("UTF-8")));
List<String> mpgTeams = Arrays.asList("Amiens", "Annecy", "Bastia", "Bordeaux", "Caen", "Dijon", "Grenoble", "Guingamp", "Laval", "Le Havre", "Metz", "Nîmes", "Niort", "Paris FC", "Pau",
"QRM", "Rodez", "AS Saint-Étienne", "FCSM", "VAFC");
Document doc = Jsoup.parse(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20220724.html"), Charset.forName("UTF-8")));
List<String> maLigue2Teams = new ArrayList<>();
for (Element item : doc.select("tr")) {
if (item.selectFirst("th.column-1") != null && "Club".equals(item.selectFirst("th.column-1").text())) {
Expand All @@ -50,11 +105,9 @@ public void testCheckTeams2022L2() throws Exception {
}

@Test
public void testReasons2022() throws Exception {
public void testSeasons2022() throws Exception {
InjuredSuspendedMaLigue2Client client = spy(InjuredSuspendedMaLigue2Client.build(null));
doReturn(
FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20220724.html"), Charset.forName("UTF-8")))
.when(client).getHtmlContent();
doReturn(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20220724.html"), Charset.forName("UTF-8"))).when(client).getHtmlContent();

Assert.assertNotNull("Annecy Spano Blesse", client.getPlayer("Spano", "Annecy"));
Assert.assertEquals("Annecy Spano Blesse", OutType.INJURY_RED, client.getPlayer("Spano", "Annecy").getOutType());
Expand All @@ -74,18 +127,17 @@ public void testReasons2022() throws Exception {
Assert.assertEquals("Paris Name Suspendu", OutType.SUSPENDED, client.getPlayer("Name", "Paris FC").getOutType());
Assert.assertNotNull("Paris Camara Suspendu", client.getPlayer("Camara", "Paris FC"));
Assert.assertEquals("Paris Camara Suspendu", OutType.SUSPENDED, client.getPlayer("Camara", "Paris FC").getOutType());
Assert.assertNotNull("Saint-Etienne Chambost Blesse", client.getPlayer("Chambost", "St Etienne"));
Assert.assertEquals("Saint-Etienne Chambost Blesse", OutType.INJURY_RED, client.getPlayer("Chambost", "St Etienne").getOutType());
Assert.assertNotNull("Sochaux Lopy Blesse", client.getPlayer("Lopy", "Sochaux"));
Assert.assertEquals("Sochaux Lopy Blesse", OutType.INJURY_RED, client.getPlayer("Lopy", "Sochaux").getOutType());
Assert.assertNotNull("Saint-Etienne Chambost Blesse", client.getPlayer("Chambost", "AS Saint-Étienne"));
Assert.assertEquals("Saint-Etienne Chambost Blesse", OutType.INJURY_RED, client.getPlayer("Chambost", "AS Saint-Étienne").getOutType());
Assert.assertNotNull("Sochaux Lopy Blesse", client.getPlayer("Lopy", "FCSM"));
Assert.assertEquals("Sochaux Lopy Blesse", OutType.INJURY_RED, client.getPlayer("Lopy", "FCSM").getOutType());
}

@Test
public void testCheckTeams2021L2() throws Exception {
List<String> mpgTeams = Arrays.asList("Ajaccio", "Amiens", "Auxerre", "Bastia", "Caen", "Dijon", "Dunkerque", "Grenoble", "Guingamp",
"Le Havre", "Nancy", "Nîmes", "Niort", "Paris FC", "Pau", "Quevilly Rouen", "Rodez", "Sochaux", "Toulouse", "Valenciennes");
Document doc = Jsoup.parse(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20210804.html"),
Charset.forName("UTF-8")));
List<String> mpgTeams = Arrays.asList("Ajaccio", "Amiens", "Auxerre", "Bastia", "Caen", "Dijon", "Dunkerque", "Grenoble", "Guingamp", "Le Havre", "Nancy", "Nîmes", "Niort", "Paris FC", "Pau",
"QRM", "Rodez", "FCSM", "Toulouse", "VAFC");
Document doc = Jsoup.parse(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20210804.html"), Charset.forName("UTF-8")));
List<String> maLigue2Teams = new ArrayList<>();
for (Element item : doc.select("tr")) {
if (item.selectFirst("th.column-1") != null && "Club".equals(item.selectFirst("th.column-1").text())) {
Expand All @@ -105,11 +157,9 @@ public void testCheckTeams2021L2() throws Exception {
}

@Test
public void testReasons2021() throws Exception {
public void testSeasons2021() throws Exception {
InjuredSuspendedMaLigue2Client client = spy(InjuredSuspendedMaLigue2Client.build(null));
doReturn(
FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20210804.html"), Charset.forName("UTF-8")))
.when(client).getHtmlContent();
doReturn(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20210804.html"), Charset.forName("UTF-8"))).when(client).getHtmlContent();

Assert.assertNotNull("Absent : Diallo (JO) / AC Ajaccio", client.getPlayer("Diallo", "Ajaccio"));
Assert.assertEquals("Absent : Diallo (JO) / AC Ajaccio", OutType.ASBENT, client.getPlayer("Diallo", "Ajaccio").getOutType());
Expand All @@ -124,9 +174,7 @@ public void testReasons2021() throws Exception {
@Test
public void testInjuriesSameName() throws Exception {
InjuredSuspendedMaLigue2Client client = spy(InjuredSuspendedMaLigue2Client.build(null));
doReturn(
FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20210804.html"), Charset.forName("UTF-8")))
.when(client).getHtmlContent();
doReturn(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20210804.html"), Charset.forName("UTF-8"))).when(client).getHtmlContent();

Assert.assertNull("Ba from Niort is NOT injured", client.getPlayer("Ba", "Niort"));
Assert.assertNotNull("Ba from Dunkerque is injured", client.getPlayer("Ba", "Dunkerque"));
Expand All @@ -135,10 +183,9 @@ public void testInjuriesSameName() throws Exception {

@Test
public void testCheckTeams2019L2() throws Exception {
List<String> mpgTeams = Arrays.asList("Ajaccio", "Auxerre", "Caen", "Chambly", "Châteauroux", "Clermont", "Grenoble", "Guingamp", "Le Havre",
"Le Mans", "Lens", "Lorient", "Nancy", "Niort", "Orléans", "Paris", "Rodez", "Sochaux", "Troyes", "Valenciennes");
Document doc = Jsoup.parse(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190823.html"),
Charset.forName("UTF-8")));
List<String> mpgTeams = Arrays.asList("Ajaccio", "Auxerre", "Caen", "Chambly", "Châteauroux", "Clermont", "Grenoble", "Guingamp", "Le Havre", "Le Mans", "Lens", "Lorient", "Nancy", "Niort",
"Orléans", "Paris", "Rodez", "Sochaux", "Troyes", "Valenciennes");
Document doc = Jsoup.parse(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190823.html"), Charset.forName("UTF-8")));
List<String> maLigue2Teams = new ArrayList<>();
for (Element item : doc.select("tr")) {
if (item.selectFirst("th.column-1") != null && "Club".equals(item.selectFirst("th.column-1").text())) {
Expand All @@ -159,8 +206,8 @@ public void testCheckTeams2019L2() throws Exception {

@Test
public void testLocalMapping() throws Exception {
List<Player> players = InjuredSuspendedMaLigue2Client.build(Config.build("src/test/resources/mpg.properties.here")).getPlayers(FileUtils
.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190818.html"), Charset.forName("UTF-8")));
List<Player> players = InjuredSuspendedMaLigue2Client.build(Config.build("src/test/resources/mpg.properties.here"))
.getPlayers(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190818.html"), Charset.forName("UTF-8")));
Assert.assertNotNull(players);
Assert.assertEquals(32, players.size());
for (Player player : players) {
Expand All @@ -174,19 +221,15 @@ public void testLocalMapping() throws Exception {
@Test
public void testFrenchAccent() throws Exception {
InjuredSuspendedMaLigue2Client client = spy(InjuredSuspendedMaLigue2Client.build(null));
doReturn(
FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20201006.html"), Charset.forName("UTF-8")))
.when(client).getHtmlContent();
doReturn(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20201006.html"), Charset.forName("UTF-8"))).when(client).getHtmlContent();

Assert.assertNotNull("Barthelme Maxime injured", client.getPlayer("Barthelme Maxime", "Troyes"));
}

@Test
public void testSomeInjuriesWithNoParentheseEnding() throws Exception {
InjuredSuspendedMaLigue2Client client = spy(InjuredSuspendedMaLigue2Client.build(null));
doReturn(
FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190822.html"), Charset.forName("UTF-8")))
.when(client).getHtmlContent();
doReturn(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190822.html"), Charset.forName("UTF-8"))).when(client).getHtmlContent();

Assert.assertNotNull("Boissier Remy is injured", client.getPlayer("Boissier Remy", "Le Mans"));
Assert.assertEquals("Boissier J5", "J5", client.getPlayer("Boissier Remy", "Le Mans").getLength());
Expand All @@ -196,9 +239,7 @@ public void testSomeInjuriesWithNoParentheseEnding() throws Exception {
@Test
public void testSomeInjuries() throws Exception {
InjuredSuspendedMaLigue2Client client = spy(InjuredSuspendedMaLigue2Client.build(null));
doReturn(
FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190818.html"), Charset.forName("UTF-8")))
.when(client).getHtmlContent();
doReturn(FileUtils.readFileToString(new File(TESTFILES_BASE, "maligue2.joueurs-blesses-et-suspendus.20190818.html"), Charset.forName("UTF-8"))).when(client).getHtmlContent();

Assert.assertNotNull("Boissier Remy is injured", client.getPlayer("Boissier Remy", "Le Mans"));
Assert.assertNotNull("Valette is injured", client.getPlayer("Valette", "Nancy"));
Expand All @@ -211,27 +252,26 @@ public void testSomeInjuries() throws Exception {

@Test
public void testMock() throws Exception {
stubFor(get("/2020/08/20/joueurs-blesses-et-suspendus/").willReturn(
aResponse().withHeader("Content-Type", "application/json").withBodyFile("maligue2.joueurs-blesses-et-suspendus.20190818.html")));
stubFor(get("/2020/08/20/joueurs-blesses-et-suspendus/")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("maligue2.joueurs-blesses-et-suspendus.20190818.html")));

InjuredSuspendedMaLigue2Client injuredSuspendedClient = InjuredSuspendedMaLigue2Client.build(getConfig(),
"http://localhost:" + getServer().port() + "/2020/08/20/joueurs-blesses-et-suspendus/");

// Remove cache
File tmpFile = InjuredSuspendedMaLigue2Client
.getCacheFile("http://localhost:" + getServer().port() + "/2020/08/20/joueurs-blesses-et-suspendus/", "");
File tmpFile = InjuredSuspendedMaLigue2Client.getCacheFile("http://localhost:" + getServer().port() + "/2020/08/20/joueurs-blesses-et-suspendus/", "");
tmpFile.delete();
Assert.assertFalse(tmpFile.exists());

List<Player> players = injuredSuspendedClient.getPlayers();
Assert.assertNotNull(players);
Assert.assertEquals(32, players.size());

// Verify cache file has been created, recall and verify date file doesn't change
// Verify cache file has been created, recall and verify date file doesn't
// change
Assert.assertTrue(tmpFile.exists());
long cacheDate = tmpFile.lastModified();
injuredSuspendedClient = InjuredSuspendedMaLigue2Client.build(getConfig(),
"http://localhost:" + getServer().port() + "/2020/08/20/joueurs-blesses-et-suspendus/");
injuredSuspendedClient = InjuredSuspendedMaLigue2Client.build(getConfig(), "http://localhost:" + getServer().port() + "/2020/08/20/joueurs-blesses-et-suspendus/");
injuredSuspendedClient.getPlayers();
Assert.assertEquals(cacheDate, tmpFile.lastModified());
}
Expand Down
Loading

0 comments on commit ec5435b

Please sign in to comment.