From 1229cec774b609fa7599dbd792629a0c2a15a5a6 Mon Sep 17 00:00:00 2001 From: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com> Date: Tue, 24 Mar 2026 09:56:27 +0000 Subject: [PATCH] chore: add test --- .devcontainer/devcontainer.json | 2 +- .../AuthenticationService.java} | 7 ++- eatery-service/pom.xml | 26 ++++----- .../main/java/com/drinkool/ReviewService.java | 53 ------------------- .../com/drinkool/services/ReviewService.java | 22 ++++++++ .../utils/SendReviewDeserializer.java | 13 ----- .../src/main/resources/application.properties | 21 +------- .../drinkool/MyMessagingApplicationTest.java | 18 ------- .../{ => services}/ReviewServiceTest.java | 34 ++++-------- .../src/test/resources/application.properties | 17 +----- .../com/drinkool/entities/BaseEntity.java | 4 +- 11 files changed, 54 insertions(+), 163 deletions(-) rename account-service/src/main/java/com/drinkool/{RestAPI.java => services/AuthenticationService.java} (93%) delete mode 100644 eatery-service/src/main/java/com/drinkool/ReviewService.java create mode 100644 eatery-service/src/main/java/com/drinkool/services/ReviewService.java delete mode 100644 eatery-service/src/main/java/com/drinkool/utils/SendReviewDeserializer.java delete mode 100644 eatery-service/src/test/java/com/drinkool/MyMessagingApplicationTest.java rename eatery-service/src/test/java/com/drinkool/{ => services}/ReviewServiceTest.java (54%) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 4f74317..900e04f 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -23,4 +23,4 @@ "containerEnv": { "TESTCONTAINERS_RYUK_DISABLED": "true" } -} +} \ No newline at end of file diff --git a/account-service/src/main/java/com/drinkool/RestAPI.java b/account-service/src/main/java/com/drinkool/services/AuthenticationService.java similarity index 93% rename from account-service/src/main/java/com/drinkool/RestAPI.java rename to account-service/src/main/java/com/drinkool/services/AuthenticationService.java index 22d1cd0..140ebde 100644 --- a/account-service/src/main/java/com/drinkool/RestAPI.java +++ b/account-service/src/main/java/com/drinkool/services/AuthenticationService.java @@ -1,16 +1,15 @@ -package com.drinkool; +package com.drinkool.services; import com.drinkool.dtos.*; import com.drinkool.entities.CustomerEntity; -import com.drinkool.entities.CustomerEntity; -import com.drinkool.services.*; + import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.ws.rs.*; import jakarta.ws.rs.core.*; @Path("/api") -public class RestAPI { +public class AuthenticationService { @Inject OtpService otpService; diff --git a/eatery-service/pom.xml b/eatery-service/pom.xml index 4d7c813..144a68b 100644 --- a/eatery-service/pom.xml +++ b/eatery-service/pom.xml @@ -1,8 +1,8 @@ - + 4.0.0 @@ -20,11 +20,9 @@ 3.15.0 21 UTF-8 - UTF-8 + UTF-8 quarkus-bom - io.quarkus.platform + io.quarkus.platform 3.32.4 true 3.5.4 @@ -49,8 +47,8 @@ ${project.version} - org.awaitility - awaitility + io.rest-assured + rest-assured test @@ -109,8 +107,7 @@ @{argLine} - org.jboss.logmanager.LogManager + org.jboss.logmanager.LogManager ${maven.home} @@ -131,8 +128,7 @@ ${project.build.directory}/${project.build.finalName}-runner - org.jboss.logmanager.LogManager + org.jboss.logmanager.LogManager ${maven.home} @@ -155,4 +151,4 @@ - + \ No newline at end of file diff --git a/eatery-service/src/main/java/com/drinkool/ReviewService.java b/eatery-service/src/main/java/com/drinkool/ReviewService.java deleted file mode 100644 index ea5d76f..0000000 --- a/eatery-service/src/main/java/com/drinkool/ReviewService.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.drinkool; - -import com.drinkool.dtos.SendReview; -import com.drinkool.entities.ReviewEntity; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.transaction.Transactional; -import org.eclipse.microprofile.reactive.messaging.Incoming; - -@ApplicationScoped -public class ReviewService { - - /** - * Injects an emitter to send messages to the "words-out" channel. - */ - // @Channel("words-out") - // Emitter emitter; - - // /** - // * Sends message to the "words-out" channel, can be used from a JAX-RS resource or any bean of your application. - // * Messages are sent to the broker. - // **/ - // void onStart(@Observes StartupEvent ev) { - // Stream.of("Hello", "with", "Quarkus", "Messaging", "message").forEach( - // string -> emitter.send(string) - // ); - // } - - /** - * Consume the message from the "words-in" channel, uppercase it and send it to the uppercase channel. - * This method is called by the framework when a message is received on the "words-in" channel (from the broker). - **/ - // @Incoming("words-in") - // @Outgoing("uppercase") - // public String toUpperCase(String message) { - // return message.toUpperCase(); - // } - - /** - * Consume the uppercase channel (coming from within the application) and print the messages to the console. - **/ - // @Incoming("uppercase") - // public void sink(String word) { - // System.out.println(">> " + word); - // } - - @Incoming("review") - @Transactional - public void receiveReview(SendReview input) { - ReviewEntity newReview = new ReviewEntity(); - - newReview.receive(input.reviewerId, input.orderId, input.review); - } -} diff --git a/eatery-service/src/main/java/com/drinkool/services/ReviewService.java b/eatery-service/src/main/java/com/drinkool/services/ReviewService.java new file mode 100644 index 0000000..4a774de --- /dev/null +++ b/eatery-service/src/main/java/com/drinkool/services/ReviewService.java @@ -0,0 +1,22 @@ +package com.drinkool.services; + +import com.drinkool.dtos.SendReview; +import com.drinkool.entities.ReviewEntity; +import jakarta.transaction.Transactional; +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.Response; + +@Path("/api") +public class ReviewService { + + @POST + @Path("/leaveReview") + @Transactional + public Response leaveReview(SendReview input) { + ReviewEntity newReview = new ReviewEntity(); + + newReview.receive(input.reviewerId, input.orderId, input.review); + + return Response.status(201).build(); + } +} diff --git a/eatery-service/src/main/java/com/drinkool/utils/SendReviewDeserializer.java b/eatery-service/src/main/java/com/drinkool/utils/SendReviewDeserializer.java deleted file mode 100644 index 9b0f920..0000000 --- a/eatery-service/src/main/java/com/drinkool/utils/SendReviewDeserializer.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.drinkool.utils; - -import com.drinkool.dtos.SendReview; -import io.quarkus.kafka.client.serialization.ObjectMapperDeserializer; - -public class SendReviewDeserializer - extends ObjectMapperDeserializer -{ - - public SendReviewDeserializer() { - super(SendReview.class); - } -} diff --git a/eatery-service/src/main/resources/application.properties b/eatery-service/src/main/resources/application.properties index a7a355d..88f3d6f 100644 --- a/eatery-service/src/main/resources/application.properties +++ b/eatery-service/src/main/resources/application.properties @@ -1,31 +1,14 @@ -# Cấu hình Database - # Kafka -# Địa chỉ server Kafka kafka.bootstrap.servers=localhost:9092 -# --- CẤU HÌNH GỬI TIN (OUTGOING) --- -# Kênh "eatery-out" sẽ gửi vào topic "eatery-events" -# mp.messaging.outgoing.eatery-out.connector=smallrye-kafka -# mp.messaging.outgoing.eatery-out.topic=eatery-events -# # Tự động biến Object thành JSON -# mp.messaging.outgoing.eatery-out.value.serializer=io.quarkus.kafka.client.serialization.ObjectMapperSerializer - -# --- CẤU HÌNH NHẬN TIN (INCOMING) --- -# Kênh "review-in" sẽ nhận từ topic "review-topic" -mp.messaging.incoming.review.connector=smallrye-kafka -mp.messaging.incoming.review.topic=review-topic -# Tự động biến JSON thành Object (giả sử là class ReviewModel) -mp.messaging.incoming.review.value.deserializer=com.drinkool.utils.SendReviewDeserializer - # Postgress -# Cấu hình Database +## Cấu hình Database quarkus.datasource.db-kind=postgresql quarkus.datasource.username=postgres quarkus.datasource.password=password quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/eatery -# Tự động tạo bảng từ Entity (chỉ dùng cho môi trường dev) +## Tự động tạo bảng từ Entity (chỉ dùng cho môi trường dev) quarkus.hibernate-orm.database.generation=update # Dependency diff --git a/eatery-service/src/test/java/com/drinkool/MyMessagingApplicationTest.java b/eatery-service/src/test/java/com/drinkool/MyMessagingApplicationTest.java deleted file mode 100644 index 3d49f1a..0000000 --- a/eatery-service/src/test/java/com/drinkool/MyMessagingApplicationTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.drinkool; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import io.quarkus.test.junit.QuarkusTest; -import jakarta.inject.Inject; -import org.junit.jupiter.api.Test; - -@QuarkusTest -class MyMessagingApplicationTest { - // @Inject - // MyMessagingApplication application; - // @Test - // void test() { - // assertEquals("HELLO", application.toUpperCase("Hello")); - // assertEquals("BONJOUR", application.toUpperCase("bonjour")); - // } -} diff --git a/eatery-service/src/test/java/com/drinkool/ReviewServiceTest.java b/eatery-service/src/test/java/com/drinkool/services/ReviewServiceTest.java similarity index 54% rename from eatery-service/src/test/java/com/drinkool/ReviewServiceTest.java rename to eatery-service/src/test/java/com/drinkool/services/ReviewServiceTest.java index fb4892e..4f788fe 100644 --- a/eatery-service/src/test/java/com/drinkool/ReviewServiceTest.java +++ b/eatery-service/src/test/java/com/drinkool/services/ReviewServiceTest.java @@ -1,30 +1,20 @@ -package com.drinkool; +package com.drinkool.services; +import static io.restassured.RestAssured.given; import static org.junit.jupiter.api.Assertions.*; import com.drinkool.dtos.SendReview; import com.drinkool.entities.ReviewEntity; import com.drinkool.models.ReviewModel; -import io.quarkus.narayana.jta.QuarkusTransaction; import io.quarkus.test.junit.QuarkusTest; -import jakarta.inject.Inject; +import io.restassured.http.ContentType; import java.util.Random; import java.util.UUID; -import java.util.concurrent.TimeUnit; -import org.awaitility.Awaitility; -import org.eclipse.microprofile.reactive.messaging.*; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; @QuarkusTest public class ReviewServiceTest { - @Inject - ReviewService service; - - @Inject - @Channel("review-test") - Emitter producer; - @Test void receiveReview() { Random random = new Random(); @@ -42,15 +32,13 @@ public class ReviewServiceTest { reviewModel.rating = rating; input.review = reviewModel; - producer.send(input); + given() + .contentType(ContentType.JSON) + .body(input) + .when() + .post("/api/leaveReview") + .then(); - Awaitility.await() - .atMost(5, TimeUnit.SECONDS) - .untilAsserted(() -> { - ReviewEntity saved = QuarkusTransaction.requiringNew().call(() -> - ReviewEntity.find("reviewerId", reviewerId).firstResult() - ); - assertNotNull(saved); - }); + assertEquals(1, ReviewEntity.find("reviewerId", reviewerId).count()); } } diff --git a/eatery-service/src/test/resources/application.properties b/eatery-service/src/test/resources/application.properties index 8c405bc..b3f5547 100644 --- a/eatery-service/src/test/resources/application.properties +++ b/eatery-service/src/test/resources/application.properties @@ -1,15 +1,2 @@ -# Kafka -kafka.bootstrap.servers=localhost:9092 - -# --- CẤU HÌNH GỬI TIN (OUTGOING) --- -# Kênh "eatery-out" sẽ gửi vào topic "eatery-events" -# mp.messaging.outgoing.eatery-out.connector=smallrye-kafka -# mp.messaging.outgoing.eatery-out.topic=eatery-events -# # Tự động biến Object thành JSON -# mp.messaging.outgoing.eatery-out.value.serializer=io.quarkus.kafka.client.serialization.ObjectMapperSerializer - -# --- CẤU HÌNH NHẬN TIN (INCOMING) --- -mp.messaging.outgoing.review-test.connector=smallrye-kafka -mp.messaging.outgoing.review-test.topic=review-topic -# Sửa serializer cho đúng loại dành cho việc GỬI -mp.messaging.outgoing.review-test.value.serializer=io.quarkus.kafka.client.serialization.ObjectMapperSerializer \ No newline at end of file +# Quarkus +quarkus.http.host=0.0.0.0 diff --git a/lib/src/main/java/com/drinkool/entities/BaseEntity.java b/lib/src/main/java/com/drinkool/entities/BaseEntity.java index a1c66a0..97e6206 100644 --- a/lib/src/main/java/com/drinkool/entities/BaseEntity.java +++ b/lib/src/main/java/com/drinkool/entities/BaseEntity.java @@ -5,8 +5,8 @@ import java.util.UUID; import io.quarkus.hibernate.orm.panache.PanacheEntityBase; -@Entity -public abstract class BaseEntity extends PanacheEntityBase { +@MappedSuperclass +public class BaseEntity extends PanacheEntityBase { @Id @GeneratedValue(strategy = GenerationType.UUID)