From 836d02af3e6f58b87d1e0831376ae04fdb42c7fe Mon Sep 17 00:00:00 2001 From: marioferro Date: Fri, 5 Apr 2024 21:23:11 +0200 Subject: [PATCH] Working Test! --- .../demo/exceptions/InvalidPostRequest.java | 7 +++++ .../demo/handler/ProcessAdoptionHandler.java | 12 ++++++-- .../demo/steps/ProcessAdoptionStepDefs.java | 30 +++++++++---------- .../features/ProcessAdoption.feature | 2 +- 4 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/exceptions/InvalidPostRequest.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/exceptions/InvalidPostRequest.java b/src/main/java/cat/udl/eps/softarch/demo/exceptions/InvalidPostRequest.java new file mode 100644 index 00000000..87820593 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/exceptions/InvalidPostRequest.java @@ -0,0 +1,7 @@ +package cat.udl.eps.softarch.demo.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.CONFLICT, reason = "Bad post request") +public class InvalidPostRequest extends RuntimeException {} \ No newline at end of file diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionHandler.java index 5fd1a20d..5388ba73 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ProcessAdoptionHandler.java @@ -2,6 +2,7 @@ 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.security.core.Authentication; @@ -13,6 +14,8 @@ import java.util.Arrays; import java.util.List; + + public class ProcessAdoptionHandler { private static final Logger logger = LoggerFactory.getLogger(ProcessAdoptionHandler.class); @@ -25,15 +28,20 @@ public class ProcessAdoptionHandler { @HandleBeforeCreate public void handleAdoptionBeforeCreate(Adoption adoption) throws UnauthorizedAccessException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - User user; - if(!isAuthorized(authentication)) { + + 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()); + throw new InvalidPostRequest(); + } + 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 c05f6c60..32e5d2dc 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 @@ -4,6 +4,8 @@ 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; import io.cucumber.java.en.And; @@ -36,7 +38,8 @@ public class ProcessAdoptionStepDefs { @Autowired PetRepository petRepository; - Adopt + @Autowired + AdoptionRepository adoptionRepository; protected ResultActions result; @@ -79,13 +82,11 @@ public void iRequestToAdoptThePetWithName(String arg0) throws Throwable { stepDefs.result = stepDefs.mockMvc.perform( post("/adoptions") .contentType(MediaType.APPLICATION_JSON) - .content(stepDefs.mapper.writeValueAsString(adoption)) + .content(stepDefs.mapper.writeValueAsString(adoptionRepository.findAll().iterator().next())) .characterEncoding(StandardCharsets.UTF_8) .with(AuthenticationStepDefs.authenticate())) .andDo(print()); - - } @@ -93,20 +94,17 @@ 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) { - Pet pet = new Pet(); - pet.setName("pet"); - 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); + petRepository.findByName(arg0).get(0).setAdopted(true); Adoption adoption = new Adoption(); adoption.setPet(petRepository.findAll().iterator().next()); - adoptionR + adoption.setUser(userRepository.findAll().iterator().next()); + adoption.setStartDate(ZonedDateTime.now()); + adoption.setConfirmed(true); + adoption.setType("Adoption"); + adoption.setEndDate(ZonedDateTime.now()); + + adoptionRepository.save(adoption); } @@ -123,6 +121,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") @@ -131,5 +130,6 @@ public void iRequestToAdoptWithoutAPet() throws Throwable{ .characterEncoding(StandardCharsets.UTF_8) .with(AuthenticationStepDefs.authenticate())) .andDo(print()); + } } diff --git a/src/test/resources/features/ProcessAdoption.feature b/src/test/resources/features/ProcessAdoption.feature index 099c86ec..e637a548 100644 --- a/src/test/resources/features/ProcessAdoption.feature +++ b/src/test/resources/features/ProcessAdoption.feature @@ -15,7 +15,7 @@ Feature: Process Adoption (User) Scenario: Process adoption request for available pet Given I login as "username" with password "password" When I request to adopt the pet with name "pet" - Then The response code is 200 + Then The response code is 201 Scenario: Process adoption request without pet