diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java index 97e5bea..3e61324 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java @@ -22,7 +22,6 @@ public abstract class Pet extends UriEntity { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @NotBlank private String name; private LocalDate dateOfBirth; @@ -42,12 +41,10 @@ public abstract class Pet extends UriEntity { private boolean dangerous; @OneToOne - @NotNull @JsonIdentityReference(alwaysAsId = true) private Adoptions adoptions; @ManyToOne - @NotNull @JsonIdentityReference(alwaysAsId = true) private Shelter shelter; diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionsRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionsRepository.java index cdc69fb..70ec062 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionsRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionsRepository.java @@ -7,4 +7,5 @@ @RepositoryRestResource public interface AdoptionsRepository extends CrudRepository, PagingAndSortingRepository { + } diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java index 0e4cbc5..5829505 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java @@ -3,8 +3,10 @@ import cat.udl.eps.softarch.demo.domain.Pet; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource public interface PetRepository extends CrudRepository, PagingAndSortingRepository { + Pet findPetById(@Param("id") Long id); } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java b/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java index 996e6cf..c4bd530 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java @@ -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 = adoptionsRepository.findById(id); + adoptions.ifPresent(adoption -> { + User user = adoption.getUser(); + Optional 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); } + } diff --git a/src/test/resources/features/RegisterAdoptions.feature b/src/test/resources/features/RegisterAdoptions.feature index 111d2b9..a8aadb9 100644 --- a/src/test/resources/features/RegisterAdoptions.feature +++ b/src/test/resources/features/RegisterAdoptions.feature @@ -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 "email2@gmail.com" 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