Skip to content

Commit

Permalink
Solved ShelterHandler values inicialization
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianJitaru29 committed Mar 26, 2024
1 parent b94a6f8 commit 051ccbc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

}
}

Expand Down Expand Up @@ -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);
}
}
}
8 changes: 8 additions & 0 deletions src/test/resources/features/CreateShelter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Feature: Create Shelter
Background:
Given There is a registered user with username "user" and password "password" and email "[email protected]"
Given There is a registered admin with name "admin" and password "password" and email "[email protected]"
Given There is a registered volunteer with name "volunteer" and password "password" and email "[email protected]"


Scenario: Create a shelter without being logged in
Expand All @@ -20,6 +21,13 @@ Feature: Create Shelter
When I create a shelter with a name "name", email "[email protected]" 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 "[email protected]" 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 "[email protected]" and phone "123123123" and location "location"
Expand Down

0 comments on commit 051ccbc

Please sign in to comment.