From 85d044635951900042f14c4b0cddfe767c1c69c6 Mon Sep 17 00:00:00 2001 From: BKadirkhodjaev Date: Mon, 4 Nov 2024 12:32:57 +0500 Subject: [PATCH] [MODAUD-196]. Implement consumer & endpoint for invoice line records --- descriptors/ModuleDescriptor-template.json | 23 +++- .../dao/acquisition/InvoiceLineEventsDao.java | 32 +++++ .../impl/InvoiceLineEventsDaoImpl.java | 129 ++++++++++++++++++ .../rest/impl/AuditDataAcquisitionImpl.java | 21 +++ .../java/org/folio/rest/impl/InitAPIs.java | 10 +- .../InvoiceLineAuditEventsService.java | 31 +++++ .../InvoiceLineAuditEventsServiceImpl.java | 42 ++++++ .../org/folio/util/AcquisitionEventType.java | 3 +- .../org/folio/util/AuditEventDBConstants.java | 4 + .../InvoiceLineEventConsumersVerticle.java | 26 ++++ .../consumers/InvoiceLineEventsHandler.java | 62 +++++++++ ...ate_acquisition_invoice_line_log_table.sql | 12 ++ .../templates/db_scripts/schema.json | 5 + .../test/java/org/folio/CopilotGenerated.java | 25 ++++ .../src/test/java/org/folio/TestSuite.java | 15 ++ .../folio/dao/InvoiceLineEventsDaoTest.java | 80 +++++++++++ .../impl/AuditDataAcquisitionAPITest.java | 43 ++++++ .../InvoiceLineEventsHandlerMockTest.java | 88 ++++++++++++ .../InvoiceLineAuditEventsServiceTest.java | 64 +++++++++ .../java/org/folio/utils/EntityUtils.java | 18 +++ ramls/acquisition-events.raml | 33 +++++ ramls/invoice_line_audit_event.json | 44 ++++++ .../invoice_line_audit_event_collection.json | 25 ++++ 23 files changed, 832 insertions(+), 3 deletions(-) create mode 100644 mod-audit-server/src/main/java/org/folio/dao/acquisition/InvoiceLineEventsDao.java create mode 100644 mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/InvoiceLineEventsDaoImpl.java create mode 100644 mod-audit-server/src/main/java/org/folio/services/acquisition/InvoiceLineAuditEventsService.java create mode 100644 mod-audit-server/src/main/java/org/folio/services/acquisition/impl/InvoiceLineAuditEventsServiceImpl.java create mode 100644 mod-audit-server/src/main/java/org/folio/verticle/acquisition/InvoiceLineEventConsumersVerticle.java create mode 100644 mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/InvoiceLineEventsHandler.java create mode 100644 mod-audit-server/src/main/resources/templates/db_scripts/acquisition/create_acquisition_invoice_line_log_table.sql create mode 100644 mod-audit-server/src/test/java/org/folio/CopilotGenerated.java create mode 100644 mod-audit-server/src/test/java/org/folio/dao/InvoiceLineEventsDaoTest.java create mode 100644 mod-audit-server/src/test/java/org/folio/rest/impl/InvoiceLineEventsHandlerMockTest.java create mode 100644 mod-audit-server/src/test/java/org/folio/services/InvoiceLineAuditEventsServiceTest.java create mode 100644 ramls/invoice_line_audit_event.json create mode 100644 ramls/invoice_line_audit_event_collection.json diff --git a/descriptors/ModuleDescriptor-template.json b/descriptors/ModuleDescriptor-template.json index 9cadecb2..70e9915e 100644 --- a/descriptors/ModuleDescriptor-template.json +++ b/descriptors/ModuleDescriptor-template.json @@ -166,6 +166,21 @@ } ] }, + { + "id": "acquisition-invoice-line-events", + "version": "1.0", + "handlers": [ + { + "methods": [ + "GET" + ], + "pathPattern": "/audit-data/acquisition/invoice-line/{id}", + "permissionsRequired": [ + "acquisition.invoice-line.events.get" + ] + } + ] + }, { "id": "audit-data-event-handlers", "version": "1.1", @@ -264,6 +279,11 @@ "displayName": "Acquisition piece status change history events - get piece status change events", "description": "Get piece status change events" }, + { + "permissionName": "acquisition.invoice-line.events.get", + "displayName": "Acquisition invoice-line events - get invoice-line change events", + "description": "Get invoice-line change events" + }, { "permissionName": "audit.all", "displayName": "Audit - all permissions", @@ -278,7 +298,8 @@ "acquisition.order.events.get", "acquisition.order-line.events.get", "acquisition.piece.events.get", - "acquisition.piece.events.history.get" + "acquisition.piece.events.history.get", + "acquisition.invoice-line.events.get" ] } ], diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/InvoiceLineEventsDao.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/InvoiceLineEventsDao.java new file mode 100644 index 00000000..066c8954 --- /dev/null +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/InvoiceLineEventsDao.java @@ -0,0 +1,32 @@ +package org.folio.dao.acquisition; + +import io.vertx.core.Future; +import io.vertx.sqlclient.Row; +import io.vertx.sqlclient.RowSet; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEventCollection; + +public interface InvoiceLineEventsDao { + + /** + * Saves invoiceLineAuditEvent entity to DB + * + * @param invoiceLineAuditEvent InvoiceLineAuditEvent entity to save + * @param tenantId tenant id + * @return future with created row + */ + Future> save(InvoiceLineAuditEvent invoiceLineAuditEvent, String tenantId); + + /** + * Searches for invoice_line audit events by id + * + * @param invoiceLineId invoice_line id + * @param sortBy sort by + * @param sortOrder sort order + * @param limit limit + * @param offset offset + * @param tenantId tenant id + * @return future with InvoiceLineAuditEventCollection + */ + Future getAuditEventsByInvoiceLineId(String invoiceLineId, String sortBy, String sortOrder, int limit, int offset, String tenantId); +} diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/InvoiceLineEventsDaoImpl.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/InvoiceLineEventsDaoImpl.java new file mode 100644 index 00000000..ca42d9c5 --- /dev/null +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/InvoiceLineEventsDaoImpl.java @@ -0,0 +1,129 @@ +package org.folio.dao.acquisition.impl; + +import static java.lang.String.format; +import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD; +import static org.folio.util.AuditEventDBConstants.ACTION_FIELD; +import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD; +import static org.folio.util.AuditEventDBConstants.ID_FIELD; +import static org.folio.util.AuditEventDBConstants.INVOICE_ID_FIELD; +import static org.folio.util.AuditEventDBConstants.INVOICE_LINE_ID_FIELD; +import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD; +import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN; +import static org.folio.util.AuditEventDBConstants.TOTAL_RECORDS_FIELD; +import static org.folio.util.AuditEventDBConstants.USER_ID_FIELD; +import static org.folio.util.DbUtils.formatDBTableName; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.util.Date; +import java.util.UUID; + +import io.vertx.core.Future; +import io.vertx.core.Promise; +import io.vertx.core.json.JsonObject; +import io.vertx.sqlclient.Row; +import io.vertx.sqlclient.RowSet; +import io.vertx.sqlclient.Tuple; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.folio.dao.acquisition.InvoiceLineEventsDao; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEventCollection; +import org.folio.util.PostgresClientFactory; +import org.springframework.stereotype.Repository; + +@Repository +public class InvoiceLineEventsDaoImpl implements InvoiceLineEventsDao { + + private static final Logger LOGGER = LogManager.getLogger(); + + public static final String TABLE_NAME = "acquisition_invoice_line_log"; + + public static final String GET_BY_INVOICE_LINE_ID_SQL = "SELECT id, action, invoice_id, invoice_line_id, user_id, event_date, action_date, modified_content_snapshot," + + " (SELECT count(*) AS total_records FROM %s WHERE invoice_line_id = $1) " + + " FROM %s WHERE invoice_line_id = $1 %s LIMIT $2 OFFSET $3"; + + private static final String INSERT_SQL = "INSERT INTO %s (id, action, invoice_id, invoice_line_id, user_id, event_date, action_date, modified_content_snapshot) " + + "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"; + + private final PostgresClientFactory pgClientFactory; + + public InvoiceLineEventsDaoImpl(PostgresClientFactory pgClientFactory) { + this.pgClientFactory = pgClientFactory; + } + + @Override + public Future> save(InvoiceLineAuditEvent invoiceLineAuditEvent, String tenantId) { + LOGGER.debug("save:: Saving InvoiceLine AuditEvent with tenant id : {}", tenantId); + Promise> promise = Promise.promise(); + LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); + String logTable = formatDBTableName(tenantId, TABLE_NAME); + String query = format(INSERT_SQL, logTable); + makeSaveCall(promise, query, invoiceLineAuditEvent, tenantId); + LOGGER.info("save:: Saved InvoiceLine AuditEvent with tenant id : {}", tenantId); + return promise.future(); + } + + @Override + public Future getAuditEventsByInvoiceLineId(String invoiceLineId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { + LOGGER.debug("getAuditEventsByInvoiceLineId:: Retrieving AuditEvent with invoice line id : {}", invoiceLineId); + Promise> promise = Promise.promise(); + try { + LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); + String logTable = formatDBTableName(tenantId, TABLE_NAME); + String query = format(GET_BY_INVOICE_LINE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder)); + Tuple queryParams = Tuple.of(UUID.fromString(invoiceLineId), limit, offset); + pgClientFactory.createInstance(tenantId).selectRead(query, queryParams, promise); + } catch (Exception e) { + LOGGER.warn("Error getting invoice line audit events by invoice line id: {}", invoiceLineId, e); + promise.fail(e); + } + + LOGGER.info("getAuditEventsByInvoiceLineId:: Retrieved AuditEvent with invoice line id : {}", invoiceLineId); + return promise.future().map(rowSet -> rowSet.rowCount() == 0 ? new InvoiceLineAuditEventCollection().withTotalItems(0) + : mapRowToListOfInvoiceLineEvent(rowSet)); + } + + private void makeSaveCall(Promise> promise, String query, InvoiceLineAuditEvent invoiceLineAuditEvent, String tenantId) { + LOGGER.debug("makeSaveCall:: Making save call with query : {} and tenant id : {}", query, tenantId); + try { + pgClientFactory.createInstance(tenantId).execute(query, Tuple.of(invoiceLineAuditEvent.getId(), + invoiceLineAuditEvent.getAction(), + invoiceLineAuditEvent.getInvoiceId(), + invoiceLineAuditEvent.getInvoiceLineId(), + invoiceLineAuditEvent.getUserId(), + LocalDateTime.ofInstant(invoiceLineAuditEvent.getEventDate().toInstant(), ZoneId.systemDefault()), + LocalDateTime.ofInstant(invoiceLineAuditEvent.getActionDate().toInstant(), ZoneId.systemDefault()), + JsonObject.mapFrom(invoiceLineAuditEvent.getInvoiceLineSnapshot())), promise); + } catch (Exception e) { + LOGGER.error("Failed to save record with id: {} for invoice line id: {} in to table {}", + invoiceLineAuditEvent.getId(), invoiceLineAuditEvent.getInvoiceLineId(), TABLE_NAME, e); + promise.fail(e); + } + } + + private InvoiceLineAuditEventCollection mapRowToListOfInvoiceLineEvent(RowSet rowSet) { + LOGGER.debug("mapRowToListOfInvoiceLineEvent:: Mapping row to List of Invoice Line Events"); + InvoiceLineAuditEventCollection invoiceLineAuditEventCollection = new InvoiceLineAuditEventCollection(); + rowSet.iterator().forEachRemaining(row -> { + invoiceLineAuditEventCollection.getInvoiceLineAuditEvents().add(mapRowToInvoiceLineEvent(row)); + invoiceLineAuditEventCollection.setTotalItems(row.getInteger(TOTAL_RECORDS_FIELD)); + }); + LOGGER.debug("mapRowToListOfInvoiceLineEvent:: Mapped row to List of Invoice Line Events"); + return invoiceLineAuditEventCollection; + } + + private InvoiceLineAuditEvent mapRowToInvoiceLineEvent(Row row) { + LOGGER.debug("mapRowToInvoiceLineEvent:: Mapping row to Invoice Line Event"); + return new InvoiceLineAuditEvent() + .withId(row.getValue(ID_FIELD).toString()) + .withAction(row.get(InvoiceLineAuditEvent.Action.class, ACTION_FIELD)) + .withInvoiceId(row.getValue(INVOICE_ID_FIELD).toString()) + .withInvoiceLineId(row.getValue(INVOICE_LINE_ID_FIELD).toString()) + .withUserId(row.getValue(USER_ID_FIELD).toString()) + .withEventDate(Date.from(row.getLocalDateTime(EVENT_DATE_FIELD).toInstant(ZoneOffset.UTC))) + .withActionDate(Date.from(row.getLocalDateTime(ACTION_DATE_FIELD).toInstant(ZoneOffset.UTC))) + .withInvoiceLineSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD))); + } +} diff --git a/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java b/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java index f405be20..57ede443 100644 --- a/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java +++ b/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java @@ -7,12 +7,14 @@ import io.vertx.core.Future; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.folio.rest.jaxrs.model.AuditDataAcquisitionInvoiceLineIdGetSortOrder; import org.folio.rest.jaxrs.model.AuditDataAcquisitionOrderIdGetSortOrder; import org.folio.rest.jaxrs.model.AuditDataAcquisitionOrderLineIdGetSortOrder; import org.folio.rest.jaxrs.model.AuditDataAcquisitionPieceIdGetSortOrder; import org.folio.rest.jaxrs.model.AuditDataAcquisitionPieceIdStatusChangeHistoryGetSortOrder; import org.folio.rest.jaxrs.resource.AuditDataAcquisition; import org.folio.rest.tools.utils.TenantTool; +import org.folio.services.acquisition.InvoiceLineAuditEventsService; import org.folio.services.acquisition.OrderAuditEventsService; import org.folio.services.acquisition.OrderLineAuditEventsService; import org.folio.services.acquisition.PieceAuditEventsService; @@ -35,6 +37,8 @@ public class AuditDataAcquisitionImpl implements AuditDataAcquisition { private OrderLineAuditEventsService orderLineAuditEventsService; @Autowired private PieceAuditEventsService pieceAuditEventsService; + @Autowired + private InvoiceLineAuditEventsService invoiceLineAuditEventsService; public AuditDataAcquisitionImpl() { SpringContextUtil.autowireDependencies(this, Vertx.currentContext()); @@ -110,6 +114,23 @@ public void getAuditDataAcquisitionPieceStatusChangeHistoryById(String pieceId, } } + @Override + public void getAuditDataAcquisitionInvoiceLineById(String invoiceLineId, String sortBy, AuditDataAcquisitionInvoiceLineIdGetSortOrder sortOrder, + int limit, int offset, Map okapiHeaders, Handler> asyncResultHandler, Context vertxContext) { + LOGGER.debug("getAuditDataAcquisitionInvoiceLineById:: Retrieving Audit Data Acquisition Invoice Line By Id : {}", invoiceLineId); + String tenantId = TenantTool.tenantId(okapiHeaders); + try { + invoiceLineAuditEventsService.getAuditEventsByInvoiceLineId(invoiceLineId, sortBy, sortOrder.name(), limit, offset, tenantId) + .map(GetAuditDataAcquisitionInvoiceLineByIdResponse::respond200WithApplicationJson) + .map(Response.class::cast) + .otherwise(this::mapExceptionToResponse) + .onComplete(asyncResultHandler); + } catch (Exception e) { + LOGGER.error("Failed to get order line audit events by order line id: {}", invoiceLineId, e); + asyncResultHandler.handle(Future.succeededFuture(mapExceptionToResponse(e))); + } + } + private Response mapExceptionToResponse(Throwable throwable) { LOGGER.debug("mapExceptionToResponse:: Mapping Exception :{} to Response", throwable.getMessage(), throwable); return GetAuditDataAcquisitionOrderByIdResponse diff --git a/mod-audit-server/src/main/java/org/folio/rest/impl/InitAPIs.java b/mod-audit-server/src/main/java/org/folio/rest/impl/InitAPIs.java index 7a5028d7..e6fcaf5c 100644 --- a/mod-audit-server/src/main/java/org/folio/rest/impl/InitAPIs.java +++ b/mod-audit-server/src/main/java/org/folio/rest/impl/InitAPIs.java @@ -15,6 +15,7 @@ import org.folio.rest.resource.interfaces.InitAPI; import org.folio.spring.SpringContextUtil; import org.folio.verticle.SpringVerticleFactory; +import org.folio.verticle.acquisition.InvoiceLineEventConsumersVerticle; import org.folio.verticle.acquisition.OrderEventConsumersVerticle; import org.folio.verticle.acquisition.OrderLineEventConsumersVerticle; import org.folio.verticle.acquisition.PieceEventConsumersVerticle; @@ -64,6 +65,7 @@ private Future deployConsumersVerticles(Vertx vertx) { Promise orderEventsConsumer = Promise.promise(); Promise orderLineEventsConsumer = Promise.promise(); Promise pieceEventsConsumer = Promise.promise(); + Promise invoiceLineEventsConsumer = Promise.promise(); vertx.deployVerticle(getVerticleName(verticleFactory, OrderEventConsumersVerticle.class), new DeploymentOptions() @@ -80,11 +82,17 @@ private Future deployConsumersVerticles(Vertx vertx) { .setWorker(true) .setInstances(acqPieceConsumerInstancesNumber), pieceEventsConsumer); + vertx.deployVerticle(getVerticleName(verticleFactory, InvoiceLineEventConsumersVerticle.class), + new DeploymentOptions() + .setWorker(true) + .setInstances(acqPieceConsumerInstancesNumber), invoiceLineEventsConsumer); + LOGGER.info("deployConsumersVerticles:: All consumer verticles were successfully deployed"); return GenericCompositeFuture.all(Arrays.asList( orderEventsConsumer.future(), orderLineEventsConsumer.future(), - pieceEventsConsumer.future())); + pieceEventsConsumer.future(), + invoiceLineEventsConsumer.future())); } private String getVerticleName(VerticleFactory verticleFactory, Class clazz) { diff --git a/mod-audit-server/src/main/java/org/folio/services/acquisition/InvoiceLineAuditEventsService.java b/mod-audit-server/src/main/java/org/folio/services/acquisition/InvoiceLineAuditEventsService.java new file mode 100644 index 00000000..f096c7dc --- /dev/null +++ b/mod-audit-server/src/main/java/org/folio/services/acquisition/InvoiceLineAuditEventsService.java @@ -0,0 +1,31 @@ +package org.folio.services.acquisition; + +import io.vertx.core.Future; +import io.vertx.sqlclient.Row; +import io.vertx.sqlclient.RowSet; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEventCollection; + +public interface InvoiceLineAuditEventsService { + + /** + * Saves InvoiceLineAuditEvent + * + * @param invoiceLineAuditEvent invoice line event to save + * @param tenantId id of tenant + * @return successful future if event has not been processed, or failed future otherwise + */ + Future> saveInvoiceLineAuditEvent(InvoiceLineAuditEvent invoiceLineAuditEvent, String tenantId); + + /** + * Searches for invoice line audit events by invoice line id + * + * @param invoiceLineId invoice line id + * @param sortBy sort by + * @param sortOrder sort order + * @param limit limit + * @param offset offset + * @return future with InvoiceLineAuditEventCollection + */ + Future getAuditEventsByInvoiceLineId(String invoiceLineId, String sortBy, String sortOrder, int limit, int offset, String tenantId); +} diff --git a/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/InvoiceLineAuditEventsServiceImpl.java b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/InvoiceLineAuditEventsServiceImpl.java new file mode 100644 index 00000000..31c5f38e --- /dev/null +++ b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/InvoiceLineAuditEventsServiceImpl.java @@ -0,0 +1,42 @@ +package org.folio.services.acquisition.impl; + +import static org.folio.util.ErrorUtils.handleFailures; + +import io.vertx.core.Future; +import io.vertx.sqlclient.Row; +import io.vertx.sqlclient.RowSet; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.folio.dao.acquisition.InvoiceLineEventsDao; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEventCollection; +import org.folio.services.acquisition.InvoiceLineAuditEventsService; +import org.springframework.stereotype.Service; + +@Service +public class InvoiceLineAuditEventsServiceImpl implements InvoiceLineAuditEventsService { + + private static final Logger LOGGER = LogManager.getLogger(); + + private final InvoiceLineEventsDao invoiceLineEventsDao; + + public InvoiceLineAuditEventsServiceImpl(InvoiceLineEventsDao invoiceLineEventsDao) { + this.invoiceLineEventsDao = invoiceLineEventsDao; + } + + @Override + public Future> saveInvoiceLineAuditEvent(InvoiceLineAuditEvent invoiceLineAuditEvent, String tenantId) { + LOGGER.debug("saveInvoiceLineAuditEvent:: Saving invoice line audit event with id: {} in tenant Id : {}", invoiceLineAuditEvent.getId(), tenantId); + return invoiceLineEventsDao.save(invoiceLineAuditEvent, tenantId) + .recover(throwable -> { + LOGGER.error("handleFailures:: Could not save invoice audit event for InvoiceLine id: {} in tenantId: {}", invoiceLineAuditEvent.getInvoiceLineId(), tenantId); + return handleFailures(throwable, invoiceLineAuditEvent.getId()); + }); + } + + @Override + public Future getAuditEventsByInvoiceLineId(String invoiceLineId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { + LOGGER.debug("getAuditEventsByInvoiceLineId:: Retrieving audit events for invoice line Id : {} and tenant Id : {}", invoiceLineId, tenantId); + return invoiceLineEventsDao.getAuditEventsByInvoiceLineId(invoiceLineId, sortBy, sortOrder, limit, offset, tenantId); + } +} diff --git a/mod-audit-server/src/main/java/org/folio/util/AcquisitionEventType.java b/mod-audit-server/src/main/java/org/folio/util/AcquisitionEventType.java index 3ecb182e..3a863ce8 100644 --- a/mod-audit-server/src/main/java/org/folio/util/AcquisitionEventType.java +++ b/mod-audit-server/src/main/java/org/folio/util/AcquisitionEventType.java @@ -3,7 +3,8 @@ public enum AcquisitionEventType { ACQ_ORDER_CHANGED("ACQ_ORDER_CHANGED"), ACQ_ORDER_LINE_CHANGED("ACQ_ORDER_LINE_CHANGED"), - ACQ_PIECE_CHANGED("ACQ_PIECE_CHANGED"); + ACQ_PIECE_CHANGED("ACQ_PIECE_CHANGED"), + ACQ_INVOICE_LINE_CHANGED("ACQ_INVOICE_LINE_CHANGED") ; private final String topicName; diff --git a/mod-audit-server/src/main/java/org/folio/util/AuditEventDBConstants.java b/mod-audit-server/src/main/java/org/folio/util/AuditEventDBConstants.java index 62d40cfc..27f0cbca 100644 --- a/mod-audit-server/src/main/java/org/folio/util/AuditEventDBConstants.java +++ b/mod-audit-server/src/main/java/org/folio/util/AuditEventDBConstants.java @@ -14,6 +14,10 @@ private AuditEventDBConstants() {} public static final String PIECE_ID_FIELD = "piece_id"; + public static final String INVOICE_ID_FIELD = "invoice_id"; + + public static final String INVOICE_LINE_ID_FIELD = "invoice_line_id"; + public static final String USER_ID_FIELD = "user_id"; public static final String EVENT_DATE_FIELD = "event_date"; diff --git a/mod-audit-server/src/main/java/org/folio/verticle/acquisition/InvoiceLineEventConsumersVerticle.java b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/InvoiceLineEventConsumersVerticle.java new file mode 100644 index 00000000..ff371601 --- /dev/null +++ b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/InvoiceLineEventConsumersVerticle.java @@ -0,0 +1,26 @@ +package org.folio.verticle.acquisition; + +import org.folio.kafka.AsyncRecordHandler; +import org.folio.util.AcquisitionEventType; +import org.folio.verticle.AbstractConsumersVerticle; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class InvoiceLineEventConsumersVerticle extends AbstractConsumersVerticle { + + @Autowired + private AsyncRecordHandler invoiceLineEventsHandler; + + @Override + public List getEvents() { + return List.of(AcquisitionEventType.ACQ_INVOICE_LINE_CHANGED.getTopicName()); + } + + @Override + public AsyncRecordHandler getHandler() { + return invoiceLineEventsHandler; + } +} diff --git a/mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/InvoiceLineEventsHandler.java b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/InvoiceLineEventsHandler.java new file mode 100644 index 00000000..538ac0e4 --- /dev/null +++ b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/InvoiceLineEventsHandler.java @@ -0,0 +1,62 @@ +package org.folio.verticle.acquisition.consumers; + +import io.vertx.core.Future; +import io.vertx.core.Promise; +import io.vertx.core.Vertx; +import io.vertx.core.json.JsonObject; +import io.vertx.kafka.client.consumer.KafkaConsumerRecord; +import io.vertx.kafka.client.producer.KafkaHeader; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.folio.kafka.AsyncRecordHandler; +import org.folio.kafka.KafkaHeaderUtils; +import org.folio.kafka.exception.DuplicateEventException; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.util.OkapiConnectionParams; +import org.folio.services.acquisition.InvoiceLineAuditEventsService; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class InvoiceLineEventsHandler implements AsyncRecordHandler { + + private static final Logger LOGGER = LogManager.getLogger(); + + private final InvoiceLineAuditEventsService invoiceLineAuditEventsService; + private final Vertx vertx; + + public InvoiceLineEventsHandler(Vertx vertx, + InvoiceLineAuditEventsService invoiceLineAuditEventsService) { + this.vertx = vertx; + this.invoiceLineAuditEventsService = invoiceLineAuditEventsService; + } + + @Override + public Future handle(KafkaConsumerRecord kafkaConsumerRecord) { + Promise result = Promise.promise(); + List kafkaHeaders = kafkaConsumerRecord.headers(); + OkapiConnectionParams okapiConnectionParams = new OkapiConnectionParams(KafkaHeaderUtils.kafkaHeadersToMap(kafkaHeaders), vertx); + InvoiceLineAuditEvent event = new JsonObject(kafkaConsumerRecord.value()).mapTo(InvoiceLineAuditEvent.class); + LOGGER.info("handle:: Starting processing of Invoice Line audit event with id: {} for invoice line id: {}", + event.getId(), event.getInvoiceLineId()); + + invoiceLineAuditEventsService.saveInvoiceLineAuditEvent(event, okapiConnectionParams.getTenantId()) + .onSuccess(ar -> { + LOGGER.info("handle:: Invoice Line audit event with id: {} has been processed for invoice line id: {}", + event.getId(), event.getInvoiceLineId()); + result.complete(event.getId()); + }) + .onFailure(e -> { + if (e instanceof DuplicateEventException) { + LOGGER.info("handle:: Duplicate Invoice Line audit event with id: {} for invoice line id: {} received, skipped processing", event.getId(), event.getInvoiceLineId()); + result.complete(event.getId()); + } else { + LOGGER.error("Processing of Invoice Line audit event with id: {} for invoice line id: {} has been failed", event.getId(), event.getInvoiceLineId(), e); + result.fail(e); + } + }); + + return result.future(); + } +} diff --git a/mod-audit-server/src/main/resources/templates/db_scripts/acquisition/create_acquisition_invoice_line_log_table.sql b/mod-audit-server/src/main/resources/templates/db_scripts/acquisition/create_acquisition_invoice_line_log_table.sql new file mode 100644 index 00000000..3bfce0c6 --- /dev/null +++ b/mod-audit-server/src/main/resources/templates/db_scripts/acquisition/create_acquisition_invoice_line_log_table.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS acquisition_invoice_line_log ( + id uuid PRIMARY KEY, + action text NOT NULL, + invoice_id uuid NOT NULL, + invoice_line_id uuid NOT NULL, + user_id uuid NOT NULL, + event_date timestamp NOT NULL, + action_date timestamp NOT NULL, + modified_content_snapshot jsonb +); + +CREATE INDEX IF NOT EXISTS invoice_line_id_index ON acquisition_invoice_line_log USING BTREE (invoice_line_id); diff --git a/mod-audit-server/src/main/resources/templates/db_scripts/schema.json b/mod-audit-server/src/main/resources/templates/db_scripts/schema.json index be4d0804..97eb2cc8 100644 --- a/mod-audit-server/src/main/resources/templates/db_scripts/schema.json +++ b/mod-audit-server/src/main/resources/templates/db_scripts/schema.json @@ -79,6 +79,11 @@ "run": "after", "snippetPath": "acquisition/create_acquisition_piece_log_table.sql", "fromModuleVersion": "mod-audit-2.9.0" + }, + { + "run": "after", + "snippetPath": "acquisition/create_acquisition_invoice_line_log_table.sql", + "fromModuleVersion": "mod-audit-2.10.1" } ] } diff --git a/mod-audit-server/src/test/java/org/folio/CopilotGenerated.java b/mod-audit-server/src/test/java/org/folio/CopilotGenerated.java new file mode 100644 index 00000000..590f0c49 --- /dev/null +++ b/mod-audit-server/src/test/java/org/folio/CopilotGenerated.java @@ -0,0 +1,25 @@ +package org.folio; + +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to indicate that test(s) in the class are generated by GitHub Copilot. + *

+ * Set value or partiallyGenerated attribute to true + * if the generated test(s) were significantly modified/altered by the developer. + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.SOURCE) +public @interface CopilotGenerated { + + @AliasFor("partiallyGenerated") + boolean value() default false; + + @AliasFor("value") + boolean partiallyGenerated() default false; +} diff --git a/mod-audit-server/src/test/java/org/folio/TestSuite.java b/mod-audit-server/src/test/java/org/folio/TestSuite.java index 797a0916..a7cad1fe 100644 --- a/mod-audit-server/src/test/java/org/folio/TestSuite.java +++ b/mod-audit-server/src/test/java/org/folio/TestSuite.java @@ -23,6 +23,7 @@ import org.folio.builder.service.ManualBlockRecordBuilderTest; import org.folio.builder.service.NoticeRecordBuilderTest; import org.folio.builder.service.RequestRecordBuilderTest; +import org.folio.dao.InvoiceLineEventsDaoTest; import org.folio.dao.OrderEventsDaoTest; import org.folio.dao.OrderLineEventsDaoTest; import org.folio.dao.PieceEventsDaoTest; @@ -32,10 +33,12 @@ import org.folio.rest.impl.AuditDataImplApiTest; import org.folio.rest.impl.AuditHandlersImplApiTest; import org.folio.rest.impl.CirculationLogsImplApiTest; +import org.folio.rest.impl.InvoiceLineEventsHandlerMockTest; import org.folio.rest.impl.OrderEventsHandlerMockTest; import org.folio.rest.impl.OrderLineEventsHandlerMockTest; import org.folio.rest.impl.PieceEventsHandlerMockTest; import org.folio.rest.persist.PostgresClient; +import org.folio.services.InvoiceLineAuditEventsServiceTest; import org.folio.services.OrderAuditEventsServiceTest; import org.folio.services.OrderLineAuditEventsServiceTest; import org.folio.services.PieceAuditEventsServiceTest; @@ -194,6 +197,18 @@ class PieceAuditEventsServiceNestedTest extends PieceAuditEventsServiceTest { class PieceEventsHandlerMockNestedTest extends PieceEventsHandlerMockTest { } + @Nested + class InvoiceLineEventsHandlerMockNestedTest extends InvoiceLineEventsHandlerMockTest { + } + + @Nested + class InvoiceLineAuditEventsServiceNestedTest extends InvoiceLineAuditEventsServiceTest { + } + + @Nested + class InvoiceLineEventsDaoNestedTest extends InvoiceLineEventsDaoTest { + } + @Nested class AuditDataImplApiTestNested extends AuditDataImplApiTest { } diff --git a/mod-audit-server/src/test/java/org/folio/dao/InvoiceLineEventsDaoTest.java b/mod-audit-server/src/test/java/org/folio/dao/InvoiceLineEventsDaoTest.java new file mode 100644 index 00000000..6bc2b4d2 --- /dev/null +++ b/mod-audit-server/src/test/java/org/folio/dao/InvoiceLineEventsDaoTest.java @@ -0,0 +1,80 @@ +package org.folio.dao; + +import static org.folio.utils.EntityUtils.TENANT_ID; +import static org.folio.utils.EntityUtils.createInvoiceLineAuditEvent; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.UUID; + +import io.vertx.core.Future; +import io.vertx.core.Vertx; +import io.vertx.pgclient.PgException; +import io.vertx.sqlclient.Row; +import io.vertx.sqlclient.RowSet; +import org.folio.CopilotGenerated; +import org.folio.dao.acquisition.impl.InvoiceLineEventsDaoImpl; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEventCollection; +import org.folio.util.PostgresClientFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; + +@CopilotGenerated +public class InvoiceLineEventsDaoTest { + + @Spy + private PostgresClientFactory postgresClientFactory = new PostgresClientFactory(Vertx.vertx()); + + @InjectMocks + InvoiceLineEventsDaoImpl invoiceLineEventsDao = new InvoiceLineEventsDaoImpl(postgresClientFactory); + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + invoiceLineEventsDao = new InvoiceLineEventsDaoImpl(postgresClientFactory); + } + + @Test + void shouldCreateEventProcessed() { + var invoiceLineAuditEvent = createInvoiceLineAuditEvent(UUID.randomUUID().toString()); + Future> saveFuture = invoiceLineEventsDao.save(invoiceLineAuditEvent, TENANT_ID); + saveFuture.onComplete(ar -> assertTrue(ar.succeeded())); + } + + @Test + void shouldThrowConstraintViolation() { + var invoiceLineAuditEvent = createInvoiceLineAuditEvent(UUID.randomUUID().toString()); + + Future> saveFuture = invoiceLineEventsDao.save(invoiceLineAuditEvent, TENANT_ID); + saveFuture.onComplete(ar -> { + Future> reSaveFuture = invoiceLineEventsDao.save(invoiceLineAuditEvent, TENANT_ID); + reSaveFuture.onComplete(re -> { + assertTrue(re.failed()); + assertTrue(re.cause() instanceof PgException); + assertEquals("ERROR: duplicate key value violates unique constraint \"acquisition_invoice_line_log_pkey\" (23505)", re.cause().getMessage()); + }); + }); + } + + @Test + void shouldGetCreatedEvent() { + String id = UUID.randomUUID().toString(); + var invoiceLineAuditEvent = createInvoiceLineAuditEvent(id); + + invoiceLineEventsDao.save(invoiceLineAuditEvent, TENANT_ID); + + Future dto = invoiceLineEventsDao.getAuditEventsByInvoiceLineId(id, "action_date", "asc", 1, 1, TENANT_ID); + dto.onComplete(ar -> { + InvoiceLineAuditEventCollection invoiceLineAuditEventOptional = ar.result(); + List invoiceLineAuditEventList = invoiceLineAuditEventOptional.getInvoiceLineAuditEvents(); + + assertEquals(invoiceLineAuditEventList.get(0).getId(), id); + assertEquals(InvoiceLineAuditEvent.Action.CREATE.value(), invoiceLineAuditEventList.get(0).getAction().value()); + }); + } +} diff --git a/mod-audit-server/src/test/java/org/folio/rest/impl/AuditDataAcquisitionAPITest.java b/mod-audit-server/src/test/java/org/folio/rest/impl/AuditDataAcquisitionAPITest.java index 08788e50..353c02ba 100644 --- a/mod-audit-server/src/test/java/org/folio/rest/impl/AuditDataAcquisitionAPITest.java +++ b/mod-audit-server/src/test/java/org/folio/rest/impl/AuditDataAcquisitionAPITest.java @@ -15,9 +15,11 @@ import io.restassured.http.Header; import io.vertx.core.Vertx; import io.vertx.core.json.JsonObject; +import org.folio.dao.acquisition.impl.InvoiceLineEventsDaoImpl; import org.folio.dao.acquisition.impl.OrderEventsDaoImpl; import org.folio.dao.acquisition.impl.OrderLineEventsDaoImpl; import org.folio.dao.acquisition.impl.PieceEventsDaoImpl; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; import org.folio.rest.jaxrs.model.OrderAuditEvent; import org.folio.rest.jaxrs.model.OrderLineAuditEvent; import org.folio.rest.jaxrs.model.PieceAuditEvent; @@ -38,6 +40,7 @@ public class AuditDataAcquisitionAPITest extends ApiTestBase { private static final String ACQ_AUDIT_ORDER_LINE_PATH = "/audit-data/acquisition/order-line/"; private static final String ACQ_AUDIT_PIECE_PATH = "/audit-data/acquisition/piece/"; private static final String ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH = "/status-change-history"; + private static final String ACQ_AUDIT_INVOICE_LINE_PATH = "/audit-data/acquisition/invoice-line/"; private static final String TENANT_ID = "modaudittest"; @Spy @@ -49,6 +52,8 @@ public class AuditDataAcquisitionAPITest extends ApiTestBase { OrderLineEventsDaoImpl orderLineEventDao; @InjectMocks PieceEventsDaoImpl pieceEventsDao; + @InjectMocks + InvoiceLineEventsDaoImpl invoiceLineEventsDao; @BeforeEach public void setUp() { @@ -248,4 +253,42 @@ void shouldReturnPieceEventsStatusChangesHistoryGetByPieceId() { .then().log().all().statusCode(500) .body(containsString("UUID string too large")); } + + @Test + void shouldReturnInvoiceLineEventsOnGetByInvoiceLineId() { + JsonObject jsonObject = new JsonObject(); + jsonObject.put("name", "Test Product2"); + + InvoiceLineAuditEvent invoiceLineAuditEvent = new InvoiceLineAuditEvent() + .withId(UUID.randomUUID().toString()) + .withAction(InvoiceLineAuditEvent.Action.CREATE) + .withInvoiceId(UUID.randomUUID().toString()) + .withInvoiceLineId(UUID.randomUUID().toString()) + .withUserId(UUID.randomUUID().toString()) + .withEventDate(new Date()) + .withActionDate(new Date()) + .withInvoiceLineSnapshot(jsonObject); + + invoiceLineEventsDao.save(invoiceLineAuditEvent, TENANT_ID).onComplete(v -> { + given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_INVOICE_LINE_PATH + INVALID_ID) + .then().log().all().statusCode(200) + .body(containsString("invoiceLineAuditEvents")).body(containsString("totalItems")); + + given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_INVOICE_LINE_PATH + invoiceLineAuditEvent.getInvoiceLineId()) + .then().log().all().statusCode(200) + .body(containsString(invoiceLineAuditEvent.getInvoiceLineId())); + + given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_INVOICE_LINE_PATH + invoiceLineAuditEvent.getInvoiceLineId() + "?limit=1") + .then().log().all().statusCode(200) + .body(containsString(invoiceLineAuditEvent.getInvoiceLineId())); + + given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_INVOICE_LINE_PATH + invoiceLineAuditEvent.getInvoiceLineId() + "?sortBy=action_date") + .then().log().all().statusCode(200) + .body(containsString(invoiceLineAuditEvent.getInvoiceLineId())); + + given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_INVOICE_LINE_PATH + invoiceLineAuditEvent.getInvoiceLineId() + 123) + .then().log().all().statusCode(500) + .body(containsString("UUID string too large")); + }); + } } diff --git a/mod-audit-server/src/test/java/org/folio/rest/impl/InvoiceLineEventsHandlerMockTest.java b/mod-audit-server/src/test/java/org/folio/rest/impl/InvoiceLineEventsHandlerMockTest.java new file mode 100644 index 00000000..f4eba2b7 --- /dev/null +++ b/mod-audit-server/src/test/java/org/folio/rest/impl/InvoiceLineEventsHandlerMockTest.java @@ -0,0 +1,88 @@ +package org.folio.rest.impl; + +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; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.common.header.internals.RecordHeader; +import org.folio.CopilotGenerated; +import org.folio.dao.acquisition.impl.InvoiceLineEventsDaoImpl; +import org.folio.kafka.KafkaTopicNameHelper; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.util.OkapiConnectionParams; +import org.folio.services.acquisition.impl.InvoiceLineAuditEventsServiceImpl; +import org.folio.util.PostgresClientFactory; +import org.folio.verticle.acquisition.consumers.InvoiceLineEventsHandler; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Spy; + +import java.nio.charset.StandardCharsets; +import java.util.Date; +import java.util.UUID; + +import static org.folio.kafka.KafkaTopicNameHelper.getDefaultNameSpace; + +@CopilotGenerated(partiallyGenerated = true) +public class InvoiceLineEventsHandlerMockTest { + + private static final String TENANT_ID = "diku"; + protected static final String TOKEN = "token"; + private static final String KAFKA_ENV = "folio"; + + public static final String OKAPI_TOKEN_HEADER = "x-okapi-token"; + public static final String OKAPI_URL_HEADER = "x-okapi-url"; + + @Spy + private Vertx vertx = Vertx.vertx(); + + @Spy + private PostgresClientFactory postgresClientFactory = new PostgresClientFactory(Vertx.vertx()); + + @InjectMocks + InvoiceLineEventsDaoImpl invoiceLineEventsDao = new InvoiceLineEventsDaoImpl(postgresClientFactory); + + @InjectMocks + InvoiceLineAuditEventsServiceImpl invoiceLineAuditEventServiceImpl = new InvoiceLineAuditEventsServiceImpl(invoiceLineEventsDao); + + @InjectMocks + private InvoiceLineEventsHandler invoiceLineEventsHandler = new InvoiceLineEventsHandler(vertx, invoiceLineAuditEventServiceImpl); + + @Test + void shouldProcessEvent() { + JsonObject jsonObject = new JsonObject(); + jsonObject.put("Test", "TestValue"); + + InvoiceLineAuditEvent invoiceLineAuditEvent = new InvoiceLineAuditEvent() + .withId(UUID.randomUUID().toString()) + .withInvoiceLineId(UUID.randomUUID().toString()) + .withEventDate(new Date()) + .withInvoiceId(UUID.randomUUID().toString()) + .withActionDate(new Date()) + .withAction(InvoiceLineAuditEvent.Action.CREATE) + .withInvoiceLineSnapshot(jsonObject) + .withUserId(UUID.randomUUID().toString()); + KafkaConsumerRecord kafkaConsumerRecord = buildKafkaConsumerRecord(invoiceLineAuditEvent); + + Future saveFuture = invoiceLineEventsHandler.handle(kafkaConsumerRecord); + saveFuture.onComplete(ar -> Assertions.assertTrue(ar.succeeded())); + } + + private KafkaConsumerRecord buildKafkaConsumerRecord(InvoiceLineAuditEvent record) { + String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV, getDefaultNameSpace(), TENANT_ID, record.getAction().toString()); + ConsumerRecord consumerRecord = buildConsumerRecord(topic, record); + return new KafkaConsumerRecordImpl<>(consumerRecord); + } + + protected ConsumerRecord buildConsumerRecord(String topic, InvoiceLineAuditEvent event) { + ConsumerRecord consumerRecord = new ConsumerRecord<>(topic, 0, 0, topic, Json.encode(event)); + consumerRecord.headers().add(new RecordHeader(OkapiConnectionParams.OKAPI_TENANT_HEADER, TENANT_ID.getBytes(StandardCharsets.UTF_8))); + consumerRecord.headers().add(new RecordHeader(OKAPI_URL_HEADER, ("http://localhost:" + 8080).getBytes(StandardCharsets.UTF_8))); + consumerRecord.headers().add(new RecordHeader(OKAPI_TOKEN_HEADER, TOKEN.getBytes(StandardCharsets.UTF_8))); + return consumerRecord; + } +} diff --git a/mod-audit-server/src/test/java/org/folio/services/InvoiceLineAuditEventsServiceTest.java b/mod-audit-server/src/test/java/org/folio/services/InvoiceLineAuditEventsServiceTest.java new file mode 100644 index 00000000..7cf7aa58 --- /dev/null +++ b/mod-audit-server/src/test/java/org/folio/services/InvoiceLineAuditEventsServiceTest.java @@ -0,0 +1,64 @@ +package org.folio.services; + +import io.vertx.core.Future; +import io.vertx.core.Vertx; +import io.vertx.sqlclient.Row; +import io.vertx.sqlclient.RowSet; +import org.folio.CopilotGenerated; +import org.folio.dao.acquisition.InvoiceLineEventsDao; +import org.folio.dao.acquisition.impl.InvoiceLineEventsDaoImpl; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEventCollection; +import org.folio.services.acquisition.impl.InvoiceLineAuditEventsServiceImpl; +import org.folio.util.PostgresClientFactory; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; + +import java.util.List; +import java.util.UUID; + +import static org.folio.utils.EntityUtils.TENANT_ID; +import static org.folio.utils.EntityUtils.createInvoiceLineAuditEvent; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@CopilotGenerated +public class InvoiceLineAuditEventsServiceTest { + + @Spy + private PostgresClientFactory postgresClientFactory = new PostgresClientFactory(Vertx.vertx()); + @Mock + InvoiceLineEventsDao invoiceLineEventsDao = new InvoiceLineEventsDaoImpl(postgresClientFactory); + @InjectMocks + InvoiceLineAuditEventsServiceImpl invoiceLineAuditEventService = new InvoiceLineAuditEventsServiceImpl(invoiceLineEventsDao); + + @Test + public void shouldCallDaoForSuccessfulCase() { + var invoiceLineAuditEvent = createInvoiceLineAuditEvent(UUID.randomUUID().toString()); + + Future> saveFuture = invoiceLineAuditEventService.saveInvoiceLineAuditEvent(invoiceLineAuditEvent, TENANT_ID); + + saveFuture.onComplete(ar -> { + assertTrue(ar.succeeded()); + }); + } + + @Test + void shouldGetInvoiceLineDto() { + String id = UUID.randomUUID().toString(); + var invoiceLineAuditEvent = createInvoiceLineAuditEvent(id); + + invoiceLineAuditEventService.saveInvoiceLineAuditEvent(invoiceLineAuditEvent, TENANT_ID); + + Future dto = invoiceLineAuditEventService.getAuditEventsByInvoiceLineId(id, "action_date", "desc", 1, 1, TENANT_ID); + dto.onComplete(ar -> { + InvoiceLineAuditEventCollection invoiceLineAuditEventOptional = ar.result(); + List invoiceLineAuditEventList = invoiceLineAuditEventOptional.getInvoiceLineAuditEvents(); + + assertEquals(invoiceLineAuditEventList.get(0).getId(), id); + assertEquals(InvoiceLineAuditEvent.Action.CREATE, invoiceLineAuditEventList.get(0).getAction()); + }); + } +} 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 d359311f..77d8e491 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 @@ -4,6 +4,7 @@ import java.util.UUID; import io.vertx.core.json.JsonObject; +import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent; import org.folio.rest.jaxrs.model.OrderAuditEvent; import org.folio.rest.jaxrs.model.OrderLineAuditEvent; import org.folio.rest.jaxrs.model.PieceAuditEvent; @@ -14,6 +15,8 @@ public class EntityUtils { public static String PIECE_ID = "2cd4adc4-f287-49b6-a9c6-9eacdc4868e7"; public static String ORDER_ID = "a21fc51c-d46b-439b-8c79-9b2be41b79a6"; public static String ORDER_LINE_ID = "a22fc51c-d46b-439b-8c79-9b2be41b79a6"; + public static String INVOICE_ID = "550e8400-e29b-41d4-a716-446655440001"; + public static String INVOICE_LINE_ID = "550e8400-e29b-41d4-a716-446655440001"; public static OrderAuditEvent createOrderAuditEvent(String id) { JsonObject jsonObject = new JsonObject(); @@ -96,4 +99,19 @@ public static PieceAuditEvent createPieceAuditEventWithoutSnapshot() { .withActionDate(new Date()) .withPieceSnapshot("Test"); } + + public static InvoiceLineAuditEvent createInvoiceLineAuditEvent(String id) { + JsonObject jsonObject = new JsonObject(); + jsonObject.put("name", "Test Product"); + + return new InvoiceLineAuditEvent() + .withId(id) + .withAction(InvoiceLineAuditEvent.Action.CREATE) + .withInvoiceId(INVOICE_ID) + .withInvoiceLineId(INVOICE_LINE_ID) + .withUserId(UUID.randomUUID().toString()) + .withEventDate(new Date()) + .withActionDate(new Date()) + .withInvoiceLineSnapshot(jsonObject); + } } diff --git a/ramls/acquisition-events.raml b/ramls/acquisition-events.raml index 8bdd0850..ccdc0574 100644 --- a/ramls/acquisition-events.raml +++ b/ramls/acquisition-events.raml @@ -17,6 +17,7 @@ types: order-line-audit-event-collection: !include order_line_audit_event_collection.json piece-audit-event: !include piece_audit_event.json piece-audit-event-collection: !include piece_audit_event_collection.json + invoice-line-audit-event-collection: !include invoice_line_audit_event_collection.json traits: searchable: !include raml-util/traits/searchable.raml @@ -159,3 +160,35 @@ traits: example: strict: false value: !include raml-util/examples/errors.sample + + /invoice-line/{id}: + get: + description: Get list of invoice_line events by invoice_line_id + is: [ + pageable, + validate + ] + queryParameters: + sortBy: + description: "sorting by field: actionDate" + type: string + default: action_date + sortOrder: + description: "sort order: asc or desc" + enum: [asc, desc] + type: string + default: desc + limit: + default: 2147483647 + offset: + default: 0 + responses: + 200: + body: + application/json: + type: invoice-line-audit-event-collection + 500: + description: "Internal server error" + body: + application/json: + type: errors diff --git a/ramls/invoice_line_audit_event.json b/ramls/invoice_line_audit_event.json new file mode 100644 index 00000000..1a4bb237 --- /dev/null +++ b/ramls/invoice_line_audit_event.json @@ -0,0 +1,44 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Invoice line audit event", + "type": "object", + "properties": { + "id": { + "description": "UUID of the event", + "$ref": "common/uuid.json" + }, + "action": { + "description": "Action for invoice line (Create, Edit or Delete)", + "type": "string", + "$ref": "event_action.json" + }, + "invoiceId": { + "description": "UUID of the invoice", + "$ref": "common/uuid.json" + }, + "invoiceLineId": { + "description": "UUID of the invoice line", + "$ref": "common/uuid.json" + }, + "userId": { + "description": "UUID of the user who performed the action", + "$ref": "common/uuid.json" + }, + "eventDate": { + "description": "Date time when event triggered", + "format": "date-time", + "type": "string" + }, + "actionDate": { + "description": "Date time when invoice action occurred", + "format": "date-time", + "type": "string" + }, + "invoiceLineSnapshot": { + "description": "Full snapshot of the invoice line", + "type": "object", + "javaType": "java.lang.Object" + } + }, + "additionalProperties": false +} diff --git a/ramls/invoice_line_audit_event_collection.json b/ramls/invoice_line_audit_event_collection.json new file mode 100644 index 00000000..b6832e71 --- /dev/null +++ b/ramls/invoice_line_audit_event_collection.json @@ -0,0 +1,25 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Collection of invoiceLineAuditEvents", + "type": "object", + "additionalProperties": false, + "properties": { + "invoiceLineAuditEvents": { + "description": "List of invoiceLineAuditEvents", + "type": "array", + "id": "invoiceLineAuditEventsList", + "items": { + "type": "object", + "$ref": "invoice_line_audit_event.json" + } + }, + "totalItems": { + "description": "total records", + "type": "integer" + } + }, + "required": [ + "invoiceLineAuditEvents", + "totalItems" + ] +}