Skip to content

Commit

Permalink
[MODAUD-174] - Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Nov 9, 2023
1 parent 854e82a commit 3a35fb9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(orderAuditEvent);

Future<String> saveFuture = orderEventsHandler.handle(kafkaConsumerRecord);
Expand All @@ -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<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(orderAuditEvent);

Future<String> save = orderEventsHandler.handle(kafkaConsumerRecord);
assertTrue(save.failed());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,9 +60,7 @@ public void setUp() throws Exception {
@Test
void shouldProcessEvent() {
var pieceAuditEvent = createPieceAuditEvent(UUID.randomUUID().toString());

KafkaConsumerRecord<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(pieceAuditEvent);

Future<String> saveFuture = pieceEventsHandler.handle(kafkaConsumerRecord);
saveFuture.onComplete(are -> {
assertTrue(are.succeeded());
Expand All @@ -70,11 +69,10 @@ void shouldProcessEvent() {

@Test
void shouldNotProcessEvent() {
var pieceAuditEvent = createPieceAuditEvent(UUID.randomUUID().toString());

var pieceAuditEvent = createPieceAuditEventWithoutSnapshot();
KafkaConsumerRecord<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(pieceAuditEvent);
Future<String> save = pieceEventsHandler.handle(kafkaConsumerRecord);
assertTrue(save.failed());
Future<String> saveFuture = pieceEventsHandler.handle(kafkaConsumerRecord);
assertTrue(saveFuture.failed());
}

private KafkaConsumerRecord<String, String> buildKafkaConsumerRecord(PieceAuditEvent event) {
Expand Down
28 changes: 25 additions & 3 deletions mod-audit-server/src/test/java/org/folio/utils/EntityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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");
}
}

0 comments on commit 3a35fb9

Please sign in to comment.