Skip to content

Commit

Permalink
Merge branch 'main' into 77-fix-update-room-test
Browse files Browse the repository at this point in the history
  • Loading branch information
samplecode17 authored Nov 7, 2024
2 parents 3c8ec51 + fe2d483 commit 4fc43f9
Show file tree
Hide file tree
Showing 23 changed files with 916 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package cat.udl.eps.softarch.demo.config;
import cat.udl.eps.softarch.demo.domain.Owner;
import cat.udl.eps.softarch.demo.domain.Advertisement;
import cat.udl.eps.softarch.demo.domain.User;
import cat.udl.eps.softarch.demo.repository.OwnerRepository;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import cat.udl.eps.softarch.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import jakarta.annotation.PostConstruct;

import java.math.BigDecimal;
import java.util.Arrays;
import cat.udl.eps.softarch.demo.domain.Advertisement;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import java.util.Set;

@Configuration
Expand All @@ -17,10 +23,12 @@ public class DBInitialization {
@Value("${spring.profiles.active:}")
private String activeProfiles;
private final UserRepository userRepository;
private final AdvertisementRepository advertisementRepository;

public DBInitialization(UserRepository userRepository, OwnerRepository ownerRepository) {
public DBInitialization(UserRepository userRepository, OwnerRepository ownerRepository, AdvertisementRepository advertisementRepository) {
this.userRepository = userRepository;
this.ownerRepository = ownerRepository;
this.advertisementRepository = advertisementRepository;
}

@PostConstruct
Expand All @@ -45,6 +53,7 @@ public void initializeDatabase() {
owner.encodePassword();
ownerRepository.save(owner);
}

if (!ownerRepository.existsById("owner1")) {
Owner owner = new Owner();
owner.setEmail("[email protected]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.PATCH, "/rooms/*").hasAnyRole("OWNER")
.requestMatchers(HttpMethod.POST, "/properties").hasAuthority("ROLE_OWNER")
.requestMatchers(HttpMethod.PUT, "/properties/*").hasAuthority("ROLE_OWNER")
.requestMatchers(HttpMethod.DELETE, "/properties/*").hasAuthority("ROLE_OWNER")
.requestMatchers(HttpMethod.POST, "/visits").authenticated()
.requestMatchers(HttpMethod.DELETE, "/visits/*").authenticated()
.requestMatchers(HttpMethod.POST, "/*/*").authenticated()
.requestMatchers(HttpMethod.PUT, "/*/*").authenticated()
.requestMatchers(HttpMethod.PATCH, "/*/*").authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class Advertisement extends UriEntity<Long> {
@JsonIdentityReference(alwaysAsId = true)
public AdvertisementStatus adStatus;

@NotNull
@ManyToOne
@JsonIdentityReference(alwaysAsId = true)
public Apartment apartment;


public Advertisement() {
this.creationDate = ZonedDateTime.now();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cat/udl/eps/softarch/demo/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
@NoArgsConstructor
public class Room extends UriEntity<Long> {
@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private int surface;

@ColumnDefault("false")
Expand Down
29 changes: 20 additions & 9 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Visit.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package cat.udl.eps.softarch.demo.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.*;

import java.time.ZonedDateTime;


@EqualsAndHashCode(callSuper = true)
@Entity
@Entity(name = "visit")
@Data
public class Visit extends UriEntity<Long>{
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Visit extends UriEntity<Long> {

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

@NotNull
@Column(name = "visit_date")
private ZonedDateTime when;

}

@ManyToOne
@NotNull
@JsonIdentityReference(alwaysAsId = true)
private Advertisement advertisement;

public void setVisitDateTime(ZonedDateTime parse) {
this.when = parse;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cat.udl.eps.softarch.demo.repository;


import cat.udl.eps.softarch.demo.domain.Advertisement;
import cat.udl.eps.softarch.demo.domain.Visit;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
Expand All @@ -14,4 +15,5 @@ public interface VisitRepository extends CrudRepository<Visit, Long>, PagingAndS

Optional<Visit> findById(@Param("id") Long id);
List<Visit> findByWhen(@Param("when") ZonedDateTime when);
List<Visit> findByAdvertisement(@Param("advertisement") Advertisement advertisement);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

import cat.udl.eps.softarch.demo.repository.*;
import org.antlr.v4.runtime.misc.LogManager;
import org.springframework.http.MediaType;
import cat.udl.eps.softarch.demo.domain.*;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import cat.udl.eps.softarch.demo.repository.AdvertisementStatusRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
Expand All @@ -19,14 +19,21 @@
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AdvertisementStepDefs {

@Autowired
private OwnerRepository ownerRepository;
@Autowired
private RoomRepository roomRepository;
@Autowired
private AdvertisementRepository advertisementRepository;
@Autowired
private ApartmentRepository apartmentRepository;
@Autowired
private AdvertisementStatusRepository advertisementStatusRepository;
private AdvertisementStatus status;
private ResponseEntity<String> response;
Expand All @@ -35,46 +42,62 @@ public class AdvertisementStepDefs {

@Given("There is an existing apartment with id {string} named {string}")
public void thereIsAnExistingApartmentWithIdNamed(String id, String name) {
// Crea un objeto Owner (propietario)
Owner owner = new Owner();
owner.setName("John Doe");
owner.setPhoneNumber("123456789");
owner.setAddress("456 Another St");
Optional<Owner> ownerOptional = ownerRepository.findById("owner");
Owner owner;
if (ownerOptional.isPresent()) {
owner = ownerOptional.get();
} else {
owner = new Owner();
owner.setEmail("[email protected]");
owner.setId("owner");
owner.setName("Ramon");
owner.setPhoneNumber("639826878");
owner.setPassword("password");
owner.encodePassword();
ownerRepository.save(owner);
}


Apartment apartment = new Apartment();
apartment.setName(name);
apartment.setFloor(5);
apartment.setAddress("123 Example St");
apartment.setPostalCode("08001");
apartment.setCity("Barcelona");
apartment.setCountry("Spain");
apartment.setDescription("A beautiful luxury apartment in the city center.");
apartment.setRegistrationDate(ZonedDateTime.now());
apartment.setOwner(owner);


// Crea una lista de habitaciones (rooms)
Room room1 = new Room();
room1.setId(1L);
room1.setSurface(20);
room1.setOccupied(false);
room1.setHasWindow(true);
room1.setHasDesk(true);
room1.setHasBed(true);
room1.setOwner(owner);
room1.setApart(apartment);

Room room2 = new Room();
room2.setId(2L);
room2.setSurface(15);
room2.setOccupied(true);
room2.setHasWindow(false);
room2.setHasDesk(false);
room2.setHasBed(true);
room2.setOwner(owner);
room2.setApart(apartment);

List<Room> rooms = List.of(room1, room2);
for (Room room : rooms) {
room.setApart(apartment);
}

Apartment apartment = new Apartment();
apartment.setId(Long.parseLong(id));
apartment.setName(name);
apartment.setFloor(5);
apartment.setAddress("123 Example St");
apartment.setPostalCode("08001");
apartment.setCity("Barcelona");
apartment.setCountry("Spain");
apartment.setDescription("A beautiful luxury apartment in the city center.");
apartment.setRegistrationDate(ZonedDateTime.now());
apartment.setOwner(owner);
apartment.setRooms(rooms);

apartmentRepository.save(apartment);
}


@Given("There is an existing advertisement status {string}")
public void thereIsAnExistingAdvertisementStatusWithIdAndStatus(String status) {
AdvertisementStatus advertisementStatus = new AdvertisementStatus();
Expand All @@ -83,17 +106,19 @@ public void thereIsAnExistingAdvertisementStatusWithIdAndStatus(String status) {

}

@When("I create a new advertisement with title {string}, description {string}, price {string}, zipCode {string}, address {string}, country {string}, status {string}")
public void iCreateANewAdvertisement(String title, String description, String price, String zipCode, String adress, String country, String adStatusId) throws Exception {
@When("I create a new advertisement with title {string}, description {string}, price {string}, zipCode {string}, address {string}, country {string}, status {string}, apartment title {string}")
public void iCreateANewAdvertisement(String title, String description, String price, String zipCode, String adress, String country, String status_state, String apartment) throws Exception {
Advertisement ad = new Advertisement();
ad.setTitle(title);
ad.setDescription(description);
ad.setPrice(new BigDecimal(price));
ad.setZipCode(zipCode);
ad.setAddress(adress);
ad.setCountry(country);
AdvertisementStatus cur_status = advertisementStatusRepository.findByStatus("Available").stream().findFirst().orElse(null);
AdvertisementStatus cur_status = advertisementStatusRepository.findByStatus(status_state).stream().findFirst().orElse(null);
Apartment cur_apartment = apartmentRepository.findByName(apartment).stream().findFirst().orElse(null);
ad.setAdStatus(cur_status);
ad.setApartment(cur_apartment);


stepDefs.result = stepDefs.mockMvc.perform(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cat.udl.eps.softarch.demo.steps;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;

import cat.udl.eps.softarch.demo.repository.ApartmentRepository;
import io.cucumber.java.en.And;
import org.springframework.http.MediaType;
import cat.udl.eps.softarch.demo.domain.*;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import cat.udl.eps.softarch.demo.repository.AdvertisementStatusRepository;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DeleteAdvStepsDefs {

@Autowired
private AdvertisementRepository advertisementRepository;

@Autowired
private ApartmentRepository apartmentRepository;
@Autowired
private AdvertisementStatusRepository advertisementStatusRepository;
private AdvertisementStatus status;
private ResponseEntity<String> response;
@Autowired
private StepDefs stepDefs;

@And("There is an advertisement with title {string}, description {string}, price {string}, zipCode {string}, address {string}, country {string}, status {string}, apartment title {string}")
public void iCreateANewAdvertisement(String title, String description, String price, String zipCode, String adress, String country, String status, String apartmentTitle) {
Advertisement ad = new Advertisement();
ad.setTitle(title);
ad.setDescription(description);
ad.setPrice(new BigDecimal(price));
ad.setZipCode(zipCode);
ad.setAddress(adress);
ad.setCountry(country);
Apartment apartment = apartmentRepository.findByName(apartmentTitle).stream().findFirst().orElse(null);
ad.setApartment(apartment);
AdvertisementStatus cur_status = advertisementStatusRepository.findByStatus(status).stream().findFirst().orElse(null);
ad.setAdStatus(cur_status);
advertisementRepository.save(ad);
}

@When("I delete the apartment advertisement with title {string}")
public void iDeleteTheApartmentAdvertisement(String title) throws Exception {
Advertisement ad = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null);
if (ad == null) {
stepDefs.result = stepDefs.mockMvc.perform(
delete("/advertisements/{id}", 9999).accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate())
).andDo(print());
} else {
stepDefs.result = stepDefs.mockMvc.perform(
delete("/advertisements/{id}", ad.getId()).accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate())
).andDo(print());
}
}

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

import cat.udl.eps.softarch.demo.domain.Property;
import cat.udl.eps.softarch.demo.repository.PropertyRepository;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class DeletePropertyStepDefs {
@Autowired
StepDefs stepDefs;
@Autowired
PropertyRepository propertyRepository;

@When("^I delete a property with a description \"([^\"]*)\"$")
public void modifyProperty(String previousDescription)throws Throwable {
Property property = propertyRepository.findByDescription(previousDescription).get(0);
Long propertyId = property.getId();
this.stepDefs.result = this.stepDefs.mockMvc.perform(delete("/properties/"+propertyId)
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(property))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}
}
Loading

0 comments on commit 4fc43f9

Please sign in to comment.