chore: update
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.drinkool.dtos;
|
||||
package com.drinkool.entities;
|
||||
|
||||
import io.quarkus.mongodb.panache.PanacheMongoEntity;
|
||||
import io.quarkus.mongodb.panache.common.MongoEntity;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.drinkool.dtos;
|
||||
package com.drinkool.entities;
|
||||
|
||||
import java.util.UUID;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -2,13 +2,14 @@ package com.drinkool.services;
|
||||
|
||||
import com.drinkool.InternalValue;
|
||||
import com.drinkool.controllers.EateryController;
|
||||
import com.drinkool.dtos.Cart;
|
||||
import com.drinkool.entities.Cart;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.util.UUID;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.eclipse.microprofile.graphql.GraphQLApi;
|
||||
import org.eclipse.microprofile.graphql.GraphQLException;
|
||||
import org.eclipse.microprofile.graphql.Mutation;
|
||||
@@ -56,13 +57,13 @@ public class CartService {
|
||||
}
|
||||
|
||||
@Query("getCart")
|
||||
public Cart getCart(@Name("cartId") UUID cartId) throws GraphQLException {
|
||||
public Cart getCart(@Name("cartId") ObjectId cartId) throws GraphQLException {
|
||||
return Cart.findById(cartId);
|
||||
}
|
||||
|
||||
@Mutation("addItem")
|
||||
public Uni<Cart> addItem(
|
||||
@Name("cartId") UUID cartId,
|
||||
@Name("cartId") ObjectId cartId,
|
||||
@Name("menuItemId") UUID menuItemId,
|
||||
@Name("quantity") Integer quantity
|
||||
) {
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.drinkool.services;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.drinkool.controllers.EateryController;
|
||||
import com.drinkool.dtos.Cart;
|
||||
import com.drinkool.entities.Cart;
|
||||
import com.drinkool.entities.CartItem;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
@@ -16,9 +14,10 @@ import io.vertx.core.http.HttpServerRequest;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.eclipse.microprofile.graphql.GraphQLException;
|
||||
import org.eclipse.microprofile.rest.client.inject.RestClient;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -37,6 +36,11 @@ public class CartServiceTest {
|
||||
@RestClient
|
||||
EateryController eateryController;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
Cart.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateCart_SuccessWithoutLogin() {
|
||||
UUID eateryId = UUID.randomUUID();
|
||||
@@ -45,54 +49,52 @@ public class CartServiceTest {
|
||||
Uni.createFrom().item(Response.ok().build())
|
||||
);
|
||||
|
||||
String cartId = cartService
|
||||
String cartIdStr = cartService
|
||||
.createCart(eateryId)
|
||||
.subscribe()
|
||||
.withSubscriber(UniAssertSubscriber.create())
|
||||
.awaitItem()
|
||||
.getItem();
|
||||
|
||||
assertNotNull(cartId);
|
||||
assertNotNull(cartIdStr);
|
||||
// Kiểm tra xem dữ liệu có thực sự nằm trong MongoDB không
|
||||
assertNotNull(Cart.findById((cartIdStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetCart_Success() throws Exception {
|
||||
UUID cartId = UUID.randomUUID();
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
UUID eateryId = UUID.randomUUID();
|
||||
String key = "cart:" + cartId;
|
||||
|
||||
Map<String, String> mockData = new HashMap<>();
|
||||
mockData.put("_metadata_owner", ownerId.toString());
|
||||
mockData.put("_metadata_eatery", eateryId.toString());
|
||||
mockData.put("product-1", "2");
|
||||
mockData.put("product-2", "5");
|
||||
UUID productId = UUID.randomUUID();
|
||||
|
||||
hashCommands.hset(key, mockData);
|
||||
Cart cart = new Cart();
|
||||
cart.userId = ownerId.toString();
|
||||
cart.eateryId = eateryId;
|
||||
cart.items = new ArrayList<>();
|
||||
cart.items.add(new CartItem(productId, 2));
|
||||
cart.items.add(new CartItem(UUID.randomUUID(), 5));
|
||||
|
||||
cart.persist();
|
||||
|
||||
ObjectId cartId = cart.id;
|
||||
|
||||
Cart result = cartService.getCart(cartId);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(cartId, result.getId());
|
||||
assertEquals(ownerId, result.getUserId());
|
||||
assertEquals(eateryId, result.getEateryId());
|
||||
assertEquals(cartId, result.id);
|
||||
assertEquals(ownerId.toString(), result.userId);
|
||||
assertEquals(2, result.items.size());
|
||||
|
||||
assertEquals(2, result.getItems().size());
|
||||
|
||||
boolean hasProduct1 = result
|
||||
.getItems()
|
||||
boolean hasProduct1 = result.items
|
||||
.stream()
|
||||
.anyMatch(
|
||||
item ->
|
||||
item.getProductId().equals("product-1") && item.getQuantity() == 2
|
||||
);
|
||||
|
||||
assertTrue(hasProduct1, "Phải chứa product-1 với số lượng 2");
|
||||
.anyMatch(item -> item.productId.equals(productId) && item.quantity == 2);
|
||||
assertTrue(hasProduct1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetCart_NotFound() {
|
||||
UUID nonExistentId = UUID.randomUUID();
|
||||
ObjectId nonExistentId = ObjectId.get();
|
||||
|
||||
GraphQLException exception = assertThrows(GraphQLException.class, () -> {
|
||||
cartService.getCart(nonExistentId);
|
||||
@@ -103,37 +105,35 @@ public class CartServiceTest {
|
||||
|
||||
@Test
|
||||
void testAddItem_Success() {
|
||||
UUID cartId = UUID.randomUUID();
|
||||
UUID eateryId = UUID.randomUUID();
|
||||
UUID menuItemId = UUID.randomUUID();
|
||||
String key = "cart:" + cartId;
|
||||
|
||||
hashCommands.hset(key, "_metadata_eatery", eateryId.toString());
|
||||
hashCommands.hset(key, "_metadata_owner", UUID.randomUUID().toString());
|
||||
Cart cart = new Cart();
|
||||
cart.eateryId = eateryId;
|
||||
cart.userId = UUID.randomUUID().toString();
|
||||
cart.items = new ArrayList<>();
|
||||
cart.persist();
|
||||
|
||||
ObjectId cartId = cart.id;
|
||||
|
||||
// 2. Mock API check món ăn
|
||||
when(eateryController.checkMenuItemExists(eateryId, menuItemId)).thenReturn(
|
||||
Uni.createFrom().item(Response.ok().build())
|
||||
);
|
||||
|
||||
// 3. Thực thi (Lưu ý: addItem trả về Uni nên cần await)
|
||||
Cart result = cartService
|
||||
.addItem(cartId, menuItemId, 2L)
|
||||
.addItem(cartId, menuItemId, 2)
|
||||
.await()
|
||||
.atMost(Duration.ofSeconds(5));
|
||||
|
||||
// 4. Kiểm tra kết quả trả về
|
||||
assertNotNull(result);
|
||||
assertEquals(cartId, result.getId());
|
||||
assertEquals(1, result.items.size());
|
||||
assertEquals(menuItemId, result.items.get(0).productId);
|
||||
assertEquals(2, result.items.get(0).quantity);
|
||||
|
||||
boolean hasItem = result
|
||||
.getItems()
|
||||
.stream()
|
||||
.anyMatch(
|
||||
item ->
|
||||
item.getProductId().equals(menuItemId.toString()) &&
|
||||
item.getQuantity() == 2
|
||||
);
|
||||
assertTrue(hasItem, "Sản phẩm phải có trong giỏ hàng sau khi thêm");
|
||||
|
||||
String quantityInRedis = hashCommands.hget(key, menuItemId.toString());
|
||||
assertEquals("2", quantityInRedis);
|
||||
Cart dbCart = Cart.findById(cartId);
|
||||
assertEquals(2, dbCart.items.get(0).quantity);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user