From eff06de4478245cff8c361bf78365db039807269 Mon Sep 17 00:00:00 2001 From: Alix Lourme Date: Tue, 8 Aug 2023 20:56:52 +0200 Subject: [PATCH] Fix quality flow --- .../out/InjuredSuspendedWrapperClient.java | 43 ++++++++---------- .../InjuredSuspendedWrapperClientTest.java | 44 +++++++++++++++++++ 2 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 src/test/java/org/blondin/mpg/out/InjuredSuspendedWrapperClientTest.java diff --git a/src/main/java/org/blondin/mpg/out/InjuredSuspendedWrapperClient.java b/src/main/java/org/blondin/mpg/out/InjuredSuspendedWrapperClient.java index e44f52c..ca30dc7 100644 --- a/src/main/java/org/blondin/mpg/out/InjuredSuspendedWrapperClient.java +++ b/src/main/java/org/blondin/mpg/out/InjuredSuspendedWrapperClient.java @@ -5,8 +5,6 @@ import org.blondin.mpg.out.model.OutType; import org.blondin.mpg.out.model.Player; import org.blondin.mpg.out.model.Position; -import org.blondin.mpg.root.exception.TeamsNotFoundException; -import org.blondin.mpg.root.exception.UrlForbiddenException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,10 +39,10 @@ public static InjuredSuspendedWrapperClient build(Config config, String urlOverr * Return injured or suspended player * * @param championship Championship of player - * @param name Player Name - * @param position Position (used to improve "out player" matching if not null) - * @param teamName Team Name - * @param excludes {@link OutType} to exclude + * @param name Player Name + * @param position Position (used to improve "out player" matching if not null) + * @param teamName Team Name + * @param excludes {@link OutType} to exclude * @return Player or null if not found */ public Player getPlayer(ChampionshipOutType championship, String playerName, Position position, String teamName, OutType... excludes) { @@ -52,30 +50,27 @@ public Player getPlayer(ChampionshipOutType championship, String playerName, Pos throw new UnsupportedOperationException("Main parameters (championship, playerName, position, teamName) can not be null"); } if (ChampionshipOutType.LIGUE_2.equals(championship)) { - if (!maLigue2Reachable) { - return null; - } try { - return maLigue2Client.getPlayer(playerName, teamName); - } catch (ServiceUnavailableException e) { - LOG.error("WARN: Maligue2.fr is unavailable, L2 injured/suspended players not taken into account :-("); + if (maLigue2Reachable) { + return maLigue2Client.getPlayer(playerName, teamName); + } + } catch (UnsupportedOperationException | ServiceUnavailableException e) { + LOG.warn("WARN: Maligue2.fr is unavailable, L2 injured/suspended players not taken into account :-("); maLigue2Reachable = false; - return null; + } - } - try { - if (sportsGamblerReachable) { - return useDirectlyOnlyForTestGetSportsGamblerClient().getPlayer(championship, playerName, teamName); + } else { + try { + if (sportsGamblerReachable) { + return sportsGamblerClient.getPlayer(championship, playerName, teamName); + } + } catch (UnsupportedOperationException | ServiceUnavailableException e) { + LOG.warn("WARN: SportsGambler is unavailable, injured/suspended players not taken into account :-("); + LOG.warn("(Your IP is perhaps temporary ban, try to increase 'request.wait.time' parameter)"); + sportsGamblerReachable = false; } - } catch (UrlForbiddenException | ServiceUnavailableException | TeamsNotFoundException e) { - LOG.error("WARN: SportsGambler is unavailable, injured/suspended players not taken into account :-("); - LOG.error("(Your IP is perhaps temporary ban, try to increase 'request.wait.time' parameter)"); } return null; } - public InjuredSuspendedSportsGamblerClient useDirectlyOnlyForTestGetSportsGamblerClient() { - return sportsGamblerClient; - } - } diff --git a/src/test/java/org/blondin/mpg/out/InjuredSuspendedWrapperClientTest.java b/src/test/java/org/blondin/mpg/out/InjuredSuspendedWrapperClientTest.java new file mode 100644 index 0000000..74e070a --- /dev/null +++ b/src/test/java/org/blondin/mpg/out/InjuredSuspendedWrapperClientTest.java @@ -0,0 +1,44 @@ +package org.blondin.mpg.out; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; + +import org.blondin.mpg.AbstractMockTestClient; +import org.blondin.mpg.out.model.Position; +import org.junit.Assert; +import org.junit.Test; + +import com.github.tomakehurst.wiremock.http.Fault; + +public class InjuredSuspendedWrapperClientTest extends AbstractMockTestClient { + + @Test + public void testType() throws Exception { + stubFor(get("/injuries/football/france-ligue-1/").willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("sportsgambler.ligue-1.20230807.html"))); + stubFor(get("/2020/08/20/joueurs-blesses-et-suspendus/") + .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("maligue2.joueurs-blesses-et-suspendus.20230802.html"))); + + InjuredSuspendedWrapperClient client = InjuredSuspendedWrapperClient.build(getConfig(), "http://localhost:" + getServer().port() + "/injuries/football/", + "http://localhost:" + getServer().port() + "/2020/08/20/joueurs-blesses-et-suspendus/"); + + Assert.assertNotNull("L1", client.getPlayer(ChampionshipOutType.LIGUE_1, "Sergio Rico", Position.G, "Paris SG")); + Assert.assertNotNull("L2", client.getPlayer(ChampionshipOutType.LIGUE_2, "Guidi", Position.D, "Bastia")); + } + + @Test + public void testFailSportsgambler() throws Exception { + stubFor(get("/injuries/football/france-ligue-1/").willReturn(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE))); + InjuredSuspendedWrapperClient client = InjuredSuspendedWrapperClient.build(getConfig(), "http://localhost:" + getServer().port() + "/injuries/football/", null); + Assert.assertNull(client.getPlayer(ChampionshipOutType.LIGUE_1, "Sergio Rico", Position.G, "Paris SG")); + Assert.assertTrue(getLogOut(), getLogOut().contains("WARN: SportsGambler is unavailable, injured/suspended players not taken into account")); + } + + @Test + public void testFailMaligue2() throws Exception { + stubFor(get("/2020/08/20/joueurs-blesses-et-suspendus/").willReturn(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE))); + InjuredSuspendedWrapperClient client = InjuredSuspendedWrapperClient.build(getConfig(), null, "http://localhost:" + getServer().port() + "/2020/08/20/joueurs-blesses-et-suspendus/"); + Assert.assertNull(client.getPlayer(ChampionshipOutType.LIGUE_2, "Guidi", Position.D, "Bastia")); + Assert.assertTrue(getLogOut(), getLogOut().contains("WARN: Maligue2.fr is unavailable, L2 injured/suspended players not taken into account")); + } +}