From bc4ecae28d9825b69469cfd50e50658d8c54bb6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCcke?= Date: Sun, 11 Sep 2022 18:44:53 +0200 Subject: [PATCH] replaced LocalDateTime with LocalDate to simplify date caluclation (#10) * replaced LocalDateTime with LocalDate to simplify date caluclation * fixed tests, update dependencies + example ics Co-authored-by: paulbrejla --- build.gradle | 39 ++++++++------ gradle/wrapper/gradle-wrapper.properties | 2 +- .../holidays/HolidaysApiApplication.kt | 2 + .../holidays/application/Assembler.kt | 4 +- .../de/paulbrejla/holidays/domain/Models.kt | 6 +-- .../de/paulbrejla/holidays/rest/Assembler.kt | 4 +- .../kotlin/de/paulbrejla/holidays/rest/Dto.kt | 2 +- src/main/resources/application.yml | 3 ++ .../holidays/HolidaysApiApplicationTests.kt | 4 +- ...ocalFilesystemCalendarLoaderGatewayTest.kt | 29 +++++++++++ .../holidays/rest/AssemblerKtTest.kt | 10 ++-- .../ferien_baden-wuerttemberg_2017.ics | 52 +++++++++++++++++++ 12 files changed, 123 insertions(+), 34 deletions(-) create mode 100644 src/test/kotlin/de/paulbrejla/holidays/infrastructure/loader/impl/LocalFilesystemCalendarLoaderGatewayTest.kt create mode 100644 src/test/resources/holidays/ferien_baden-wuerttemberg_2017.ics diff --git a/build.gradle b/build.gradle index fd31c9c..804e21e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ buildscript { ext { - kotlinVersion = '1.4.0' - springBootVersion = '2.3.3.RELEASE' + kotlinVersion = '1.7.10' + springBootVersion = '2.7.3' } repositories { mavenCentral() @@ -35,25 +35,30 @@ repositories { mavenCentral() } +test { + useJUnitPlatform() +} + + dependencies { - compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") - compile("org.springframework.boot:spring-boot-starter-jetty:${springBootVersion}") - compile("org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}") - compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}") - compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}") - compile 'net.sf.biweekly:biweekly:0.6.1' - compile group: 'commons-io', name: 'commons-io', version: '2.4' - compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") - compile("com.h2database:h2:1.4.200") - compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.1.0.Final' - compile("org.springframework.boot:spring-boot-devtools:${springBootVersion}") - compile("io.springfox:springfox-boot-starter:3.0.0") - compile("io.springfox:springfox-swagger-ui:2.9.2") + implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") + implementation("org.springframework.boot:spring-boot-starter-jetty:${springBootVersion}") + implementation("org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}") + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}") + implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}") + implementation 'net.sf.biweekly:biweekly:0.6.1' + implementation group: 'commons-io', name: 'commons-io', version: '2.4' + implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") + implementation("com.h2database:h2:1.4.200") + implementation group: 'org.hibernate', name: 'hibernate-java8', version: '5.1.0.Final' + implementation("org.springframework.boot:spring-boot-devtools:${springBootVersion}") + implementation("io.springfox:springfox-boot-starter:3.0.0") + implementation("io.springfox:springfox-swagger-ui:2.9.2") // Foundation - compile 'org.webjars:foundation:6.3.1' + implementation('org.webjars:foundation:6.3.1') - testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}") + testImplementation("org.springframework.boot:spring-boot-starter-test:${springBootVersion}") } noArg { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index cc966ea..8c615d7 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip diff --git a/src/main/kotlin/de/paulbrejla/holidays/HolidaysApiApplication.kt b/src/main/kotlin/de/paulbrejla/holidays/HolidaysApiApplication.kt index ed4792a..ff7b616 100644 --- a/src/main/kotlin/de/paulbrejla/holidays/HolidaysApiApplication.kt +++ b/src/main/kotlin/de/paulbrejla/holidays/HolidaysApiApplication.kt @@ -3,9 +3,11 @@ package de.paulbrejla.holidays import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.scheduling.annotation.EnableScheduling +import org.springframework.web.servlet.config.annotation.EnableWebMvc @SpringBootApplication @EnableScheduling +@EnableWebMvc class HolidaysApiApplication fun main(args: Array) { diff --git a/src/main/kotlin/de/paulbrejla/holidays/application/Assembler.kt b/src/main/kotlin/de/paulbrejla/holidays/application/Assembler.kt index 57e9a21..2c65925 100644 --- a/src/main/kotlin/de/paulbrejla/holidays/application/Assembler.kt +++ b/src/main/kotlin/de/paulbrejla/holidays/application/Assembler.kt @@ -13,8 +13,8 @@ import java.time.temporal.TemporalAccessor fun assembleHoliday(event: VEvent, state: String): Holiday = Holiday(id = 0, stateCode = assembleStateCode(state), summary = event.summary.value.toLowerCase(), - start = LocalDateTime.ofInstant(event.dateStart.value.toInstant(), ZoneOffset.UTC), - end = LocalDateTime.ofInstant(event.dateEnd.value.toInstant(), ZoneOffset.UTC), + start = LocalDateTime.ofInstant(event.dateStart.value.toInstant(), ZoneOffset.UTC).toLocalDate(), + end = LocalDateTime.ofInstant(event.dateEnd.value.toInstant(), ZoneOffset.UTC).toLocalDate(), year = event.dateStart.value.rawComponents.year, slug = assembleSlug(event.dateStart.value.rawComponents.year, event.summary.value, assembleStateCode(state))) diff --git a/src/main/kotlin/de/paulbrejla/holidays/domain/Models.kt b/src/main/kotlin/de/paulbrejla/holidays/domain/Models.kt index 0dca4a0..0fbc293 100644 --- a/src/main/kotlin/de/paulbrejla/holidays/domain/Models.kt +++ b/src/main/kotlin/de/paulbrejla/holidays/domain/Models.kt @@ -1,7 +1,7 @@ package de.paulbrejla.holidays.domain import java.io.Serializable -import java.time.LocalDateTime +import java.time.LocalDate import javax.persistence.* @@ -13,6 +13,6 @@ data class Holiday(@Id @Enumerated(EnumType.STRING) var stateCode: State, val year: Int, var summary: String, - var start: LocalDateTime, - var end: LocalDateTime, + var start: LocalDate, + var end: LocalDate, var slug: String) : Serializable \ No newline at end of file diff --git a/src/main/kotlin/de/paulbrejla/holidays/rest/Assembler.kt b/src/main/kotlin/de/paulbrejla/holidays/rest/Assembler.kt index 43c336a..4a7d730 100644 --- a/src/main/kotlin/de/paulbrejla/holidays/rest/Assembler.kt +++ b/src/main/kotlin/de/paulbrejla/holidays/rest/Assembler.kt @@ -4,8 +4,8 @@ import de.paulbrejla.holidays.domain.Holiday import java.time.ZoneOffset fun assembleHolidayDto(holiday: Holiday) = HolidayDto( - start = holiday.start.atOffset(ZoneOffset.UTC).toString(), - end = holiday.end.atOffset(ZoneOffset.UTC).toString(), + start = holiday.start.toString(), + end = holiday.end.toString(), year = holiday.year, stateCode = holiday.stateCode, name = holiday.summary, diff --git a/src/main/kotlin/de/paulbrejla/holidays/rest/Dto.kt b/src/main/kotlin/de/paulbrejla/holidays/rest/Dto.kt index 454ddaf..daef824 100644 --- a/src/main/kotlin/de/paulbrejla/holidays/rest/Dto.kt +++ b/src/main/kotlin/de/paulbrejla/holidays/rest/Dto.kt @@ -1,6 +1,6 @@ package de.paulbrejla.holidays.rest import de.paulbrejla.holidays.domain.State -import java.time.LocalDateTime +import java.time.LocalDate data class HolidayDto(val start: String, val end: String, val year: Int, val stateCode: State, val name: String, val slug: String) \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7cd74d2..6703743 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,9 @@ spring: application: name: holidays-api + mcv: + pathmatch: + matching-strategy: ant_path_matcher h2: console: enabled: false diff --git a/src/test/kotlin/de/paulbrejla/holidays/HolidaysApiApplicationTests.kt b/src/test/kotlin/de/paulbrejla/holidays/HolidaysApiApplicationTests.kt index 05bcfcf..521b523 100644 --- a/src/test/kotlin/de/paulbrejla/holidays/HolidaysApiApplicationTests.kt +++ b/src/test/kotlin/de/paulbrejla/holidays/HolidaysApiApplicationTests.kt @@ -1,11 +1,9 @@ package de.paulbrejla.holidays -import org.junit.Test -import org.junit.runner.RunWith +import org.junit.jupiter.api.Test import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.junit4.SpringRunner -@RunWith(SpringRunner::class) @SpringBootTest class HolidaysApiApplicationTests { diff --git a/src/test/kotlin/de/paulbrejla/holidays/infrastructure/loader/impl/LocalFilesystemCalendarLoaderGatewayTest.kt b/src/test/kotlin/de/paulbrejla/holidays/infrastructure/loader/impl/LocalFilesystemCalendarLoaderGatewayTest.kt new file mode 100644 index 0000000..dc66e9a --- /dev/null +++ b/src/test/kotlin/de/paulbrejla/holidays/infrastructure/loader/impl/LocalFilesystemCalendarLoaderGatewayTest.kt @@ -0,0 +1,29 @@ +package de.paulbrejla.holidays.infrastructure.loader.impl + +import de.paulbrejla.holidays.infrastructure.loader.api.CalendarLoader +import org.junit.jupiter.api.Test + +import org.junit.jupiter.api.Assertions.* +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +internal class LocalFilesystemCalendarLoaderGatewayTest { + + @Autowired + lateinit var calendarLoader: CalendarLoader + + @Test + fun `calendar files are loaded`() { + // Given + val eventCount = 5 + + // When + val calendars = calendarLoader.loadCalendarFiles() + + // Then + assertNotNull(calendars.first().calendars) + assertEquals(calendars.first().calendars.first().events.count(), eventCount) + + } +} \ No newline at end of file diff --git a/src/test/kotlin/de/paulbrejla/holidays/rest/AssemblerKtTest.kt b/src/test/kotlin/de/paulbrejla/holidays/rest/AssemblerKtTest.kt index 714bf41..170295d 100644 --- a/src/test/kotlin/de/paulbrejla/holidays/rest/AssemblerKtTest.kt +++ b/src/test/kotlin/de/paulbrejla/holidays/rest/AssemblerKtTest.kt @@ -2,10 +2,10 @@ package de.paulbrejla.holidays.rest import de.paulbrejla.holidays.domain.Holiday import de.paulbrejla.holidays.domain.State -import org.junit.Test +import org.junit.jupiter.api.Test -import org.junit.Assert.* -import java.time.LocalDateTime +import org.junit.jupiter.api.Assertions.* +import java.time.LocalDate class AssemblerKtTest { @@ -13,12 +13,12 @@ class AssemblerKtTest { fun `a HolidayDto is assembled from a Holiday`() { // Given val holiday = Holiday(id = 1, stateCode = State.HB, year = 2020, summary = "Winterferien Bremen", - start = LocalDateTime.now(), end = LocalDateTime.now().plusYears(2), slug = "ferien-hb") + start = LocalDate.now(), end = LocalDate.now().plusYears(2), slug = "ferien-hb") // When val holidayDto = assembleHolidayDto(holiday) // Then - assertNotNull(holiday) + assertNotNull(holidayDto) } } \ No newline at end of file diff --git a/src/test/resources/holidays/ferien_baden-wuerttemberg_2017.ics b/src/test/resources/holidays/ferien_baden-wuerttemberg_2017.ics new file mode 100644 index 0000000..dba8d85 --- /dev/null +++ b/src/test/resources/holidays/ferien_baden-wuerttemberg_2017.ics @@ -0,0 +1,52 @@ +BEGIN:VCALENDAR +VERSION:2.0 +CALSCALE:GREGORIAN +PRODID:FERIEN_API +METHOD:PUBLISH + +BEGIN:VEVENT +DTSTAMP:20170922T052539Z +UID:bdfb478f-7169-4171-ad9f-da1887f20676 +DTSTART;VALUE=DATE:20170410 +DTEND;VALUE=DATE:20170422 +DESCRIPTION:Osterferien 2017 Baden-Württemberg +SUMMARY:Osterferien +END:VEVENT + +BEGIN:VEVENT +DTSTAMP:20170922T052539Z +UID:9c25e98e-939e-4912-ae58-153cdc88a8ed +DTSTART;VALUE=DATE:20170606 +DTEND;VALUE=DATE:20170617 +DESCRIPTION:Pfingstferien 2017 Baden-Württemberg +SUMMARY:Pfingstferien +END:VEVENT + +BEGIN:VEVENT +DTSTAMP:20170922T052539Z +UID:ed7ebf1a-a8b1-4139-984f-2244b7550978 +DTSTART;VALUE=DATE:20170727 +DTEND;VALUE=DATE:20170910 +DESCRIPTION:Sommerferien 2017 Baden-Württemberg +SUMMARY:Sommerferien +END:VEVENT + +BEGIN:VEVENT +DTSTAMP:20170922T052539Z +UID:250f4e37-4ada-492e-a2d1-5b6fc3f619b7 +DTSTART;VALUE=DATE:20171030 +DTEND;VALUE=DATE:20171104 +DESCRIPTION:Herbstferien 2017 Baden-Württemberg +SUMMARY:Herbstferien +END:VEVENT + +BEGIN:VEVENT +DTSTAMP:20170922T052539Z +UID:027811ca-48cb-432b-b02a-d63f0d8b842a +DTSTART;VALUE=DATE:20171222 +DTEND;VALUE=DATE:20180106 +DESCRIPTION:Weihnachtsferien 2017 Baden-Württemberg +SUMMARY:Weihnachtsferien +END:VEVENT + +END:VCALENDAR