Skip to content

Commit

Permalink
πŸ§ͺ Weather Test code
Browse files Browse the repository at this point in the history
  • Loading branch information
jun108059 committed Oct 3, 2024
1 parent 0800bb9 commit 4ab64aa
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/kotlin/nexters/weski/weather/WeatherUpdateService.kt
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 src/test/kotlin/nexters/weski/weather/WeatherControllerTest.kt
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 src/test/kotlin/nexters/weski/weather/WeatherServiceTest.kt
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)
}
}

0 comments on commit 4ab64aa

Please sign in to comment.