Skip to content

Commit

Permalink
Merge pull request #41 from UdL-EPS-SoftArch/feature-test-create-adve…
Browse files Browse the repository at this point in the history
…rtisement

added test + little update on addvertisement  class
  • Loading branch information
rogargon authored Oct 17, 2024
2 parents 5f2e51a + 95febc7 commit 672bfb2
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 13 deletions.
28 changes: 17 additions & 11 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Advertisement.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package cat.udl.eps.softarch.demo.domain;

import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.*;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.math.BigDecimal;
import java.time.ZonedDateTime;

@Setter
Expand All @@ -18,32 +22,34 @@ public class Advertisement extends UriEntity<Long> {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@NotBlank
private String title;

@NotNull
@NotBlank
private String description;

@NotNull
private Double price;
@DecimalMin(value = "0.01")
private BigDecimal price;

@NotNull
@NotBlank
private String zipCode;

@NotNull
@NotBlank
private String country;

private ZonedDateTime creationDate;
@NotBlank
private String address;

@NotNull
private String city;
private ZonedDateTime creationDate;

private ZonedDateTime expirationDate;

@NotNull
private String address;

@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@NotNull
@ManyToOne
@JsonIdentityReference(alwaysAsId = true)
public AdvertisementStatus adStatus;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Setter
@Getter
@Entity
public class AdvertisementStatus {
public class AdvertisementStatus extends UriEntity<Long>{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import cat.udl.eps.softarch.demo.domain.Advertisement;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AdvertisementRepository extends CrudRepository<Advertisement, Long>, PagingAndSortingRepository<Advertisement, Long> {
List<Advertisement> findByTitle(@Param("title") String title);

public interface AdvertisementRepository extends CrudRepository<Advertisement, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import cat.udl.eps.softarch.demo.domain.AdvertisementStatus;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

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

public interface AdvertisementStatusRepository extends CrudRepository<AdvertisementStatus, Long> {
boolean existsByStatus(String status);
List<AdvertisementStatus> findByStatus(@Param("status")String status);
}
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());
}
}
59 changes: 59 additions & 0 deletions src/test/resources/features/CreateAdvertisement.feature
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

0 comments on commit 672bfb2

Please sign in to comment.