Skip to content

Commit

Permalink
Merge pull request #23 from UdL-EPS-SoftArch/adoptions-scenarios
Browse files Browse the repository at this point in the history
Register adoption
  • Loading branch information
rogargon authored Apr 16, 2024
2 parents 3882a33 + 6e3d2bc commit dd799af
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class Pet extends UriEntity<Long> {
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 @@ -9,8 +9,8 @@
@RepositoryRestResource
public interface PetRepository extends CrudRepository<Pet, Long>, PagingAndSortingRepository<Pet, Long> {

Pet findPetById(@Param("id") Long id);
Pet findByName(@Param("name") String name);

Pet findByChip(@Param("chip") String name);

}
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();
}
}
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();
}
}
28 changes: 28 additions & 0 deletions src/test/resources/features/RegisterAdoptions.feature
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
24 changes: 24 additions & 0 deletions src/test/resources/features/UpdateAdoptions.feature
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

0 comments on commit dd799af

Please sign in to comment.