From 7a66b76f785d822738368f837487a115a14b2c81 Mon Sep 17 00:00:00 2001
From: This-Is-Ko <52279273+This-Is-Ko@users.noreply.github.com>
Date: Mon, 9 Oct 2023 00:03:56 +1100
Subject: [PATCH] Added tests for parsingService + updated logging
---
logback-spring.xml | 11 -----------
.../footballupdater/datasource/FbrefDataSource.java | 10 +++++-----
.../footballupdater/datasource/FotmobDataSource.java | 8 ++++----
.../com/ko/footballupdater/models/CheckedStatus.java | 5 +++++
.../java/com/ko/footballupdater/models/Player.java | 10 ++++++++++
.../models/PlayerMatchPerformanceStats.java | 5 +++++
.../ko/footballupdater/services/AmazonS3Service.java | 10 +++++-----
.../services/ImageGeneratorService.java | 4 ++--
.../ko/footballupdater/services/ParsingService.java | 8 ++++----
src/main/resources/logback-spring.xml | 7 +++++++
10 files changed, 47 insertions(+), 31 deletions(-)
delete mode 100644 logback-spring.xml
create mode 100644 src/main/resources/logback-spring.xml
diff --git a/logback-spring.xml b/logback-spring.xml
deleted file mode 100644
index 38fa4f4..0000000
--- a/logback-spring.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
- %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %kvp%n
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/main/java/com/ko/footballupdater/datasource/FbrefDataSource.java b/src/main/java/com/ko/footballupdater/datasource/FbrefDataSource.java
index be0ee84..4ff9c79 100644
--- a/src/main/java/com/ko/footballupdater/datasource/FbrefDataSource.java
+++ b/src/main/java/com/ko/footballupdater/datasource/FbrefDataSource.java
@@ -68,10 +68,10 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
for (Element resultRow : resultRows) {
// For games not played, appears as unused_sub class
if (!resultRow.getElementsByClass("unused_sub").isEmpty()) {
- log.atInfo().setMessage(player.getName() + " " + "Skip latest due to unused sub").addKeyValue("player", player.getName()).log();
+ log.atInfo().setMessage("Skip latest due to unused sub").addKeyValue("player", player.getName()).log();
continue;
} else if (!resultRow.getElementsByClass("partial_table").isEmpty()) {
- log.atInfo().setMessage(player.getName() + " " + "Spacer row skipped").addKeyValue("player", player.getName()).log();
+ log.atInfo().setMessage("Spacer row skipped").addKeyValue("player", player.getName()).log();
continue;
}
String latestMatchUrl = resultRow.select("th[data-stat=date] > a").attr("href");
@@ -81,17 +81,17 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
if (!resultRow.select("th[data-stat=date] > a").text().isEmpty()) {
selectedMatchDate = dateFormat.parse(resultRow.select("th[data-stat=date] > a").text());
} else {
- log.atInfo().setMessage(player.getName() + " - Unable to get date from match row").addKeyValue("player", player.getName()).log();
+ log.atInfo().setMessage("Unable to get date from match row").addKeyValue("player", player.getName()).log();
return null;
}
if (player.getCheckedStatus() != null) {
if (player.getCheckedStatus().getLatestCheckedMatchDate() != null && !(selectedMatchDate.compareTo(player.getCheckedStatus().getLatestCheckedMatchDate()) > 0)) {
- log.atInfo().setMessage(player.getName() + " - Selected match is not newer than last checked").addKeyValue("player", player.getName()).log();
+ log.atInfo().setMessage("Selected match is not newer than last checked").addKeyValue("player", player.getName()).log();
return null;
} else if (player.getCheckedStatus().getLatestCheckedMatchUrl() != null && player.getCheckedStatus().getLatestCheckedMatchUrl().equals(latestMatchUrl)) {
// No new updates
- log.atInfo().setMessage(player.getName() + " - latestMatchUrl matches last checked").addKeyValue("player", player.getName()).log();
+ log.atInfo().setMessage("latestMatchUrl matches last checked").addKeyValue("player", player.getName()).log();
return null;
}
} else {
diff --git a/src/main/java/com/ko/footballupdater/datasource/FotmobDataSource.java b/src/main/java/com/ko/footballupdater/datasource/FotmobDataSource.java
index f97e611..70ced5b 100644
--- a/src/main/java/com/ko/footballupdater/datasource/FotmobDataSource.java
+++ b/src/main/java/com/ko/footballupdater/datasource/FotmobDataSource.java
@@ -51,7 +51,7 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
// Assume first row is the latest match
Elements latestMatchRow = document.selectXpath("//main/div[2]/div[1]/div[4]/section/div/article/table/tbody/tr[1]/td[2]/a");
if (latestMatchRow.isEmpty()) {
- log.info("Cannot find any match results on player page");
+ log.atInfo().setMessage("Cannot find any match results on player page").addKeyValue("player", player.getName()).log();
return null;
}
@@ -82,7 +82,7 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
// Check whether this match is newer than last checked
Date selectedMatchDate = dateFormat.parse(jsonNode.get("general").get("matchTimeUTCDate").textValue());
if (player.getCheckedStatus().getLatestCheckedMatchDate() != null && !(selectedMatchDate.compareTo(player.getCheckedStatus().getLatestCheckedMatchDate()) > 0)) {
- log.info(player.getName() + " - Selected match is not newer than last checked");
+ log.atInfo().setMessage("Selected match is not newer than last checked").addKeyValue("player", player.getName()).log();
return null;
}
@@ -125,9 +125,9 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
}
}
}
- log.atInfo().setMessage(player.getName() + " " + "Unable to update player, parsed all players in the match").addKeyValue("player", player.getName()).log();
+ log.atInfo().setMessage("Unable to update player, parsed all players in the match").addKeyValue("player", player.getName()).log();
} catch (Exception ex) {
- log.warn("Error while trying to update player: " + player.getName(), ex);
+ log.atWarn().setMessage("Error while trying to update player").setCause(ex).addKeyValue("player", player.getName()).log();
}
return null;
}
diff --git a/src/main/java/com/ko/footballupdater/models/CheckedStatus.java b/src/main/java/com/ko/footballupdater/models/CheckedStatus.java
index 780c4e1..a8ae329 100644
--- a/src/main/java/com/ko/footballupdater/models/CheckedStatus.java
+++ b/src/main/java/com/ko/footballupdater/models/CheckedStatus.java
@@ -45,4 +45,9 @@ public CheckedStatus() {
public CheckedStatus(DataSourceSiteName siteName) {
this.siteName = siteName;
}
+
+ public CheckedStatus(Integer id, DataSourceSiteName siteName) {
+ this.id = id;
+ this.siteName = siteName;
+ }
}
diff --git a/src/main/java/com/ko/footballupdater/models/Player.java b/src/main/java/com/ko/footballupdater/models/Player.java
index 4efbf50..8ce2036 100644
--- a/src/main/java/com/ko/footballupdater/models/Player.java
+++ b/src/main/java/com/ko/footballupdater/models/Player.java
@@ -67,4 +67,14 @@ public Player(String name, Date dob) {
this.name = name;
this.dob = dob;
}
+
+ public Player(Integer id, String name, Team team, Date dob, Set images, Set dataSources, CheckedStatus checkedStatus) {
+ this.id = id;
+ this.name = name;
+ this.team = team;
+ this.dob = dob;
+ this.images = images;
+ this.dataSources = dataSources;
+ this.checkedStatus = checkedStatus;
+ }
}
\ No newline at end of file
diff --git a/src/main/java/com/ko/footballupdater/models/PlayerMatchPerformanceStats.java b/src/main/java/com/ko/footballupdater/models/PlayerMatchPerformanceStats.java
index c96bff2..9628a37 100644
--- a/src/main/java/com/ko/footballupdater/models/PlayerMatchPerformanceStats.java
+++ b/src/main/java/com/ko/footballupdater/models/PlayerMatchPerformanceStats.java
@@ -59,6 +59,11 @@ public class PlayerMatchPerformanceStats {
private Integer gkPenaltiesScoredAgainst;
private Integer gkPenaltiesSaved;
+ // For tests
+ public PlayerMatchPerformanceStats(DataSourceSiteName dataSourceSiteName) {
+ this.dataSourceSiteName = dataSourceSiteName;
+ }
+
public PlayerMatchPerformanceStats(DataSourceSiteName dataSourceSiteName, Match match, Integer minutesPlayed, Integer goals, Integer assists, Integer shots, Integer shotsBlocked, Integer fouls, Integer fouled, Integer offsides, String crossingAccuracyAll, Integer dispossessed, Integer touches, String tacklingSuccessAll, Integer defensiveActions, Integer recoveries, Integer duelsWon, Integer duelsLost, Integer groundDuelsWon, Integer aerialDuelsWon, Integer chancesCreatedAll, String passingAccuracyAll, Integer passesIntoFinalThird, String carriesSuccessAll) {
this.dataSourceSiteName = dataSourceSiteName;
this.match = match;
diff --git a/src/main/java/com/ko/footballupdater/services/AmazonS3Service.java b/src/main/java/com/ko/footballupdater/services/AmazonS3Service.java
index f51edf8..05db301 100644
--- a/src/main/java/com/ko/footballupdater/services/AmazonS3Service.java
+++ b/src/main/java/com/ko/footballupdater/services/AmazonS3Service.java
@@ -42,7 +42,7 @@ public void uploadtoS3(InstagramPost post) {
.withCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(request);
String imageUrl = s3Client.getUrl(amazonS3Properties.getBucketName(), imageFileName).toString();
- log.atInfo().setMessage(post.getPlayer().getName() + " - Successfully uploaded image " + imageFileName + " to S3 @ " + imageUrl).log();
+ log.atInfo().setMessage("Successfully uploaded image " + imageFileName + " to S3 @ " + imageUrl).addKeyValue("player", post.getPlayer().getName()).log();
post.getImagesS3Urls().add(imageUrl);
cleanUpFile(file);
@@ -50,11 +50,11 @@ public void uploadtoS3(InstagramPost post) {
} catch (AmazonServiceException ex) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
- log.warn(post.getPlayer().getName() + " - Error attempting to upload", ex);
+ log.atWarn().setMessage("Error attempting to upload").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
} catch (SdkClientException ex) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
- log.warn(post.getPlayer().getName() + " - Error attempting to upload", ex);
+ log.atWarn().setMessage("Error attempting to upload").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
}
} else {
log.atInfo().setMessage(post.getPlayer().getName() + " - No images to upload").log();
@@ -64,9 +64,9 @@ public void uploadtoS3(InstagramPost post) {
// Delete files uploaded to S3
private void cleanUpFile(File file) {
if (file.delete()) {
- log.info("Deleted file: " + file.getAbsolutePath());
+ log.atInfo().setMessage("Deleted file: " + file.getAbsolutePath()).log();
} else {
- log.warn("Unable to delete file: " + file.getAbsolutePath());
+ log.atWarn().setMessage("Unable to delete file: " + file.getAbsolutePath()).log();
}
}
}
diff --git a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java
index fc1bdfe..14a8f9f 100644
--- a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java
+++ b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java
@@ -85,7 +85,7 @@ public void generatePlayerStatImage(InstagramPost post) throws Exception {
createdImageCounter++;
saveImage(post, image, createdImageCounter);
} catch (Exception ex) {
- log.warn(post.getPlayer().getName() + " - Error while generating stat image ", ex);
+ log.atWarn().setMessage("Error while generating stat image").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
throw new Exception(post.getPlayer().getName() + " - Error while generating stat image ", ex);
}
}
@@ -138,7 +138,7 @@ private void saveImage(InstagramPost post, BufferedImage image, int createdImage
String outputImageFilePath = imageGeneratorProperies.getOutputPath() + fileName;
ImageIO.write(image, "jpg", new File(outputImageFilePath));
post.getImagesFileNames().add(fileName);
- log.atInfo().setMessage(post.getPlayer().getName() + " - Generated stat image " + createdImageCounter).addKeyValue("player", post.getPlayer().getName()).log();
+ log.atInfo().setMessage("Generated stat image " + createdImageCounter).addKeyValue("player", post.getPlayer().getName()).log();
}
private static List getZeroValueFilter() {
diff --git a/src/main/java/com/ko/footballupdater/services/ParsingService.java b/src/main/java/com/ko/footballupdater/services/ParsingService.java
index 5aea3ac..74d8ebf 100644
--- a/src/main/java/com/ko/footballupdater/services/ParsingService.java
+++ b/src/main/java/com/ko/footballupdater/services/ParsingService.java
@@ -109,7 +109,7 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player) {
if (dataSources.stream().anyMatch(o -> o.getSiteName().equals(source))) {
DataSource dataSource = dataSources.stream().filter(o -> o.getSiteName().equals(source)).findFirst().get();
- log.info(player.getName() + " - Last checked was " + player.getCheckedStatus().getSiteName() + "; dataSource is " + dataSource.getSiteName());
+ log.atInfo().setMessage("Attempting dataSource " + dataSource.getSiteName()).addKeyValue("player", player.getName()).log();
for (DataSourceParser dataSourceParser : dataSourceParsers) {
if (dataSourceParser.getDataSourceSiteName().equals(dataSource.getSiteName())) {
@@ -117,12 +117,12 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player) {
Document doc = Jsoup.connect(dataSource.getUrl()).get();
PlayerMatchPerformanceStats playerMatchPerformanceStats = dataSourceParser.parsePlayerMatchData(player, doc);
if (playerMatchPerformanceStats != null) {
- log.atInfo().setMessage(player.getName() + " - " + dataSource.getSiteName() + " - Successfully parse player data").addKeyValue("player", player.getName()).log();
+ log.atInfo().setMessage(dataSource.getSiteName() + " - Successfully parse player data").addKeyValue("player", player.getName()).log();
player.getCheckedStatus().setSiteName(dataSource.getSiteName());
return playerMatchPerformanceStats;
}
- } catch (IOException e) {
- log.warn("Unable to retrieve page at " + dataSource.getUrl() + '\n' + e);
+ } catch (IOException ex) {
+ log.atWarn().setMessage("Unable to retrieve page at " + dataSource.getUrl()).setCause(ex).addKeyValue("player", player.getName()).log();
return null;
}
break;
diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml
new file mode 100644
index 0000000..601155b
--- /dev/null
+++ b/src/main/resources/logback-spring.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file