forked from pierront/mylibrary-template
Feature/manage reviews #2
@@ -117,6 +117,11 @@
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||
|
||||
public class NotValidReviewException extends RuntimeException {
|
||||
public NotValidReviewException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class ReviewValidator {
|
||||
|
||||
public static final String NOTE_CANNOT_BE_LOWER_THAN_1 = "Note is greater than or equal to 1";
|
||||
public static final String NOTE_CANNOT_BE_UPPER_THAN_5 = "Note is less than or equal to 5";
|
||||
public static final String COMMENT_CANNOT_BE_BLANK = "Comment cannot be blank";
|
||||
public static final String PURCHASE_DATE_IS_NOT_VALID = "Date is not valid";
|
||||
|
||||
public ReviewValidator() {
|
||||
|
||||
}
|
||||
|
||||
public static void validate(ReviewInfo newReview) throws NotValidReviewException {
|
||||
validateNoteLower1(newReview);
|
||||
validateNoteUpper5(newReview);
|
||||
validateComment(newReview);
|
||||
validatePurchaseDate(newReview);
|
||||
}
|
||||
|
||||
private static void validateNoteLower1(ReviewInfo newReview)
|
||||
throws NotValidReviewException {
|
||||
if (newReview.note() <= 1) {
|
||||
throw new NotValidReviewException(NOTE_CANNOT_BE_LOWER_THAN_1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateNoteUpper5(ReviewInfo newReview)
|
||||
throws NotValidReviewException {
|
||||
if (newReview.note() >= 5) {
|
||||
throw new NotValidReviewException("Note is less than or equal to 5");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateComment(ReviewInfo newReview) throws NotValidReviewException {
|
||||
if (newReview.comment().isBlank()) {
|
||||
throw new NotValidReviewException(COMMENT_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePurchaseDate(ReviewInfo newReview) throws NotValidReviewException {
|
||||
if (newReview.purchaseDate().isAfter(LocalDate.now())) {
|
||||
throw new NotValidReviewException(PURCHASE_DATE_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
}
|
||||
-1
@@ -1,6 +1,5 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
|
||||
+3
-4
@@ -1,8 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -67,7 +66,7 @@ public class ReviewValidatorTest {
|
||||
@DisplayName("Should throw exception when comment is blank")
|
||||
void testValidateBlankComment() {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
ReviewInfo reviewWithBlankComment = new ReviewInfo(6, "", purchaseDate);
|
||||
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, "", purchaseDate);
|
||||
|
||||
NotValidReviewException exception = assertThrows(
|
||||
NotValidReviewException.class,
|
||||
@@ -82,7 +81,7 @@ public class ReviewValidatorTest {
|
||||
@DisplayName("Should throw exception when last name contains only whitespace")
|
||||
void testValidateWhitespaceLastName(String whitespace) {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
ReviewInfo reviewWithBlankComment = new ReviewInfo(6, whitespace, purchaseDate);
|
||||
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, whitespace, purchaseDate);
|
||||
|
||||
NotValidReviewException exception = assertThrows(
|
||||
NotValidReviewException.class,
|
||||
|
||||
Reference in New Issue
Block a user