-
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.
Created Shelter event handler and added new scenarios
- Loading branch information
1 parent
d28e5a3
commit fad2ff3
Showing
3 changed files
with
71 additions
and
14 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cat.udl.eps.softarch.demo.handler; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Shelter; | ||
import cat.udl.eps.softarch.demo.repository.ShelterRepository; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeCreate; | ||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RepositoryEventHandler | ||
public class ShelterEventHandler { | ||
final ShelterRepository shelterRepository; | ||
|
||
public ShelterEventHandler(ShelterRepository shelterRepository) { | ||
this.shelterRepository = shelterRepository; | ||
} | ||
|
||
@HandleBeforeCreate | ||
public void handleShelterPreCreate(Shelter shelter) { | ||
if (shelter.getRating() == null) { | ||
shelter.setRating(0); | ||
} | ||
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"); | ||
} | ||
if (checkIfValidEmail(shelter)) { | ||
shelterRepository.save(shelter); | ||
} | ||
|
||
} | ||
private boolean checkIfValidEmail(Shelter shelter){ | ||
if (shelter.getEmail() == null) { | ||
throw new IllegalArgumentException("Shelter must have an email"); | ||
}else if (!shelter.getEmail().contains("@")) { | ||
throw new IllegalArgumentException("Shelter email must contain @"); | ||
}else if (!shelter.getEmail().contains(".")) { | ||
throw new IllegalArgumentException("Shelter email must contain ."); | ||
} | ||
return true; | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -10,45 +10,51 @@ Feature: Create Shelter | |
|
||
Scenario: Create a shelter without being logged in | ||
Given I'm not logged in | ||
When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" | ||
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 Admin | ||
Given I login as "admin" with password "password" | ||
When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" | ||
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 Client | ||
Given I login as "client" with password "password" | ||
When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" | ||
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 with missing name | ||
Given I login as "admin" with password "password" | ||
When I create a shelter with a name "" email "testemail" and mobile "123456789" | ||
When I create a shelter with a name "", email "[email protected]" and phone "123123123" and location "location" | ||
Then The response code is 400 | ||
And There is 0 Shelter created | ||
|
||
Scenario: Create shelter with missing email | ||
Given I login as "admin" with password "password" | ||
When I create a shelter with a name "testShelter" email "" and mobile "123456789" | ||
When I create a shelter with a name "name", email "" and phone "123123123" and location "location" | ||
Then The response code is 400 | ||
And There is 0 Shelter created | ||
|
||
Scenario: Create shelter with missing mobile | ||
Given I login as "admin" with password "password" | ||
When I create a shelter with a name "testShelter" email "testemail" and mobile "" | ||
When I create a shelter with a name "name", email "[email protected]" and phone "" and location "location" | ||
Then The response code is 400 | ||
And There is 0 Shelter created | ||
|
||
Scenario: Create a shelter that already exists: | ||
Given I login as "admin" with password "password" | ||
When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" | ||
Then The response code is 400 | ||
When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" | ||
Then The response code is 400 | ||
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location" | ||
Then The response code is 201 | ||
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location" | ||
Then The response code is 409 | ||
And There is 1 Shelter created | ||
|
||
Scenario: Create a shelter with invalid email | ||
Given I login as "admin" with password "password" | ||
When I create a shelter with a name "name", email "shelter@sample" and phone "123123123" and location "location" | ||
Then The response code is 500 | ||
And There is 0 Shelter created |