diff --git a/src/main/kotlin/com/stefata/sofiasupermarketsapi/model/ProductImage.kt b/src/main/kotlin/com/stefata/sofiasupermarketsapi/model/ProductImage.kt index 132cd34..457929c 100644 --- a/src/main/kotlin/com/stefata/sofiasupermarketsapi/model/ProductImage.kt +++ b/src/main/kotlin/com/stefata/sofiasupermarketsapi/model/ProductImage.kt @@ -6,5 +6,5 @@ import javax.persistence.Id @Entity data class ProductImage( @Id val product: String, - val url: String? + var url: String? ) diff --git a/src/main/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifier.kt b/src/main/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifier.kt index 8fb5499..a1822d5 100644 --- a/src/main/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifier.kt +++ b/src/main/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifier.kt @@ -4,6 +4,7 @@ import com.stefata.sofiasupermarketsapi.common.Log import com.stefata.sofiasupermarketsapi.common.Log.Companion.log import com.stefata.sofiasupermarketsapi.common.checkIfUrlHasAcceptableHttpResponse import com.stefata.sofiasupermarketsapi.image.GoogleImageSearch +import com.stefata.sofiasupermarketsapi.repository.ProductImageRepository import org.springframework.cache.CacheManager import org.springframework.scheduling.annotation.Scheduled import org.springframework.stereotype.Component @@ -12,7 +13,8 @@ import org.springframework.stereotype.Component @Component class ScheduledImagesVerifier( val googleImageSearch: GoogleImageSearch, - val cacheManager: CacheManager + val cacheManager: CacheManager, + val productImageRepository: ProductImageRepository, ) { @Scheduled(cron = "\${image.verifier.cron}") @@ -25,9 +27,21 @@ class ScheduledImagesVerifier( !checkIfUrlHasAcceptableHttpResponse(it) }?.forEach { googleImageSearch.search(it.key, false)?.let { imageUrl -> - log.info("Changing cached image of {} from {} to {}", it.key, it.value, imageUrl) + log.info( + "Changing cached image for {} from {} to {}", + it.key, it.value, imageUrl + ) productImagesCache[it.key] = imageUrl + productImageRepository.findById(it.key).ifPresent { dbRow -> + log.info( + "Changing database image for {} from {} to {}", + it.key, dbRow.url, imageUrl + ) + dbRow.url = imageUrl + productImageRepository.save(dbRow) + } } + } } diff --git a/src/test/kotlin/com/stefata/sofiasupermarketsapi/repository/ProductImageRepositoryTest.kt b/src/test/kotlin/com/stefata/sofiasupermarketsapi/repository/ProductImageRepositoryTest.kt index cf94e9e..f827beb 100644 --- a/src/test/kotlin/com/stefata/sofiasupermarketsapi/repository/ProductImageRepositoryTest.kt +++ b/src/test/kotlin/com/stefata/sofiasupermarketsapi/repository/ProductImageRepositoryTest.kt @@ -34,4 +34,21 @@ class ProductImageRepositoryTest { assertThat(missing.isPresent).isFalse() } + + @Test + fun `updating product`() { + val toSave = ProductImage("foo", "http://test.com") + + underTest.save(toSave) + + underTest.findById("foo").ifPresent { + it.url = "http://test69.com" + underTest.save(it) + } + + val updated = underTest.findById("foo") + + assertThat(updated.isPresent).isTrue() + assertThat(updated.get()).isEqualTo(toSave.copy(url = "http://test69.com")) + } } \ No newline at end of file diff --git a/src/test/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifierTest.kt b/src/test/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifierTest.kt index 4b503a8..ddd7185 100644 --- a/src/test/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifierTest.kt +++ b/src/test/kotlin/com/stefata/sofiasupermarketsapi/scheduled/ScheduledImagesVerifierTest.kt @@ -4,6 +4,8 @@ import assertk.assertThat import assertk.assertions.containsOnly import com.ninjasquad.springmockk.MockkBean import com.stefata.sofiasupermarketsapi.image.GoogleImageSearch +import com.stefata.sofiasupermarketsapi.model.ProductImage +import com.stefata.sofiasupermarketsapi.repository.ProductImageRepository import io.mockk.every import io.mockk.verify import org.junit.jupiter.api.Test @@ -15,6 +17,7 @@ import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.junit.jupiter.SpringExtension +import java.util.* @ExtendWith(SpringExtension::class) @ContextConfiguration( @@ -34,6 +37,9 @@ internal class ScheduledImagesVerifierTest { @MockkBean lateinit var googleImageSearch: GoogleImageSearch + @MockkBean + lateinit var productImageRepository: ProductImageRepository + @Autowired lateinit var underTest: ScheduledImagesVerifier @@ -43,11 +49,13 @@ internal class ScheduledImagesVerifierTest { @Test fun `test verifying images`() { val cache = cacheManager.getCache("productImages")?.nativeCache as MutableMap - cache["test"] = "https://p1.akcdn.net/full/652773636.bira-astika-ken-0-5-l.jpg" + val invalidUrl = "https://p1.akcdn.net/full/652773636.bira-astika-ken-0-5-l.jpg" + cache["test"] = invalidUrl cache["another test"] = "https://bbc.co.uk" every { googleImageSearch.search("test", false) } returns "https://www.telegraph.co.uk/" - + every { productImageRepository.findById("test") } returns Optional.of(ProductImage("test", invalidUrl)) + every { productImageRepository.save(any()) } returnsArgument 0 underTest.verifyImages() underTest.verifyImages() @@ -60,6 +68,10 @@ internal class ScheduledImagesVerifierTest { googleImageSearch.search("test", false) } + verify(exactly = 1) { + productImageRepository.save(ProductImage("test", "https://www.telegraph.co.uk/")) + } + } } \ No newline at end of file