diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Adoptions.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Adoption.java similarity index 87% rename from src/main/java/cat/udl/eps/softarch/demo/domain/Adoptions.java rename to src/main/java/cat/udl/eps/softarch/demo/domain/Adoption.java index f397141..18a54d3 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Adoptions.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Adoption.java @@ -1,6 +1,5 @@ package cat.udl.eps.softarch.demo.domain; import jakarta.persistence.*; -import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Data; import lombok.EqualsAndHashCode; @@ -16,7 +15,7 @@ @Data @EqualsAndHashCode (callSuper = false) -public class Adoptions { +public class Adoption { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -33,7 +32,7 @@ public class Adoptions { @NotNull private LocalDateTime dateOfAdoption; - public Adoptions() { + public Adoption() { this.dateOfAdoption = LocalDateTime.now(ZoneOffset.UTC); } diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java index bd7825c..dfe86ae 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.annotation.JsonIdentityReference; import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; @@ -45,7 +44,7 @@ public class Pet extends UriEntity { @OneToOne @JsonIdentityReference(alwaysAsId = true) - private Adoptions adoptions; + private Adoption adoption; @ManyToOne @JsonIdentityReference(alwaysAsId = true) diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionsRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionRepository.java similarity index 61% rename from src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionsRepository.java rename to src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionRepository.java index 70ec062..1917a52 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionsRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/AdoptionRepository.java @@ -1,11 +1,11 @@ package cat.udl.eps.softarch.demo.repository; -import cat.udl.eps.softarch.demo.domain.Adoptions; +import cat.udl.eps.softarch.demo.domain.Adoption; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource -public interface AdoptionsRepository extends CrudRepository, PagingAndSortingRepository { +public interface AdoptionRepository extends CrudRepository, PagingAndSortingRepository { } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java b/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java index 1c58d44..0a8d7e1 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/StepRegisterAdoption.java @@ -1,6 +1,6 @@ package cat.udl.eps.softarch.demo.steps; import cat.udl.eps.softarch.demo.domain.*; -import cat.udl.eps.softarch.demo.repository.AdoptionsRepository; +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; @@ -28,7 +28,7 @@ public class StepRegisterAdoption { private PetRepository petRepository; @Autowired - private AdoptionsRepository adoptionsRepository; + private AdoptionRepository adoptionRepository; @Autowired private UserRepository userRepository; @@ -48,9 +48,9 @@ public void isNotAdopted (String chip) { @Given("^The Pet with chip \"([^\"]*)\" is adopted by user \"([^\"]*)\"") public void isAdopted (String chip, String username) { Pet pet = petRepository.findByChip(chip); - Adoptions adoptions = new Adoptions(); - userRepository.findById(username).ifPresent(user -> adoptions.setUser(user)); - adoptions.setPet(pet); + Adoption adoption = new Adoption(); + userRepository.findById(username).ifPresent(user -> adoption.setUser(user)); + adoption.setPet(pet); petRepository.save(pet); assertTrue(pet.isAdopted()); } @@ -62,9 +62,9 @@ public void adopt(String username, String chip) { if (pet != null) { if (!pet.isAdopted()) { - Adoptions adoptions = new Adoptions(); - adoptions.setPet(pet); - userRepository.findById(username).ifPresent(adoptions::setUser); + Adoption adoption = new Adoption(); + adoption.setPet(pet); + userRepository.findById(username).ifPresent(adoption::setUser); pet.setAdopted(true); petRepository.save(pet); @@ -72,7 +72,7 @@ public void adopt(String username, String chip) { stepDefs.result = stepDefs.mockMvc.perform( post("/adoptions") .contentType(MediaType.APPLICATION_JSON) - .content(stepDefs.mapper.writeValueAsString(adoptions)) + .content(stepDefs.mapper.writeValueAsString(adoption)) .characterEncoding(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON) .with(AuthenticationStepDefs.authenticate())) @@ -93,7 +93,7 @@ public void petAdopted(String chip) { @Then("^The system should display an error message indicating the Pet with chip \"([^\"]*)\" is already adopted by another user \"([^\"]*)\"") public void petAlreadyAdopted(String chip, String username){ Pet pet = petRepository.findByChip(chip); - Optional adoptions = adoptionsRepository.findById(parseLong("1")); + Optional adoptions = adoptionRepository.findById(parseLong("1")); adoptions.ifPresent(adoption -> { User user = adoption.getUser(); Optional opt = userRepository.findById(username); 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 index 2064c3d..d50a460 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/StepUpdateAdoption.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/StepUpdateAdoption.java @@ -1,12 +1,11 @@ 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.domain.Adoption; +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 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; @@ -29,7 +28,7 @@ public class StepUpdateAdoption { private PetRepository petRepository; @Autowired - private AdoptionsRepository adoptionsRepository; + private AdoptionRepository adoptionRepository; @Autowired private UserRepository userRepository; @@ -37,19 +36,19 @@ public class StepUpdateAdoption { @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); + Optional opt = adoptionRepository.findById(id_adoption); - opt.ifPresent(adoptions -> { + opt.ifPresent(adoption -> { userRepository.findById(final_username).ifPresent(user -> { - adoptions.setUser(user); - adoptions.setDateOfAdoption(LocalDateTime.now()); + adoption.setUser(user); + adoption.setDateOfAdoption(LocalDateTime.now()); }); try { stepDefs.result = stepDefs.mockMvc.perform( - patch("/adoptions/{id}", adoptions.getId()) + patch("/adoptions/{id}", adoption.getId()) .contentType(MediaType.APPLICATION_JSON) - .content(stepDefs.mapper.writeValueAsString(adoptions)) + .content(stepDefs.mapper.writeValueAsString(adoption)) .characterEncoding(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON) .with(AuthenticationStepDefs.authenticate())) @@ -64,10 +63,10 @@ public void updateAdoptionUsername(int adoption_id, String final_username) { @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); + Optional opt = adoptionRepository.findById(id); - opt.ifPresent(adoptions -> { - userRepository.findById(username).ifPresent(user -> assertEquals(user, adoptions.getUser())); + opt.ifPresent(adoption -> { + userRepository.findById(username).ifPresent(user -> assertEquals(user, adoption.getUser())); }); } @@ -75,12 +74,12 @@ public void theAdoptionUserForThePetWithIdShouldBeUpdatedToUsername(int id_int, @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); + Optional opt = adoptionRepository.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()); + opt.ifPresent(adoption -> { + assertEquals(adoption.getDateOfAdoption().getDayOfMonth(), LocalDateTime.now().getDayOfMonth()); + assertEquals(adoption.getDateOfAdoption().getMonth(), LocalDateTime.now().getMonth()); + assertEquals(adoption.getDateOfAdoption().getYear(), LocalDateTime.now().getYear()); }); }