Skip to content

Commit

Permalink
update adoptions
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardmallol committed Apr 4, 2024
1 parent 0efda3c commit 03b945c
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
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();
}
}
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 protected]"
And There is a registered user with username "username2" and password "password2" and email "[email protected]"
And There is a registered user with username "username3" and password "password3" and email "[email protected]"

Scenario: User successfully updates the adoption User of a Pet
And I can login with username "username" and password "password"
Given The Pet with id 1 exists
Given The Pet with id 1 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
And I can login with username "username" and password "password"
Given The Pet with id 1 exists
Given The Pet with id 1 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 03b945c

Please sign in to comment.