forked from pierront/mylibrary-template
Compare commits
9 Commits
main
...
495343ddb8
| Author | SHA1 | Date | |
|---|---|---|---|
| 495343ddb8 | |||
| 2b29d34615 | |||
| 961b3b69ba | |||
| 958f391b05 | |||
| e8563d44b1 | |||
| 55e43fca89 | |||
| c767f47719 | |||
| 370d35078f | |||
| 80c2b82c98 |
@@ -14,6 +14,7 @@
|
||||
"langue": "string"
|
||||
},
|
||||
"output": {
|
||||
"_comment": "c'est ce qui affiche quand tu crée avec la usecase (regarde register de customer)",
|
||||
"isbn": "string(13)"
|
||||
}
|
||||
},
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class BookDTO {
|
||||
private String isbn;
|
||||
private String title;
|
||||
private String author;
|
||||
private String editor;
|
||||
private LocalDate date;
|
||||
private double price;
|
||||
private Integer stock;
|
||||
private ArrayList<String> categories = new ArrayList<>();
|
||||
private String description ;
|
||||
private String language;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class BookDetails {
|
||||
private ArrayList<String> categories = new ArrayList<>();
|
||||
private String description ;
|
||||
private String language;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public record BookInfo(String title, String author, String editor, LocalDate date) {
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.converter;
|
||||
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDetails;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
|
||||
public final class BookConverter {
|
||||
private BookConverter(){}
|
||||
|
||||
public static Book ToDomain(BookInfo bookinfo, BookDetails bookdetails){
|
||||
return Book.builder()
|
||||
.isbn("0000000000000")
|
||||
.title(bookinfo.title())
|
||||
.author(bookinfo.author())
|
||||
.editor(bookinfo.editor())
|
||||
.date(bookinfo.date())
|
||||
.price(50.99)
|
||||
.stock(5)
|
||||
.categories(bookdetails.getCategories())
|
||||
.description(bookdetails.getDescription())
|
||||
.language(bookdetails.getLanguage())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static BookDTO ToDTO(Book book){
|
||||
return BookDTO.builder()
|
||||
.isbn(book.getIsbn())
|
||||
.title(book.getTitle())
|
||||
.author(book.getAuthor())
|
||||
.editor(book.getEditor())
|
||||
.date(book.getDate())
|
||||
.price(book.getPrice())
|
||||
.stock(book.getStock())
|
||||
.categories(book.getCategories())
|
||||
.description(book.getDescription())
|
||||
.language(book.getLanguage())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
|
||||
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class Book {
|
||||
private String isbn;
|
||||
private String title;
|
||||
private String author;
|
||||
private String editor;
|
||||
private LocalDate date;
|
||||
private double price;
|
||||
private Integer stock;
|
||||
private ArrayList<String> categories = new ArrayList<>();
|
||||
private String description ;
|
||||
private String language;
|
||||
|
||||
public void addStock(Integer copyToAdd){
|
||||
this.stock += copyToAdd;
|
||||
}
|
||||
|
||||
public void removeStock(Integer copyToRemomve) throws IllegalBookStockException {
|
||||
if (copyToRemomve > this.stock){
|
||||
throw new IllegalBookStockException(copyToRemomve, this.stock);
|
||||
}
|
||||
this.stock -= copyToRemomve;
|
||||
}
|
||||
}
|
||||
+13
@@ -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));
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDetails;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@DisplayName("BookConverterTest Unit Tests")
|
||||
public class BookConverterTest {
|
||||
|
||||
@Nested
|
||||
@DisplayName("toDomain() method tests")
|
||||
class ToDomainTests {
|
||||
|
||||
@Test
|
||||
void ShouldConvertBookToDomain(){
|
||||
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||
BookInfo bookinfo = new BookInfo("La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||
ArrayList<String> categories = new ArrayList<>();
|
||||
categories.add("Thriller");
|
||||
categories.add("Biographie");
|
||||
BookDetails bookdetails = BookDetails.builder()
|
||||
.categories(categories)
|
||||
.description("C'était un brave partit trop tôt")
|
||||
.language("Français")
|
||||
.build();
|
||||
|
||||
Book result = BookConverter.ToDomain(bookinfo, bookdetails);
|
||||
|
||||
assertEquals(bookinfo.title(), result.getTitle());
|
||||
assertEquals(bookinfo.author(), result.getAuthor());
|
||||
assertEquals(bookinfo.editor(), result.getEditor());
|
||||
assertEquals(bookinfo.date(), result.getDate());
|
||||
assertEquals(categories, result.getCategories());
|
||||
assertEquals(bookdetails.getDescription(), result.getDescription());
|
||||
assertEquals(bookdetails.getLanguage(), result.getLanguage());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("toDTO() method tests")
|
||||
class ToDTOTests {
|
||||
|
||||
@Test
|
||||
void ShouldConvertBookToDTO() {
|
||||
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||
ArrayList<String> categories = new ArrayList<>();
|
||||
categories.add("Thriller");
|
||||
categories.add("Biographie");
|
||||
Book book = Book.builder()
|
||||
.isbn("1234567891012")
|
||||
.title("La vie de Maxime")
|
||||
.author("Marvin Aubert")
|
||||
.editor("Kioon")
|
||||
.date(date)
|
||||
.price(12.99)
|
||||
.stock(50)
|
||||
.categories(categories)
|
||||
.description("C'était un brave partit trop tôt")
|
||||
.language("Français")
|
||||
.build();
|
||||
|
||||
BookDTO result = BookConverter.ToDTO(book);
|
||||
|
||||
assertEquals(book.getIsbn(), result.getIsbn());
|
||||
assertEquals(book.getTitle(), result.getTitle());
|
||||
assertEquals(book.getAuthor(), result.getAuthor());
|
||||
assertEquals(book.getEditor(), result.getEditor());
|
||||
assertEquals(book.getDate(), result.getDate());
|
||||
assertEquals(book.getPrice(), result.getPrice());
|
||||
assertEquals(book.getStock(), result.getStock());
|
||||
assertEquals(book.getCategories(), result.getCategories());
|
||||
assertEquals(book.getDescription(), result.getDescription());
|
||||
assertEquals(book.getLanguage(), result.getLanguage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should preserve empty string values during conversion")
|
||||
void shouldPreserveEmptyStrings() {
|
||||
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||
BookInfo bookinfo = new BookInfo("La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||
ArrayList<String> categories = new ArrayList<>();
|
||||
categories.add("Thriller");
|
||||
categories.add("Biographie");
|
||||
BookDetails bookdetails = BookDetails.builder()
|
||||
.categories(categories)
|
||||
.description("")
|
||||
.language("Français")
|
||||
.build();
|
||||
|
||||
Book book = BookConverter.ToDomain(bookinfo, bookdetails);
|
||||
BookDTO result = BookConverter.ToDTO(book);
|
||||
|
||||
assertEquals("", result.getDescription());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
|
||||
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class BookTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Builder should create a valid Customer instance")
|
||||
void testCustomerBuilder() {
|
||||
String isbn = "1234567891012";
|
||||
String title = "La vie de Maxime";
|
||||
String author = "Marvin Aubert";
|
||||
String editor = "Kioon";
|
||||
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||
double price = 12.99;
|
||||
Integer stock = 50;
|
||||
ArrayList<String> categories = new ArrayList<>();
|
||||
categories.add("Thriller");
|
||||
categories.add("Biographie");
|
||||
String description = "C'était un brave partit trop tôt";
|
||||
String language = "Français";
|
||||
Book book = Book.builder()
|
||||
.isbn(isbn)
|
||||
.title(title)
|
||||
.author(author)
|
||||
.editor(editor)
|
||||
.date(date)
|
||||
.price(price)
|
||||
.stock(stock)
|
||||
.categories(categories)
|
||||
.description(description)
|
||||
.language(language)
|
||||
.build();
|
||||
|
||||
assertEquals(isbn, book.getIsbn());
|
||||
assertEquals(title, book.getTitle());
|
||||
assertEquals(author, book.getAuthor());
|
||||
assertEquals(editor, book.getEditor());
|
||||
assertEquals(date, book.getDate());
|
||||
assertEquals(price, book.getPrice());
|
||||
assertEquals(stock, book.getStock());
|
||||
assertEquals(categories, book.getCategories());
|
||||
assertEquals(description, book.getDescription());
|
||||
assertEquals(language, book.getLanguage());
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Stock Tests")
|
||||
class StockTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("addCopy should correctly increment stocks")
|
||||
void testAddCopy() {
|
||||
Book book =Book.builder()
|
||||
.stock(5)
|
||||
.build();
|
||||
Integer copyToAdd = 5;
|
||||
Integer copyExpected = 10;
|
||||
|
||||
book.addStock(copyToAdd);
|
||||
|
||||
assertEquals(copyExpected, book.getStock());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("addCopy should correctly increment zero points correctly")
|
||||
void testAddZeroToCopy() {
|
||||
Book book =Book.builder()
|
||||
.stock(5)
|
||||
.build();
|
||||
Integer copyToAdd = 0;
|
||||
Integer copyExpected = 5;
|
||||
|
||||
book.addStock(copyToAdd);
|
||||
|
||||
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())));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Price Tests")
|
||||
class PriceTests {
|
||||
@Test
|
||||
@DisplayName("addCopy should correctly increment stocks")
|
||||
void testAddCopy() {
|
||||
Book book =Book.builder()
|
||||
.stock(5)
|
||||
.build();
|
||||
Integer copyToAdd = 5;
|
||||
Integer copyExpected = 10;
|
||||
|
||||
book.addStock(copyToAdd);
|
||||
|
||||
assertEquals(copyExpected, book.getStock());
|
||||
}
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.IllegalCustomerPointException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class IllegalBookStockExceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the needed and actual stock")
|
||||
void testExceptionMessageContainsStock() {
|
||||
int neededStock = 100;
|
||||
int actualStock = 50;
|
||||
|
||||
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||
|
||||
String expectedMessage = "Cannot remove 100 copy from 50 copy";
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"100, 50",
|
||||
"75, 25",
|
||||
"200, 150",
|
||||
"1000, 750"
|
||||
})
|
||||
@DisplayName("Exception message should be formatted correctly for different stock values")
|
||||
void testExceptionMessageForDifferentStockValues(int neededStock, int actualStock) {
|
||||
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||
|
||||
String expectedMessage = MessageFormat.format(IllegalBookStockException.CANNOT_REMOVE_COPY, neededStock, actualStock);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format")
|
||||
void testExceptionUsesConstantMessageFormat() {
|
||||
int neededStock = 100;
|
||||
int actualStock = 50;
|
||||
|
||||
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||
|
||||
String expectedFormatWithPlaceholder = "Cannot remove {0} copy from {1} copy";
|
||||
assertEquals(IllegalBookStockException.CANNOT_REMOVE_COPY,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(String.valueOf(neededStock)));
|
||||
assertTrue(exception.getMessage().contains(String.valueOf(actualStock)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
int neededStock = 100;
|
||||
int actualStock = 50;
|
||||
|
||||
try {
|
||||
throw new IllegalBookStockException(neededStock, actualStock);
|
||||
} catch (IllegalBookStockException e) {
|
||||
String expectedMessage = String.format("Cannot remove %d copy from %d copy", neededStock, actualStock);
|
||||
assertEquals(expectedMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user