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..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; @@ -51,6 +56,7 @@ public class ImageGeneratorService { @Autowired private TeamHelpers teamHelpers; + @Autowired private ImageGeneratorProperties imageGeneratorProperties; @@ -351,6 +357,7 @@ public void generateStandoutStatsImage(Post post, 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 +198,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()); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 398e3da..4361a96 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -58,6 +58,7 @@ tiktok.api.clientKey=${TIKTOK_API_CLIENT_KEY} tiktok.api.clientSecret=${TIKTOK_API_CLIENT_SECRET} tiktok.api.scope=${TIKTOK_API_SCOPE} tiktok.api.redirectUri=${TIKTOK_API_REDIRECT_URI} +tiktok.api.privacyLevel=${TIKTOK_API_PRIVACY_LEVEL} # Team team.nameSuffixesToRemove=${TEAM_NAME_SUFFIXES_TO_REMOVE} 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..d386636 100644 --- a/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java +++ b/src/test/java/com/ko/footballupdater/unit/services/PlayerServiceTest.java @@ -1,17 +1,15 @@ 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; 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; @@ -27,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; @@ -43,6 +39,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 { @@ -53,9 +51,6 @@ public class PlayerServiceTest { @Mock private PlayerRepository playerRepository; - @Mock - private TeamRepository teamRepository; - @Mock private PostRepository postRepository; @@ -77,12 +72,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 +196,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