Skip to content

Commit

Permalink
Created Shelter event handler and added new scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianJitaru29 committed Mar 20, 2024
1 parent d28e5a3 commit fad2ff3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

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.UserRepository;
import io.cucumber.java.en.And;
Expand All @@ -19,6 +20,7 @@
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;

public class CreateShelterStepDefs {
@Autowired
Expand All @@ -30,6 +32,9 @@ public class CreateShelterStepDefs {
@Autowired
private UserRepository userRepository;

@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) {
if (!userRepository.existsById(username)) {
Expand All @@ -38,25 +43,27 @@ public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username,
user.setId(username);
user.setPassword(password);
user.encodePassword();
userRepository.save(user);
adminRepository.save(user);
}
}

@When("^I create a shelter with a name \"([^\"]*)\" email \"([^\"]*)\" and mobile \"([^\"]*)\"$")
public void iCreateAShelterWithAName(String name, String email, String mobile) throws Throwable {
@When("^I create a shelter with a name \"([^\"]*)\", email \"([^\"]*)\" and phone \"([^\"]*)\" and location \"([^\"]*)\"$")
public void iCreateAShelterWithAName(String name, String email, String mobile, String location) throws Throwable {;
Shelter newshelter = new Shelter();
newshelter.setName(name);
newshelter.setEmail(email);
newshelter.setMobile(mobile);
newshelter.setCreatedAt(ZonedDateTime.now());
newshelter.setUpdatedAt(ZonedDateTime.now());
//Following code is doing a post request to /shelters storing the response in stepDefs.result to test REST functionalty
stepDefs.result = stepDefs.mockMvc.perform(
post("/shelters")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(newshelter))
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());

}

@And("^There is (\\d+) Shelter created$")
Expand Down
26 changes: 16 additions & 10 deletions src/test/resources/features/CreateShelter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fad2ff3

Please sign in to comment.