|
| 1 | +package com.back.koreaTravelGuide.domain.ai.tour.service.core |
| 2 | + |
| 3 | +import com.back.koreaTravelGuide.domain.ai.tour.client.TourApiClient |
| 4 | +import com.back.koreaTravelGuide.domain.ai.tour.dto.TourItem |
| 5 | +import com.back.koreaTravelGuide.domain.ai.tour.dto.TourParams |
| 6 | +import com.back.koreaTravelGuide.domain.ai.tour.dto.TourResponse |
| 7 | +import com.back.koreaTravelGuide.domain.ai.tour.service.usecase.TourAreaBasedUseCase |
| 8 | +import io.mockk.every |
| 9 | +import io.mockk.mockk |
| 10 | +import io.mockk.verify |
| 11 | +import org.junit.jupiter.api.DisplayName |
| 12 | +import org.junit.jupiter.api.Test |
| 13 | +import org.springframework.beans.factory.annotation.Autowired |
| 14 | +import org.springframework.cache.CacheManager |
| 15 | +import org.springframework.cache.annotation.EnableCaching |
| 16 | +import org.springframework.cache.concurrent.ConcurrentMapCacheManager |
| 17 | +import org.springframework.context.annotation.Bean |
| 18 | +import org.springframework.context.annotation.Configuration |
| 19 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig |
| 20 | +import kotlin.test.assertEquals |
| 21 | + |
| 22 | +@SpringJUnitConfig(TourAreaBasedServiceCoreCacheTest.Config::class) |
| 23 | +class TourAreaBasedServiceCoreCacheTest { |
| 24 | + @Autowired |
| 25 | + private lateinit var service: TourAreaBasedUseCase |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private lateinit var tourApiClient: TourApiClient |
| 29 | + |
| 30 | + @DisplayName("fetchAreaBasedTours - 동일 파라미터 두 번 호출 시 API는 한 번만 호출된다") |
| 31 | + @Test |
| 32 | + fun cachesAreaBasedTours() { |
| 33 | + val params = TourParams(contentTypeId = "15", areaCode = "3", sigunguCode = "5") |
| 34 | + val apiResponse = |
| 35 | + TourResponse( |
| 36 | + items = |
| 37 | + listOf( |
| 38 | + TourItem( |
| 39 | + contentId = "88888", |
| 40 | + contentTypeId = "15", |
| 41 | + createdTime = "202401010000", |
| 42 | + modifiedTime = "202401020000", |
| 43 | + title = "캐시 검증 관광지", |
| 44 | + addr1 = "대전 어딘가", |
| 45 | + areaCode = "3", |
| 46 | + firstimage = null, |
| 47 | + firstimage2 = null, |
| 48 | + mapX = null, |
| 49 | + mapY = null, |
| 50 | + distance = null, |
| 51 | + mlevel = null, |
| 52 | + sigunguCode = "5", |
| 53 | + lDongRegnCd = null, |
| 54 | + lDongSignguCd = null, |
| 55 | + ), |
| 56 | + ), |
| 57 | + ) |
| 58 | + |
| 59 | + every { tourApiClient.fetchTourInfo(params) } returns apiResponse |
| 60 | + |
| 61 | + val firstCall = service.fetchAreaBasedTours(params) |
| 62 | + val secondCall = service.fetchAreaBasedTours(params) |
| 63 | + |
| 64 | + assertEquals(apiResponse, firstCall) |
| 65 | + assertEquals(apiResponse, secondCall) |
| 66 | + verify(exactly = 1) { tourApiClient.fetchTourInfo(params) } |
| 67 | + } |
| 68 | + |
| 69 | + @Configuration |
| 70 | + @EnableCaching |
| 71 | + class Config { |
| 72 | + @Bean |
| 73 | + fun tourApiClient(): TourApiClient = mockk(relaxed = true) |
| 74 | + |
| 75 | + @Bean |
| 76 | + fun cacheManager(): CacheManager = ConcurrentMapCacheManager("tourAreaBased", "tourLocationBased", "tourDetail") |
| 77 | + |
| 78 | + @Bean |
| 79 | + fun tourAreaBasedServiceCore(tourApiClient: TourApiClient): TourAreaBasedServiceCore = TourAreaBasedServiceCore(tourApiClient) |
| 80 | + } |
| 81 | +} |
0 commit comments