Skip to content

Commit

Permalink
Merge pull request #107 from UdL-EPS-SoftArch/DBinit-Kick-User-From-S…
Browse files Browse the repository at this point in the history
…helter

DBinit-Kick-User-From-Shelter
  • Loading branch information
rogargon authored Jun 7, 2024
2 parents d185971 + a824970 commit 7c9cb10
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -51,17 +55,65 @@ public void initializeDatabase() {
admin.encodePassword();
userRepository.save(admin);
}
if (!adminRepository.existsById("admin")) {
Admin admin = new Admin();
admin.setEmail("[email protected]");
admin.setId("admin");
admin.setPassword(defaultPassword);
admin.encodePassword();
userRepository.save(admin);
}

// Default ShelterVolunteer
if(shelterRepository.findByEmail("[email protected]").isEmpty()) {
Shelter shelter = new Shelter();
shelter.setName("shelter");
shelter.setEmail("[email protected]");
shelter.setMobile("420420420");
shelter.setActive(true);
shelterRepository.save(shelter);
}
if(shelterRepository.findByEmail("[email protected]").isEmpty()) {
Shelter shelter = new Shelter();
shelter.setName("shelter1");
shelter.setEmail("[email protected]");
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("[email protected]");
volunteer.setId("volunteer1");
volunteer.setPassword(defaultPassword);
volunteer.setUserShelter(shelter);
volunteer.encodePassword();
shelterVolunteerRepository.save(volunteer);
}

if (!shelterVolunteerRepository.existsById("volunteer2")) {
ShelterVolunteer volunteer = new ShelterVolunteer();
volunteer.setEmail("[email protected]");
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")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 { }

Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]").isEmpty())
sheltersCreatedNum++;
if(!shelterRepository.findByEmail("[email protected]").isEmpty())
sheltersCreatedNum++;

Assert.assertEquals("Shelters created", sheltersCreatedNum, shelterRepository.count());
}

Expand Down

0 comments on commit 7c9cb10

Please sign in to comment.