Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #229 : Update mpgstats endpoints to v2 #230

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```
17 changes: 9 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<version>1.9-SNAPSHOT</version>
<name>MPG Coach Bot</name>
<properties>
<goals>package</goals>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<main.class>org.blondin.mpg.Main</main.class>
<jdk.version>1.8</jdk.version>
<jersey.version>2.35</jersey.version>
<jackson.version>2.13.1</jackson.version>
<slf4j.version>1.7.32</slf4j.version>
<log4j.version>2.17.1</log4j.version>
<jersey.version>3.0.4</jersey.version>
<jackson.version>2.13.2</jackson.version>
<slf4j.version>1.7.36</slf4j.version>
<log4j.version>2.17.2</log4j.version>
<!-- Goals for release plugin-->
<goals>package</goals>
</properties>
<scm>
<connection>scm:git:https://github.com/axel3rd/mpg-coach-bot</connection>
Expand All @@ -26,15 +27,15 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.10.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.1</version>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
Expand Down Expand Up @@ -204,7 +205,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.2.0</version>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/blondin/mpg/AbstractClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.ServiceUnavailableException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
Expand All @@ -42,6 +32,17 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.ServiceUnavailableException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;

public abstract class AbstractClient {

private static final Logger LOG = LoggerFactory.getLogger(AbstractClient.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.blondin.mpg.out;

import javax.ws.rs.ServiceUnavailableException;

import org.apache.commons.lang3.ObjectUtils;
import org.blondin.mpg.config.Config;
import org.blondin.mpg.out.model.OutType;
Expand All @@ -12,6 +10,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.ws.rs.ServiceUnavailableException;

/**
* Wrapper for:<br/>
* - https://www.sportsgambler.com/injuries/football<br/>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/blondin/mpg/root/MpgClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.lang3.StringUtils;
import org.blondin.mpg.AbstractClient;
import org.blondin.mpg.config.Config;
Expand All @@ -21,6 +18,9 @@
import org.blondin.mpg.root.model.Team;
import org.blondin.mpg.root.model.UserSignIn;

import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;

/**
* Client for https://www.mpgstats.fr/
*/
Expand Down
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
93 changes: 81 additions & 12 deletions src/test/java/org/blondin/mpg/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import java.util.Arrays;
import java.util.Date;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.Response;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -33,6 +30,9 @@
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.stubbing.Scenario;

import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.core.Response;

public class MainTest extends AbstractMockTestClient {

@Test
Expand Down 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
4 changes: 2 additions & 2 deletions src/test/java/org/blondin/mpg/SslCertificateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import javax.ws.rs.ProcessingException;

import org.blondin.mpg.config.Config;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

import com.github.tomakehurst.wiremock.junit.WireMockRule;

import jakarta.ws.rs.ProcessingException;

public class SslCertificateTest {

@Rule
Expand Down
Loading