-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 77-fix-update-room-test
- Loading branch information
Showing
23 changed files
with
916 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -45,6 +53,7 @@ public void initializeDatabase() { | |
owner.encodePassword(); | ||
ownerRepository.save(owner); | ||
} | ||
|
||
if (!ownerRepository.existsById("owner1")) { | ||
Owner owner = new Owner(); | ||
owner.setEmail("[email protected]"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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(); | ||
|
@@ -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( | ||
|
71 changes: 71 additions & 0 deletions
71
src/test/java/cat/udl/eps/softarch/demo/steps/DeleteAdvStepsDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/cat/udl/eps/softarch/demo/steps/DeletePropertyStepDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.