-
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 #99 from UdL-EPS-SoftArch/96-test-create-review-test
96 test create review test
- Loading branch information
Showing
13 changed files
with
633 additions
and
5 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
6 changes: 6 additions & 0 deletions
6
src/main/java/cat/udl/eps/softarch/demo/repository/ReviewRepository.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 |
---|---|---|
@@ -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);; | ||
} |
120 changes: 120 additions & 0 deletions
120
src/test/java/cat/udl/eps/softarch/demo/steps/CreateReviewStepDefs.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,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()); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/test/java/cat/udl/eps/softarch/demo/steps/DeleteAdvReviewStepsDefs.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,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()); | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/test/java/cat/udl/eps/softarch/demo/steps/GetReviewStepDefs.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,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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.