Skip to content

Commit

Permalink
Fix #229 : Update Mpgstats endpoints to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
axel3rd committed Mar 28, 2022
1 parent 7508eff commit 587fbec
Show file tree
Hide file tree
Showing 38 changed files with 15,331 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,5 @@ This project is using [NPM](https://docs.npmjs.com/) as complementary tool for d
For format documentation, use (see [package.json](./package.json) for details):

```
npm run format all
npm run format-all
```
4 changes: 2 additions & 2 deletions src/main/java/org/blondin/mpg/stats/MpgStatsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public synchronized Championship getStats(ChampionshipStatsType type) {
if (!cache.containsKey(type)) {
// FR : "Ligue-1" / EN : "Premier-League" / ES : "Liga"
// Call with infinite cache and verify timestamp after
Championship championship = get("leagues/" + type.getValue(), Championship.class, 0);
Championship championship = get("leagues/" + type.getValue() + "_v2.json", Championship.class, 0);
Date leagueDateRefresh = getLeaguesRefresh().getDate(championship.getInfos().getId());
if (leagueDateRefresh == null || championship.getDate().before(leagueDateRefresh)) {
// Force refresh by using a mini cache time
Expand All @@ -46,6 +46,6 @@ public synchronized Championship getStats(ChampionshipStatsType type) {
}

protected synchronized LeaguesRefresh getLeaguesRefresh() {
return get("builds", LeaguesRefresh.class, TIME_HOUR_IN_MILLI_SECOND);
return get("builds.json", LeaguesRefresh.class, TIME_HOUR_IN_MILLI_SECOND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ public StatsDayOrPositionPlayer deserialize(JsonParser jp, DeserializationContex
sop.setPosition(Position.getNameByValue((String) o));
} else if (o instanceof ArrayList<?>) {
Map<Integer, StatsDay> statsDay = new HashMap<>();
for (ArrayList<?> e : (ArrayList<ArrayList<?>>) o) {
Integer day = (Integer) e.get(0);
Map<String, Object> values = (Map<String, Object>) e.get(1);
statsDay.put(day, new StatsDay(Double.valueOf(ObjectUtils.defaultIfNull(values.get("n"), 0).toString()),
Integer.valueOf(ObjectUtils.defaultIfNull(values.get("g"), 0).toString())));
if (!((ArrayList<?>) o).isEmpty() && ((ArrayList<?>) o).get(0) instanceof HashMap<?, ?>) {
// API v2 format
for (HashMap<?, ?> e : (ArrayList<HashMap<?, ?>>) o) {
statsDay.put(Integer.parseInt(e.get("D").toString()),
new StatsDay(Double.valueOf(ObjectUtils.defaultIfNull(e.get("n"), 0).toString()),
Integer.valueOf(ObjectUtils.defaultIfNull(e.get("g"), 0).toString())));
}
} else {
for (ArrayList<?> e : (ArrayList<ArrayList<?>>) o) {
Integer day = (Integer) e.get(0);
Map<String, Object> values = (Map<String, Object>) e.get(1);
statsDay.put(day, new StatsDay(Double.valueOf(ObjectUtils.defaultIfNull(values.get("n"), 0).toString()),
Integer.valueOf(ObjectUtils.defaultIfNull(values.get("g"), 0).toString())));
}
}
sop.setStatsDay(statsDay);
} else {
Expand Down
87 changes: 78 additions & 9 deletions src/test/java/org/blondin/mpg/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,73 @@ public void testRealWithBadCredentials() throws Exception {
}
}

@Test
public void testMpgStatsApiV2() throws Exception {
prepareMainFrenchLigue1Mocks("20220327", "2022", "20220327", "20220327");
stubFor(get("/division/mpg_division_MLAX7HMK_4_1")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.division.MLAX7HMK.20220327.json")));
stubFor(get("/division/mpg_division_MLAX7HMK_4_1/coach")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.coach.MLAX7HMK.20220327.json")));
stubFor(get("/team/mpg_team_MLAX7HMK_4_1_6")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.team.MLAX7HMK.20220327.json")));

prepareMainFrenchLigue2Mocks("20220327", "2022", "20220327", "20220327");
stubFor(get("/division/mpg_division_MLEFEX6G_4_1")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.division.MLEFEX6G.20220327.json")));
stubFor(get("/division/mpg_division_MLEFEX6G_4_1/coach")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.coach.MLEFEX6G.20220327.json")));
stubFor(get("/team/mpg_team_MLEFEX6G_4_1_2")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.team.MLEFEX6G.20220327.json")));

// Liga
stubFor(get("/division/mpg_division_PYUJXJM_1_1")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.division.PYUJXJM.20220327.json")));
stubFor(get("/team/mpg_team_PYUJXJM_1_1_3")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.team.PYUJXJM.20220327.json")));
stubFor(get("/championship-players-pool/3")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.poolPlayers.3.2022.json")));
stubFor(get("/division/mpg_division_PYUJXJM_1_1/coach")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.coach.PYUJXJM.20220327.json")));
stubFor(get("/leagues/Liga_v2.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.liga.20220327.json")));
stubFor(get("/injuries/football/spain-la-liga/")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("sportsgambler.liga.20220327.html")));

// SerieA
stubFor(get("/division/mpg_division_NKCDJTKS_2_1")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.division.NKCDJTKS.20220327.json")));
stubFor(get("/team/mpg_team_NKCDJTKS_2_1_1")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.team.NKCDJTKS.20220327.json")));
stubFor(get("/championship-players-pool/5")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.poolPlayers.5.2022.json")));
stubFor(get("/division/mpg_division_NKCDJTKS_2_1/coach")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.coach.NKCDJTKS.20220327.json")));
stubFor(get("/leagues/Serie-A_v2.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.serie-a.20220327.json")));
stubFor(get("/injuries/football/italy-serie-a/")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("sportsgambler.serie-a.20220327.html")));

// PL
stubFor(get("/division/mpg_division_MLMHBPCB_4_1")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.division.MLMHBPCB.20220327.json")));
stubFor(get("/team/mpg_team_MLMHBPCB_4_1_4")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.team.MLMHBPCB.20220327.json")));
stubFor(get("/division/mpg_division_MLMHBPCB_4_1/coach")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.coach.MLMHBPCB.20220327.json")));
stubFor(get("/championship-players-pool/2")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.poolPlayers.2.2022.json")));
stubFor(get("/leagues/Premier-League_v2.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.premier-league.20220327.json")));
stubFor(get("/injuries/football/england-premier-league/")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("sportsgambler.premier-league.20211122.html")));

Config config = spy(getConfig());
doReturn(false).when(config).isTeampUpdate();
executeMainProcess(config);

Assert.assertTrue(getLogOut(), getLogOut().contains("==="));
}

@Test
public void testKeepAtLeastOneGoalkeeperEvenIfAbsent() throws Exception {
prepareMainFrenchLigue2Mocks("MLEFEX6G-20220227", "2022", "20220227", "20220227");
Expand All @@ -81,8 +148,8 @@ public void testKeepAtLeastOneGoalkeeperEvenIfAbsent() throws Exception {
executeMainProcess(config);

Assert.assertTrue(getLogOut(), getLogOut().contains("All goalkeeper(s) are injured/absent, so maintained on the pitch"));
Assert.assertTrue(getLogOut(), getLogOut().contains("| G | Braat Quentin | 4.81 | 21 |"));
Assert.assertTrue(getLogOut(), getLogOut().contains("| G | Maronne Yanis | 0.00 | 7 |"));
Assert.assertTrue(getLogOut(), getLogOut().contains("| G | Braat Quentin | 4.81 | 19 |"));
Assert.assertTrue(getLogOut(), getLogOut().contains("| G | Maronne Yanis | 0.00 | 3 |"));
}

@Test
Expand Down Expand Up @@ -142,8 +209,9 @@ public void testCaptainAndBoostPlayerBonus2() throws Exception {
.willReturn(aResponse().withStatus(Response.Status.OK.getStatusCode()).withHeader("Content-Type", "application/json")
.withBody("Fake: mpg_match_team_formation_MLMHBPCB_3_1_12_5_4")));

stubFor(get("/builds").willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.builds.20211122.json")));
stubFor(get("/leagues/Premier-League")
stubFor(get("/builds.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.builds.20211122.json")));
stubFor(get("/leagues/Premier-League_v2.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.premier-league.20211122.json")));
stubFor(get("/injuries/football/england-premier-league/")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("sportsgambler.premier-league.20211122.html")));
Expand Down Expand Up @@ -303,10 +371,11 @@ public void testPrepareMercatoTurn0Day0EnglishSerieARecentFocusEnabled() throws
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.clubs.2021.json")));
stubFor(get("/division/mpg_division_MLMHBPCB_3_1/available-players").willReturn(
aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.division.available.players.MLMHBPCB.20210813.json")));
stubFor(get("/builds").willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.builds.20210813.json")));
stubFor(get("/leagues/Serie-A")
stubFor(get("/builds.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.builds.20210813.json")));
stubFor(get("/leagues/Serie-A_v2.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.serie-a.20210813.json")));
stubFor(get("/leagues/Premier-League")
stubFor(get("/leagues/Premier-League_v2.json")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.premier-league.20210813.json")));
stubFor(get("/injuries/football/italy-serie-a/")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("sportsgambler.serie-a.20210224.html")));
Expand Down Expand Up @@ -433,9 +502,9 @@ private static void prepareMainFrenchLigueMocks(String dashboard, int frenchLigu
}
if (StringUtils.isNotBlank(statsLeaguesDate)) {
dateDayFormat.parse(statsLeaguesDate);
stubFor(get("/builds").willReturn(
stubFor(get("/builds.json").willReturn(
aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.builds." + statsLeaguesDate + ".json")));
stubFor(get("/leagues/Ligue-" + frenchLigue).willReturn(aResponse().withHeader("Content-Type", "application/json")
stubFor(get("/leagues/Ligue-" + frenchLigue + "_v2.json").willReturn(aResponse().withHeader("Content-Type", "application/json")
.withBodyFile("mlnstats.ligue-" + frenchLigue + "." + statsLeaguesDate + ".json")));
}
if (StringUtils.isNotBlank(sportsGamblerDate)) {
Expand Down
Loading

0 comments on commit 587fbec

Please sign in to comment.