diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java index 78b2e537..93176c97 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java @@ -2,10 +2,13 @@ import cat.udl.eps.softarch.demo.domain.Shelter; import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import org.springframework.data.rest.core.annotation.HandleAfterSave; import org.springframework.data.rest.core.annotation.HandleBeforeCreate; import org.springframework.data.rest.core.annotation.RepositoryEventHandler; import org.springframework.stereotype.Component; +import java.time.ZonedDateTime; + @Component @RepositoryEventHandler public class ShelterEventHandler { @@ -17,13 +20,12 @@ public ShelterEventHandler(ShelterRepository shelterRepository) { @HandleBeforeCreate public void handleShelterPreCreate(Shelter shelter) { - if (shelter.getCreatedAt() == null) { - throw new IllegalArgumentException("Shelter must have a creation date"); - } - if (shelter.getUpdatedAt() == null) { - throw new IllegalArgumentException("Shelter must have an update date"); - } - + shelter.setCreatedAt(ZonedDateTime.now()); + shelter.setUpdatedAt(ZonedDateTime.now()); } + @HandleAfterSave + public void handleShelterPostSave(Shelter shelter) { + shelter.setUpdatedAt(ZonedDateTime.now()); + } } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index 25d0f810..f9b3e4f3 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -9,6 +9,7 @@ import cat.udl.eps.softarch.demo.domain.*; import cat.udl.eps.softarch.demo.repository.AdminRepository; import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; @@ -32,25 +33,22 @@ public class CreateShelterStepDefs { @Autowired private UserRepository userRepository; + @Autowired + private ShelterVolunteerRepository ShelterVolunteerRepository; + @Autowired private AdminRepository adminRepository; @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) throws Throwable { - if (!userRepository.existsById(username)) { + if (!adminRepository.existsById(username)) { Admin user = new Admin(); user.setEmail(email); user.setId(username); user.setPassword(password); user.encodePassword(); - userRepository.save(user); - stepDefs.result = stepDefs.mockMvc.perform( - post("/admins") - .contentType(MediaType.APPLICATION_JSON) - .content(stepDefs.mapper.writeValueAsString(user)) - .characterEncoding(StandardCharsets.UTF_8) - .with(AuthenticationStepDefs.authenticate())) - .andDo(print()); + adminRepository.save(user); + } } @@ -83,4 +81,16 @@ public void thereIsNoShelterRegisteredWithTheName(String name) { Assert.assertTrue("Shelter with name \"" + name + "\" shouldn't exist", shelterRepository.findByName(name).isEmpty()); } + + @Given("^There is a registered volunteer with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") + public void thereIsARegisteredVolunteerWithNameAndPasswordAndEmail(String name, String password, String email) { + if(!ShelterVolunteerRepository.existsById(name)) { + ShelterVolunteer user = new ShelterVolunteer(); + user.setEmail(email); + user.setId(name); + user.setPassword(password); + user.encodePassword(); + ShelterVolunteerRepository.save(user); + } + } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 5db8e08b..482130a9 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -6,6 +6,7 @@ Feature: Create Shelter Background: Given There is a registered user with username "user" and password "password" and email "user@sample.app" Given There is a registered admin with name "admin" and password "password" and email "admin@sample.app" + Given There is a registered volunteer with name "volunteer" and password "password" and email "volunteer@sample.app" Scenario: Create a shelter without being logged in @@ -20,6 +21,13 @@ Feature: Create Shelter When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 201 + #Scenario: Create shelter as Volunteer + # Given I login as "volunteer" with password "password" + # When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" + # Then The response code is 401 + # And The error message is "Unauthorized" + # And There is 0 Shelter created + Scenario: Create shelter as Client Given I login as "client" with password "password" When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location"