-
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 87-test---create-advertisementstatus
- Loading branch information
Showing
14 changed files
with
683 additions
and
10 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
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
56 changes: 56 additions & 0 deletions
56
src/test/java/cat/udl/eps/softarch/demo/steps/DeleteVisitSteps.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,56 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Advertisement; | ||
import cat.udl.eps.softarch.demo.domain.Visit; | ||
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository; | ||
import cat.udl.eps.softarch.demo.repository.VisitRepository; | ||
import io.cucumber.java.en.Then; | ||
import io.cucumber.java.en.When; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
public class DeleteVisitSteps { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private VisitRepository visitRepository; | ||
|
||
@Autowired | ||
private AdvertisementRepository advertisementRepository; | ||
|
||
private MvcResult result; | ||
|
||
|
||
|
||
@When("I delete the visit request to the advertisement with title {string}") | ||
public void iDeleteTheVisitRequestToTheAdvertisementWithTitle(String title) throws Exception { | ||
|
||
Advertisement advertisement = advertisementRepository.findByTitle(title).get(0); | ||
Visit visit = visitRepository.findByAdvertisement(advertisement).get(0); | ||
|
||
result = stepDefs.mockMvc.perform(delete( "/visits/" + visit.getId() ) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()) | ||
.andExpect(status().isNoContent()) | ||
.andReturn(); | ||
|
||
|
||
|
||
} | ||
|
||
@Then("The visit is successfully deleted") | ||
public void theVisitIsSuccessfullyDeleted() { | ||
assertNotNull(result, "Result should not be null after visit deletion"); | ||
Assertions.assertEquals(204, result.getResponse().getStatus(), "Expected HTTP status 200 OK"); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/test/java/cat/udl/eps/softarch/demo/steps/GetAdvertismentStepDefs.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,56 @@ | ||
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.get; | ||
|
||
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 GetAdvertismentStepDefs { | ||
|
||
@Autowired | ||
private AdvertisementRepository advertisementRepository; | ||
|
||
@Autowired | ||
private ApartmentRepository apartmentRepository; | ||
@Autowired | ||
private AdvertisementStatusRepository advertisementStatusRepository; | ||
private AdvertisementStatus status; | ||
private ResponseEntity<String> response; | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@When("I get the apartment advertisement with title {string}") | ||
public void iGetTheApartmentAdvertisement(String title) throws Exception { | ||
Advertisement ad = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null); | ||
|
||
if (ad == null) { | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
get("/advertisements/{id}", 9999) | ||
.accept(MediaType.APPLICATION_JSON) | ||
).andDo(print()); | ||
} else { | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
get("/advertisements/{id}", ad.getId()) | ||
.accept(MediaType.APPLICATION_JSON) | ||
).andDo(print()); | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/cat/udl/eps/softarch/demo/steps/PatchAdvStepDefs.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,50 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Advertisement; | ||
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository; | ||
import io.cucumber.core.internal.com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.cucumber.java.en.When; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
|
||
|
||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class PatchAdvStepDefs { | ||
|
||
@Autowired | ||
private AdvertisementRepository advertisementRepository; | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@When("I patch the apartment advertisement with title {string} and new description {string}") | ||
public void iPatchTheApartmentAdvertisement(String title, String newDescription) throws Exception { | ||
Advertisement ad = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null); | ||
|
||
Map<String, String> updatedFields = new HashMap<>(); | ||
updatedFields.put("description", newDescription); | ||
String jsonContent = new ObjectMapper().writeValueAsString(updatedFields); | ||
|
||
if (ad == null) { | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
patch("/advertisements/{id}", 9999) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(jsonContent) | ||
.with(AuthenticationStepDefs.authenticate()) | ||
).andDo(print()); | ||
} else { | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
patch("/advertisements/{id}", ad.getId()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(jsonContent) | ||
.with(AuthenticationStepDefs.authenticate()) | ||
).andDo(print()); | ||
} | ||
} | ||
} |
Oops, something went wrong.