-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6964f76
create feature file
faysalbadaoui 333b3f9
Merge remote-tracking branch 'origin/main' into Feature-validate-shel…
faysalbadaoui 01b76dc
implementation Validate certificate test
faysalbadaoui 3ffc284
Merge branch 'main' into Feature-validate-shelter-certificate
faysalbadaoui 5880aa7
try to use api on stepdefs
faysalbadaoui 0cf38ef
Implement test cases
peremunoz 4049f5a
Merge branch 'main' into Feature-validate-shelter-certificate
peremunoz 173f85c
Refine a little bit the feature scenario
peremunoz e362c5a
Improve step implementation and add a new one to double-check the cer…
peremunoz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/test/java/cat/udl/eps/softarch/demo/steps/ValidateShelterCertificateStepDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/test/resources/features/ValidateShelterCertificate.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.