diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Shelter.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Shelter.java index 725876e..1de2039 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Shelter.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Shelter.java @@ -18,17 +18,14 @@ public class Shelter extends UriEntity { @ManyToOne - @NotNull @JsonIdentityReference(alwaysAsId = true) private User user; @ManyToOne - @NotNull @JsonIdentityReference(alwaysAsId = true) private Pet pet; @OneToOne - @NotNull @JsonIdentityReference(alwaysAsId = true) private ShelterCertificate shelterCertificate; @@ -45,7 +42,7 @@ public class Shelter extends UriEntity { private String email; @NotBlank - @Pattern(regexp="(^$|[0-9]{11})") + @Pattern(regexp="(^[+]*[(]?[0-9]{1,4}[)]?[-\\s./0-9]*$)") private String mobile; @NotNull diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java index 78b6dc1..65ccd1c 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java @@ -3,9 +3,10 @@ import cat.udl.eps.softarch.demo.domain.Shelter; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource public interface ShelterRepository extends CrudRepository, PagingAndSortingRepository { - + Shelter findByName(@Param("name") String name); } \ No newline at end of file diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterStepDefs.java new file mode 100644 index 0000000..4704a70 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterStepDefs.java @@ -0,0 +1,49 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import io.cucumber.java.en.And; +import io.cucumber.java.en.When; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.assertj.core.api.Assertions.assertThat; + + +public class DeleteShelterStepDefs { + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterRepository shelterRepository; + + @When("^I delete the shelter with name \"([^\"]*)\"$") + public void iDeleteAShelterWithName(String name) throws Exception { + Shelter shelter = shelterRepository.findByName(name); + + stepDefs.result = stepDefs.mockMvc.perform( + delete("/shelters/{id}", (shelter != null) ? shelter.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelter)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } + + @And("^The shelter with name \"([^\"]*)\" has been deleted$") + public void theShelterWithNameHasBeenDeleted(String name) { + Shelter shelter = shelterRepository.findByName(name); + assertThat(shelter).isNull(); + } + + @And("^The shelter with name \"([^\"]*)\" has not been deleted$") + public void theShelterWithNameHasNotBeenDeleted(String name) { + Shelter shelter = shelterRepository.findByName(name); + assertThat(shelter).isNotNull(); + } +} diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/GetShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/GetShelterStepDefs.java new file mode 100644 index 0000000..878f091 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/GetShelterStepDefs.java @@ -0,0 +1,34 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import io.cucumber.java.en.When; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +public class GetShelterStepDefs { + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterRepository shelterRepository; + + @When("^I retrieve the shelter with name \"([^\"]*)\"$") + public void iRetrieveShelterWithName(String name) throws Exception { + Shelter shelter = shelterRepository.findByName(name); + + stepDefs.result = stepDefs.mockMvc.perform( + get("/shelters/{id}", (shelter != null) ? shelter.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelter)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } +} diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterShelterStepDefs.java new file mode 100644 index 0000000..dd91c84 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterShelterStepDefs.java @@ -0,0 +1,84 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import io.cucumber.java.ParameterType; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +public class RegisterShelterStepDefs { + + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterRepository shelterRepository; + public static String newResourceUri; + + @ParameterType(value = "true|True|TRUE|false|False|FALSE") + public Boolean booleanValue(String value) { + return Boolean.valueOf(value); + } + @When("I register a new shelter with name \"([^\"]*)\", email \"([^\"]*)\", mobile \"([^\"]*)\" and isActive (True|False)$") + public void iRegisterANewShelterWithNameEmailMobileAndIsActive(String name, String email, String mobile, boolean isActive) throws Exception { + Shelter shelter = new Shelter(); + shelter.setName(name); + shelter.setEmail(email); + shelter.setMobile(mobile); + shelter.setActive(isActive); + shelter.setCreatedAt(java.time.LocalDateTime.now()); + shelter.setUpdatedAt(java.time.LocalDateTime.now()); + + + stepDefs.result = stepDefs.mockMvc.perform( + post("/shelters") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelter)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + + newResourceUri = stepDefs.result.andReturn().getResponse().getHeader("Location"); + + } + + @Then("^It has been created a shelter with name \"([^\"]*)\", email \"([^\"]*)\", mobile \"([^\"]*)\", the isActive is not returned$") + public void itHasBeenCreatedAShelterWithNameEmailMobileAndTheIsActiveIsNotReturned(String name, String email, String mobile) { + Shelter createdShelter = shelterRepository.findByName(name); + assertThat(createdShelter).isNotNull(); + assertThat(createdShelter.getEmail()).isEqualTo(email); + assertThat(createdShelter.getMobile()).isEqualTo(mobile); + } + + @Then("^It has not been created a shelter with name \"([^\"]*)\"$") + public void itHasNotBeenCreatedAShelterWithName(String name) { + Shelter shelter = shelterRepository.findByName(name); + assertThat(shelter).isNull(); + } + + + @Given("^There is a registered shelter with name \"([^\"]*)\", email \"([^\"]*)\", mobile \"([^\"]*)\"$") + public void thereIsARegisteredShelterWithNameEmailMobileAndIsActiveTrue(String name, String email, String mobile) { + Shelter shelter = new Shelter(); + shelter.setName(name); + shelter.setEmail(email); + shelter.setMobile(mobile); + shelter.setCreatedAt(java.time.LocalDateTime.now()); + shelter.setUpdatedAt(java.time.LocalDateTime.now()); + shelterRepository.save(shelter); + + Shelter shelter_find = shelterRepository.findByName(name); + assertThat(shelter_find).isNotNull(); + } +} + diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateShelterStepDefs.java new file mode 100644 index 0000000..c4f265c --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateShelterStepDefs.java @@ -0,0 +1,54 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import io.cucumber.datatable.DataTable; +import io.cucumber.java.en.When; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +public class UpdateShelterStepDefs { + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterRepository shelterRepository; + + @When("^I update the shelter with name \"([^\"]*)\" and new attributes:$") + public void iUpdateShelterWithName(String name, DataTable table) throws Exception { + List> attributesList = table.asMaps(String.class, String.class); + + // Assuming only one row of attributes is provided, so taking the first map from the list + Map attributes = attributesList.get(0); + + // Fetch attributes from the map + String email = attributes.get("email"); + String mobile = attributes.get("mobile"); + String isActive = attributes.get("isActive"); + + Shelter shelter = shelterRepository.findByName(name); + if (shelter != null) { + if (email != null) shelter.setEmail(email); + if (mobile != null) shelter.setMobile(mobile); + if (isActive != null) shelter.setActive(Boolean.parseBoolean(isActive)); + shelter.setUpdatedAt(java.time.LocalDateTime.now()); + } + + stepDefs.result = stepDefs.mockMvc.perform( + patch("/shelters/{id}", (shelter != null) ? shelter.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelter)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + + } +} diff --git a/src/test/resources/features/DeleteShelter.feature b/src/test/resources/features/DeleteShelter.feature new file mode 100644 index 0000000..52115ba --- /dev/null +++ b/src/test/resources/features/DeleteShelter.feature @@ -0,0 +1,27 @@ +Feature: Delete Shelter + In order to maintain shelter data + As a user with appropriate permissions + I want to be able to delete a shelter + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + And There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + + Scenario: Delete existing shelter + Given I can login with username "username" and password "password" + When I delete the shelter with name "existing_shelter" + Then The response code is 200 + And The shelter with name "existing_shelter" has been deleted + + Scenario: Delete shelter when I am not logged in + Given I'm not logged in + When I delete the shelter with name "existing_shelter" + Then The response code is 401 + And The shelter with name "existing_shelter" has not been deleted + + Scenario: Delete non-existent shelter + Given I can login with username "username" and password "password" + When I delete the shelter with name "non_existent_shelter" + Then The response code is 404 + + diff --git a/src/test/resources/features/GetShelter.feature b/src/test/resources/features/GetShelter.feature new file mode 100644 index 0000000..4091d1c --- /dev/null +++ b/src/test/resources/features/GetShelter.feature @@ -0,0 +1,23 @@ +Feature: Get Shelter + In order to retrieve shelter information + As a user with appropriate permissions + I want to be able to get shelter details + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + + Scenario: Get existing shelter details + Given I can login with username "username" and password "password" + And There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + When I retrieve the shelter with name "existing_shelter" + Then The response code is 200 + + Scenario: Get non-existent shelter details + Given I can login with username "username" and password "password" + When I retrieve the shelter with name "non_existent_shelter" + Then The response code is 404 + + Scenario: Get shelter details when I am not logged in + Given I'm not logged in + When I retrieve the shelter with name "existing_shelter" + Then The response code is 404 \ No newline at end of file diff --git a/src/test/resources/features/RegisterShelter.feature b/src/test/resources/features/RegisterShelter.feature new file mode 100644 index 0000000..a4e3899 --- /dev/null +++ b/src/test/resources/features/RegisterShelter.feature @@ -0,0 +1,81 @@ +Feature: Register Shelter + In order to use the app + As a user + I want to register a Shelter + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + + Scenario: Register existing shelter email + Given I can login with username "username" and password "password" + # And I am a user with role "ShelterVolunteer" Temporarily commented because we need to do first the Role tests + Given There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + When I register a new shelter with name "new_shelter", email "existing_shelter@gmail.com", mobile "+34 567 56 56 56" and isActive True + Then The response code is 409 + + Scenario: Register shelter with no permission + Given I can login with username "username" and password "password" +# And I am a user with role "User" Temporarily commented because we need to do first the Role tests + When I register a new shelter with name "new_shelter", email "new_shelter@gmail.com", mobile "+34 567 56 56 56" and isActive True +# Then The response code is 403 Temporarily commented because we need to do first the Role tests + Then The response code is 201 +# And It has not been created a shelter with name "new_shelter" Temporarily commented because we need to do first the Role tests + + Scenario: Register shelter with empty name + Given I can login with username "username" and password "password" +# Given I am a user with role "ShelterVolunteer" Temporarily commented because we need to do first the Role tests + When I register a new shelter with name "", email "new_shelter@gmail.com", mobile "+34 567 56 56 56" and isActive True + Then The response code is 400 + And The error message is "must not be blank" + And It has not been created a shelter with name "" + + Scenario: Register shelter with empty email + Given I can login with username "username" and password "password" +# Given I am a user with role "ShelterVolunteer" Temporarily commented because we need to do first the Role tests + When I register a new shelter with name "new_shelter", email "", mobile "+34 567 56 56 56" and isActive True + Then The response code is 400 + And The error message is "must not be blank" + And It has not been created a shelter with name "new_shelter" + + Scenario: Register shelter with invalid email + Given I can login with username "username" and password "password" +# Given I am a user with role "ShelterVolunteer" Temporarily commented because we need to do first the Role tests + When I register a new shelter with name "new_shelter", email "invalid_email", mobile "+34 567 56 56 56" and isActive True + Then The response code is 400 + And The error message is "must be a well-formed email address" + And It has not been created a shelter with name "new_shelter" + + Scenario: Register shelter with empty mobile + Given I can login with username "username" and password "password" +# Given I am a user with role "ShelterVolunteer" Temporarily commented because we need to do first the Role tests + When I register a new shelter with name "new_shelter", email "new_shelter@gmail.com", mobile "" and isActive True + Then The response code is 400 + And The error message is "must not be blank" + And It has not been created a shelter with name "new_shelter" + + Scenario: Register shelter with invalid mobile format + Given I can login with username "username" and password "password" +# Given I am a user with role "ShelterVolunteer" Temporarily commented because we need to do first the Role tests + When I register a new shelter with name "new_shelter", email "new_shelter@gmail.com", mobile "invalid_mobile" and isActive True + Then The response code is 400 + And It has not been created a shelter with name "new_shelter" + + Scenario: Register shelter with existing email + Given I can login with username "username" and password "password" +# And I am a user with role "ShelterVolunteer" Temporarily commented because we need to do first the Role tests + Given There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + When I register a new shelter with name "new_shelter", email "existing_shelter@gmail.com", mobile "+34 567 56 56 56" and isActive True + Then The response code is 409 + And It has not been created a shelter with name "new_shelter" + + Scenario: Register shelter when I am not logged in + Given I'm not logged in + When I register a new shelter with name "new_shelter", email "new_shelter@gmail.com", mobile "+34 567 56 56 56" and isActive True + Then The response code is 401 + And It has not been created a shelter with name "new_shelter" + + Scenario: Register shelter with valid attributes + Given I can login with username "username" and password "password" + When I register a new shelter with name "new_shelter", email "new_shelter@gmail.com", mobile "+34 567 56 56 56" and isActive True + Then The response code is 201 + And It has been created a shelter with name "new_shelter", email "new_shelter@gmail.com", mobile "+34 567 56 56 56", the isActive is not returned \ No newline at end of file diff --git a/src/test/resources/features/UpdateShelter.feature b/src/test/resources/features/UpdateShelter.feature new file mode 100644 index 0000000..0702653 --- /dev/null +++ b/src/test/resources/features/UpdateShelter.feature @@ -0,0 +1,47 @@ +Feature: Update Shelter + In order to maintain shelter information + As a user with appropriate permissions + I want to be able to update a shelter + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + + Scenario: Update existing shelter + Given I can login with username "username" and password "password" + And There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + When I update the shelter with name "existing_shelter" and new attributes: + | email | mobile | isActive | + | new_email@gmail.com | +34 567 56 56 56 | True | + Then The response code is 200 + + Scenario: Update non-existent shelter + Given I can login with username "username" and password "password" + When I update the shelter with name "non_existent_shelter" and new attributes: + | email | mobile | isActive | + | new_email@gmail.com | +34 567 56 56 56 | True | + Then The response code is 404 + + Scenario: Update shelter with invalid email format + Given I can login with username "username" and password "password" + And There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + When I update the shelter with name "existing_shelter" and new attributes: + | email | + | invalid_email | + Then The response code is 400 + And The error message is "must be a well-formed email address" + + Scenario: Update shelter with invalid mobile format + Given I can login with username "username" and password "password" + And There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + When I update the shelter with name "existing_shelter" and new attributes: + | mobile | + | invalid_mobile | + Then The response code is 400 + + Scenario: Update shelter when I am not logged in + Given I'm not logged in + And There is a registered shelter with name "existing_shelter", email "existing_shelter@gmail.com", mobile "+34 123 45 67 89" + When I update the shelter with name "existing_shelter" and new attributes: + | email | mobile | isActive | + | new_email@gmail.com | +34 567 56 56 56 | True | + Then The response code is 401 \ No newline at end of file