diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionEventHandler.java similarity index 71% rename from src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionHandler.java rename to src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionEventHandler.java index 5388ba73..1aa80063 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionEventHandler.java @@ -1,47 +1,50 @@ package cat.udl.eps.softarch.demo.handler; import cat.udl.eps.softarch.demo.domain.Adoption; -import cat.udl.eps.softarch.demo.domain.User; import cat.udl.eps.softarch.demo.exceptions.InvalidPostRequest; -import org.springframework.data.rest.core.annotation.HandleAfterSave; import org.springframework.data.rest.core.annotation.HandleBeforeCreate; +import org.springframework.data.rest.core.annotation.RepositoryEventHandler; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import cat.udl.eps.softarch.demo.exceptions.UnauthorizedAccessException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; +@Component +@RepositoryEventHandler() +public class ProcessAdoptionEventHandler { -public class ProcessAdoptionHandler { - - private static final Logger logger = LoggerFactory.getLogger(ProcessAdoptionHandler.class); - + private static final Logger logger = LoggerFactory.getLogger(ProcessAdoptionEventHandler.class); + // These are the roles that are allowed to create an adoption private static final String ROLE_USER = "ROLE_USER"; private static final String ROLE_SHELTER_VOLUNTEER = "ROLE_SHELTER_VOLUNTEER"; private static final String ROLE_ADMIN = "ROLE_ADMIN"; + // This function is called before creating an adoption @HandleBeforeCreate - public void handleAdoptionBeforeCreate(Adoption adoption) throws UnauthorizedAccessException { + public void handleAdoptionBeforeCreate(Adoption adoption) throws UnauthorizedAccessException, InvalidPostRequest { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - + // If the user is not authorized, an exception is thrown if(!isAuthorized(authentication) ) { String userName = authentication != null ? authentication.getName() : "anonymous"; String errorMessage = String.format("Unauthorized attempt to create an adoption by user: %s", userName); logger.error(errorMessage); throw new UnauthorizedAccessException(); } - else if (adoption.getPet().isAdopted() || adoption.getConfirmed()) { - logger.error("Pet {} is already or bad request", adoption.getPet().getName()); + // If the pet is already adopted or the pet is null, an exception is thrown + else if (adoption.getPet() == null || adoption.getPet().isAdopted() || adoption.getConfirmed()) { + logger.error("Pet is already adopted or bad request"); throw new InvalidPostRequest(); } + // If the adoption is successful, the adoption is in process - adoption.getPet().setAdopted(true); logger.info("Adoption for pet {} created successfully by user {}", adoption.getPet().getName(), authentication.getName()); } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/ProcessAdoptionStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/ProcessAdoptionStepDefs.java index 32e5d2dc..bc657ad4 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/ProcessAdoptionStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/ProcessAdoptionStepDefs.java @@ -3,8 +3,6 @@ import cat.udl.eps.softarch.demo.domain.Adoption; import cat.udl.eps.softarch.demo.domain.Pet; -import cat.udl.eps.softarch.demo.domain.User; -import cat.udl.eps.softarch.demo.exceptions.InvalidPostRequest; import cat.udl.eps.softarch.demo.repository.AdoptionRepository; import cat.udl.eps.softarch.demo.repository.PetRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; @@ -17,7 +15,6 @@ import java.nio.charset.StandardCharsets; import java.time.ZonedDateTime; -import java.util.List; import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -44,7 +41,6 @@ public class ProcessAdoptionStepDefs { protected ResultActions result; - @And("I receive a confirmation message for adopting the pet") public void iReceiveAConfirmationMessageForAdoptingThePet() throws Throwable { result.andExpect(status().isOk()) @@ -52,7 +48,7 @@ public void iReceiveAConfirmationMessageForAdoptingThePet() throws Throwable { } - @Given("There is an available pet with name {string}") + @Given("There is an available pet with name {string} i want to adopt") public void thereIsAnAvailablePetWithName(String arg0) { Pet pet = new Pet(); @@ -67,10 +63,13 @@ public void thereIsAnAvailablePetWithName(String arg0) { petRepository.save(pet); + + } @When("I request to adopt the pet with name {string}") public void iRequestToAdoptThePetWithName(String arg0) throws Throwable { + Adoption adoption = new Adoption(); adoption.setPet(petRepository.findByName(arg0).get(0)); adoption.setUser(userRepository.findAll().iterator().next()); @@ -82,7 +81,7 @@ public void iRequestToAdoptThePetWithName(String arg0) throws Throwable { stepDefs.result = stepDefs.mockMvc.perform( post("/adoptions") .contentType(MediaType.APPLICATION_JSON) - .content(stepDefs.mapper.writeValueAsString(adoptionRepository.findAll().iterator().next())) + .content(stepDefs.mapper.writeValueAsString(adoption)) .characterEncoding(StandardCharsets.UTF_8) .with(AuthenticationStepDefs.authenticate())) .andDo(print()); @@ -91,27 +90,6 @@ public void iRequestToAdoptThePetWithName(String arg0) throws Throwable { - @Given("There is a pet with name {string} and it is already adopted") - public void thereIsAPetWithNameAndItIsAlreadyAdopted(String arg0) { - - petRepository.findByName(arg0).get(0).setAdopted(true); - - Adoption adoption = new Adoption(); - adoption.setPet(petRepository.findAll().iterator().next()); - adoption.setUser(userRepository.findAll().iterator().next()); - adoption.setStartDate(ZonedDateTime.now()); - adoption.setConfirmed(true); - adoption.setType("Adoption"); - adoption.setEndDate(ZonedDateTime.now()); - - adoptionRepository.save(adoption); - - - } - - - - @When("I request to adopt without a pet") public void iRequestToAdoptWithoutAPet() throws Throwable{ // Proceed with adoption logic @@ -121,7 +99,7 @@ public void iRequestToAdoptWithoutAPet() throws Throwable{ adoption.setConfirmed(false); adoption.setType("Adoption"); adoption.setEndDate(null); - adoption.setPet(null); + stepDefs.result = stepDefs.mockMvc.perform( post("/adoptions") @@ -132,4 +110,21 @@ public void iRequestToAdoptWithoutAPet() throws Throwable{ .andDo(print()); } + + @And("The pet with name {string} is already adopted") + public void thePetWithNameIsAlreadyAdopted(String arg0) { + + Pet pet = new Pet(); + pet.setName(arg0); + pet.setAdopted(true); + pet.setColor("color"); + pet.setSize("size"); + pet.setWeight(1.0); + pet.setAge("age"); + pet.setDescription("description"); + pet.setBreed("breed"); + petRepository.save(pet); + + + } } diff --git a/src/test/resources/features/ProcessAdoption.feature b/src/test/resources/features/ProcessAdoption.feature index e637a548..13d578d3 100644 --- a/src/test/resources/features/ProcessAdoption.feature +++ b/src/test/resources/features/ProcessAdoption.feature @@ -5,15 +5,17 @@ Feature: Process Adoption (User) Background: Given There is a registered user with username "username" and password "password" and email "user@sample.app" - Given There is an available pet with name "pet" + Scenario: User is not logged in Given I'm not logged in + And There is an available pet with name "pet" i want to adopt When I request to adopt the pet with name "pet" Then The response code is 401 Scenario: Process adoption request for available pet Given I login as "username" with password "password" + And There is an available pet with name "pet" i want to adopt When I request to adopt the pet with name "pet" Then The response code is 201 @@ -21,12 +23,12 @@ Feature: Process Adoption (User) Scenario: Process adoption request without pet Given I login as "username" with password "password" When I request to adopt without a pet - Then The response code is 404 + Then The response code is 409 Scenario: Process adoption request for already adopted pet Given I login as "username" with password "password" - And There is a pet with name "pet" and it is already adopted + And The pet with name "pet" is already adopted When I request to adopt the pet with name "pet" Then The response code is 409