Skip to content

Commit

Permalink
Fix quality flow
Browse files Browse the repository at this point in the history
  • Loading branch information
axel3rd committed Aug 9, 2023
1 parent 97b1ad6 commit eff06de
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,41 +39,38 @@ 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) {
if (!ObjectUtils.allNotNull(championship, playerName, position, teamName)) {
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;
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit eff06de

Please sign in to comment.