Skip to content

Commit

Permalink
change database as well in case of invalid url for image
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Aug 6, 2021
1 parent 8557a0c commit 3672d80
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import javax.persistence.Id
@Entity
data class ProductImage(
@Id val product: String,
val url: String?
var url: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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)
}
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -34,6 +37,9 @@ internal class ScheduledImagesVerifierTest {
@MockkBean
lateinit var googleImageSearch: GoogleImageSearch

@MockkBean
lateinit var productImageRepository: ProductImageRepository

@Autowired
lateinit var underTest: ScheduledImagesVerifier

Expand All @@ -43,11 +49,13 @@ internal class ScheduledImagesVerifierTest {
@Test
fun `test verifying images`() {
val cache = cacheManager.getCache("productImages")?.nativeCache as MutableMap<String, String>
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()

Expand All @@ -60,6 +68,10 @@ internal class ScheduledImagesVerifierTest {
googleImageSearch.search("test", false)
}

verify(exactly = 1) {
productImageRepository.save(ProductImage("test", "https://www.telegraph.co.uk/"))
}


}
}

0 comments on commit 3672d80

Please sign in to comment.