Skip to content

Commit

Permalink
Working Test!
Browse files Browse the repository at this point in the history
  • Loading branch information
marioferro2002 committed Apr 5, 2024
1 parent ea1055e commit 836d02a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,8 @@
import java.util.Arrays;
import java.util.List;



public class ProcessAdoptionHandler {

private static final Logger logger = LoggerFactory.getLogger(ProcessAdoptionHandler.class);
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +38,8 @@ public class ProcessAdoptionStepDefs {
@Autowired
PetRepository petRepository;

Adopt
@Autowired
AdoptionRepository adoptionRepository;

protected ResultActions result;

Expand Down Expand Up @@ -79,34 +82,29 @@ 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());



}



@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);


}
Expand All @@ -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")
Expand All @@ -131,5 +130,6 @@ public void iRequestToAdoptWithoutAPet() throws Throwable{
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());

}
}
2 changes: 1 addition & 1 deletion src/test/resources/features/ProcessAdoption.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 836d02a

Please sign in to comment.