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

Shelter certificate tests (Steps and Features) #33

Merged
merged 11 commits into from
Apr 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
@EqualsAndHashCode(callSuper = false)
public class ShelterCertificate {
@OneToOne
@NotNull
@JsonIdentityReference(alwaysAsId = true)
private Shelter shelter;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
private LocalDateTime expirationDate;

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Map<String, String>> attributesList = table.asMaps(String.class, String.class);

// Assuming only one row of attributes is provided, so taking the first map from the list
Map<String, String> 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());

}
}
25 changes: 25 additions & 0 deletions src/test/resources/features/DeleteShelterCertificate.feature
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
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
23 changes: 23 additions & 0 deletions src/test/resources/features/GetShelterCertificate.feature
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"

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
28 changes: 28 additions & 0 deletions src/test/resources/features/RegisterShelterCertificate.feature
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"

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 "[email protected]", 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"
30 changes: 30 additions & 0 deletions src/test/resources/features/UpdateShelterCertificate.feature
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"

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
Loading