From b2543912fab87d012cc4ac7d13ffafcbbe54bc23 Mon Sep 17 00:00:00 2001 From: This-Is-Ko <52279273+This-Is-Ko@users.noreply.github.com> Date: Sat, 19 Oct 2024 15:08:39 +1100 Subject: [PATCH 1/6] Added config check to image gen --- .../com/ko/footballupdater/models/Post.java | 9 +++++++-- .../services/ImageGeneratorService.java | 1 + .../footballupdater/services/PlayerService.java | 17 +++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ko/footballupdater/models/Post.java b/src/main/java/com/ko/footballupdater/models/Post.java index 38d278f..58b3330 100644 --- a/src/main/java/com/ko/footballupdater/models/Post.java +++ b/src/main/java/com/ko/footballupdater/models/Post.java @@ -113,13 +113,11 @@ public Post(PostType postType, Player player, PlayerMatchPerformanceStats player this.postType = postType; this.player = player; this.playerMatchPerformanceStats = playerMatchPerformanceStats; - imagesUrls = new ArrayList<>(); } public Post(Integer id, boolean postedStatus) { this.id = id; this.postedStatus = postedStatus; - imagesUrls = new ArrayList<>(); } public Post(Player player, List imagesUrls) { @@ -127,4 +125,11 @@ public Post(Player player, List imagesUrls) { this.imagesUrls = imagesUrls; this.postedStatus = false; } + + public List getImagesUrls() { + if (imagesUrls == null) { + imagesUrls = new ArrayList<>(); + } + return imagesUrls; + } } diff --git a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java index c75af0a..88fc9a6 100644 --- a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java +++ b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java @@ -51,6 +51,7 @@ public class ImageGeneratorService { @Autowired private TeamHelpers teamHelpers; + @Autowired private ImageGeneratorProperties imageGeneratorProperties; diff --git a/src/main/java/com/ko/footballupdater/services/PlayerService.java b/src/main/java/com/ko/footballupdater/services/PlayerService.java index 4d382dd..e277059 100644 --- a/src/main/java/com/ko/footballupdater/services/PlayerService.java +++ b/src/main/java/com/ko/footballupdater/services/PlayerService.java @@ -1,18 +1,17 @@ package com.ko.footballupdater.services; import com.amazonaws.services.kms.model.NotFoundException; +import com.ko.footballupdater.configuration.ImageGeneratorProperties; import com.ko.footballupdater.configuration.InstagramPostProperies; import com.ko.footballupdater.models.AlternativePlayerName; import com.ko.footballupdater.models.CheckedStatus; import com.ko.footballupdater.models.DataSource; import com.ko.footballupdater.models.DataSourceSiteName; import com.ko.footballupdater.models.DataSourceType; -import com.ko.footballupdater.models.Hashtag; import com.ko.footballupdater.models.Player; import com.ko.footballupdater.models.PlayerMatchPerformanceStats; import com.ko.footballupdater.models.Post; import com.ko.footballupdater.models.PostType; -import com.ko.footballupdater.models.Team; import com.ko.footballupdater.repositories.PlayerRepository; import com.ko.footballupdater.repositories.PostRepository; import com.ko.footballupdater.repositories.TeamRepository; @@ -64,6 +63,9 @@ public class PlayerService { @Autowired private InstagramPostProperies instagramPostProperies; + @Autowired + private ImageGeneratorProperties imageGeneratorProperties; + public Player addPlayer(Player newPlayer, DataSourceSiteName dataSourceSiteName) throws IllegalArgumentException { if (!playerRepository.findByNameEquals(newPlayer.getName()).isEmpty()) { throw new IllegalArgumentException("Player already exists"); @@ -148,10 +150,12 @@ public UpdatePlayersResponse updateDataForPlayers(List requestPlayersToU // Generate post and caption Post post = new Post(PostType.ALL_STAT_POST, player, playerMatchPerformanceStats); try { - // Generate stat images - imageGeneratorService.generatePlayerStatImage(post); - // Upload stat images to s3 - amazonS3Service.uploadToS3(post); + if (imageGeneratorProperties.isEnabled()) { + // Generate stat images + imageGeneratorService.generatePlayerStatImage(post); + // Upload stat images to s3 + amazonS3Service.uploadToS3(post); + } // Generate any additional team hashtags String hashtags = ""; if (post.getPlayerMatchPerformanceStats().getMatch().getRelevantTeam() != null && !post.getPlayerMatchPerformanceStats().getMatch().getRelevantTeam().isEmpty()) { @@ -198,6 +202,7 @@ public UpdatePlayersResponse updateDataForPlayers(List requestPlayersToU } playerRepository.saveAll(playersToUpdate); log.atInfo().setMessage("Updated player entries with latest data").log(); + // Populate response response.setPlayersUpdated(playersToUpdate); response.setNumPlayersUpdated(playersToUpdate.size()); From f7d43f9145429b24bd909a78cd384591fbe36287 Mon Sep 17 00:00:00 2001 From: This-Is-Ko <52279273+This-Is-Ko@users.noreply.github.com> Date: Sun, 20 Oct 2024 11:35:42 +1100 Subject: [PATCH 2/6] Added debug logs --- .../com/ko/footballupdater/services/ImageGeneratorService.java | 2 ++ src/main/resources/application.properties | 1 + 2 files changed, 3 insertions(+) diff --git a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java index 88fc9a6..420b165 100644 --- a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java +++ b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java @@ -352,6 +352,7 @@ public void generateStandoutStatsImage(Post post, List Date: Sun, 20 Oct 2024 11:36:59 +1100 Subject: [PATCH 3/6] Clean up logs --- .../ko/footballupdater/services/ImageGeneratorService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java index 420b165..4113440 100644 --- a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java +++ b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java @@ -383,10 +383,10 @@ public void generateStandoutStatsImage(Post post, List Date: Sun, 20 Oct 2024 11:38:42 +1100 Subject: [PATCH 4/6] Clean up --- .../services/ImageGeneratorService.java | 11 ++++++++--- .../ko/footballupdater/services/PlayerService.java | 4 ---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java index 4113440..d364ee2 100644 --- a/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java +++ b/src/main/java/com/ko/footballupdater/services/ImageGeneratorService.java @@ -5,14 +5,13 @@ import com.ko.footballupdater.configuration.TeamProperties; import com.ko.footballupdater.models.ImageStatEntry; import com.ko.footballupdater.models.PlayerMatchPerformanceStats; -import com.ko.footballupdater.models.PostType; import com.ko.footballupdater.models.Post; +import com.ko.footballupdater.models.PostType; import com.ko.footballupdater.models.Team; import com.ko.footballupdater.models.form.HorizontalTranslation; import com.ko.footballupdater.models.form.ImageGenParams; import com.ko.footballupdater.models.form.StatisticEntryGenerateDto; import com.ko.footballupdater.models.form.VerticalTranslation; -import com.ko.footballupdater.repositories.TeamRepository; import com.ko.footballupdater.utils.DateTimeHelper; import com.ko.footballupdater.utils.LogHelper; import com.ko.footballupdater.utils.PostHelper; @@ -22,7 +21,13 @@ import org.springframework.stereotype.Service; import javax.imageio.ImageIO; -import java.awt.*; +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.GradientPaint; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; diff --git a/src/main/java/com/ko/footballupdater/services/PlayerService.java b/src/main/java/com/ko/footballupdater/services/PlayerService.java index e277059..2416308 100644 --- a/src/main/java/com/ko/footballupdater/services/PlayerService.java +++ b/src/main/java/com/ko/footballupdater/services/PlayerService.java @@ -14,7 +14,6 @@ import com.ko.footballupdater.models.PostType; import com.ko.footballupdater.repositories.PlayerRepository; import com.ko.footballupdater.repositories.PostRepository; -import com.ko.footballupdater.repositories.TeamRepository; import com.ko.footballupdater.request.RequestDataSource; import com.ko.footballupdater.request.UpdatePlayerRequest; import com.ko.footballupdater.responses.UpdatePlayersResponse; @@ -39,9 +38,6 @@ public class PlayerService { @Autowired private PlayerRepository playerRepository; - @Autowired - private TeamRepository teamRepository; - @Autowired private PostRepository postRepository; From ae8083ea7756772a21a5eff667968fdec0bc5622 Mon Sep 17 00:00:00 2001 From: This-Is-Ko <52279273+This-Is-Ko@users.noreply.github.com> Date: Sun, 20 Oct 2024 13:15:29 +1100 Subject: [PATCH 5/6] Testcase updates --- .../unit/services/PlayerServiceTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java b/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java index c7bf7c2..99b4292 100644 --- a/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java +++ b/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java @@ -1,6 +1,7 @@ package com.ko.footballupdater.unit.services; import com.amazonaws.services.kms.model.NotFoundException; +import com.ko.footballupdater.configuration.ImageGeneratorProperties; import com.ko.footballupdater.configuration.InstagramPostProperies; import com.ko.footballupdater.models.CheckedStatus; import com.ko.footballupdater.models.Hashtag; @@ -43,6 +44,8 @@ import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class PlayerServiceTest { @@ -77,12 +80,16 @@ public class PlayerServiceTest { @Mock private InstagramPostProperies instagramPostProperies; + @Mock + private ImageGeneratorProperties imageGeneratorProperties; + @BeforeEach public void setup() { MockitoAnnotations.openMocks(this); when(instagramPostProperies.getVersion()).thenReturn(2); when(instagramPostProperies.getDefaultHashtags()).thenReturn("#default"); when(instagramPostProperies.getAccountName()).thenReturn("Insta account name"); + when(imageGeneratorProperties.isEnabled()).thenReturn(true); } @Test @@ -197,6 +204,51 @@ public void updateDataForAllPlayers_playersFound_successful() throws Exception { assertEquals(2, response.getPlayersUpdated().size()); assertEquals(playerToUpdate1, response.getPlayersUpdated().get(0)); assertEquals(playerToUpdate2, response.getPlayersUpdated().get(1)); + + // Image gen and upload should be called for each player + verify(imageGeneratorService, times(2)).generatePlayerStatImage(any(Post.class)); + verify(amazonS3Service, times(2)).uploadToS3(any(Post.class)); + } + + @Test + public void updateDataForAllPlayers_playersFound_imageGenIsDisabled_successful() throws Exception { + when(imageGeneratorProperties.isEnabled()).thenReturn(false); + + List players = new ArrayList<>(); + Player playerToUpdate1 = new Player("Player1"); + playerToUpdate1.setCheckedStatus(new CheckedStatus()); + playerToUpdate1.setId(1); + Player playerToUpdate2 = new Player("Player1"); + playerToUpdate2.setCheckedStatus(new CheckedStatus()); + playerToUpdate2.setId(2); + players.add(playerToUpdate1); + players.add(playerToUpdate2); + when(playerRepository.findAll()).thenReturn(players); + + PlayerMatchPerformanceStats mockPerformanceStats = mock(PlayerMatchPerformanceStats.class); + when(parsingService.parsePlayerMatchData(playerToUpdate1)).thenReturn(mockPerformanceStats); + when(parsingService.parsePlayerMatchData(playerToUpdate2)).thenReturn(mockPerformanceStats); + + Match match = new Match("https://url", Date.from(Instant.now()), "homeTeamName", "awayTeamName", "relevantTeamName"); + when(mockPerformanceStats.getMatch()).thenReturn(match); + + when(emailService.sendEmailUpdate(anyList())).thenReturn(true); + + when(postRepository.save(any(Post.class))).thenReturn(new Post()); + + when(playerRepository.saveAll(any())).thenReturn(List.of(playerToUpdate1)); + UpdatePlayersResponse response = playerService.updateDataForAllPlayers(); + + assertTrue(response.isEmailSent()); + assertEquals(2, response.getNumPlayersUpdated()); + assertNotNull(response.getPlayersUpdated()); + assertEquals(2, response.getPlayersUpdated().size()); + assertEquals(playerToUpdate1, response.getPlayersUpdated().get(0)); + assertEquals(playerToUpdate2, response.getPlayersUpdated().get(1)); + + // Should not call image gen + verify(imageGeneratorService, times(0)).generatePlayerStatImage(any(Post.class)); + verify(amazonS3Service, times(0)).uploadToS3(any(Post.class)); } @Test From 8438540429dc74c6930acc9b346e12139643585f Mon Sep 17 00:00:00 2001 From: This-Is-Ko <52279273+This-Is-Ko@users.noreply.github.com> Date: Sun, 20 Oct 2024 13:15:49 +1100 Subject: [PATCH 6/6] Clean up --- .../footballupdater/unit/services/PlayerServiceTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java b/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java index 99b4292..d386636 100644 --- a/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java +++ b/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java @@ -4,15 +4,12 @@ import com.ko.footballupdater.configuration.ImageGeneratorProperties; import com.ko.footballupdater.configuration.InstagramPostProperies; import com.ko.footballupdater.models.CheckedStatus; -import com.ko.footballupdater.models.Hashtag; import com.ko.footballupdater.models.Match; import com.ko.footballupdater.models.Player; import com.ko.footballupdater.models.PlayerMatchPerformanceStats; import com.ko.footballupdater.models.Post; -import com.ko.footballupdater.models.Team; import com.ko.footballupdater.repositories.PlayerRepository; import com.ko.footballupdater.repositories.PostRepository; -import com.ko.footballupdater.repositories.TeamRepository; import com.ko.footballupdater.responses.UpdatePlayersResponse; import com.ko.footballupdater.services.AmazonS3Service; import com.ko.footballupdater.services.EmailService; @@ -28,11 +25,9 @@ import java.time.Instant; import java.util.ArrayList; -import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Optional; -import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -56,9 +51,6 @@ public class PlayerServiceTest { @Mock private PlayerRepository playerRepository; - @Mock - private TeamRepository teamRepository; - @Mock private PostRepository postRepository;