diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterCertificate.java b/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterCertificate.java index 9d50754..9e18147 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterCertificate.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterCertificate.java @@ -13,7 +13,6 @@ @EqualsAndHashCode(callSuper = false) public class ShelterCertificate { @OneToOne - @NotNull @JsonIdentityReference(alwaysAsId = true) private Shelter shelter; @@ -21,7 +20,6 @@ public class ShelterCertificate { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @NotNull private LocalDateTime expirationDate; } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterCertificateStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterCertificateStepDefs.java new file mode 100644 index 0000000..6656f3e --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterCertificateStepDefs.java @@ -0,0 +1,49 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.ShelterCertificate; +import cat.udl.eps.softarch.demo.repository.ShelterCertificateRepository; +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.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + + +public class DeleteShelterCertificateStepDefs { + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterCertificateRepository shelterCertificateRepository; + + @When("^I delete the shelter certificate with id \"([^\"]*)\"$") + public void iDeleteAShelterCertificateWithId(Long id) throws Exception { + ShelterCertificate shelterCertificate = shelterCertificateRepository.findById(id).orElse(null); + + stepDefs.result = stepDefs.mockMvc.perform( + delete("/shelterCertificates/{id}", (shelterCertificate != null) ? shelterCertificate.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelterCertificate)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } + + @And("^The shelter certificate with id \"([^\"]*)\" has been deleted$") + public void theShelterCertificateWithIdHasBeenDeleted(Long id) { + boolean exists = shelterCertificateRepository.existsById(id); + assertThat(exists).isFalse(); + } + + @And("^The shelter certificate with id \"([^\"]*)\" has not been deleted$") + public void theShelterCertificateWithIdHasNotBeenDeleted(Long id) { + boolean exists = shelterCertificateRepository.existsById(id); + assertThat(exists).isTrue(); + } +} \ No newline at end of file diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/GetShelterCertificateStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/GetShelterCertificateStepDefs.java new file mode 100644 index 0000000..bafb9ec --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/GetShelterCertificateStepDefs.java @@ -0,0 +1,34 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.ShelterCertificate; +import cat.udl.eps.softarch.demo.repository.ShelterCertificateRepository; +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 GetShelterCertificateStepDefs { + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterCertificateRepository shelterCertificateRepository; + + @When("^I retrieve the shelter certificate with id \"([^\"]*)\"$") + public void iRetrieveShelterCertificateWithId(String id) throws Exception { + ShelterCertificate shelterCertificate = shelterCertificateRepository.findById(Long.parseLong(id)).orElse(null); + + stepDefs.result = stepDefs.mockMvc.perform( + get("/shelterCertificates/{id}", (shelterCertificate != null) ? shelterCertificate.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelterCertificate)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } +} \ No newline at end of file diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterShelterCertificateStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterShelterCertificateStepDefs.java new file mode 100644 index 0000000..8b30cbb --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterShelterCertificateStepDefs.java @@ -0,0 +1,80 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.domain.ShelterCertificate; +import cat.udl.eps.softarch.demo.repository.ShelterCertificateRepository; +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 RegisterShelterCertificateStepDefs { + + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterCertificateRepository shelterCertificateRepository; + + @Autowired + private ShelterRepository shelterRepository; + + public static String newResourceUri; + + @When("I register a new shelter certificate with id \"([^\"]*)\" for shelter with name \"([^\"]*)\"$") + public void iRegisterANewShelterCertificateWithIdForShelterWithName(Long id, String shelterName) throws Exception { + ShelterCertificate shelterCertificate = new ShelterCertificate(); + shelterCertificate.setId(id); + + Shelter shelter = shelterRepository.findByName(shelterName); + shelterCertificate.setShelter(shelter); + + stepDefs.result = stepDefs.mockMvc.perform( + post("/shelterCertificates") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelterCertificate)) + .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 certificate with id \"([^\"]*)\" for shelter with name \"([^\"]*)\"$") + public void itHasBeenCreatedAShelterCertificateWithIdForShelterWithName(Long id, String shelterName) { + ShelterCertificate createdShelterCertificate = shelterCertificateRepository.findById(id).orElse(null); + assertThat(createdShelterCertificate).isNotNull(); + assertThat(createdShelterCertificate.getShelter().getName()).isEqualTo(shelterName); + } + + @Then("^It has not been created a shelter certificate with id \"([^\"]*)\"$") + public void itHasNotBeenCreatedAShelterCertificateWithId(Long id) { + boolean exists = shelterCertificateRepository.existsById(id); + assertThat(exists).isFalse(); + } + + @Given("^There is a registered shelter certificate with id \"([^\"]*)\" for shelter with name \"([^\"]*)\"$") + public void thereIsARegisteredShelterCertificateWithIdForShelterWithName(Long id, String shelterName) { + ShelterCertificate shelterCertificate = new ShelterCertificate(); + shelterCertificate.setId(id); + + Shelter shelter = shelterRepository.findByName(shelterName); + shelterCertificate.setShelter(shelter); + + shelterCertificateRepository.save(shelterCertificate); + + boolean exists = shelterCertificateRepository.existsById(id); + assertThat(exists).isTrue(); + } +} \ No newline at end of file diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateShelterCertificateStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateShelterCertificateStepDefs.java new file mode 100644 index 0000000..c71b657 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateShelterCertificateStepDefs.java @@ -0,0 +1,50 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.ShelterCertificate; +import cat.udl.eps.softarch.demo.repository.ShelterCertificateRepository; +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.time.LocalDateTime; +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 UpdateShelterCertificateStepDefs { + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterCertificateRepository shelterCertificateRepository; + + @When("^I update the shelter certificate with id \"([^\"]*)\" and new attributes:$") + public void iUpdateShelterCertificateWithId(Long id, 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 expirationDate = attributes.get("expirationDate"); + + ShelterCertificate shelterCertificate = shelterCertificateRepository.findById(id).orElse(null); + if (shelterCertificate != null) { + if (expirationDate != null) shelterCertificate.setExpirationDate(LocalDateTime.parse(expirationDate)); + } + + stepDefs.result = stepDefs.mockMvc.perform( + patch("/shelterCertificates/{id}", (shelterCertificate != null) ? shelterCertificate.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(shelterCertificate)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + + } +} \ No newline at end of file diff --git a/src/test/resources/features/DeleteShelterCertificate.feature b/src/test/resources/features/DeleteShelterCertificate.feature new file mode 100644 index 0000000..71e92ad --- /dev/null +++ b/src/test/resources/features/DeleteShelterCertificate.feature @@ -0,0 +1,25 @@ +Feature: Delete Shelter Certificate + In order to maintain shelter certificate data + As a user with appropriate permissions + I want to be able to delete a shelter certificate + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + And There is a registered shelter certificate with id "1" for shelter with name "Shelter Certificate 1" + + Scenario: Delete existing shelter certificate + Given I can login with username "username" and password "password" + When I delete the shelter certificate with id "1" + Then The response code is 200 + And The shelter certificate with id "1" has been deleted + + Scenario: Delete shelter certificate when I am not logged in + Given I'm not logged in + When I delete the shelter certificate with id "1" + Then The response code is 401 + And The shelter certificate with id "1" has not been deleted + + Scenario: Delete non-existent shelter certificate + Given I can login with username "username" and password "password" + When I delete the shelter certificate with id "999" + Then The response code is 404 \ No newline at end of file diff --git a/src/test/resources/features/GetShelterCertificate.feature b/src/test/resources/features/GetShelterCertificate.feature new file mode 100644 index 0000000..0cb8884 --- /dev/null +++ b/src/test/resources/features/GetShelterCertificate.feature @@ -0,0 +1,23 @@ +Feature: Get Shelter Certificate + In order to retrieve shelter certificate information + As a user with appropriate permissions + I want to be able to get shelter certificate details + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + + Scenario: Get existing shelter certificate details + Given I can login with username "username" and password "password" + And There is a registered shelter certificate with id "1" for shelter with name "Shelter Certificate 1" + When I retrieve the shelter certificate with id "1" + Then The response code is 200 + + Scenario: Get non-existent shelter certificate details + Given I can login with username "username" and password "password" + When I retrieve the shelter certificate with id "999" + Then The response code is 404 + + Scenario: Get shelter certificate details when I am not logged in + Given I'm not logged in + When I retrieve the shelter certificate with id "1" + Then The response code is 404 \ No newline at end of file diff --git a/src/test/resources/features/RegisterShelterCertificate.feature b/src/test/resources/features/RegisterShelterCertificate.feature new file mode 100644 index 0000000..7056e2c --- /dev/null +++ b/src/test/resources/features/RegisterShelterCertificate.feature @@ -0,0 +1,28 @@ +Feature: Register Shelter Certificate + In order to use the app + As a user + I want to register a Shelter Certificate + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + + Scenario: Register shelter certificate 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 certificate with id "1" for shelter with name "new_shelter" +# 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 certificate with id "1" Temporarily commented because we need to do first the Role tests + + Scenario: Register shelter certificate when I am not logged in + Given I'm not logged in + When I register a new shelter certificate with id "1" for shelter with name "new_shelter" + Then The response code is 401 + And It has not been created a shelter certificate with id "1" + + Scenario: Register shelter certificate with valid attributes + Given I can login with username "username" and password "password" + And I register a new shelter with name "new_shelter", email "test@example.com", mobile "+34 123 456 789" and isActive True + When I register a new shelter certificate with id "1" for shelter with name "new_shelter" + Then The response code is 201 + And It has been created a shelter certificate with id "1" for shelter with name "new_shelter" \ No newline at end of file diff --git a/src/test/resources/features/UpdateShelterCertificate.feature b/src/test/resources/features/UpdateShelterCertificate.feature new file mode 100644 index 0000000..271ab79 --- /dev/null +++ b/src/test/resources/features/UpdateShelterCertificate.feature @@ -0,0 +1,30 @@ +Feature: Update Shelter Certificate + In order to maintain shelter certificate information + As a user with appropriate permissions + I want to be able to update a shelter certificate + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + + Scenario: Update existing shelter certificate + Given I can login with username "username" and password "password" + And There is a registered shelter certificate with id "1" for shelter with name "existing_shelter" + When I update the shelter certificate with id "1" and new attributes: + | expirationDate | + | 2023-12-31T00:00:00 | + Then The response code is 200 + + Scenario: Update non-existent shelter certificate + Given I can login with username "username" and password "password" + When I update the shelter certificate with id "999" and new attributes: + | expirationDate | + | 2023-12-31T00:00:00 | + Then The response code is 404 + + Scenario: Update shelter certificate when I am not logged in + Given I'm not logged in + And There is a registered shelter certificate with id "1" for shelter with name "existing_shelter" + When I update the shelter certificate with id "1" and new attributes: + | expirationDate | + | 2023-12-31T00:00:00 | + Then The response code is 401 \ No newline at end of file