diff --git a/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java b/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java index 0e7bf3c..3fca76d 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java +++ b/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java @@ -1,9 +1,11 @@ package cat.udl.eps.softarch.demo.config; import cat.udl.eps.softarch.demo.domain.Admin; +import cat.udl.eps.softarch.demo.domain.Shelter; import cat.udl.eps.softarch.demo.domain.ShelterVolunteer; import cat.udl.eps.softarch.demo.domain.User; import cat.udl.eps.softarch.demo.repository.AdminRepository; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository; import org.springframework.beans.factory.annotation.Value; @@ -21,13 +23,15 @@ public class DBInitialization { private final UserRepository userRepository; private final AdminRepository adminRepository; private final ShelterVolunteerRepository shelterVolunteerRepository; - + private final ShelterRepository shelterRepository; public DBInitialization(UserRepository userRepository, AdminRepository adminRepository, - ShelterVolunteerRepository shelterVolunteerRepository) { + ShelterVolunteerRepository shelterVolunteerRepository, + ShelterRepository shelterRepository) { this.userRepository = userRepository; this.adminRepository = adminRepository; this.shelterVolunteerRepository = shelterVolunteerRepository; + this.shelterRepository = shelterRepository; } @PostConstruct @@ -51,17 +55,65 @@ public void initializeDatabase() { admin.encodePassword(); userRepository.save(admin); } + if (!adminRepository.existsById("admin")) { + Admin admin = new Admin(); + admin.setEmail("admin@sample.app"); + admin.setId("admin"); + admin.setPassword(defaultPassword); + admin.encodePassword(); + userRepository.save(admin); + } // Default ShelterVolunteer + if(shelterRepository.findByEmail("shelter@dbsample.app").isEmpty()) { + Shelter shelter = new Shelter(); + shelter.setName("shelter"); + shelter.setEmail("shelter@dbsample.app"); + shelter.setMobile("420420420"); + shelter.setActive(true); + shelterRepository.save(shelter); + } + if(shelterRepository.findByEmail("shelter1@dbsample.app").isEmpty()) { + Shelter shelter = new Shelter(); + shelter.setName("shelter1"); + shelter.setEmail("shelter1@dbsample.app"); + shelter.setMobile("420420421"); + shelter.setActive(true); + shelterRepository.save(shelter); + if (!shelterVolunteerRepository.existsById("volunteer")) { ShelterVolunteer volunteer = new ShelterVolunteer(); - volunteer.setEmail("volunteer@sample.app"); + volunteer.setEmail("volunteer@dbsample.app"); volunteer.setId("volunteer"); volunteer.setPassword(defaultPassword); volunteer.encodePassword(); + volunteer.setUserShelter(shelter); + shelterVolunteerRepository.save(volunteer); + } + if (!shelterVolunteerRepository.existsById("volunteer1")) { + ShelterVolunteer volunteer = new ShelterVolunteer(); + volunteer.setEmail("volunteer1@dbsample.app"); + volunteer.setId("volunteer1"); + volunteer.setPassword(defaultPassword); + volunteer.setUserShelter(shelter); + volunteer.encodePassword(); shelterVolunteerRepository.save(volunteer); } + if (!shelterVolunteerRepository.existsById("volunteer2")) { + ShelterVolunteer volunteer = new ShelterVolunteer(); + volunteer.setEmail("volunteer2@dbsample.app"); + volunteer.setId("volunteer2"); + volunteer.setPassword(defaultPassword); + volunteer.setUserShelter(shelter); + volunteer.encodePassword(); + shelterVolunteerRepository.save(volunteer); + } + + + } + + if (Arrays.asList(activeProfiles.split(",")).contains("test")) { // Testing instances if (!userRepository.existsById("test")) { diff --git a/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java index 82b7dd4..0ad12a2 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java +++ b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java @@ -1,6 +1,6 @@ package cat.udl.eps.softarch.demo.exceptions; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; -@ResponseStatus(code = HttpStatus.PRECONDITION_FAILED, reason = "El usuario debe ser de tipo shelterVolunteer") +@ResponseStatus(code = HttpStatus.PRECONDITION_FAILED, reason = "The volunteers must be in the same shelter") public class VolunteerFromDifferentShelter extends RuntimeException { } 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 d69e67a..20d7c82 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 @@ -73,6 +73,12 @@ public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, @And("^There is (\\d+) Shelter created$") public void thereIsShelterCreated(int sheltersCreatedNum) { + //Adding the dbInit shelters + if(!shelterRepository.findByEmail("shelter@dbsample.app").isEmpty()) + sheltersCreatedNum++; + if(!shelterRepository.findByEmail("shelter1@dbsample.app").isEmpty()) + sheltersCreatedNum++; + Assert.assertEquals("Shelters created", sheltersCreatedNum, shelterRepository.count()); }