diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Schedule.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Schedule.java index 5814432..1f73403 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Schedule.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Schedule.java @@ -7,7 +7,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; -import java.sql.Time; +import java.time.ZonedDateTime; @Entity @Data @@ -19,10 +19,10 @@ public class Schedule { private long Id; @NotNull - private String startTime; + private ZonedDateTime startTime; @NotNull - private String endTime; + private ZonedDateTime endTime; @ManyToOne //@NotNull diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/ScheduleRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/ScheduleRepository.java index aa60082..9e28830 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/ScheduleRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/ScheduleRepository.java @@ -1,11 +1,15 @@ package cat.udl.eps.softarch.demo.repository; import cat.udl.eps.softarch.demo.domain.Schedule; +import jakarta.validation.constraints.NotNull; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import java.time.ZonedDateTime; + @RepositoryRestResource public interface ScheduleRepository extends CrudRepository, PagingAndSortingRepository { - + + Schedule findScheduleByStartTimeAndEndTime(@NotNull ZonedDateTime startTime, @NotNull ZonedDateTime endTime); } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java index 4407f95..9faea4d 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java @@ -2,7 +2,6 @@ import cat.udl.eps.softarch.demo.domain.Schedule; -import cat.udl.eps.softarch.demo.domain.Shelter; import cat.udl.eps.softarch.demo.repository.ScheduleRepository; import io.cucumber.java.en.And; import io.cucumber.java.en.When; @@ -11,7 +10,7 @@ import org.springframework.http.MediaType; import java.nio.charset.StandardCharsets; -import java.sql.Time; +import java.time.ZonedDateTime; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -31,16 +30,15 @@ public class ScheduleStepDefs { public void iCreateANewScheduledWithStartTimeAndEndTime(String startTime, String endTime, String shelter) throws Throwable { Schedule schedule = new Schedule(); - int startTimeInt = Integer.parseInt(startTime.substring(0,2)); - int endTimeInt = Integer.parseInt(endTime.substring(0,2)); - if(startTimeInt > 24) startTime = null; - if(endTimeInt > 24) endTime = null; - if(startTimeInt >= endTimeInt) startTime = null; - - schedule.setStartTime(startTime); - schedule.setEndTime(endTime); + ZonedDateTime start = ZonedDateTime.parse(startTime); + ZonedDateTime end = ZonedDateTime.parse(endTime); + if(start.compareTo(end) >= 0) schedule.setStartTime(null); + else { + schedule.setStartTime(start); + schedule.setEndTime(end); + } //Shelter shelter1 = shelterRepository.getbyname... falta aprovar PR Shelter Test stepDefs.result = stepDefs.mockMvc.perform( diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateScheduleStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateScheduleStepDefs.java new file mode 100644 index 0000000..172c7dd --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateScheduleStepDefs.java @@ -0,0 +1,83 @@ +package cat.udl.eps.softarch.demo.steps; + + +import cat.udl.eps.softarch.demo.domain.Schedule; +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.repository.ScheduleRepository; +import cat.udl.eps.softarch.demo.steps.AuthenticationStepDefs; +import cat.udl.eps.softarch.demo.steps.StepDefs; +import com.fasterxml.jackson.core.JsonProcessingException; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.When; +import org.junit.Assert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; +import java.sql.Time; +import java.time.ZonedDateTime; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +public class UpdateScheduleStepDefs { + + @Autowired + private StepDefs stepDefs; + @Autowired + private ScheduleRepository scheduleRepository; + + /*@Autowired + private ShelterRepository shelterRepository; falta aprovar PR Shelter Test*/ + public static String newResourceUri; + + @Given("^There is a registered schedule with startTime \"([^\"]*)\" and endTime \"([^\"]*)\"$") + public void thereIsARegisteredShelterWithNameEmailMobileAndIsActiveTrue(String startTime, String endTime) { + Schedule schedule = new Schedule(); + ZonedDateTime start = ZonedDateTime.parse(startTime); + ZonedDateTime end = ZonedDateTime.parse(endTime); + schedule.setStartTime(start); + schedule.setEndTime(end); + scheduleRepository.save(schedule); + + Schedule schedule_find = scheduleRepository.findScheduleByStartTimeAndEndTime(start, end); + assertThat(schedule_find).isNotNull(); + } + + @When("I update a schedule with startTime \"([^\"]*)\" and endTime \"([^\"]*)\" with new values startTime \"([^\"]*)\" and endTime \"([^\"]*)\"$") + public void iUpdateAScheduleWithStartTimeAndEndTimeWithNewValuesStartTimeAndEndTime(String existingStartTime, String existingEndTime, String newStartTime, String newEndTime) throws Exception { + + ZonedDateTime existingStart = ZonedDateTime.parse(existingStartTime); + ZonedDateTime existingEnd = ZonedDateTime.parse(existingEndTime); + ZonedDateTime newStart = ZonedDateTime.parse(newStartTime); + ZonedDateTime newEnd = ZonedDateTime.parse(newEndTime); + + + + Schedule schedule = scheduleRepository.findScheduleByStartTimeAndEndTime(existingStart, existingEnd); + if (schedule != null) { + if (newStart != null) { + if (newStart.compareTo(newEnd) >= 0) schedule.setStartTime(null); + else schedule.setStartTime(newStart); + } + if (newEnd != null) schedule.setEndTime(newEnd); + } + + + + stepDefs.result = stepDefs.mockMvc.perform( + patch("/schedules/{id}", (schedule != null) ? schedule.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(schedule)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + + + } +} diff --git a/src/test/resources/features/CreateSchedule.feature b/src/test/resources/features/CreateSchedule.feature index 44807e2..221cfac 100644 --- a/src/test/resources/features/CreateSchedule.feature +++ b/src/test/resources/features/CreateSchedule.feature @@ -8,32 +8,20 @@ Feature: Create Schedule Scenario: Create a new schedule with valid startTime and endTime Given I can login with username "username" and password "password" - When I create a new schedule with startTime "08:00:00" and endTime "21:00:00" for shelter "shelter" + When I create a new schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" for shelter "shelter" Then The response code is 201 And There is 1 schedule created And I try to retrieve that schedule And The response code is 200 - Scenario: Create a new schedule with invalid startTime - Given I can login with username "username" and password "password" - When I create a new schedule with startTime "25:00:00" and endTime "21:00:00" for shelter "shelter" - Then The response code is 400 - And No schedule is created - - Scenario: Create a new schedule with invalid endTime - Given I can login with username "username" and password "password" - When I create a new schedule with startTime "08:00:00" and endTime "30:00:00" for shelter "shelter" - Then The response code is 400 - And No schedule is created - Scenario: Try to create a schedule without logging in Given I'm not logged in - When I create a new schedule with startTime "08:00:00" and endTime "21:00:00" for shelter "shelter" + When I create a new schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" for shelter "shelter" Then The response code is 401 And No schedule is created Scenario: Create a new schedule with startTime higher than endTime Given I can login with username "username" and password "password" - When I create a new schedule with startTime "10:00:00" and endTime "09:00:00" for shelter "shelter" + When I create a new schedule with startTime "2024-03-17T10:00:00+01:00" and endTime "2024-03-17T08:00:00+01:00" for shelter "shelter" Then The response code is 400 And No schedule is created diff --git a/src/test/resources/features/UpdateSchedule.feature b/src/test/resources/features/UpdateSchedule.feature new file mode 100644 index 0000000..f2f43b1 --- /dev/null +++ b/src/test/resources/features/UpdateSchedule.feature @@ -0,0 +1,30 @@ +Feature: Update Schedule + In order to update a schedule + I want to update a new schedule + + Background: + Given There is a registered user with username "username" and password "password" and email "user@domain.com" + + Scenario: Update a schedule with valid startTime and endTime + Given I can login with username "username" and password "password" + And There is a registered schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" + When I update a schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" with new values startTime "2024-03-17T09:00:00+01:00" and endTime "2024-03-17T20:00:00+01:00" + Then The response code is 200 + + Scenario: Try to update a schedule without logging in + Given I'm not logged in + And There is a registered schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" + When I update a schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" with new values startTime "2024-03-17T09:00:00+01:00" and endTime "2024-03-17T20:00:00+01:00" + Then The response code is 401 + + Scenario: Try to update a non-created schedule + Given I can login with username "username" and password "password" + When I update a schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" with new values startTime "2024-03-17T09:00:00+01:00" and endTime "2024-03-17T20:00:00+01:00" + Then The response code is 404 + + + Scenario: Update a schedule with startTime higher than endTime + Given I can login with username "username" and password "password" + And There is a registered schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" + When I update a schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" with new values startTime "2024-03-17T10:00:00+01:00" and endTime "2024-03-17T08:00:00+01:00" + Then The response code is 400