Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Házi feladat megoldás #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
testImplementation(kotlin("test"))
testImplementation("io.mockk:mockk:1.4.1")
testImplementation("org.mockito:mockito-core:5.11.0")
}

tasks.test {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/DataUnavailableException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package hu.vanio.kotlin.feladat.ms

class DataUnavailableException(message: String): Exception(message) {
}
3 changes: 3 additions & 0 deletions app/src/main/kotlin/DayWithTemperature.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package hu.vanio.kotlin.feladat.ms

class DayWithTemperature (val day: Int, val temp: Double){ }
6 changes: 6 additions & 0 deletions app/src/main/kotlin/HourlyData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hu.vanio.kotlin.feladat.ms

import java.time.LocalDateTime


data class HourlyData(val time: Array<LocalDateTime>, val temperature_2m: Array<String>)
3 changes: 3 additions & 0 deletions app/src/main/kotlin/HourlyUnits.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package hu.vanio.kotlin.feladat.ms

data class HourlyUnits(val time: String, val temperature_2m: String)
47 changes: 45 additions & 2 deletions app/src/main/kotlin/WeatherApp.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
package hu.vanio.kotlin.feladat.ms

import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

@SpringBootApplication
class WeatherApp
class WeatherApp {
fun calculateAvgTemp(getUrl: String): MutableMap<Int, Double> {
val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder()
.uri(URI.create(getUrl))
.build();

val response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw DataUnavailableException("Weather datas are unavailable! Response was: " + response.body())
}
val mapper = jacksonObjectMapper()
mapper.registerModule(JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
val weatherData: WeatherData = mapper.readValue(response.body())
val times = weatherData.hourly.time.toList()
val temperatures = weatherData.hourly.temperature_2m.toList()
val dayWithTemperatures = mutableListOf<DayWithTemperature>()
for (i in times.indices) {
dayWithTemperatures.add(DayWithTemperature(times[i].dayOfMonth, temperatures[i].toDouble()))
}

val daysWithTemperaturesMap = mutableMapOf<Int, Double>()
dayWithTemperatures.forEach {
if (!daysWithTemperaturesMap.containsKey(it.day)) {
daysWithTemperaturesMap[it.day] = it.temp
} else {
daysWithTemperaturesMap[it.day] = daysWithTemperaturesMap[it.day]!! + it.temp
}
}
return daysWithTemperaturesMap
}
}

fun main() {
runApplication<WeatherApp>()
val weatherApp = WeatherApp()
weatherApp.calculateAvgTemp("https://api.open-meteo.com/v1/forecast?latitude=47.4984&longitude=19.0404&hourly=temperature_2m&timezone=auto").forEach {
println("${it.key}: ${it.value/24}")
}
}

13 changes: 13 additions & 0 deletions app/src/main/kotlin/WeatherData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hu.vanio.kotlin.feladat.ms

data class WeatherData(
val latitude: Double,
val longitude: Double,
val generationtime_ms: Double,
val utc_offset_seconds: Int,
val timezone: String,
val timezone_abbreviation: String,
val elevation: Int,
val hourly_units: HourlyUnits,
val hourly: HourlyData
)
20 changes: 19 additions & 1 deletion app/src/test/kotlin/WeatherAppTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package hu.vanio.kotlin.feladat.ms

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.assertThrows
import org.mockito.Mockito
import org.mockito.Mockito.verify
import kotlin.test.Test
import kotlin.test.assertEquals

class WeatherAppTest {

@Test fun `sikeres lekerdezes`() {
TODO()
val mockWeatherApp = Mockito.mock(WeatherApp::class.java)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ez a teszt nem az alkalmazást, hanem a Mockito-t teszteli.

val daysWithTemperaturesMap = mutableMapOf<Int, Double>()
daysWithTemperaturesMap[11] = 3.6
daysWithTemperaturesMap[12] = 5.1
Mockito.`when`(mockWeatherApp.calculateAvgTemp("https://goodURL.hu")).thenReturn(daysWithTemperaturesMap)
val result = mockWeatherApp.calculateAvgTemp("https://goodURL.hu")
verify(mockWeatherApp).calculateAvgTemp("https://goodURL.hu")
assertEquals(result, daysWithTemperaturesMap)
}

@Test fun `rossz lekerdezes`() {
val weatherApp = WeatherApp()
val exception = assertThrows<DataUnavailableException> { weatherApp.calculateAvgTemp("https://api.open-meteo.com/v1/forecasto?latitude=47.4984&longitude=19.0404&hourly=temperature_2m&timezone=auto") }
assertTrue(exception.message!!.startsWith("Weather datas are unavailable! Response was:"))
}

}