diff --git a/src/main/kotlin/nexters/weski/weather/WeatherUpdateService.kt b/src/main/kotlin/nexters/weski/weather/WeatherUpdateService.kt new file mode 100644 index 0000000..49cf2a9 --- /dev/null +++ b/src/main/kotlin/nexters/weski/weather/WeatherUpdateService.kt @@ -0,0 +1,22 @@ +package nexters.weski.weather + + +import nexters.weski.ski_resort.SkiResortRepository +import org.springframework.scheduling.annotation.Scheduled +import org.springframework.stereotype.Service + +@Service +class WeatherUpdateService( + private val skiResortRepository: SkiResortRepository, + private val currentWeatherRepository: CurrentWeatherRepository, +) { +// @Scheduled(fixedRate = 3600000) // 1시간마다 실행 + fun updateWeatherData() { + val skiResorts = skiResortRepository.findAll() + skiResorts.forEach { resort -> + println(resort) + // 외부 API 호출하여 날씨 정보 가져오기 + // currentWeatherRepository.save(업데이트된 데이터) + } + } +} diff --git a/src/test/kotlin/nexters/weski/weather/WeatherControllerTest.kt b/src/test/kotlin/nexters/weski/weather/WeatherControllerTest.kt new file mode 100644 index 0000000..f8594d6 --- /dev/null +++ b/src/test/kotlin/nexters/weski/weather/WeatherControllerTest.kt @@ -0,0 +1,48 @@ +package nexters.weski.weather + +import com.ninjasquad.springmockk.MockkBean +import io.mockk.every +import nexters.weski.common.config.JpaConfig +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.context.annotation.ComponentScan +import org.springframework.context.annotation.FilterType +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status + +@WebMvcTest(WeatherController::class) +@ComponentScan( + excludeFilters = [ComponentScan.Filter( + type = FilterType.ASSIGNABLE_TYPE, + classes = [JpaConfig::class, WeatherUpdateService::class] + )] +) +class WeatherControllerTest @Autowired constructor( + private val mockMvc: MockMvc +) { + + @MockkBean + lateinit var weatherService: WeatherService + + @Test + fun `GET api_weather_resortId should return weather data`() { + // Given + val resortId = 1L + val weatherDto = WeatherDto( + resortId, + CurrentWeatherDto(-5, -2, -8, -10, "눈이 내리고 있습니다.", "눈"), + listOf(), + listOf() + ) + every { weatherService.getWeatherByResortId(resortId) } returns weatherDto + + // When & Then + mockMvc.perform(get("/api/weather/$resortId")) + .andExpect(status().isOk) + .andExpect(jsonPath("$.currentWeather.temperature").value(-5)) + .andExpect(jsonPath("$.currentWeather.condition").value("눈")) + } +} \ No newline at end of file diff --git a/src/test/kotlin/nexters/weski/weather/WeatherServiceTest.kt b/src/test/kotlin/nexters/weski/weather/WeatherServiceTest.kt new file mode 100644 index 0000000..1cd627c --- /dev/null +++ b/src/test/kotlin/nexters/weski/weather/WeatherServiceTest.kt @@ -0,0 +1,44 @@ +package nexters.weski.weather + +import io.mockk.every +import io.mockk.mockk +import nexters.weski.ski_resort.ResortStatus +import nexters.weski.ski_resort.SkiResort +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test + +class WeatherServiceTest { + + private val currentWeatherRepository: CurrentWeatherRepository = mockk() + private val hourlyWeatherRepository: HourlyWeatherRepository = mockk() + private val dailyWeatherRepository: DailyWeatherRepository = mockk() + private val weatherService = + WeatherService(currentWeatherRepository, hourlyWeatherRepository, dailyWeatherRepository) + + @Test + fun `getWeatherByResortId should return WeatherDto`() { + // Given + val resortId = 1L + val skiResort = SkiResort(resortId, "스키장 A", ResortStatus.운영중, null, null, 5, 10) + val currentWeather = CurrentWeather( + resortId, -5, -2, -8, -10, "눈이 내리고 있습니다.", "눈", skiResort + ) + every { currentWeatherRepository.findBySkiResortResortId(resortId) } returns currentWeather + every { + hourlyWeatherRepository.findAllBySkiResortResortIdAndForecastTimeBetween( + resortId, + any(), + any() + ) + } returns listOf() + every { dailyWeatherRepository.findAllBySkiResortResortId(resortId) } returns listOf() + + // When + val result = weatherService.getWeatherByResortId(resortId) + + // Then + assertNotNull(result) + assertEquals(-5, result?.currentWeather?.temperature) + } +} \ No newline at end of file