From eb1233d5a86dfdb6cc8ec9c1cfc9f79dadf002f0 Mon Sep 17 00:00:00 2001 From: Mateusz Czeladka Date: Tue, 13 Aug 2024 18:58:53 +0200 Subject: [PATCH] feat: json metadata checker. --- .../service/internal/FailureResponses.java | 4 +- .../TransactionRepositoryGateway.java | 2 +- .../service/MetadataSerialiser.java | 3 +- ...lockchain_transaction_metadata_schema.json | 189 +++++++++++------- .../service/MetadataSerialiserTest.java | 3 +- 5 files changed, 124 insertions(+), 77 deletions(-) diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/FailureResponses.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/FailureResponses.java index 75a7fbc3..16656a7d 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/FailureResponses.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/FailureResponses.java @@ -74,8 +74,8 @@ public static List> transacti }).toList(); } - public static Either transactionItemCannotRejectAlreadyApprovedResponse(String transactionId, - String txItemId) { + public static Either transactionItemCannotRejectAlreadyApprovedForDispatchResponse(String transactionId, + String txItemId) { val problem = Problem.builder() .withTitle("TX_ALREADY_APPROVED_CANNOT_REJECT_TX_ITEM") .withDetail(STR."Cannot reject transaction item \{txItemId} because transaction \{transactionId} has already been approved for dispatch") diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/TransactionRepositoryGateway.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/TransactionRepositoryGateway.java index 8059a08c..9c1c6baa 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/TransactionRepositoryGateway.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/TransactionRepositoryGateway.java @@ -198,7 +198,7 @@ public List> rejectTransactio val txItem = txItemM.orElseThrow(); if (tx.getLedgerDispatchApproved()) { - transactionItemEntitiesE.add(transactionItemCannotRejectAlreadyApprovedResponse(transactionId, txItemId)); + transactionItemEntitiesE.add(transactionItemCannotRejectAlreadyApprovedForDispatchResponse(transactionId, txItemId)); continue; } diff --git a/blockchain_publisher/src/main/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiser.java b/blockchain_publisher/src/main/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiser.java index e640ffda..af1dd0ae 100644 --- a/blockchain_publisher/src/main/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiser.java +++ b/blockchain_publisher/src/main/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiser.java @@ -7,7 +7,6 @@ import org.cardanofoundation.lob.app.blockchain_publisher.domain.entity.*; import org.springframework.stereotype.Service; -import java.math.BigInteger; import java.time.Clock; import java.time.Instant; import java.time.format.DateTimeFormatter; @@ -51,7 +50,7 @@ private MetadataMap createMetadataSection(long creationSlot) { val now = Instant.now(clock); - metadataMap.put("creation_slot", BigInteger.valueOf(creationSlot)); + metadataMap.put("creation_slot", String.valueOf(creationSlot)); metadataMap.put("timestamp", DateTimeFormatter.ISO_INSTANT.format(now)); metadataMap.put("version", VERSION); diff --git a/blockchain_publisher/src/main/resources/blockchain_transaction_metadata_schema.json b/blockchain_publisher/src/main/resources/blockchain_transaction_metadata_schema.json index f7211854..42e6af35 100644 --- a/blockchain_publisher/src/main/resources/blockchain_transaction_metadata_schema.json +++ b/blockchain_publisher/src/main/resources/blockchain_transaction_metadata_schema.json @@ -6,21 +6,39 @@ "type": "object", "properties": { "creation_slot": { - "type": "integer" + "type": "string", + "pattern": "^[0-9]+$" + }, + "version": { + "type": "string" } }, - "required": ["creation_slot"] + "required": ["creation_slot", "version"] }, - "organisation": { + "org": { "type": "object", "properties": { + "country_code": { + "type": "string", + "pattern": "^[A-Z]{2}$" + }, + "name": { + "type": "string" + }, + "tax_id_number": { + "type": "string" + }, "id": { "type": "string" + }, + "currency_id": { + "type": "string", + "pattern": "^ISO_4217:[A-Z]{3}$|^ISO_24165:[A-Z0-9]+$" } }, - "required": ["id"] + "required": ["country_code", "name", "tax_id_number", "id", "currency_id"] }, - "transactions": { + "txs": { "type": "array", "items": { "type": "object", @@ -29,97 +47,128 @@ "type": "string", "format": "date" }, - "base_currency": { - "type": "object", - "properties": { - "internal_number": { - "type": "string" - }, - "id": { - "type": "string" - } - }, - "required": ["internal_code", "id"] + "number": { + "type": "string" + }, + "batch_id": { + "type": "string" + }, + "id": { + "type": "string" }, - "documents": { + "type": { + "type": "string", + "enum": [ + "CardCharge", + "VendorBill", + "CardRefund", + "Journal", + "FxRevaluation", + "Transfer", + "CustomerPayment", + "ExpenseReport", + "VendorPayment", + "BillCredit" + ] + }, + "items": { "type": "array", "items": { "type": "object", "properties": { - "number": { - "type": "string" + "amount": { + "type": "string", + "pattern": "^[0-9]+(\\.[0-9]{1,2})?$" }, - "vat": { + "event_code": { "type": "object", "properties": { - "rate": { + "code": { "type": "string" }, - "internal_code": { + "name": { "type": "string" } - } + }, + "required": ["code", "name"] }, - "counterparty": { + "project": { "type": "object", "properties": { - "internal_code": { + "cust_code": { + "type": "string" + }, + "name": { "type": "string" } - } - } - } - } - }, - "cost_center": { - "type": "string" - }, - "project_code": { - "type": "string" - }, - "target_currency": { - "type": "object", - "properties": { - "internal_code": { - "type": "string" - }, - "id": { - "type": "string" - } - }, - "required": ["internal_code", "id"] - }, - "internal_number": { - "type": "string" - }, - "id": { - "type": "string" - }, - "type": { - "type": "string" - }, - "fx_rate": { - "type": "string" - }, - "items": { - "type": "array", - "items": { - "type": "object", - "properties": { - "amount": { - "type": "string" + }, + "required": ["cust_code", "name"] + }, + "cost_center": { + "type": "object", + "properties": { + "cust_code": { + "type": "string" + }, + "name": { + "type": "string" + } + }, + "required": ["cust_code", "name"] + }, + "document": { + "type": "object", + "properties": { + "number": { + "type": "string" + }, + "currency": { + "type": "object", + "properties": { + "cust_code": { + "type": "string" + }, + "id": { + "type": "string", + "pattern": "^ISO_4217:[A-Z]{3}$|^ISO_24165:[A-Z0-9]+$" + } + }, + "required": ["cust_code", "id"] + }, + "counterparty": { + "type": "object", + "properties": { + "cust_code": { + "type": "string" + }, + "type": { + "type": "string" + } + }, + "required": ["cust_code", "type"] + } + }, + "required": ["number", "currency"] }, "id": { "type": "string" + }, + "fx_rate": { + "type": "string", + "pattern": "^[0-9]+(\\.[0-9]+)?$" } }, - "required": ["amount", "id"] + "required": ["amount", "event_code", "document", "id", "fx_rate"] } + }, + "accounting_period": { + "type": "string", + "pattern": "^[0-9]{4}-[0-9]{2}$" } }, - "required": ["date", "base_currency", "target_currency", "internal_number", "id", "type", "fx_rate", "items"] + "required": ["date", "number", "batch_id", "id", "type", "items", "accounting_period"] } } }, - "required": ["metadata", "organisation", "transactions"] + "required": ["metadata", "org", "txs"] } diff --git a/blockchain_publisher/src/test/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiserTest.java b/blockchain_publisher/src/test/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiserTest.java index c5e4a8f7..ba01b85b 100644 --- a/blockchain_publisher/src/test/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiserTest.java +++ b/blockchain_publisher/src/test/java/org/cardanofoundation/lob/app/blockchain_publisher/service/MetadataSerialiserTest.java @@ -9,7 +9,6 @@ import org.junit.jupiter.api.Test; import java.math.BigDecimal; -import java.math.BigInteger; import java.time.*; import java.util.Set; @@ -96,7 +95,7 @@ void testSerialiseToMetadataMap() { assertThat(result.get("metadata")).isInstanceOf(MetadataMap.class); MetadataMap metadata = (MetadataMap) result.get("metadata"); - assertThat(metadata.get("creation_slot")).isEqualTo(BigInteger.valueOf(creationSlot)); + assertThat(metadata.get("creation_slot")).isEqualTo(String.valueOf(creationSlot)); assertThat(metadata.get("timestamp")).isEqualTo("2023-06-01T10:15:30Z"); assertThat(metadata.get("version")).isEqualTo(MetadataSerialiser.VERSION);