Skip to content

Commit

Permalink
Merge pull request #91 from UdL-EPS-SoftArch/Feature-Process-adoption
Browse files Browse the repository at this point in the history
Feature process adoption
  • Loading branch information
rogargon authored Apr 9, 2024
2 parents abbd973 + 829bd53 commit 32c33d3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cat.udl.eps.softarch.demo.handler;

import cat.udl.eps.softarch.demo.domain.Adoption;
import cat.udl.eps.softarch.demo.exceptions.InvalidPostRequest;
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 {

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, 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();
}
// 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

logger.info("Adoption for pet {} created successfully by user {}", adoption.getPet().getName(), authentication.getName());
}


// This function allows to check if the user is authorized to perform the action
private boolean isAuthorized(Authentication authentication) {
if (authentication == null || !authentication.isAuthenticated()) {
return false;
}

List<String> requiredAuthorities = Arrays.asList(ROLE_USER, ROLE_SHELTER_VOLUNTEER, ROLE_ADMIN);

return authentication.getAuthorities().stream()
.anyMatch(grantedAuthority -> requiredAuthorities.contains(grantedAuthority.getAuthority()));
}



}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cat.udl.eps.softarch.demo.steps;


import cat.udl.eps.softarch.demo.domain.Adoption;
import cat.udl.eps.softarch.demo.domain.Pet;
import cat.udl.eps.softarch.demo.repository.AdoptionRepository;
Expand All @@ -25,7 +24,6 @@
@SuppressWarnings("ALL")
public class ProcessAdoptionStepDefs {


@Autowired
StepDefs stepDefs;

Expand All @@ -40,12 +38,10 @@ 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())
.andExpect(jsonPath("$.message", is("Adoption successful")));

}

@Given("There is an available pet with name {string} i want to adopt")
Expand All @@ -61,12 +57,10 @@ public void thereIsAnAvailablePetWithName(String arg0) {
pet.setDescription("description");
pet.setBreed("breed");
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());
Expand All @@ -82,11 +76,8 @@ public void iRequestToAdoptThePetWithName(String arg0) throws Throwable {
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());

}



@When("I request to adopt without a pet")
public void iRequestToAdoptWithoutAPet() throws Throwable{
// Proceed with adoption logic
Expand All @@ -97,20 +88,17 @@ public void iRequestToAdoptWithoutAPet() throws Throwable{
adoption.setType("Adoption");
adoption.setEndDate(null);


stepDefs.result = stepDefs.mockMvc.perform(
post("/adoptions")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(adoption))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.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);
Expand All @@ -121,7 +109,5 @@ public void thePetWithNameIsAlreadyAdopted(String arg0) {
pet.setDescription("description");
pet.setBreed("breed");
petRepository.save(pet);


}
}

0 comments on commit 32c33d3

Please sign in to comment.