Reussite du test de reduction des stock avec l'exception possible

This commit is contained in:
2026-03-25 12:24:46 +01:00
parent 961b3b69ba
commit 2b29d34615
3 changed files with 86 additions and 3 deletions
@@ -1,6 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity; package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
@@ -24,4 +25,11 @@ public class Book {
public void addStock(Integer copyToAdd){ public void addStock(Integer copyToAdd){
this.stock += copyToAdd; this.stock += copyToAdd;
} }
public void removeStock(Integer copyToRemomve) throws IllegalBookStockException {
if (copyToRemomve > this.stock){
throw new IllegalBookStockException(copyToRemomve, this.stock);
}
this.stock -= copyToRemomve;
}
} }
@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
import java.text.MessageFormat;
public class IllegalBookStockException extends Exception {
public static final String CANNOT_REMOVE_COPY = "Cannot remove {0} copy from {1} copy";
public IllegalBookStockException(Integer toremove, Integer actual ) {
super(MessageFormat.format(CANNOT_REMOVE_COPY, toremove,
actual));
}
}
@@ -1,7 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity; package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter; import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -9,7 +9,8 @@ import org.junit.jupiter.api.Test;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class BookTest { public class BookTest {
@@ -84,5 +85,66 @@ public class BookTest {
assertEquals(copyExpected, book.getStock()); assertEquals(copyExpected, book.getStock());
} }
@Test
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
void testRemoveCopy() throws IllegalBookStockException {
Book book =Book.builder()
.stock(5)
.build();
Integer copyToRemove = 2;
Integer copyExpected = 3;
book.removeStock(copyToRemove);
assertEquals(copyExpected, book.getStock());
}
@Test
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
void testRemoveZeroToCopy() throws IllegalBookStockException {
Book book =Book.builder()
.stock(5)
.build();
Integer copyToRemove = 0;
Integer copyExpected = 5;
book.removeStock(copyToRemove);
assertEquals(copyExpected, book.getStock());
}
@Test
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
void testRemoveAllToCopy() throws IllegalBookStockException {
Book book =Book.builder()
.stock(5)
.build();
Integer copyToRemove = 5;
Integer copyExpected = 0;
book.removeStock(copyToRemove);
assertEquals(copyExpected, book.getStock());
}
@Test
@DisplayName("removeSTock should throw exception when trying to remove more copy than available")
void testRemoveTooManyCopy() {
Book book = Book.builder()
.stock(50)
.build();
int copyToRemove = 75;
IllegalBookStockException exception = assertThrows(
IllegalBookStockException.class,
() -> book.removeStock(copyToRemove)
);
assertEquals(50, book.getStock());
assertTrue(exception.getMessage().contains(String.valueOf(copyToRemove)));
assertTrue(exception.getMessage().contains(String.valueOf(book.getStock())));
}
} }
} }