-
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.
- Loading branch information
1 parent
d01374c
commit 0efda3c
Showing
5 changed files
with
115 additions
and
32 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
107 changes: 96 additions & 11 deletions
107
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 |
---|---|---|
@@ -1,34 +1,119 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Adoptions; | ||
import cat.udl.eps.softarch.demo.domain.Cat; | ||
import cat.udl.eps.softarch.demo.domain.Pet; | ||
import cat.udl.eps.softarch.demo.domain.User; | ||
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 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.mockito.internal.matchers.text.ValuePrinter.print; | ||
|
||
public class StepRegisterAdoption { | ||
private Pet pet; | ||
|
||
private User user; | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@Autowired | ||
private AdoptionsRepository adoptionsRepository; | ||
|
||
@Given("The user is logged in with username \"username\" and password \"password\"") | ||
public void userLogged(String username, String password) { | ||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
|
||
|
||
@Given("^The Pet with id (\\d+) does not exists") | ||
public void nonExistingPet (int id_str) { | ||
Long id = (long) id_str; | ||
Pet pet = petRepository.findPetById(id); | ||
assertNull(pet); | ||
} | ||
|
||
@And("The Pet with id \"id\" exists and is not adopted") | ||
public void existingPet (String id, Boolean isAdopted) { | ||
@Given("^The Pet with id (\\d+) is not adopted") | ||
public void isNotAdopted (int id_int) { | ||
Long id = (long) id_int; | ||
Pet pet = petRepository.findPetById(id); | ||
assertTrue(pet != null && !pet.isAdopted()); | ||
} | ||
|
||
@Given("^The Pet with id (\\d+) is adopted by user \"([^\"]*)\"") | ||
public void isAdopted (int id_int, String username) { | ||
Long id = (long) id_int; | ||
Pet pet = petRepository.findPetById(id); | ||
Adoptions adoptions = new Adoptions(); | ||
userRepository.findById(username).ifPresent(user -> adoptions.setUser(user)); | ||
adoptions.setPet(pet); | ||
pet.setAdopted(true); | ||
petRepository.save(pet); | ||
assertTrue(pet.isAdopted()); | ||
} | ||
|
||
@When("The user adopts the Pet with id \"id\"") | ||
public void adopt(User user, Pet pet) { | ||
|
||
@When("^The user with username \"([^\"]*)\" adopts the Pet with id (\\d+)") | ||
public void adopt(String username, int petId) { | ||
Long id = (long) petId; | ||
Pet pet = petRepository.findPetById(id); | ||
|
||
if (pet != null) { | ||
if (!pet.isAdopted()) { | ||
Adoptions adoptions = new Adoptions(); | ||
adoptions.setPet(pet); | ||
userRepository.findById(username).ifPresent(user -> adoptions.setUser(user)); | ||
pet.setAdopted(true); | ||
petRepository.save(pet); | ||
} | ||
} | ||
} | ||
|
||
@Then("The Pet with id \"id\" should be marked as adopted") | ||
public void petAdopted(Pet pet) { | ||
@And("^The Pet with id (\\d+) should be marked as adopted") | ||
public void petAdopted(int id_int) { | ||
Long id = (long) id_int; | ||
Pet pet = petRepository.findPetById(id); | ||
assertTrue(pet.isAdopted()); | ||
} | ||
|
||
@Then("^The system should display an error message indicating the Pet with id (\\d+) is already adopted by another user \"([^\"]*)\"") | ||
public void petAlreadyAdopted(int id_int, String username){ | ||
Long id = (long) id_int; | ||
Pet pet = petRepository.findPetById(id); | ||
Optional<Adoptions> adoptions = adoptionsRepository.findById(id); | ||
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 id (\\d+) does not exist") | ||
public void petDoesNotExist(int id_int){ | ||
Long id = (long) id_int; | ||
Pet pet = petRepository.findPetById(id); | ||
assertThat(pet).isNull(); | ||
} | ||
|
||
@Given("^The Pet with id (\\d+) exists") | ||
public void thePetWithIdExists(int id_str) { | ||
Long id = (long) id_str; | ||
Cat cat = new Cat(); | ||
cat.setAdopted(false); | ||
petRepository.save(cat); | ||
Pet pet = petRepository.findPetById(id); | ||
assertNotNull(pet); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -4,27 +4,25 @@ Feature: Register an Adoption | |
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" | ||
And There is a registered Pet with id "id" and isAdopted "isAdopted" | ||
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 "[email protected]" | ||
|
||
Scenario: User successfully adopts a Pet | ||
Given The user is logged in with username "username" and password "password" | ||
And The Pet with id "id" exists and is not adopted | ||
When The user adopts the Pet with id "id" | ||
Then The Pet with id "id" should be marked as adopted | ||
And I can login with username "username" and password "password" | ||
Given The Pet with id 1 exists | ||
Given The Pet with id 1 is not adopted | ||
When The user with username "username" adopts the Pet with id 1 | ||
Then The Pet with id 1 should be marked as adopted | ||
|
||
Scenario: User tries to adopt a Pet that is already adopted | ||
Given The user is logged in with username "username" and password "password" | ||
And The Pet with id "id" exists and is adopted | ||
When The user tries to adopt the Pet with id "id" | ||
Then The system should display an error message indicating the Pet is already adopted | ||
|
||
Scenario: User tries to adopt a Pet without logging in | ||
Given The user is not logged in | ||
When The user tries to adopt the Pet with id "id" | ||
Then The system should prompt the user to log in | ||
And I can login with username "username" and password "password" | ||
And The Pet with id 1 exists | ||
And The Pet with id 1 is adopted by user "username2" | ||
When The user with username "username" adopts the Pet with id 1 | ||
Then The system should display an error message indicating the Pet with id 1 is already adopted by another user "username2" | ||
|
||
Scenario: User tries to adopt a Pet that does not exist | ||
Given The user is logged in with username "username" and password "password" | ||
When The user tries to adopt a non-existing Pet with id "id" | ||
Then The system should display an error message indicating the Pet does not exist | ||
And I can login with username "username" and password "password" | ||
And The Pet with id 1 does not exists | ||
When The user with username "username" adopts the Pet with id 1 | ||
Then The system should display an error message indicating the Pet with id 1 does not exist |