-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from UdL-EPS-SoftArch/pet-features-tests
Implement pet tests
- Loading branch information
Showing
10 changed files
with
430 additions
and
5 deletions.
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
49 changes: 49 additions & 0 deletions
49
src/test/java/cat/udl/eps/softarch/demo/steps/DeletePetStepDefs.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,49 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Pet; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
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 DeletePetStepDefs { | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@When("^I delete the pet with chip \"([^\"]*)\"$") | ||
public void iDeleteAPetWithChip(String chip) throws Exception { | ||
Pet pet = petRepository.findByChip(chip); | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
delete("/pets/{id}", (pet != null) ? pet.getId() : "999") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(pet)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
|
||
@And("^The pet with chip \"([^\"]*)\" has been deleted$") | ||
public void thePetWithChipHasBeenDeleted(String chip) { | ||
Pet pet = petRepository.findByChip(chip); | ||
assertThat(pet).isNull(); | ||
} | ||
|
||
@And("^The pet with chip \"([^\"]*)\" has not been deleted$") | ||
public void thePetWithChipHasNotBeenDeleted(String chip) { | ||
Pet pet = petRepository.findByChip(chip); | ||
assertThat(pet).isNotNull(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/cat/udl/eps/softarch/demo/steps/GetPetStepDefs.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,34 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Pet; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
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 GetPetStepDefs { | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@When("^I retrieve the pet with chip \"([^\"]*)\"$") | ||
public void iRetrievePetWithChip(String chip) throws Exception { | ||
Pet pet = petRepository.findByChip(chip); | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
get("/pets/{id}", (pet != null) ? pet.getId() : "999") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(pet)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/test/java/cat/udl/eps/softarch/demo/steps/RegisterPetStepDefs.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,124 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Cat; | ||
import cat.udl.eps.softarch.demo.domain.Pet; | ||
import cat.udl.eps.softarch.demo.domain.Shelter; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
import cat.udl.eps.softarch.demo.repository.ShelterRepository; | ||
import io.cucumber.cucumberexpressions.Transformer; | ||
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.checkerframework.checker.formatter.qual.Format; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
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 RegisterPetStepDefs { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@Autowired | ||
private ShelterRepository shelterRepository; | ||
public static String newResourceUri; | ||
|
||
public static LocalDate dateValue(String value) { | ||
return LocalDate.parse(value, DateTimeFormatter.ofPattern("dd-MM-yyyy")); | ||
} | ||
|
||
@ParameterType(name= "boolean", value = "true|True|TRUE|false|False|FALSE") | ||
public Boolean booleanValue(String value) { | ||
return Boolean.valueOf(value); | ||
} | ||
|
||
@When("I register a new pet with name {string}, date of birth {string}, isAdopted {boolean}, colour {string}, sex {string}, chip {string}, race {string}, isDangerous {boolean}, at the shelter named {string}") | ||
public void iRegisterANewPetWithData(String name, String dateOfBirth, boolean adoptionStatus, String colour, String sex, String chip, String race, boolean isDangerous, String shelterName) throws Exception { | ||
Pet pet = new Pet(); | ||
pet.setName(name); | ||
pet.setDateOfBirth(dateValue(dateOfBirth)); | ||
pet.setAdopted(adoptionStatus); | ||
pet.setColour(colour); | ||
pet.setSex(sex); | ||
pet.setChip(chip); | ||
pet.setRace(race); | ||
pet.setDangerous(isDangerous); | ||
|
||
Shelter shelter = shelterRepository.findByName(shelterName); | ||
pet.setShelter(shelter); | ||
|
||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/pets") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(pet)) | ||
.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 pet with name {string}, date of birth {string}, isAdopted {boolean}, colour {string}, sex {string}, chip {string}, race {string}, isDangerous {boolean}, at the shelter named {string}") | ||
public void itHasBeenCreatedAPetWithData(String name, String dateOfBirth, boolean adoptionStatus, String colour, String sex, String chip, String race, boolean isDangerous, String shelterName) { | ||
Pet createdPet = petRepository.findByChip(chip); | ||
Shelter shelter = shelterRepository.findByName(shelterName); | ||
assertThat(createdPet).isNotNull(); | ||
assertThat(createdPet.getName()).isEqualTo(name); | ||
assertThat(createdPet.getDateOfBirth()).isEqualTo(dateValue(dateOfBirth)); | ||
assertThat(createdPet.isAdopted()).isEqualTo(adoptionStatus); | ||
assertThat(createdPet.getColour()).isEqualTo(colour); | ||
assertThat(createdPet.getSex()).isEqualTo(sex); | ||
assertThat(createdPet.getChip()).isEqualTo(chip); | ||
assertThat(createdPet.getRace()).isEqualTo(race); | ||
assertThat(createdPet.isDangerous()).isEqualTo(isDangerous); | ||
assertThat(createdPet.getShelter()).isEqualTo(shelter); | ||
} | ||
|
||
@Then("It has not been created a pet with chip {string}") | ||
public void itHasNotBeenCreatedAPetWithChip(String chip) { | ||
Pet pet = petRepository.findByChip(chip); | ||
assertThat(pet).isNull(); | ||
} | ||
|
||
@Then("It has not been created a pet with name {string}") | ||
public void itHasNotBeenCreatedAPetWithName(String name) { | ||
Pet pet = petRepository.findByName(name); | ||
assertThat(pet).isNull(); | ||
} | ||
|
||
@Given("There is a registered pet with name {string}, date of birth {string}, isAdopted {boolean}, colour {string}, sex {string}, chip {string}, race {string}, isDangerous {boolean}, at the shelter named {string}") | ||
public void thereIsARegisteredPetWithData(String name, String dateOfBirth, boolean adoptionStatus, String colour, String sex, String chip, String race, boolean isDangerous, String shelterName) { | ||
Pet pet = new Pet(); | ||
pet.setName(name); | ||
pet.setDateOfBirth(dateValue(dateOfBirth)); | ||
pet.setAdopted(adoptionStatus); | ||
pet.setColour(colour); | ||
pet.setSex(sex); | ||
pet.setChip(chip); | ||
pet.setRace(race); | ||
pet.setDangerous(isDangerous); | ||
|
||
Shelter shelter = shelterRepository.findByName(shelterName); | ||
pet.setShelter(shelter); | ||
|
||
petRepository.save(pet); | ||
|
||
Pet petResult = petRepository.findByName(name); | ||
assertThat(petResult).isNotNull(); | ||
} | ||
} | ||
|
65 changes: 65 additions & 0 deletions
65
src/test/java/cat/udl/eps/softarch/demo/steps/UpdatePetStepDefs.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,65 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Pet; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
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.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
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 UpdatePetStepDefs { | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@When("^I update the pet with chip \"([^\"]*)\" and new attributes:$") | ||
public void iUpdatePetWithChip(String chip, 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 name = attributes.get("name"); | ||
String dateOfBirth = attributes.get("dateOfBirth"); | ||
String isAdopted = attributes.get("isAdopted"); | ||
String colour = attributes.get("colour"); | ||
String size = attributes.get("size"); | ||
String sex = attributes.get("sex"); | ||
String race = attributes.get("race"); | ||
String isDangerous = attributes.get("isDangerous"); | ||
|
||
Pet pet = petRepository.findByChip(chip); | ||
if (pet != null) { | ||
if (name != null) pet.setName(name); | ||
if (dateOfBirth != null) pet.setDateOfBirth(LocalDate.parse(dateOfBirth, DateTimeFormatter.ofPattern("dd-MM-yyyy"))); | ||
if (isAdopted != null) pet.setAdopted(Boolean.parseBoolean(isAdopted)); | ||
if (colour != null) pet.setColour(colour); | ||
if (size != null) pet.setColour(size); | ||
if (sex != null) pet.setSex(sex); | ||
if (race != null) pet.setRace(race); | ||
if (isDangerous != null) pet.setDangerous(Boolean.parseBoolean(isDangerous)); | ||
} | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
patch("/pets/{id}", (pet != null) ? pet.getId() : "999") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(pet)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
|
||
} | ||
} |
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,30 @@ | ||
Feature: Delete Pet | ||
In order to maintain pet data | ||
As a user with appropriate permissions | ||
I want to be able to delete a pet | ||
|
||
Background: | ||
Given There is a registered user with username "username" and password "password" and email "[email protected]" | ||
And There is a registered shelter with name "shelter1", email "[email protected]", mobile "666 55 44 33" | ||
And There is a registered pet with name "super_cat", date of birth "01-01-2024", isAdopted True, colour "black", sex "male", chip "1234", race "european", isDangerous False, at the shelter named "shelter1" | ||
|
||
Scenario: Delete existing pet | ||
Given I can login with username "username" and password "password" | ||
# And I am a user with role "petOwner" Temporarily commented because we need to do first the Role tests | ||
When I delete the pet with chip "1234" | ||
Then The response code is 200 | ||
And The pet with chip "1234" has been deleted | ||
|
||
Scenario: Delete pet when I am not logged in | ||
Given I'm not logged in | ||
When I delete the pet with chip "1234" | ||
Then The response code is 401 | ||
And The pet with chip "1234" has not been deleted | ||
|
||
Scenario: Delete non-existent pet | ||
Given I can login with username "username" and password "password" | ||
# And I am a user with role "petOwner" Temporarily commented because we need to do first the Role tests | ||
When I delete the pet with chip "9999" | ||
Then The response code is 404 | ||
|
||
|
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,24 @@ | ||
Feature: Get Pet | ||
In order to retrieve pet information | ||
As a user with appropriate permissions | ||
I want to be able to get pet details | ||
|
||
Background: | ||
Given There is a registered user with username "username" and password "password" and email "[email protected]" | ||
And There is a registered shelter with name "shelter1", email "[email protected]", mobile "666 55 44 33" | ||
|
||
Scenario: Get existing pet details | ||
Given I can login with username "username" and password "password" | ||
And There is a registered pet with name "super_cat", date of birth "01-01-2024", isAdopted True, colour "black", sex "male", chip "1234", race "european", isDangerous False, at the shelter named "shelter1" | ||
When I retrieve the pet with chip "1234" | ||
Then The response code is 200 | ||
|
||
Scenario: Get non-existent pet details | ||
Given I can login with username "username" and password "password" | ||
When I retrieve the pet with chip "1234" | ||
Then The response code is 404 | ||
|
||
Scenario: Get pet details when I am not logged in | ||
Given I'm not logged in | ||
When I retrieve the pet with chip "1234" | ||
Then The response code is 404 |
Oops, something went wrong.