163 lines
4.3 KiB
Java
163 lines
4.3 KiB
Java
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.mockito.Mockito.when;
|
|
|
|
import com.drinkool.controllers.EateryController;
|
|
import com.drinkool.controllers.ManagerController;
|
|
import com.drinkool.dtos.Cart;
|
|
import io.quarkus.redis.datasource.RedisDataSource;
|
|
import io.quarkus.redis.datasource.hash.HashCommands;
|
|
import io.quarkus.redis.datasource.value.ValueCommands;
|
|
import io.quarkus.test.InjectMock;
|
|
import io.quarkus.test.junit.QuarkusTest;
|
|
import io.smallrye.mutiny.Uni;
|
|
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber;
|
|
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.UUID;
|
|
import org.eclipse.microprofile.graphql.GraphQLException;
|
|
import org.eclipse.microprofile.rest.client.inject.RestClient;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
@QuarkusTest
|
|
public class CartServiceTest {
|
|
|
|
@Inject
|
|
CartService cartService;
|
|
|
|
@InjectMock
|
|
HttpServerRequest request;
|
|
|
|
@InjectMock
|
|
@RestClient
|
|
EateryController eateryController;
|
|
|
|
|
|
@InjectMock
|
|
@RestClient
|
|
ManagerController managerController;
|
|
|
|
@Inject
|
|
RedisDataSource redisDataSource;
|
|
|
|
HashCommands<String, String, String> hashCommands;
|
|
ValueCommands<String, String> valueCommands;
|
|
|
|
@BeforeEach
|
|
void setup() {
|
|
redisDataSource.flushall();
|
|
|
|
hashCommands = redisDataSource.hash(String.class);
|
|
valueCommands = redisDataSource.value(String.class);
|
|
}
|
|
|
|
@Test
|
|
void testCreateCart_SuccessWithoutLogin() {
|
|
UUID eateryId = UUID.randomUUID();
|
|
|
|
when(eateryController.getEateryById(eateryId)).thenReturn(
|
|
Uni.createFrom().item(Response.ok(UUID.randomUUID()).build())
|
|
);
|
|
|
|
String cartId = cartService
|
|
.createCart(eateryId)
|
|
.subscribe()
|
|
.withSubscriber(UniAssertSubscriber.create())
|
|
.awaitItem()
|
|
.getItem();
|
|
|
|
assertNotNull(cartId);
|
|
}
|
|
|
|
@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");
|
|
|
|
hashCommands.hset(key, mockData);
|
|
|
|
Cart result = cartService.getCart(cartId);
|
|
|
|
assertNotNull(result);
|
|
assertEquals(cartId, result.getId());
|
|
assertEquals(ownerId, result.getUserId());
|
|
assertEquals(eateryId, result.getEateryId());
|
|
|
|
assertEquals(2, result.getItems().size());
|
|
|
|
boolean hasProduct1 = result
|
|
.getItems()
|
|
.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");
|
|
}
|
|
|
|
@Test
|
|
void testGetCart_NotFound() {
|
|
UUID nonExistentId = UUID.randomUUID();
|
|
|
|
GraphQLException exception = assertThrows(GraphQLException.class, () -> {
|
|
cartService.getCart(nonExistentId);
|
|
});
|
|
|
|
assertEquals("NotFoundCart", exception.getMessage());
|
|
}
|
|
|
|
@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());
|
|
|
|
when(eateryController.checkMenuItemExists(eateryId, menuItemId)).thenReturn(
|
|
Uni.createFrom().item(Response.ok(17.0).build())
|
|
);
|
|
|
|
Cart result = cartService
|
|
.addItem(cartId, menuItemId, 2L)
|
|
.await()
|
|
.atMost(Duration.ofSeconds(5));
|
|
|
|
assertNotNull(result);
|
|
assertEquals(cartId, result.getId());
|
|
|
|
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);
|
|
}
|
|
}
|