-
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 #23 from UdL-EPS-SoftArch/adoptions-scenarios
Register adoption
- Loading branch information
Showing
7 changed files
with
256 additions
and
2 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
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
111 changes: 111 additions & 0 deletions
111
src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.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,111 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
import cat.udl.eps.softarch.demo.domain.*; | ||
import cat.udl.eps.softarch.demo.repository.AdoptionsRepository; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
import cat.udl.eps.softarch.demo.repository.UserRepository; | ||
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.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
|
||
import static java.lang.Long.parseLong; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
|
||
public class StepRegisterAdoption { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@Autowired | ||
private AdoptionsRepository adoptionsRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Given("^The Pet with chip \"([^\"]*)\" does not exists") | ||
public void nonExistingPet (String chip) { | ||
Pet pet = petRepository.findByChip(chip); | ||
assertNull(pet); | ||
} | ||
|
||
@Given("^The Pet with chip \"([^\"]*)\" is not adopted") | ||
public void isNotAdopted (String chip) { | ||
Pet pet = petRepository.findByChip(chip); | ||
assertTrue(pet != null && !pet.isAdopted()); | ||
} | ||
|
||
@Given("^The Pet with chip \"([^\"]*)\" is adopted by user \"([^\"]*)\"") | ||
public void isAdopted (String chip, String username) { | ||
Pet pet = petRepository.findByChip(chip); | ||
Adoptions adoptions = new Adoptions(); | ||
userRepository.findById(username).ifPresent(user -> adoptions.setUser(user)); | ||
adoptions.setPet(pet); | ||
petRepository.save(pet); | ||
assertTrue(pet.isAdopted()); | ||
} | ||
|
||
|
||
@When("^The user with username \"([^\"]*)\" adopts the Pet with chip \"([^\"]*)\"") | ||
public void adopt(String username, String chip) { | ||
Pet pet = petRepository.findByChip(chip); | ||
|
||
if (pet != null) { | ||
if (!pet.isAdopted()) { | ||
Adoptions adoptions = new Adoptions(); | ||
adoptions.setPet(pet); | ||
userRepository.findById(username).ifPresent(adoptions::setUser); | ||
pet.setAdopted(true); | ||
petRepository.save(pet); | ||
|
||
try { | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/adoptions") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(adoptions)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@And("^The Pet with chip \"([^\"]*)\" should be marked as adopted") | ||
public void petAdopted(String chip) { | ||
Pet pet = petRepository.findByChip(chip); | ||
assertTrue(pet.isAdopted()); | ||
} | ||
|
||
@Then("^The system should display an error message indicating the Pet with chip \"([^\"]*)\" is already adopted by another user \"([^\"]*)\"") | ||
public void petAlreadyAdopted(String chip, String username){ | ||
Pet pet = petRepository.findByChip(chip); | ||
Optional<Adoptions> adoptions = adoptionsRepository.findById(parseLong("1")); | ||
adoptions.ifPresent(adoption -> { | ||
User user = adoption.getUser(); | ||
Optional<User> opt = userRepository.findById(username); | ||
opt.ifPresent(user_actual -> assertTrue(pet.isAdopted() && user.equals(user_actual))); | ||
}); | ||
|
||
} | ||
|
||
|
||
@Then("^The system should display an error message indicating the Pet with chip \"([^\"]*)\" does not exist") | ||
public void petDoesNotExist(String chip){ | ||
Pet pet = petRepository.findByChip(chip); | ||
assertThat(pet).isNull(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/test/java/cat/udl/eps/softarch/demo/steps/StepUpdateAdoption.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,91 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Adoptions; | ||
import cat.udl.eps.softarch.demo.repository.AdoptionsRepository; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
import cat.udl.eps.softarch.demo.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
|
||
public class StepUpdateAdoption { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@Autowired | ||
private AdoptionsRepository adoptionsRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@When("^Update the adoption with id (\\d+) User with username \"([^\"]*)\"$") | ||
public void updateAdoptionUsername(int adoption_id, String final_username) { | ||
Long id_adoption = (long) adoption_id; | ||
Optional<Adoptions> opt = adoptionsRepository.findById(id_adoption); | ||
|
||
opt.ifPresent(adoptions -> { | ||
|
||
userRepository.findById(final_username).ifPresent(user -> { | ||
adoptions.setUser(user); | ||
adoptions.setDateOfAdoption(LocalDateTime.now()); | ||
}); | ||
try { | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
patch("/adoptions/{id}", adoptions.getId()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(adoptions)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
|
||
@Then("^The adoption with id (\\d+) should have been updated to username \"([^\"]*)\"") | ||
public void theAdoptionUserForThePetWithIdShouldBeUpdatedToUsername(int id_int, String username) { | ||
Long id = (long) id_int; | ||
Optional<Adoptions> opt = adoptionsRepository.findById(id); | ||
|
||
opt.ifPresent(adoptions -> { | ||
userRepository.findById(username).ifPresent(user -> assertEquals(user, adoptions.getUser())); | ||
}); | ||
} | ||
|
||
|
||
@And("^The adoption with id (\\d+) dateofAdoption is updated") | ||
public void theAdoptionDateofAdoptionIsUpdated(int adoption_id) { | ||
Long id = (long) adoption_id; | ||
Optional<Adoptions> opt = adoptionsRepository.findById(id); | ||
|
||
opt.ifPresent(adoptions -> { | ||
assertEquals(adoptions.getDateOfAdoption().getDayOfMonth(), LocalDateTime.now().getDayOfMonth()); | ||
assertEquals(adoptions.getDateOfAdoption().getMonth(), LocalDateTime.now().getMonth()); | ||
assertEquals(adoptions.getDateOfAdoption().getYear(), LocalDateTime.now().getYear()); | ||
}); | ||
} | ||
|
||
@Then("^The system should display an error message indicating the username \"([^\"]*)\" does not exist") | ||
public void theSystemShouldDisplayAnErrorMessageIndicatingTheUsernameDoesNotExist(String username) { | ||
assertThat(userRepository.findById(username)).isEmpty(); | ||
} | ||
} |
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,28 @@ | ||
Feature: Register an Adoption | ||
In order to be able to adopt a Pet | ||
As a User | ||
I want to be able to adopt a Pet | ||
|
||
Background: | ||
Given There is a registered user with username "username" and password "password" and email "email@gmail.com" | ||
Given There is a registered user with username "username2" and password "password2" and email "email2@gmail.com" | ||
|
||
Scenario: User successfully adopts a Pet | ||
Given I can login with username "username" and password "password" | ||
Given There is a registered pet with name "cat", date of birth "10-10-2023", isAdopted False, colour "white", sex "male", chip "1234", race "european", isDangerous False, at the shelter named "shelter1" | ||
Given The Pet with chip "1234" is not adopted | ||
When The user with username "username" adopts the Pet with chip "1234" | ||
Then The Pet with chip "1234" should be marked as adopted | ||
|
||
Scenario: User tries to adopt a Pet that is already adopted | ||
Given I can login with username "username" and password "password" | ||
Given 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" | ||
Given The Pet with chip "1234" is adopted by user "username2" | ||
When The user with username "username" adopts the Pet with chip "1234" | ||
Then The system should display an error message indicating the Pet with chip "1234" is already adopted by another user "username2" | ||
|
||
Scenario: User tries to adopt a Pet that does not exist | ||
Given I can login with username "username" and password "password" | ||
Given The Pet with chip "9876" does not exists | ||
When The user with username "username" adopts the Pet with chip "9876" | ||
Then The system should display an error message indicating the Pet with chip "9876" does not exist |
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: Update an Adoption | ||
In order to manage pet adoptions | ||
As a User | ||
I want to be able to update the adoption status of a Pet | ||
|
||
Background: | ||
Given There is a registered user with username "username" and password "password" and email "email@gmail.com" | ||
Given There is a registered user with username "username2" and password "password2" and email "email2@gmail.com" | ||
Given There is a registered user with username "username3" and password "password3" and email "email3@gmail.com" | ||
|
||
Scenario: User successfully updates the adoption User of a Pet | ||
Given I can login with username "username" and password "password" | ||
Given 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" | ||
Given The Pet with chip "1234" is adopted by user "username2" | ||
When Update the adoption with id 1 User with username "username3" | ||
Then The adoption with id 1 should have been updated to username "username3" | ||
And The adoption with id 1 dateofAdoption is updated | ||
|
||
Scenario: User successfully updates the adoption User of a Pet | ||
Given I can login with username "username" and password "password" | ||
Given 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" | ||
Given The Pet with chip "1234" is adopted by user "username2" | ||
When Update the adoption with id 1 User with username "username4" | ||
Then The system should display an error message indicating the username "username4" does not exist |