Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adoptions to adoption -> solve a problem #36

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +15,7 @@
@Data
@EqualsAndHashCode (callSuper = false)

public class Adoptions {
public class Adoption {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand All @@ -33,7 +32,7 @@ public class Adoptions {
@NotNull
private LocalDateTime dateOfAdoption;

public Adoptions() {
public Adoption() {
this.dateOfAdoption = LocalDateTime.now(ZoneOffset.UTC);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,7 +44,7 @@ public class Pet extends UriEntity<Long> {

@OneToOne
@JsonIdentityReference(alwaysAsId = true)
private Adoptions adoptions;
private Adoption adoption;

@ManyToOne
@JsonIdentityReference(alwaysAsId = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Adoptions, Long>, PagingAndSortingRepository<Adoptions, Long> {
public interface AdoptionRepository extends CrudRepository<Adoption, Long>, PagingAndSortingRepository<Adoption, Long> {

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,7 +28,7 @@ public class StepRegisterAdoption {
private PetRepository petRepository;

@Autowired
private AdoptionsRepository adoptionsRepository;
private AdoptionRepository adoptionRepository;

@Autowired
private UserRepository userRepository;
Expand All @@ -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());
}
Expand All @@ -62,17 +62,17 @@ 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);

try {
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()))
Expand All @@ -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> adoptions = adoptionsRepository.findById(parseLong("1"));
Optional<Adoption> adoptions = adoptionRepository.findById(parseLong("1"));
adoptions.ifPresent(adoption -> {
User user = adoption.getUser();
Optional<User> opt = userRepository.findById(username);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -29,27 +28,27 @@ public class StepUpdateAdoption {
private PetRepository petRepository;

@Autowired
private AdoptionsRepository adoptionsRepository;
private AdoptionRepository adoptionRepository;

@Autowired
private UserRepository userRepository;

@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<Adoptions> opt = adoptionsRepository.findById(id_adoption);
Optional<Adoption> 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()))
Expand All @@ -64,23 +63,23 @@ 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<Adoptions> opt = adoptionsRepository.findById(id);
Optional<Adoption> 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()));
});
}


@And("^The adoption with id (\\d+) dateofAdoption is updated")
public void theAdoptionDateofAdoptionIsUpdated(int adoption_id) {
Long id = (long) adoption_id;
Optional<Adoptions> opt = adoptionsRepository.findById(id);
Optional<Adoption> 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());
});
}

Expand Down
Loading