-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MORE-562: check if observation/intervetion schedule lies within study… (
#125) * MORE-562: check if observation/intervetion schedule lies within study timeframe * MORE-562: requested changes implemented * MORE-562: fixed testing error * MORE-562: impl timezone as bean in other places * MORE-562: some requested changes
- Loading branch information
Showing
13 changed files
with
210 additions
and
15 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...nager/src/main/java/io/redlink/more/studymanager/configuration/TimezoneConfiguration.java
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,27 @@ | ||
package io.redlink.more.studymanager.configuration; | ||
|
||
import io.redlink.more.studymanager.properties.TimezoneProperties; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.sql.Time; | ||
import java.time.ZoneId; | ||
import java.util.TimeZone; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties({TimezoneProperties.class}) | ||
public class TimezoneConfiguration { | ||
final TimezoneProperties properties; | ||
|
||
public TimezoneConfiguration(TimezoneProperties properties) { | ||
this.properties = properties; | ||
} | ||
|
||
@Bean | ||
public ZoneId ZoneId() { | ||
return TimeZone().toZoneId(); | ||
} | ||
|
||
@Bean public TimeZone TimeZone() { return TimeZone.getTimeZone(properties.identifier()); } | ||
} |
8 changes: 8 additions & 0 deletions
8
studymanager/src/main/java/io/redlink/more/studymanager/model/Timeframe.java
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,8 @@ | ||
package io.redlink.more.studymanager.model; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record Timeframe ( | ||
LocalDate from, | ||
LocalDate to | ||
) {} |
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
8 changes: 8 additions & 0 deletions
8
studymanager/src/main/java/io/redlink/more/studymanager/properties/TimezoneProperties.java
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,8 @@ | ||
package io.redlink.more.studymanager.properties; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "more.timezone") | ||
public record TimezoneProperties ( | ||
String identifier | ||
) {} |
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
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
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
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
30 changes: 30 additions & 0 deletions
30
studymanager/src/main/java/io/redlink/more/studymanager/service/ScheduleService.java
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,30 @@ | ||
package io.redlink.more.studymanager.service; | ||
|
||
import io.redlink.more.studymanager.exception.BadRequestException; | ||
import io.redlink.more.studymanager.model.Event; | ||
import io.redlink.more.studymanager.model.Timeframe; | ||
import io.redlink.more.studymanager.repository.StudyRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
|
||
@Service | ||
public class ScheduleService { | ||
private final StudyRepository studyRepository; | ||
private final ZoneId zoneId; | ||
|
||
public ScheduleService(StudyRepository studyRepository, ZoneId zoneId) { | ||
this.studyRepository = studyRepository; | ||
this.zoneId = zoneId; | ||
} | ||
|
||
public Event assertScheduleWithinStudyTime(Long studyId, Event schedule) { | ||
Timeframe timeframe = studyRepository.getStudyTimeframe(studyId); | ||
if(LocalDate.ofInstant(schedule.getDateStart(), zoneId).isBefore(timeframe.from()) | ||
|| LocalDate.ofInstant(schedule.getDateEnd(), zoneId).isAfter(timeframe.to())) { | ||
throw new BadRequestException("Schedule should lie within study timeframe"); | ||
} | ||
return schedule; | ||
} | ||
} |
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
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
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
84 changes: 84 additions & 0 deletions
84
studymanager/src/test/java/io/redlink/more/studymanager/service/ScheduleServiceTest.java
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,84 @@ | ||
package io.redlink.more.studymanager.service; | ||
|
||
import io.redlink.more.studymanager.configuration.TimezoneConfiguration; | ||
import io.redlink.more.studymanager.exception.BadRequestException; | ||
import io.redlink.more.studymanager.model.Event; | ||
import io.redlink.more.studymanager.model.Timeframe; | ||
import io.redlink.more.studymanager.repository.StudyRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.util.TestPropertyValues; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) | ||
@ExtendWith(MockitoExtension.class) | ||
@ContextConfiguration(initializers = ScheduleServiceTest.EnvInitializer.class, | ||
classes = { | ||
ScheduleService.class, | ||
TimezoneConfiguration.class, | ||
}) | ||
public class ScheduleServiceTest { | ||
|
||
@MockBean | ||
StudyRepository studyRepository; | ||
|
||
@Autowired | ||
@InjectMocks | ||
ScheduleService scheduleService; | ||
|
||
|
||
@Test | ||
void testAssertPasses() { | ||
when(studyRepository.getStudyTimeframe(anyLong())).thenReturn( | ||
new Timeframe( | ||
LocalDate.of(2023, 6, 2), | ||
LocalDate.of(2023,6,3)) | ||
); | ||
Event schedule = new Event() | ||
.setDateStart(Instant.parse("2023-06-02T00:00:00.00Z")) | ||
.setDateEnd(Instant.parse("2023-06-03T00:00:00.00Z")); | ||
assertThat(scheduleService.assertScheduleWithinStudyTime(1L, schedule)).isEqualTo(schedule); | ||
} | ||
|
||
@Test | ||
void testAssertFails() { | ||
when(studyRepository.getStudyTimeframe(anyLong())).thenReturn( | ||
new Timeframe( | ||
LocalDate.of(2023, 6, 2), | ||
LocalDate.of(2023,6,3)) | ||
); | ||
Event scheduleBefore = new Event() | ||
.setDateStart(Instant.parse("2023-06-01T00:00:00.00Z")) | ||
.setDateEnd(Instant.parse("2023-06-02T00:00:00.00Z")); | ||
Event scheduleAfter = new Event() | ||
.setDateStart(Instant.parse("2023-06-02T00:00:00.00Z")) | ||
.setDateEnd(Instant.parse("2023-06-04T00:00:00.00Z")); | ||
assertThrows(BadRequestException.class, () -> scheduleService.assertScheduleWithinStudyTime(1L, scheduleBefore)); | ||
assertThrows(BadRequestException.class, () -> scheduleService.assertScheduleWithinStudyTime(1L, scheduleAfter)); | ||
} | ||
|
||
static class EnvInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
TestPropertyValues.of( | ||
"more.timeframe.identifier=Europe/Vienna" | ||
).applyTo(applicationContext); | ||
} | ||
} | ||
} |