Skip to content

Commit

Permalink
Update Schedule tests and some fixes (change start and end time from …
Browse files Browse the repository at this point in the history
…String to ZonedDateTime)
  • Loading branch information
pautorne committed Apr 4, 2024
1 parent 922c9c3 commit 6f650fb
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.sql.Time;
import java.time.ZonedDateTime;

@Entity
@Data
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Schedule, Long>, PagingAndSortingRepository<Schedule, Long> {


Schedule findScheduleByStartTimeAndEndTime(@NotNull ZonedDateTime startTime, @NotNull ZonedDateTime endTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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());


}
}
18 changes: 3 additions & 15 deletions src/test/resources/features/CreateSchedule.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions src/test/resources/features/UpdateSchedule.feature
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"

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

0 comments on commit 6f650fb

Please sign in to comment.