Skip to content

Commit

Permalink
scenarios adoptions
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardmallol committed Apr 4, 2024
1 parent d01374c commit 0efda3c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 32 deletions.
3 changes: 0 additions & 3 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public abstract class Pet extends UriEntity<Long> {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotBlank
private String name;

private LocalDate dateOfBirth;
Expand All @@ -42,12 +41,10 @@ public abstract class Pet extends UriEntity<Long> {
private boolean dangerous;

@OneToOne
@NotNull
@JsonIdentityReference(alwaysAsId = true)
private Adoptions adoptions;

@ManyToOne
@NotNull
@JsonIdentityReference(alwaysAsId = true)
private Shelter shelter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

@RepositoryRestResource
public interface AdoptionsRepository extends CrudRepository<Adoptions, Long>, PagingAndSortingRepository<Adoptions, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pet, Long>, PagingAndSortingRepository<Pet, Long> {
Pet findPetById(@Param("id") Long id);
}
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);
}

}
34 changes: 16 additions & 18 deletions src/test/resources/features/RegisterAdoptions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0efda3c

Please sign in to comment.