-
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 pull request #41 from UdL-EPS-SoftArch/feature-test-create-adve…
…rtisement added test + little update on addvertisement class
- Loading branch information
Showing
6 changed files
with
209 additions
and
13 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
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
119 changes: 119 additions & 0 deletions
119
src/test/java/cat/udl/eps/softarch/demo/steps/AdvertisementStepDefs.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,119 @@ | ||
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 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; | ||
import io.cucumber.java.en.When; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.math.BigDecimal; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class AdvertisementStepDefs { | ||
|
||
@Autowired | ||
private AdvertisementRepository advertisementRepository; | ||
@Autowired | ||
private AdvertisementStatusRepository advertisementStatusRepository; | ||
private AdvertisementStatus status; | ||
private ResponseEntity<String> response; | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@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"); | ||
|
||
// 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); | ||
|
||
Room room2 = new Room(); | ||
room2.setId(2L); | ||
room2.setSurface(15); | ||
room2.setOccupied(true); | ||
room2.setHasWindow(false); | ||
room2.setHasDesk(false); | ||
room2.setHasBed(true); | ||
|
||
List<Room> rooms = List.of(room1, room2); | ||
|
||
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); | ||
|
||
} | ||
|
||
@Given("There is an existing advertisement status {string}") | ||
public void thereIsAnExistingAdvertisementStatusWithIdAndStatus(String status) { | ||
AdvertisementStatus advertisementStatus = new AdvertisementStatus(); | ||
advertisementStatus.setStatus(status); | ||
advertisementStatusRepository.save(advertisementStatus); | ||
|
||
} | ||
|
||
@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 { | ||
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); | ||
ad.setAdStatus(cur_status); | ||
|
||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/advertisements") | ||
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(ad)) | ||
.characterEncoding(StandardCharsets.UTF_8)) | ||
.andDo(print()); | ||
|
||
} | ||
|
||
|
||
@Then("The advertisement has been created with title {string}") | ||
public void theAdvertisementHasBeenCreatedWithTitle(String title) { | ||
Advertisement createdAd = advertisementRepository.findByTitle(title).get(0); | ||
assertEquals(title, createdAd.getTitle()); | ||
} | ||
|
||
@Then("It has not been created an advertisement") | ||
public void it_has_not_been_created_an_advertisement() { | ||
assertEquals(0, advertisementRepository.count()); | ||
} | ||
} |
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,59 @@ | ||
Feature: Create advertisement | ||
In order to use the app | ||
As a user | ||
I want to create my advertisement for an existing apartment with a specific status | ||
|
||
Scenario: Create new advertisement for existing apartment with existing status | ||
Given There is an existing apartment with id "1" named "Cozy Apartment" | ||
And There is an existing advertisement status "Available" | ||
When I create a new advertisement with title "Apartment for rent", description "A beautiful apartment", price "1200", zipCode "12345", address "456 Elm St", country "Spain", status "Available" | ||
Then The response code is 201 | ||
And The advertisement has been created with title "Apartment for rent" | ||
|
||
Scenario: Create advertisement with missing title | ||
Given There is an existing apartment with id "1" named "Cozy Apartment" | ||
And There is an existing advertisement status "Available" | ||
When I create a new advertisement with title "", description "A beautiful apartment", price "1200", zipCode "12345", address "456 Elm St", country "Spain", status "Available" | ||
Then The response code is 400 | ||
And The error message is "must not be blank" | ||
And It has not been created an advertisement | ||
|
||
Scenario: Create advertisement with missing description | ||
Given There is an existing apartment with id "1" named "Cozy Apartment" | ||
And There is an existing advertisement status "Available" | ||
When I create a new advertisement with title "Apartment for rent", description "", price "1200", zipCode "12345", address "456 Elm St", country "Spain", status "Available" | ||
Then The response code is 400 | ||
And The error message is "must not be blank" | ||
And It has not been created an advertisement | ||
|
||
Scenario: Create advertisement with invalid price | ||
Given There is an existing apartment with id "1" named "Cozy Apartment" | ||
And There is an existing advertisement status "Available" | ||
When I create a new advertisement with title "Apartment for rent", description "A beautiful apartment", price "-100", zipCode "12345", address "456 Elm St", country "Spain", status "Available" | ||
Then The response code is 400 | ||
And The error message is "must be greater than or equal to 0.01" | ||
And It has not been created an advertisement | ||
|
||
Scenario: Create advertisement with missing zip code | ||
Given There is an existing apartment with id "1" named "Cozy Apartment" | ||
And There is an existing advertisement status "Available" | ||
When I create a new advertisement with title "Apartment for rent", description "A beautiful apartment", price "1200", zipCode "", address "456 Elm St", country "Spain", status "Available" | ||
Then The response code is 400 | ||
And The error message is "must not be blank" | ||
And It has not been created an advertisement | ||
|
||
Scenario: Create advertisement with missing country | ||
Given There is an existing apartment with id "1" named "Cozy Apartment" | ||
And There is an existing advertisement status "Available" | ||
When I create a new advertisement with title "Apartment for rent", description "A beautiful apartment", price "1200", zipCode "12345", address "456 Elm St", country "", status "Available" | ||
Then The response code is 400 | ||
And The error message is "must not be blank" | ||
And It has not been created an advertisement | ||
|
||
Scenario: Create advertisement with missing ad status | ||
Given There is an existing apartment with id "1" named "Cozy Apartment" | ||
When I create a new advertisement with title "Apartment for rent", description "A beautiful apartment", price "1200", zipCode "12345", address "456 Elm St", country "Spain", status "" | ||
Then The response code is 400 | ||
And The error message is "must not be null" | ||
And It has not been created an advertisement | ||
|