Skip to content

Commit

Permalink
arreglar adoptions scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardmallol committed Apr 16, 2024
2 parents 03b945c + 3882a33 commit 6a263ed
Show file tree
Hide file tree
Showing 41 changed files with 1,543 additions and 90 deletions.
4 changes: 1 addition & 3 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@EqualsAndHashCode(callSuper = false)
public class Location {
@OneToOne
@NotNull
/*@NotNull*/
@JsonIdentityReference(alwaysAsId = true)
private Shelter shelter;

Expand All @@ -24,10 +24,8 @@ public class Location {
@NotBlank
private String address;

@NotNull
private Float latitude;

@NotNull
private Float longitude;

@NotBlank
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/cat/udl/eps/softarch/demo/domain/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.util.Set;

@Entity
@Setter
@Getter
@Data
@EqualsAndHashCode(callSuper = false)
public class Permission {

@Id
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
@NotBlank
private String name;

@NotNull
@ManyToMany
private Set<Role> role;

}
11 changes: 7 additions & 4 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public abstract class Pet extends UriEntity<Long> {
public class Pet extends UriEntity<Long> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(unique=true)
@NotBlank
private String chip;

@NotBlank
private String name;

private LocalDate dateOfBirth;
Expand All @@ -32,13 +37,11 @@ public abstract class Pet extends UriEntity<Long> {

private Integer size;

private String chip;

private String sex;

private String race;

private boolean dangerous;
private boolean isDangerous;

@OneToOne
@JsonIdentityReference(alwaysAsId = true)
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.EqualsAndHashCode;

import java.sql.Time;
import java.time.ZonedDateTime;

@Entity
@Data
Expand All @@ -16,16 +17,16 @@ public class Schedule {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int Id;
private long Id;

@NotNull
private Time startTime;
private ZonedDateTime startTime;

@NotNull
private Time endTime;
private ZonedDateTime endTime;

@ManyToOne
@NotNull
//@NotNull
@JsonIdentityReference(alwaysAsId = true)
private Shelter shelter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
@EqualsAndHashCode(callSuper = false)
public class ShelterCertificate {
@OneToOne
@NotNull
@JsonIdentityReference(alwaysAsId = true)
private Shelter shelter;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
private LocalDateTime expirationDate;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import cat.udl.eps.softarch.demo.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;
import java.util.Optional;

@RepositoryRestResource
public interface LocationRepository extends CrudRepository<Location, Long>, PagingAndSortingRepository<Location, Long> {
Location findLocationByAddressAndProvinceAndCityAndPostalCode(@Param("address") String address, @Param("province") String province, @Param("city") String city, @Param("postalCode") Integer postalCode);


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cat.udl.eps.softarch.demo.repository;

import cat.udl.eps.softarch.demo.domain.Permission;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface PermissionRepository extends CrudRepository<Permission, String>, PagingAndSortingRepository<Permission, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

@RepositoryRestResource
public interface PetRepository extends CrudRepository<Pet, Long>, PagingAndSortingRepository<Pet, Long> {
Pet findPetById(@Param("id") Long id);
Pet findByName(@Param("name") String name);

Pet findByChip(@Param("chip") String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

@RepositoryRestResource
public interface RoleRepository extends CrudRepository<Role, Long>, PagingAndSortingRepository<Role, Long> {
Role findByName(String oldName);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package cat.udl.eps.softarch.demo.repository;

import cat.udl.eps.softarch.demo.domain.Schedule;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.time.ZonedDateTime;

@RepositoryRestResource
public interface ScheduleRepository extends CrudRepository<Schedule, Long>, PagingAndSortingRepository<Schedule, Long> {


Schedule findScheduleByStartTimeAndEndTime(@NotNull ZonedDateTime startTime, @NotNull ZonedDateTime endTime);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Location;
import cat.udl.eps.softarch.demo.repository.LocationRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;


public class CreateLocationStepsDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private LocationRepository locationRepository;

public static String newResourceUri;

@When("There is already a Location with the following details:$")
public void thereIsAlreadyALocationWithTheFollowingDetails(Map<String, String> locationDetails) throws Throwable{
Location location = new Location();
location.setAddress(locationDetails.get("address"));
location.setProvince(locationDetails.get("province"));
location.setCity(locationDetails.get("city"));
location.setPostalCode(Integer.parseInt(locationDetails.get("postalCode")));
locationRepository.save(location);
}
@When("I create a new Location with the following details:$")
public void iCreateANewLocationWithTheFollowingDetails(Map<String, String> locationDetails) throws Throwable {
Location existingLocation = locationRepository.findLocationByAddressAndProvinceAndCityAndPostalCode(
locationDetails.get("address"),
locationDetails.get("province"),
locationDetails.get("city"),
Integer.parseInt(locationDetails.get("postalCode"))
);

if (existingLocation == null) {

Location location = new Location();
location.setAddress(locationDetails.get("address"));
location.setProvince(locationDetails.get("province"));
location.setCity(locationDetails.get("city"));
location.setPostalCode(Integer.parseInt(locationDetails.get("postalCode")));

// Verify and assign latitude and longitude if exists
if (locationDetails.containsKey("longitude") && locationDetails.get("longitude") != null) {
location.setLongitude(Float.parseFloat(locationDetails.get("longitude")));
}


if (locationDetails.containsKey("latitude") && locationDetails.get("latitude") != null) {
location.setLatitude(Float.parseFloat(locationDetails.get("latitude")));
}

stepDefs.result = stepDefs.mockMvc.perform(
post("/locations")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(location))
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
newResourceUri = stepDefs.result.andReturn().getResponse().getHeader("Location");

} else {
assertThat(existingLocation).isNotNull();
}
}



@And("There is (\\d+) Location created$")
public void thereIsLocationCreated(int locationCreatedNum) {
Assert.assertEquals(locationCreatedNum, locationRepository.count());
}

@And("There is only (\\d+) Location with the details:$")
public void thereIsOnlyLocationWithTheDeatils(int locationCreatedNum, Map<String, String> locationDetails) {
Assert.assertEquals(locationCreatedNum, locationRepository.count());
}


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


import cat.udl.eps.softarch.demo.domain.Location;
import cat.udl.eps.softarch.demo.repository.LocationRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;


public class DeleteLocationStepsDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private LocationRepository locationRepository;

public static String newResourceUri;
@When("I delete the location with the details:$")
public void iDeleteTheLocationWithTheDetails(Map<String, String> locationDetails) throws Throwable{
Location existingLocation = locationRepository.findLocationByAddressAndProvinceAndCityAndPostalCode(
locationDetails.get("address"),
locationDetails.get("province"),
locationDetails.get("city"),
Integer.parseInt(locationDetails.get("postalCode"))
);

stepDefs.result = stepDefs.mockMvc.perform(
delete("/locations/{id}", (existingLocation != null) ? existingLocation.getId() : "999")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(existingLocation))
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}

@And("There is (\\d+) Location with the details:$")
public void thereIsOnlyLocationWithTheDeatils(int locationNum, Map<String, String> locationDetails) {
Assert.assertEquals(locationNum, locationRepository.count());
}


}
Loading

0 comments on commit 6a263ed

Please sign in to comment.