création des test sur la validation des valeurs et de l'exception

This commit is contained in:
2026-06-10 20:39:17 +02:00
parent 20fafa1a1c
commit 07b9357829
2 changed files with 175 additions and 0 deletions
@@ -0,0 +1,61 @@
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;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidReviewExcpetionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "Review data is not valid";
NotValidReviewException exception = new NotValidReviewException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"Note is greater than or equal to 1",
"Note is less than or equal to 1",
"Comment cannot be empty"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidReviewException exception = new NotValidReviewException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Comment field is empty";
Exception exception = assertThrows(NotValidReviewException.class, () -> {
throw new NotValidReviewException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid review data";
try {
throw new NotValidReviewException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidReviewException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -0,0 +1,114 @@
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 org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class ReviewValidatorTest {
@Test
@DisplayName("Should validate review with valid data")
void testValidateValidReview() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo validReview = new ReviewInfo(3, "Bof", purchaseDate);
assertDoesNotThrow(() -> ReviewValidator.validate(validReview));
}
@Nested
@DisplayName("Note validation tests")
class NoteValidationTests {
@Test
@DisplayName("Should throw exception when note is lower than 1")
void testValidateNoteLower1() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewWithNoteLower1 = new ReviewInfo(0, "Bof", purchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithNoteLower1)
);
assertEquals(ReviewValidator.NOTE_CANNOT_BE_LOWER_THAN_1, exception.getMessage());
}
@Test
@DisplayName("Should throw exception when note is upper than 5")
void testValidateNoteUpper5() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewWithNoteUpper5 = new ReviewInfo(6, "Bof", purchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithNoteUpper5)
);
assertEquals(ReviewValidator.NOTE_CANNOT_BE_UPPER_THAN_5, exception.getMessage());
}
}
@Nested
@DisplayName("Comment validation tests")
class CommentValidationTests {
@Test
@DisplayName("Should throw exception when comment is blank")
void testValidateBlankComment() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewWithBlankComment = new ReviewInfo(6, "", purchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithBlankComment)
);
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {" ", " ", "\t", "\n"})
@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);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithBlankComment)
);
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
}
}
@Nested
@DisplayName("Purchase date validation tests")
class PurchaseDateValidationTests {
@Test
@DisplayName("Should throw exception when purchase date is after the actual date")
void testValidateFuturPurchaseDate() {
LocalDate futurepurchaseDate = LocalDate.of(2026, 6, 24);
ReviewInfo reviewWithFuturPurchaseDate = new ReviewInfo(2, "Bof", futurepurchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithFuturPurchaseDate)
);
assertEquals(ReviewValidator.PURCHASE_DATE_IS_NOT_VALID, exception.getMessage());
}
}
}