Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Validate Shelter Certificate #87

Merged
merged 9 commits into from
Apr 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import java.time.ZonedDateTime;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -24,6 +21,9 @@ public class ShelterCertificate extends UriEntity<Long> {
@NotNull
private ZonedDateTime expirationDate;

@NotNull
private Boolean validated;

@JsonIdentityReference(alwaysAsId = true)
@ManyToOne
private Shelter shelterServed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import cat.udl.eps.softarch.demo.domain.ShelterCertificate;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import cat.udl.eps.softarch.demo.domain.Shelter;
import org.springframework.data.repository.query.Param;

public interface ShelterCertificateRepository extends CrudRepository<ShelterCertificate, Long>, PagingAndSortingRepository<ShelterCertificate, Long> {

ShelterCertificate findByShelterServed(@Param("shelterServed") Shelter shelter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Admin;
import cat.udl.eps.softarch.demo.domain.Location;
import cat.udl.eps.softarch.demo.domain.Shelter;
import cat.udl.eps.softarch.demo.domain.ShelterCertificate;
import cat.udl.eps.softarch.demo.repository.AdminRepository;
import cat.udl.eps.softarch.demo.repository.LocationRepository;
import cat.udl.eps.softarch.demo.repository.ShelterCertificateRepository;
import cat.udl.eps.softarch.demo.repository.ShelterRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.json.JSONObject;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;

public class ValidateShelterCertificateStepDefs {

@Autowired
private ShelterCertificateRepository shelterCertificateRepository;

@Autowired
private ShelterRepository shelterRepository;

@Autowired
private LocationRepository locationRepository;

@Autowired
private AdminRepository adminRepository;

@Autowired
private StepDefs stepDefs;

@Given("^A shelter with name \"([^\"]*)\"$")
public void thereIsAShelterCreatedWithName(String name){
Location location = new Location();
locationRepository.save(location);
if(shelterRepository.findByName(name) != null){
Shelter shelter = new Shelter();
shelter.setName(name);
shelter.setEmail("[email protected]");
shelter.setActive(true);
shelter.setLocatedAt(location);
shelter.setMobile("000000000");
shelterRepository.save(shelter);
}
}

@And("^An admin with username \"([^\"]*)\" and password \"([^\"]*)\"$")
public void anAdminWithUsername(String username, String password){
Admin admin = new Admin();
admin.setId(username);
admin.setEmail("[email protected]");
admin.setPassword(password);
admin.encodePassword();
adminRepository.save(admin);
}

@Given("^a shelter certificate associated with a shelter with name \"([^\"]*)\" with valid information created by a shelter volunteer$")
public void aShelterCertificateWithValidInformationSentByAVolunteer(String name){
Shelter shelter = shelterRepository.findByName(name).get(0);
ShelterCertificate shelterCertificate = new ShelterCertificate();
shelterCertificate.setShelterServed(shelter);
shelterCertificate.setValidated(false);
shelterCertificate.setExpirationDate(ZonedDateTime.now().plusMonths(6));
shelterCertificateRepository.save(shelterCertificate);
}

@When("^the admin with username \"([^\"]*)\" and password \"([^\"]*)\" verifies the certificate validity associated with a shelter with name \"([^\"]*)\"$")
public void thenAdminShouldVerifyTheCertificateValidityAssociatedWithAShelterWithName(String username, String password, String name) throws Exception {
Shelter shelter = shelterRepository.findByName(name).get(0);
AuthenticationStepDefs.currentUsername = username;
AuthenticationStepDefs.currentPassword = password;

stepDefs.result = stepDefs.mockMvc.perform(
get( "/shelterCertificates/search/findByShelterServed?shelterServed={id}", "/shelters/"+shelter.getId())
.with(AuthenticationStepDefs.authenticate())
.accept("application/json"));

JSONObject jsonObject = new JSONObject(stepDefs.result.andReturn().getResponse().getContentAsString());

String shelterCertificateUri = jsonObject.getString("uri");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
LocalDate shelterCertificateExpirationDate = LocalDate.parse(jsonObject.getString("expirationDate"), formatter);

boolean isShelterCertificateValid = shelterCertificateExpirationDate.isAfter(LocalDate.now());
if (!isShelterCertificateValid) {
System.out.println("The Shelter Certificate IS not valid!" + stepDefs.result);
Assert.fail("The Shelter Certificate IS not valid!");
}

stepDefs.result = stepDefs.mockMvc.perform(
patch( shelterCertificateUri)
.with(AuthenticationStepDefs.authenticate())
.content("{\"validated\":true}")
.accept("application/json"));

jsonObject = new JSONObject(stepDefs.result.andReturn().getResponse().getContentAsString());
boolean isShelterCertificateValidated = jsonObject.getBoolean("validated");
if (!isShelterCertificateValidated) {
System.out.println("The Shelter Certificate IS not validated!" + stepDefs.result);
Assert.fail("The Shelter Certificate IS not validated!");
}
}

@Given("^a shelter certificate associated with a shelter with name \"([^\"]*)\" with invalid information created by a shelter volunteer$")
public void aShelterCertificateWithInvalidInformationSentByAVolunteer(String name){
Shelter shelter = shelterRepository.findByName(name).get(0);
ShelterCertificate shelterCertificate = new ShelterCertificate();
shelterCertificate.setShelterServed(shelter);
shelterCertificate.setValidated(false);
shelterCertificate.setExpirationDate(ZonedDateTime.now().minusMonths(6));
shelterCertificateRepository.save(shelterCertificate);
}

@Then("^the admin with username \"([^\"]*)\" and password \"([^\"]*)\" rejects the certificate validity associated with a shelter with name \"([^\"]*)\"$")
public void thenAdminShouldVerifyTheCertificateValidityAssociatedWithAShelterWithName1(String username, String password, String name) throws Exception {
Shelter shelter = shelterRepository.findByName(name).get(0);
AuthenticationStepDefs.currentUsername = username;
AuthenticationStepDefs.currentPassword = password;

stepDefs.result = stepDefs.mockMvc.perform(
get( "/shelterCertificates/search/findByShelterServed?shelterServed={id}", "/shelters/"+shelter.getId())
.with(AuthenticationStepDefs.authenticate())
.accept("application/json"));

JSONObject jsonObject = new JSONObject(stepDefs.result.andReturn().getResponse().getContentAsString());

boolean isValidated = jsonObject.getBoolean("validated");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
LocalDate shelterCertificateExpirationDate = LocalDate.parse(jsonObject.getString("expirationDate"), formatter);

boolean isShelterCertificateValid = shelterCertificateExpirationDate.isAfter(LocalDate.now());
Assert.assertFalse(isShelterCertificateValid && isValidated);
}

}
18 changes: 18 additions & 0 deletions src/test/resources/features/ValidateShelterCertificate.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: Validate ShelterCertificate
As an Admin
I want to validate a ShelterCertificate
So that I can ensure Its validity and authenticity

Background: Exists a shelter with name "test"
Given A shelter with name "test"
And An admin with username "admin" and password "123456789"

Scenario: Validating a correct shelter certificate
Given a shelter certificate associated with a shelter with name "test" with valid information created by a shelter volunteer
When the admin with username "admin" and password "123456789" verifies the certificate validity associated with a shelter with name "test"
Then The response code is 200

Scenario: Validating a incorrect shelter certificate
Given a shelter certificate associated with a shelter with name "test" with invalid information created by a shelter volunteer
Then the admin with username "admin" and password "123456789" rejects the certificate validity associated with a shelter with name "test"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a When and not a Then? And, accordingly, in the implementation of this step use the API to make some sort of update of the state of the certificate (beyond just reading it)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solved the feature definition and improved the step implementation.


Loading