-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/nexters/weski/weather/WeatherUpdateService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(μ λ°μ΄νΈλ λ°μ΄ν°) | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/kotlin/nexters/weski/weather/WeatherControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("λ")) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/kotlin/nexters/weski/weather/WeatherServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |