diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/StepUpdateAdoption.java b/src/test/java/cat/udl/eps/softarch/demo/steps/StepUpdateAdoption.java new file mode 100644 index 0000000..2064c3d --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/StepUpdateAdoption.java @@ -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 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 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 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(); + } +} diff --git a/src/test/resources/features/UpdateAdoptions.feature b/src/test/resources/features/UpdateAdoptions.feature new file mode 100644 index 0000000..ed61430 --- /dev/null +++ b/src/test/resources/features/UpdateAdoptions.feature @@ -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" + And There is a registered user with username "username2" and password "password2" and email "email2@gmail.com" + And 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 + 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 \ No newline at end of file