-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from UdL-EPS-SoftArch/DBinit-Kick-User-From-S…
…helter DBinit-Kick-User-From-Shelter
- Loading branch information
Showing
3 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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("[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")) { | ||
|
2 changes: 1 addition & 1 deletion
2
src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
} | ||
|
||
|