From 72467e9220f0b68fa5e7cb550136aade0b00f63c Mon Sep 17 00:00:00 2001 From: peremunoz Date: Wed, 3 Apr 2024 23:37:10 +0200 Subject: [PATCH 1/5] Implement feature and step defs --- .../steps/AddShelterCertificateStepdefs.java | 38 +++++++++++++++++++ .../features/AddShelterCertificate.feature | 18 +++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java create mode 100644 src/test/resources/features/AddShelterCertificate.feature diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java new file mode 100644 index 00000000..8d744b4d --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java @@ -0,0 +1,38 @@ +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.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; +import java.time.ZonedDateTime; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +public class AddShelterCertificateStepdefs { + + @Autowired() + private ShelterRepository shelterRepository; + + @Autowired() + private StepDefs stepDefs; + + @When("I add a Shelter Certificate with valid expiration date to the shelter with name {string}") + public void iAddAShelterCertificateWithNameAndDescriptionToTheShelterWithName(String shelterName) throws Exception { + Shelter shelter = this.shelterRepository.findByName(shelterName).get(0); + + stepDefs.result = stepDefs.mockMvc.perform( + post("/shelterCertificates/", shelterName) + .contentType(MediaType.APPLICATION_JSON) + .content(new JSONObject() + .put("expirationDate", ZonedDateTime.now().plusMonths(1)) + .put("shelterServed", "/shelter/"+shelter.getId()) + .toString() + .getBytes(StandardCharsets.UTF_8)) + .with(AuthenticationStepDefs.authenticate())); + } +} diff --git a/src/test/resources/features/AddShelterCertificate.feature b/src/test/resources/features/AddShelterCertificate.feature new file mode 100644 index 00000000..6dc1b22d --- /dev/null +++ b/src/test/resources/features/AddShelterCertificate.feature @@ -0,0 +1,18 @@ +Feature: Add Shelter Certificate + As a ShelterVolunteer + I want to add a Shelter Certificate + So that my Shelter has a Certificate + + Background: Exists a shelter with name "test" and a shelter volunteer + Given A shelter with name "test" + And There is a registered user with username "volunteer" and password "123456789" and email "volunteer@mypets.com" + + Scenario: Adding a Shelter Certificate as a Shelter Volunteer + Given I can login with username "volunteer" and password "123456789" + When I add a Shelter Certificate with valid expiration date to the shelter with name "test" + Then The response code is 201 + + Scenario: Adding a Shelter Certificate as a non Shelter Volunteer + Given I cannot login with username "test" and password "wrongPassword" + When I add a Shelter Certificate with valid expiration date to the shelter with name "test" + Then The response code is 403 \ No newline at end of file From 685907922a7b20a29218a873fb35919a4075cb4d Mon Sep 17 00:00:00 2001 From: peremunoz Date: Fri, 5 Apr 2024 21:13:31 +0200 Subject: [PATCH 2/5] Remove unused import --- .../eps/softarch/demo/steps/AddShelterCertificateStepdefs.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java index 8d744b4d..e1335493 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java @@ -11,7 +11,6 @@ import java.time.ZonedDateTime; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; public class AddShelterCertificateStepdefs { From 85d72c7348f983efde2e5b24120f1881d42632bc Mon Sep 17 00:00:00 2001 From: peremunoz Date: Sat, 6 Apr 2024 23:39:56 +0200 Subject: [PATCH 3/5] Add method to set the validation status before the certificate saving --- .../udl/eps/softarch/demo/domain/ShelterCertificate.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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 101553ad..3c8a4d88 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 @@ -17,14 +17,17 @@ public class ShelterCertificate extends UriEntity { @GeneratedValue() private Long id; - @JsonProperty(access = JsonProperty.Access.READ_ONLY) @NotNull private ZonedDateTime expirationDate; - @NotNull private Boolean validated; @JsonIdentityReference(alwaysAsId = true) @ManyToOne private Shelter shelterServed; + + @PrePersist() + public void prePersist() { + this.validated = false; + } } From 18e6327b1e6ea27477a5b414c14fcdf67dba0e22 Mon Sep 17 00:00:00 2001 From: peremunoz Date: Sat, 6 Apr 2024 23:40:33 +0200 Subject: [PATCH 4/5] Fix uri to create the shelter certificate --- .../eps/softarch/demo/steps/AddShelterCertificateStepdefs.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java index e1335493..7a8c9251 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/AddShelterCertificateStepdefs.java @@ -25,7 +25,7 @@ public void iAddAShelterCertificateWithNameAndDescriptionToTheShelterWithName(St Shelter shelter = this.shelterRepository.findByName(shelterName).get(0); stepDefs.result = stepDefs.mockMvc.perform( - post("/shelterCertificates/", shelterName) + post("/shelterCertificates") .contentType(MediaType.APPLICATION_JSON) .content(new JSONObject() .put("expirationDate", ZonedDateTime.now().plusMonths(1)) From 0dd83951083a65fbfdb7f28cd59e25dc2df03230 Mon Sep 17 00:00:00 2001 From: peremunoz Date: Sat, 6 Apr 2024 23:40:43 +0200 Subject: [PATCH 5/5] Fix step response code --- src/test/resources/features/AddShelterCertificate.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/features/AddShelterCertificate.feature b/src/test/resources/features/AddShelterCertificate.feature index 6dc1b22d..cfc814bc 100644 --- a/src/test/resources/features/AddShelterCertificate.feature +++ b/src/test/resources/features/AddShelterCertificate.feature @@ -15,4 +15,4 @@ Feature: Add Shelter Certificate Scenario: Adding a Shelter Certificate as a non Shelter Volunteer Given I cannot login with username "test" and password "wrongPassword" When I add a Shelter Certificate with valid expiration date to the shelter with name "test" - Then The response code is 403 \ No newline at end of file + Then The response code is 401 \ No newline at end of file