From 3a35fb95d8cad37550a76339af2bf8df175e8ad7 Mon Sep 17 00:00:00 2001 From: azizbekxm Date: Thu, 9 Nov 2023 16:47:03 +0500 Subject: [PATCH] [MODAUD-174] - Fixed unit tests --- .../rest/impl/OrderEventsHandlerMockTest.java | 24 ++++------------ .../rest/impl/PieceEventsHandlerMockTest.java | 10 +++---- .../java/org/folio/utils/EntityUtils.java | 28 +++++++++++++++++-- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/mod-audit-server/src/test/java/org/folio/rest/impl/OrderEventsHandlerMockTest.java b/mod-audit-server/src/test/java/org/folio/rest/impl/OrderEventsHandlerMockTest.java index af96368b..31e321d9 100644 --- a/mod-audit-server/src/test/java/org/folio/rest/impl/OrderEventsHandlerMockTest.java +++ b/mod-audit-server/src/test/java/org/folio/rest/impl/OrderEventsHandlerMockTest.java @@ -3,7 +3,6 @@ import io.vertx.core.Future; import io.vertx.core.Vertx; import io.vertx.core.json.Json; -import io.vertx.core.json.JsonObject; import io.vertx.kafka.client.consumer.KafkaConsumerRecord; import io.vertx.kafka.client.consumer.impl.KafkaConsumerRecordImpl; @@ -22,10 +21,11 @@ import org.mockito.*; import java.nio.charset.StandardCharsets; -import java.util.Date; import java.util.UUID; import static org.folio.kafka.KafkaTopicNameHelper.getDefaultNameSpace; +import static org.folio.utils.EntityUtils.createOrderAuditEvent; +import static org.folio.utils.EntityUtils.createOrderAuditEventWithoutSnapshot; import static org.junit.Assert.assertTrue; public class OrderEventsHandlerMockTest { @@ -63,16 +63,7 @@ public void setUp() { @Test void shouldProcessEvent() { - JsonObject jsonObject = new JsonObject(); - jsonObject.put("Test","TestValue"); - - OrderAuditEvent orderAuditEvent = new OrderAuditEvent() - .withId(ID) - .withEventDate(new Date()) - .withOrderId(UUID.randomUUID().toString()) - .withActionDate(new Date()) - .withAction(OrderAuditEvent.Action.CREATE) - .withOrderSnapshot(jsonObject); + var orderAuditEvent = createOrderAuditEvent(UUID.randomUUID().toString()); KafkaConsumerRecord kafkaConsumerRecord = buildKafkaConsumerRecord(orderAuditEvent); Future saveFuture = orderEventsHandler.handle(kafkaConsumerRecord); @@ -83,14 +74,9 @@ void shouldProcessEvent() { @Test void shouldNotProcessEvent() { - OrderAuditEvent orderAuditEvent = new OrderAuditEvent() - .withId(UUID.randomUUID().toString()) - .withEventDate(new Date()) - .withOrderId(UUID.randomUUID().toString()) - .withActionDate(new Date()) - .withAction(OrderAuditEvent.Action.CREATE) - .withOrderSnapshot("Test"); + var orderAuditEvent = createOrderAuditEventWithoutSnapshot(); KafkaConsumerRecord kafkaConsumerRecord = buildKafkaConsumerRecord(orderAuditEvent); + Future save = orderEventsHandler.handle(kafkaConsumerRecord); assertTrue(save.failed()); } diff --git a/mod-audit-server/src/test/java/org/folio/rest/impl/PieceEventsHandlerMockTest.java b/mod-audit-server/src/test/java/org/folio/rest/impl/PieceEventsHandlerMockTest.java index a3c5787e..0afcf97d 100644 --- a/mod-audit-server/src/test/java/org/folio/rest/impl/PieceEventsHandlerMockTest.java +++ b/mod-audit-server/src/test/java/org/folio/rest/impl/PieceEventsHandlerMockTest.java @@ -3,6 +3,7 @@ import static org.folio.kafka.KafkaTopicNameHelper.getDefaultNameSpace; import static org.folio.utils.EntityUtils.createPieceAuditEvent; +import static org.folio.utils.EntityUtils.createPieceAuditEventWithoutSnapshot; import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.charset.StandardCharsets; @@ -59,9 +60,7 @@ public void setUp() throws Exception { @Test void shouldProcessEvent() { var pieceAuditEvent = createPieceAuditEvent(UUID.randomUUID().toString()); - KafkaConsumerRecord kafkaConsumerRecord = buildKafkaConsumerRecord(pieceAuditEvent); - Future saveFuture = pieceEventsHandler.handle(kafkaConsumerRecord); saveFuture.onComplete(are -> { assertTrue(are.succeeded()); @@ -70,11 +69,10 @@ void shouldProcessEvent() { @Test void shouldNotProcessEvent() { - var pieceAuditEvent = createPieceAuditEvent(UUID.randomUUID().toString()); - + var pieceAuditEvent = createPieceAuditEventWithoutSnapshot(); KafkaConsumerRecord kafkaConsumerRecord = buildKafkaConsumerRecord(pieceAuditEvent); - Future save = pieceEventsHandler.handle(kafkaConsumerRecord); - assertTrue(save.failed()); + Future saveFuture = pieceEventsHandler.handle(kafkaConsumerRecord); + assertTrue(saveFuture.failed()); } private KafkaConsumerRecord buildKafkaConsumerRecord(PieceAuditEvent event) { diff --git a/mod-audit-server/src/test/java/org/folio/utils/EntityUtils.java b/mod-audit-server/src/test/java/org/folio/utils/EntityUtils.java index 7fe5648b..d16a90be 100644 --- a/mod-audit-server/src/test/java/org/folio/utils/EntityUtils.java +++ b/mod-audit-server/src/test/java/org/folio/utils/EntityUtils.java @@ -17,7 +17,7 @@ public class EntityUtils { public static OrderAuditEvent createOrderAuditEvent(String id) { JsonObject jsonObject = new JsonObject(); - jsonObject.put("name","Test Product 123 "); + jsonObject.put("name", "Test Product 123 "); return new OrderAuditEvent() .withId(UUID.randomUUID().toString()) @@ -29,11 +29,22 @@ public static OrderAuditEvent createOrderAuditEvent(String id) { .withOrderSnapshot(jsonObject); } + public static OrderAuditEvent createOrderAuditEventWithoutSnapshot() { + return new OrderAuditEvent() + .withId(UUID.randomUUID().toString()) + .withAction(OrderAuditEvent.Action.CREATE) + .withOrderId(ORDER_ID) + .withUserId(UUID.randomUUID().toString()) + .withEventDate(new Date()) + .withActionDate(new Date()) + .withOrderSnapshot("Test"); + } + public static OrderLineAuditEvent createOrderLineAuditEvent(String id) { JsonObject jsonObject = new JsonObject(); jsonObject.put("name", "Test Product"); - return new OrderLineAuditEvent() + return new OrderLineAuditEvent() .withId(id) .withAction(OrderLineAuditEvent.Action.CREATE) .withOrderId(ORDER_ID) @@ -48,7 +59,7 @@ public static PieceAuditEvent createPieceAuditEvent(String id) { JsonObject jsonObject = new JsonObject(); jsonObject.put("name", "Test Product"); - return new PieceAuditEvent() + return new PieceAuditEvent() .withId(id) .withAction(PieceAuditEvent.Action.CREATE) .withPieceId(PIECE_ID) @@ -57,4 +68,15 @@ public static PieceAuditEvent createPieceAuditEvent(String id) { .withActionDate(new Date()) .withPieceSnapshot(jsonObject); } + + public static PieceAuditEvent createPieceAuditEventWithoutSnapshot() { + return new PieceAuditEvent() + .withId(UUID.randomUUID().toString()) + .withAction(PieceAuditEvent.Action.CREATE) + .withPieceId(PIECE_ID) + .withUserId(UUID.randomUUID().toString()) + .withEventDate(new Date()) + .withActionDate(new Date()) + .withPieceSnapshot("Test"); + } }