Skip to content

Commit

Permalink
Merge pull request #99 from UdL-EPS-SoftArch/96-test-create-review-test
Browse files Browse the repository at this point in the history
96 test create review test
  • Loading branch information
rogargon authored Nov 15, 2024
2 parents 58839fa + 0534e83 commit 59d22cb
Show file tree
Hide file tree
Showing 13 changed files with 633 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Review.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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.*;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;

@Setter
@Getter
@Entity
Expand All @@ -25,12 +28,12 @@ public class Review extends UriEntity<Long>{
private String description;

@NotNull
@Min(value = 0)
@Max(value = 5)
private Double rating;
@DecimalMin(value = "0")
@DecimalMax(value = "5")
private BigDecimal rating;

@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@NotNull
@ManyToOne
@JsonIdentityReference(alwaysAsId = true)
public Advertisement advertisement;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package cat.udl.eps.softarch.demo.repository;

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

import java.util.Arrays;
import java.util.List;

public interface ReviewRepository extends CrudRepository<Review, Long>{
List<Review> findByTitle(@Param("title") String title);;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Advertisement;
import cat.udl.eps.softarch.demo.domain.Review;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import cat.udl.eps.softarch.demo.repository.ReviewRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class CreateReviewStepDefs {

@Autowired
private AdvertisementRepository advertisementRepository;

@Autowired
private ReviewRepository reviewRepository;

@Autowired
private StepDefs stepDefs;


@When("I create a review with title {string}, description {string} and rating {string} for the advertisement with title {string}")
public void iCreateAReview(String title, String description, String rating, String adTitle) throws Exception {
Advertisement advertisement = advertisementRepository.findByTitle(adTitle).stream().findFirst().orElseThrow(() ->
new IllegalArgumentException("Advertisement with title " + adTitle + " not found"));

Review review = new Review();
review.setTitle(title);
review.setDescription(description);
review.setRating(new BigDecimal(rating));
review.setAdvertisement(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(
post("/reviews")
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(review))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}


@Then("The review should be created with title {string}")
public void theReviewShouldBeCreatedWithTitle(String expectedTitle) throws Exception {
stepDefs.result.andExpect(MockMvcResultMatchers.status().isCreated())
.andExpect(MockMvcResultMatchers.jsonPath("$.title").value(expectedTitle));
}

@And("The review should have description {string}")
public void theReviewShouldHaveDescription(String expectedDescription) throws Exception {
stepDefs.result.andExpect(MockMvcResultMatchers.jsonPath("$.description").value(expectedDescription));
}

@And("The review should have rating {string}")
public void theReviewShouldHaveRating(String expectedRating) throws Exception {
BigDecimal expectRating = new BigDecimal(expectedRating);
stepDefs.result.andExpect(MockMvcResultMatchers.jsonPath("$.rating").value(expectRating));
}

@When("I attempt to create a review with title {string}, no description and rating {string} for the advertisement with title {string}")
public void iAttemptToCreateAReviewWithoutDescription(String title, String rating, String adTitle) throws Exception {
Advertisement advertisement = advertisementRepository.findByTitle(adTitle).stream().findFirst().orElseThrow(() ->
new IllegalArgumentException("Advertisement with title " + adTitle + " not found"));

Review review = new Review();
review.setTitle(title);
review.setRating(new BigDecimal(rating));
review.setAdvertisement(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(post("/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(review))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}

@When("I attempt to create a review with no title, description {string} and rating {string} for the advertisement with title {string}")
public void iAttemptToCreateAReviewWithoutTitle(String description, String rating, String adTitle) throws Exception {
Advertisement advertisement = advertisementRepository.findByTitle(adTitle).stream().findFirst().orElseThrow(() ->
new IllegalArgumentException("Advertisement with title " + adTitle + " not found"));

Review review = new Review();
review.setDescription(description);
review.setRating(new BigDecimal(rating));
review.setAdvertisement(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(post("/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(review))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}

@When("I attempt to create a review with title {string}, description {string} and no rating for the advertisement with title {string}")
public void iAttemptToCreateAReviewWithoutRating(String title, String description, String adTitle) throws Exception {
Advertisement advertisement = advertisementRepository.findByTitle(adTitle).stream().findFirst().orElseThrow(() ->
new IllegalArgumentException("Advertisement with title " + adTitle + " not found"));

Review review = new Review();
review.setTitle(title);
review.setDescription(description);
review.setAdvertisement(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(post("/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(review))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Advertisement;
import cat.udl.eps.softarch.demo.domain.Review;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import cat.udl.eps.softarch.demo.repository.ReviewRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class DeleteAdvReviewStepsDefs {

@Autowired
private AdvertisementRepository advertisementRepository;

@Autowired
private ReviewRepository reviewRepository;

@Autowired
private StepDefs stepDefs;

@And("There is an advertisement review with title {string}, description {string}, rating {string}, advertisement title {string}")
public void thereIsAnAdvertisementReviewWithTitleDescriptionRatingAdvertisementTitle(String title, String description, String rating, String adTitle) {
Advertisement advertisement = advertisementRepository.findByTitle(adTitle).stream()
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Advertisement with title " + adTitle + " not found"));

Review review = new Review();
review.setTitle(title);
review.setDescription(description);
review.setRating(new BigDecimal(rating));
review.setAdvertisement(advertisement);

reviewRepository.save(review);
}

@When("I delete the review with title {string} for the advertisement with title {string}")
public void iDeleteTheReviewWithTitleForTheAdvertisementWithTitle(String reviewTitle, String adTitle) throws Exception {
Review review = reviewRepository.findByTitle(reviewTitle).stream().findFirst().orElseThrow(() ->
new IllegalArgumentException("Review with title " + reviewTitle + " not found"));

stepDefs.result = stepDefs.mockMvc.perform(
delete("/reviews/{id}", review.getId())
.with(AuthenticationStepDefs.authenticate())
.accept(MediaType.APPLICATION_JSON)
).andDo(print());
}

@Then("The review should be deleted successfully")
public void theReviewShouldBeDeletedSuccessfully() throws Exception {
stepDefs.result.andExpect(MockMvcResultMatchers.status().isNoContent());
}

@When("I attempt to delete a non-existent review with id {string}")
public void iAttemptToDeleteANonExistentReviewWithId(String reviewId) throws Exception {
stepDefs.result = stepDefs.mockMvc.perform(
delete("/reviews/{id}", Long.parseLong(reviewId))
.with(AuthenticationStepDefs.authenticate())
.accept(MediaType.APPLICATION_JSON)
).andDo(print());
}

@Then("I should receive a not found error")
public void iShouldReceiveANotFoundError() throws Exception {
stepDefs.result.andExpect(MockMvcResultMatchers.status().isNotFound());
}

@When("I attempt to delete the review with title {string} without authentication")
public void iAttemptToDeleteTheReviewWithoutAuthentication(String reviewTitle) throws Exception {
Review review = reviewRepository.findByTitle(reviewTitle).stream().findFirst().orElseThrow(() ->
new IllegalArgumentException("Review with title " + reviewTitle + " not found"));

stepDefs.result = stepDefs.mockMvc.perform(
delete("/reviews/{id}", review.getId()) // sin autenticación
.accept(MediaType.APPLICATION_JSON)
).andDo(print());
}

@Then("I should receive an unauthorized error")
public void iShouldReceiveAnUnauthorizedError() throws Exception {
stepDefs.result.andExpect(MockMvcResultMatchers.status().isUnauthorized());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void iGetTheApartmentAdvertisement(String title) throws Exception {

if (ad == null) {
stepDefs.result = stepDefs.mockMvc.perform(
get("/advertisements/{id}", 9999)
get("/advertisements/{id}", -1)
.accept(MediaType.APPLICATION_JSON)
).andDo(print());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cat.udl.eps.softarch.demo.steps;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

import cat.udl.eps.softarch.demo.domain.Review;
import cat.udl.eps.softarch.demo.repository.ReviewRepository;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.List;

public class GetReviewStepDefs {

@Autowired
private ReviewRepository reviewRepository;

@Autowired
private StepDefs stepDefs;

@When("I get the review with title {string}")
public void iGetTheReviewWithTitle(String title) throws Exception {
Review review = reviewRepository.findByTitle(title).stream().findFirst().orElse(null);

if (review == null) {
stepDefs.result = stepDefs.mockMvc.perform(
get("/reviews/{id}", -1)
.accept(MediaType.APPLICATION_JSON)
).andDo(print());
} else {
stepDefs.result = stepDefs.mockMvc.perform(
get("/reviews/{id}", review.getId())
.accept(MediaType.APPLICATION_JSON)
).andDo(print());
}
}
}
Loading

0 comments on commit 59d22cb

Please sign in to comment.