From 598d83ec9de487cefc9d4bcc45e3cbb6a707e3c2 Mon Sep 17 00:00:00 2001 From: TranHuuDanh Date: Wed, 1 Apr 2026 05:59:31 +0000 Subject: [PATCH] chore: update --- .../drinkool/entities/CustomerEntityTest.java | 46 ++++++++++++ .../drinkool/utils/PhoneValidatorTest.java | 41 ++++++++++ .../entities/EateryInventoryItemEntity.java | 28 ++++--- .../EateryInventoryItemEntityTest.java | 22 ------ .../entities/InventoryItemEntityTest.java | 44 +++++++++++ .../drinkool/entities/MenuItemEntityTest.java | 58 +++++++++++++++ .../MenuItemIngredientEntityTest.java | 74 +++++++++---------- .../drinkool/entities/ReviewEntityTest.java | 35 +++++++++ .../drinkool/services/ReviewServiceTest.java | 2 +- .../java/com/drinkool/models/ReviewModel.java | 5 -- 10 files changed, 278 insertions(+), 77 deletions(-) create mode 100644 account-service/src/test/java/com/drinkool/entities/CustomerEntityTest.java create mode 100644 account-service/src/test/java/com/drinkool/utils/PhoneValidatorTest.java create mode 100644 eatery-service/src/test/java/com/drinkool/entities/InventoryItemEntityTest.java create mode 100644 eatery-service/src/test/java/com/drinkool/entities/MenuItemEntityTest.java create mode 100644 eatery-service/src/test/java/com/drinkool/entities/ReviewEntityTest.java diff --git a/account-service/src/test/java/com/drinkool/entities/CustomerEntityTest.java b/account-service/src/test/java/com/drinkool/entities/CustomerEntityTest.java new file mode 100644 index 0000000..2bac81d --- /dev/null +++ b/account-service/src/test/java/com/drinkool/entities/CustomerEntityTest.java @@ -0,0 +1,46 @@ +package com.drinkool.entities; + +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import jakarta.persistence.EntityManager; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class CustomerEntityTest { + + @Inject + EntityManager entityManager; + + @Test + @Transactional + public void testCustomerPersistence() { + // 1. Khởi tạo đúng Entity (CustomerEntity chứ không phải CustomerEntityTest) + CustomerEntity customer = new CustomerEntity(); + + // Gán đúng biến name, phone và hashedPassword (các trường kế thừa từ UserModel) + customer.name = "Nguyen Van A"; + customer.phone = "+84987654321"; + customer.hashedPassword = "dummy_hashed_password"; + + // 2. Lưu vào DB + entityManager.persist(customer); + entityManager.flush(); + entityManager.clear(); + + // 3. Truy vấn lại để kiểm tra (Sửa lại class map của query thành CustomerEntity) + var query = entityManager.createQuery( + "SELECT c FROM CustomerEntity c WHERE c.phone = :phone", CustomerEntity.class); + query.setParameter("phone", "+84987654321"); + + // Nhận kết quả về đúng kiểu CustomerEntity + CustomerEntity savedCustomer = query.getSingleResult(); + + // 4. Assert (Kiểm chứng) + Assertions.assertNotNull(savedCustomer, "Customer không được null"); + Assertions.assertEquals("Nguyen Van A", savedCustomer.name, "Tên không khớp"); + Assertions.assertEquals("+84987654321", savedCustomer.phone, "Số điện thoại không khớp"); + Assertions.assertNotNull(savedCustomer.id, "ID chưa được sinh ra"); + } +} \ No newline at end of file diff --git a/account-service/src/test/java/com/drinkool/utils/PhoneValidatorTest.java b/account-service/src/test/java/com/drinkool/utils/PhoneValidatorTest.java new file mode 100644 index 0000000..c35bcdd --- /dev/null +++ b/account-service/src/test/java/com/drinkool/utils/PhoneValidatorTest.java @@ -0,0 +1,41 @@ +package com.drinkool.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class PhoneValidatorTest { + + @Test + public void testValidVietnamesePhoneNumbers() { + // Số điện thoại VN hợp lệ (có mã vùng hoặc số 0 ở đầu đều được) + Assertions.assertTrue(PhoneValidator.isValidInternationalPhone("0987654321")); + Assertions.assertTrue(PhoneValidator.isValidInternationalPhone("+84987654321")); + Assertions.assertTrue(PhoneValidator.isValidInternationalPhone("84987654321")); + } + + @Test + public void testValidInternationalPhoneNumbers() { + // Số điện thoại quốc tế hợp lệ (Ví dụ: Mỹ +1) + Assertions.assertTrue(PhoneValidator.isValidInternationalPhone("+14155552671")); + } + + @Test + public void testInvalidPhoneNumbers() { + // Chuỗi không phải số điện thoại + Assertions.assertFalse(PhoneValidator.isValidInternationalPhone("not-a-phone-number")); + + // Số điện thoại quá ngắn hoặc sai định dạng + Assertions.assertFalse(PhoneValidator.isValidInternationalPhone("123")); + Assertions.assertFalse(PhoneValidator.isValidInternationalPhone("099999999999999")); + + // Chuỗi rỗng + Assertions.assertFalse(PhoneValidator.isValidInternationalPhone("")); + } + + @Test + public void testNullPhoneNumber() { + // Cố tình truyền null để xem hàm xử lý thế nào + // Lưu ý: Đọc thêm phần "Mẹo nhỏ" bên dưới để pass được test này + Assertions.assertFalse(PhoneValidator.isValidInternationalPhone(null)); + } +} \ No newline at end of file diff --git a/eatery-service/src/main/java/com/drinkool/entities/EateryInventoryItemEntity.java b/eatery-service/src/main/java/com/drinkool/entities/EateryInventoryItemEntity.java index 00244a1..334655b 100644 --- a/eatery-service/src/main/java/com/drinkool/entities/EateryInventoryItemEntity.java +++ b/eatery-service/src/main/java/com/drinkool/entities/EateryInventoryItemEntity.java @@ -7,15 +7,21 @@ import jakarta.transaction.Transactional; @Table public class EateryInventoryItemEntity extends BaseEntity { - public int quantity; + public double quantity; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "inventory_item_id") public InventoryItemEntity inventoryItem; + private EateryInventoryItemEntity() {} + // Create @Transactional - public void add(String name, int quantity, String unit) { + public static EateryInventoryItemEntity create( + String name, + double quantity, + String unit + ) { String itemName = name.toLowerCase(); InventoryItemEntity item = InventoryItemEntity.find( @@ -23,22 +29,22 @@ public class EateryInventoryItemEntity extends BaseEntity { itemName ).firstResult(); - if (item == null) { - if (unit != null) { + if (item == null || item.unit != unit) { + if (unit != null || unit == "") { item = InventoryItemEntity.create(itemName, unit); } else { item = InventoryItemEntity.create(itemName); } } - this.inventoryItem = item; - this.quantity = quantity; + EateryInventoryItemEntity eateryInventoryItem = + new EateryInventoryItemEntity(); - this.persist(); - } + eateryInventoryItem.quantity = quantity; + eateryInventoryItem.inventoryItem = item; - @Transactional - public void add(String name, int quantity) { - this.add(name, quantity, null); + eateryInventoryItem.persist(); + + return eateryInventoryItem; } } diff --git a/eatery-service/src/test/java/com/drinkool/entities/EateryInventoryItemEntityTest.java b/eatery-service/src/test/java/com/drinkool/entities/EateryInventoryItemEntityTest.java index 744af80..ca2717c 100644 --- a/eatery-service/src/test/java/com/drinkool/entities/EateryInventoryItemEntityTest.java +++ b/eatery-service/src/test/java/com/drinkool/entities/EateryInventoryItemEntityTest.java @@ -4,25 +4,11 @@ import static org.junit.jupiter.api.Assertions.*; import io.quarkus.test.junit.QuarkusTest; import jakarta.transaction.Transactional; -<<<<<<< HEAD -import org.junit.jupiter.api.BeforeEach; -======= ->>>>>>> origin/main import org.junit.jupiter.api.Test; @QuarkusTest public class EateryInventoryItemEntityTest { -<<<<<<< HEAD - private EateryInventoryItemEntity eateryItem; - - @BeforeEach - void setUp() { - eateryItem = new EateryInventoryItemEntity(); - } - -======= ->>>>>>> origin/main @Test @Transactional void testAdd_WhenItemExists_ShouldUseExistingItem() { @@ -31,15 +17,11 @@ public class EateryInventoryItemEntityTest { InventoryItemEntity.create(itemName); -<<<<<<< HEAD - eateryItem.add(itemName, quantity); -======= EateryInventoryItemEntity eateryItem = EateryInventoryItemEntity.create( itemName, quantity, "" ); ->>>>>>> origin/main EateryInventoryItemEntity savedEntity = EateryInventoryItemEntity.find( "id", @@ -55,15 +37,11 @@ public class EateryInventoryItemEntityTest { int quantity = 5; String unit = "Bottle"; -<<<<<<< HEAD - eateryItem.add(itemName, quantity, unit); -======= EateryInventoryItemEntity eateryItem = EateryInventoryItemEntity.create( itemName, quantity, unit ); ->>>>>>> origin/main EateryInventoryItemEntity savedEntity = EateryInventoryItemEntity.find( "id", diff --git a/eatery-service/src/test/java/com/drinkool/entities/InventoryItemEntityTest.java b/eatery-service/src/test/java/com/drinkool/entities/InventoryItemEntityTest.java new file mode 100644 index 0000000..6544fab --- /dev/null +++ b/eatery-service/src/test/java/com/drinkool/entities/InventoryItemEntityTest.java @@ -0,0 +1,44 @@ +package com.drinkool.entities; + +import io.quarkus.test.junit.QuarkusTest; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class InventoryItemEntityTest { + + @Test + @Transactional + public void testCreateInventoryItem() { + // 1. Chuẩn bị dữ liệu (Lưu ý: Không còn biến quantity nữa) + String name = "Trà sữa truyền thống"; + String unit = "Ly"; + + // 2. Gọi hàm static create (Không dùng new hay hàm add nữa) + // Hàm này vừa tạo object, vừa gán data, vừa gọi persist() luôn + InventoryItemEntity item = InventoryItemEntity.create(name, unit); + + // 3. Kiểm chứng + Assertions.assertNotNull(item, "Entity tạo ra không được null"); + Assertions.assertEquals(name, item.name, "Tên không khớp"); + Assertions.assertEquals(unit, item.unit, "Đơn vị không khớp"); + Assertions.assertNotNull(item.id, "ID phải được sinh ra tự động sau khi persist"); + } + + @Test + @Transactional + public void testCreateWithDefaultUnit() { + // 1. Chuẩn bị dữ liệu + String name = "Trân châu đen"; + + // 2. Gọi hàm static create rút gọn (chỉ truyền name, unit sẽ tự là "unit") + InventoryItemEntity item = InventoryItemEntity.create(name); + + // 3. Kiểm chứng + Assertions.assertNotNull(item, "Entity tạo ra không được null"); + Assertions.assertEquals(name, item.name, "Tên không khớp"); + Assertions.assertEquals("unit", item.unit, "Unit mặc định phải là 'unit'"); + Assertions.assertNotNull(item.id, "ID phải được sinh ra tự động sau khi persist"); + } +} \ No newline at end of file diff --git a/eatery-service/src/test/java/com/drinkool/entities/MenuItemEntityTest.java b/eatery-service/src/test/java/com/drinkool/entities/MenuItemEntityTest.java new file mode 100644 index 0000000..24adf80 --- /dev/null +++ b/eatery-service/src/test/java/com/drinkool/entities/MenuItemEntityTest.java @@ -0,0 +1,58 @@ +package com.drinkool.entities; + +import static org.junit.jupiter.api.Assertions.*; + +import io.quarkus.test.junit.QuarkusTest; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class MenuItemEntityTest { + + @Test + @Transactional + public void testCreateMenuItemSuccessfully() { + // 1. Chuẩn bị dữ liệu (Arrange) + String name = "Cà phê sữa đá"; + double price = 29000.0; + + // 2. Thực thi (Act) - Gọi hàm tĩnh create + MenuItemEntity menuItem = MenuItemEntity.create(name, price); + + // 3. Kiểm chứng (Assert) + assertNotNull(menuItem, "Thực thể MenuItem không được null"); + assertEquals(name, menuItem.name, "Tên món ăn không khớp"); + assertEquals(price, menuItem.price, "Giá món ăn không khớp"); + assertNotNull(menuItem.id, "ID phải được tự động sinh ra sau khi persist"); + assertTrue(menuItem.ingredients.isEmpty(), "Danh sách nguyên liệu ban đầu phải rỗng"); + } + + @Test + @Transactional + public void testAddIngredientSuccessfully() { + // 1. Chuẩn bị dữ liệu (Arrange) + // Tạo món ăn + MenuItemEntity menuItem = MenuItemEntity.create("Trà sữa trân châu", 45000.0); + + // Tạo nguyên liệu trong kho (Dùng đúng hàm add() của EateryInventoryItemEntity như đã fix) + EateryInventoryItemEntity inventoryItem = EateryInventoryItemEntity.create("Trân châu đen", 50, "kg"); + + double requiredQty = 0.2; // Cần 0.2 kg trân châu cho 1 ly + + // 2. Thực thi (Act) - Thêm nguyên liệu vào món ăn + menuItem.addIngredient(inventoryItem, requiredQty); + + // 3. Kiểm chứng (Assert) + // Kiểm tra danh sách nguyên liệu của món ăn + assertFalse(menuItem.ingredients.isEmpty(), "Danh sách nguyên liệu không được rỗng sau khi thêm"); + assertEquals(1, menuItem.ingredients.size(), "Phải có đúng 1 nguyên liệu trong danh sách"); + + // Lấy phần tử nguyên liệu đầu tiên ra để kiểm tra chi tiết các liên kết + MenuItemIngredientEntity addedIngredient = menuItem.ingredients.get(0); + + assertNotNull(addedIngredient.id, "ID của Entity trung gian phải được sinh ra"); + assertEquals(menuItem.id, addedIngredient.menuItem.id, "Khóa ngoại MenuItem bị sai"); + assertEquals(inventoryItem.id, addedIngredient.inventoryItem.id, "Khóa ngoại InventoryItem bị sai"); + assertEquals(requiredQty, addedIngredient.requiredQuantity, "Định lượng (requiredQty) không khớp"); + } +} \ No newline at end of file diff --git a/eatery-service/src/test/java/com/drinkool/entities/MenuItemIngredientEntityTest.java b/eatery-service/src/test/java/com/drinkool/entities/MenuItemIngredientEntityTest.java index 75624ba..b1252dc 100644 --- a/eatery-service/src/test/java/com/drinkool/entities/MenuItemIngredientEntityTest.java +++ b/eatery-service/src/test/java/com/drinkool/entities/MenuItemIngredientEntityTest.java @@ -9,50 +9,48 @@ import org.junit.jupiter.api.Test; @QuarkusTest public class MenuItemIngredientEntityTest { - @Test - @Transactional - void testCreate_ShouldPersistIngredientSuccessfully() { - // 1. Chuẩn bị dữ liệu giả định + @Test + @Transactional + void testCreate_ShouldPersistIngredientSuccessfully() { + // 1. Chuẩn bị dữ liệu giả định - String menuItemName = "Milk Tea"; - double menuItemPrice = 40; + String menuItemName = "Milk Tea"; + double menuItemPrice = 40; - MenuItemEntity menuItem = MenuItemEntity.create( - menuItemName, - menuItemPrice - ); + MenuItemEntity menuItem = MenuItemEntity.create( + menuItemName, + menuItemPrice + ); - String inventoryItemName = "Water"; - double inventoryItemQuantity = 10; - String inventoryItemUnit = "l"; + String inventoryItemName = "Water"; + int inventoryItemQuantity = 10; // Đổi thành int cho khớp với kiểu của EateryInventoryItemEntity + String inventoryItemUnit = "l"; - EateryInventoryItemEntity inventoryItem = EateryInventoryItemEntity.create( - inventoryItemName, - inventoryItemQuantity, - inventoryItemUnit - ); + // SỬA Ở ĐÂY: Khởi tạo bằng new và dùng hàm add() đúng như cấu trúc class của bạn + EateryInventoryItemEntity inventoryItem = EateryInventoryItemEntity.create(inventoryItemName, inventoryItemQuantity, inventoryItemUnit); + // Lưu ý: Hàm add() của bạn đã có sẵn lệnh this.persist() bên trong nên không cần gọi lại nữa! - double requiredQuantity = 0.5; + double requiredQuantity = 0.5; - // 2. Gọi hàm static create cần test - MenuItemIngredientEntity createdIngredient = - MenuItemIngredientEntity.create( - menuItem, - inventoryItem, - requiredQuantity - ); + // 2. Gọi hàm static create cần test + MenuItemIngredientEntity createdIngredient = + MenuItemIngredientEntity.create( + menuItem, + inventoryItem, + requiredQuantity + ); - // 3. Kiểm tra kết quả - assertNotNull(createdIngredient.id, "ID không được null sau khi persist"); + // 3. Kiểm tra kết quả + assertNotNull(createdIngredient.id, "ID không được null sau khi persist"); - // Truy vấn lại từ Database để đảm bảo dữ liệu đã được lưu đúng - MenuItemIngredientEntity savedEntity = MenuItemIngredientEntity.findById( - createdIngredient.id - ); + // Truy vấn lại từ Database để đảm bảo dữ liệu đã được lưu đúng + MenuItemIngredientEntity savedEntity = MenuItemIngredientEntity.findById( + createdIngredient.id + ); - assertNotNull(savedEntity); - assertEquals(menuItem.id, savedEntity.menuItem.id); - assertEquals(inventoryItem.id, savedEntity.inventoryItem.id); - assertEquals(requiredQuantity, savedEntity.requiredQuantity); - } -} + assertNotNull(savedEntity); + assertEquals(menuItem.id, savedEntity.menuItem.id); + assertEquals(inventoryItem.id, savedEntity.inventoryItem.id); + assertEquals(requiredQuantity, savedEntity.requiredQuantity); + } +} \ No newline at end of file diff --git a/eatery-service/src/test/java/com/drinkool/entities/ReviewEntityTest.java b/eatery-service/src/test/java/com/drinkool/entities/ReviewEntityTest.java new file mode 100644 index 0000000..8a956f1 --- /dev/null +++ b/eatery-service/src/test/java/com/drinkool/entities/ReviewEntityTest.java @@ -0,0 +1,35 @@ +package com.drinkool.entities; + +import com.drinkool.models.ReviewModel; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import java.util.UUID; + +@QuarkusTest +public class ReviewEntityTest { + + @Test + @Transactional + public void testCreateReview() { + // 1. Chuẩn bị dữ liệu (Arrange) + UUID testReviewerId = UUID.randomUUID(); + UUID testEateryId = UUID.randomUUID(); + + // Truyền trực tiếp rating và comment vào constructor của ReviewModel + ReviewModel inputModel = new ReviewModel(5, "Món ăn tuyệt vời!"); + + // 2. Thực thi (Act) + ReviewEntity savedReview = ReviewEntity.create(testReviewerId, testEateryId, inputModel); + + // 3. Kiểm chứng (Assert) + Assertions.assertNotNull(savedReview, "Thực thể Review sau khi tạo không được null"); + Assertions.assertEquals(testReviewerId, savedReview.reviewerId, "Reviewer ID không khớp"); + Assertions.assertEquals(testEateryId, savedReview.eateryId, "Eatery ID không khớp"); + Assertions.assertEquals(5, savedReview.rating, "Rating không khớp"); + Assertions.assertEquals("Món ăn tuyệt vời!", savedReview.comment, "Comment không khớp"); + + Assertions.assertNotNull(savedReview.id, "ID phải được tự động sinh ra sau khi persist"); + } +} \ No newline at end of file diff --git a/eatery-service/src/test/java/com/drinkool/services/ReviewServiceTest.java b/eatery-service/src/test/java/com/drinkool/services/ReviewServiceTest.java index e1b49ae..ab0bcdf 100644 --- a/eatery-service/src/test/java/com/drinkool/services/ReviewServiceTest.java +++ b/eatery-service/src/test/java/com/drinkool/services/ReviewServiceTest.java @@ -35,7 +35,7 @@ public class ReviewServiceTest { .body(input) .when() .post("/api/review/create") - .then(); + .then().statusCode(201); assertEquals(1, ReviewEntity.find("reviewerId", reviewerId).count()); } diff --git a/lib/src/main/java/com/drinkool/models/ReviewModel.java b/lib/src/main/java/com/drinkool/models/ReviewModel.java index dd38a24..7039015 100644 --- a/lib/src/main/java/com/drinkool/models/ReviewModel.java +++ b/lib/src/main/java/com/drinkool/models/ReviewModel.java @@ -13,11 +13,6 @@ public class ReviewModel extends BaseEntity { @Column public String comment; -<<<<<<< HEAD -======= - public ReviewModel() {} - ->>>>>>> origin/main public ReviewModel(int rating, String comment) { this.rating = rating; this.comment = comment;