FINERACT-1926: Fineract Asset Externalization events

- [x] Change events
- [x] Create data enricher
- [x] Core changes
- [x] Remove AbstractBusinessEventSerializer
- [x] BusinessEventSerializer.toAvroDto added and change the event serializers
This commit is contained in:
Janos Haber
2023-06-06 15:20:42 +02:00
committed by Adam Saghy
parent e4754b299b
commit 99470c0cf8
42 changed files with 372 additions and 110 deletions
@@ -22,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -31,13 +32,17 @@ import java.nio.ByteBuffer;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.fineract.avro.BulkMessageItemV1;
import org.apache.fineract.avro.generator.ByteBufferSerializable;
import org.apache.fineract.avro.loan.v1.LoanAccountDataV1;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
import org.apache.fineract.infrastructure.core.service.DataEnricherProcessor;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.infrastructure.event.business.domain.BulkBusinessEvent;
import org.apache.fineract.infrastructure.event.business.domain.BusinessEvent;
@@ -48,6 +53,7 @@ import org.apache.fineract.infrastructure.event.external.service.message.BulkMes
import org.apache.fineract.infrastructure.event.external.service.serialization.serializer.BusinessEventSerializer;
import org.apache.fineract.infrastructure.event.external.service.serialization.serializer.BusinessEventSerializerFactory;
import org.apache.fineract.infrastructure.event.external.service.support.ByteBufferConverter;
import org.apache.fineract.investor.enricher.LoanAccountDataV1Enricher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -59,6 +65,9 @@ import org.mockito.junit.jupiter.MockitoExtension;
@SuppressWarnings({ "rawtypes", "unchecked" })
class ExternalEventServiceTest {
public static final String DUMMY_EXTERNAL_OWNER_ID = "dummy-external-owner-id";
public static final String DUMMY_SETTLEMENT_DATE = "2021-01-01";
@Mock
private ExternalEventRepository repository;
@Mock
@@ -72,12 +81,17 @@ class ExternalEventServiceTest {
@Mock
private EntityManager entityManager;
@Mock
private LoanAccountDataV1Enricher loanAccountDataV1Enricher;
private ExternalEventService underTest;
@BeforeEach
public void setUp() {
lenient().when(loanAccountDataV1Enricher.isDataTypeSupported(any())).thenReturn(true);
DataEnricherProcessor dataEnricherProcessor = new DataEnricherProcessor(Optional.of(Arrays.asList(loanAccountDataV1Enricher)));
underTest = new ExternalEventService(repository, idempotencyKeyGenerator, serializerFactory, byteBufferConverter,
bulkMessageItemFactory);
bulkMessageItemFactory, dataEnricherProcessor);
underTest.setEntityManager(entityManager);
FineractPlatformTenant tenant = new FineractPlatformTenant(1L, "default", "Default Tenant", "Europe/Budapest", null);
ThreadLocalContextUtil.setTenant(tenant);
@@ -101,7 +115,9 @@ class ExternalEventServiceTest {
given(idempotencyKeyGenerator.generate(event)).willReturn("");
given(serializerFactory.create(event)).willReturn(eventSerializer);
given(eventSerializer.getSupportedSchema()).will(invocation -> LoanAccountDataV1.class);
given(eventSerializer.serialize(event)).willThrow(IOException.class);
ByteBufferSerializable byteBuffer = mock(LoanAccountDataV1.class);
given(eventSerializer.toAvroDTO(event)).willReturn(byteBuffer);
given(byteBuffer.toByteBuffer()).willThrow(new IOException(""));
// when & then
assertThatThrownBy(() -> underTest.postEvent(event)).isExactlyInstanceOf(RuntimeException.class);
}
@@ -121,12 +137,16 @@ class ExternalEventServiceTest {
given(event.getType()).willReturn(eventType);
given(idempotencyKeyGenerator.generate(event)).willReturn(idempotencyKey);
given(serializerFactory.create(event)).willReturn(eventSerializer);
LoanAccountDataV1 loanAccountData = new LoanAccountDataV1();
given(eventSerializer.getSupportedSchema()).will(invocation -> LoanAccountDataV1.class);
given(eventSerializer.serialize(event)).willReturn(data);
given(eventSerializer.toAvroDTO(event)).willReturn(loanAccountData);
given(byteBufferConverter.convert(any(ByteBuffer.class))).willReturn(data);
// when
underTest.postEvent(event);
// then
verify(repository).save(externalEventArgumentCaptor.capture());
verify(loanAccountDataV1Enricher).isDataTypeSupported(LoanAccountDataV1.class);
verify(loanAccountDataV1Enricher).enrich(loanAccountData);
ExternalEvent externalEvent = externalEventArgumentCaptor.getValue();
assertThat(externalEvent.getIdempotencyKey()).isEqualTo(idempotencyKey);
assertThat(externalEvent.getData()).isEqualTo(data);
@@ -171,14 +191,13 @@ class ExternalEventServiceTest {
String idempotencyKey = "key";
BusinessEvent event = mock(BusinessEvent.class);
BusinessEventSerializer eventSerializer = mock(BusinessEventSerializer.class);
byte[] data = new byte[0];
given(event.getType()).willReturn(eventType);
given(event.getCategory()).willReturn(eventCategory);
given(idempotencyKeyGenerator.generate(event)).willReturn(idempotencyKey);
given(serializerFactory.create(event)).willReturn(eventSerializer);
given(eventSerializer.getSupportedSchema()).will(invocation -> LoanAccountDataV1.class);
given(eventSerializer.serialize(event)).willReturn(data);
given(eventSerializer.toAvroDTO(event)).willReturn(new LoanAccountDataV1());
// when
underTest.postEvent(event);
// then
@@ -198,14 +217,13 @@ class ExternalEventServiceTest {
String idempotencyKey = "key";
BusinessEvent event = mock(BusinessEvent.class);
BusinessEventSerializer eventSerializer = mock(BusinessEventSerializer.class);
byte[] data = new byte[0];
given(event.getType()).willReturn(eventType);
given(event.getCategory()).willReturn(eventCategory);
given(idempotencyKeyGenerator.generate(event)).willReturn(idempotencyKey);
given(serializerFactory.create(event)).willReturn(eventSerializer);
given(eventSerializer.getSupportedSchema()).will(invocation -> LoanAccountDataV1.class);
given(eventSerializer.serialize(event)).willReturn(data);
given(eventSerializer.toAvroDTO(event)).willReturn(new LoanAccountDataV1());
// when
underTest.postEvent(event);
// then
@@ -103,7 +103,6 @@ public class LoanRepaymentBusinessEventSerializerTest {
LocalDate loanInstallmentRepaymentDueDate = DateUtils.getBusinessLocalDate().plusDays(1);
Loan loanForProcessing = Mockito.mock(Loan.class);
LoanProduct loanProduct = Mockito.mock(LoanProduct.class);
LoanSummary loanSummary = Mockito.mock(LoanSummary.class);
MonetaryCurrency loanCurrency = Mockito.mock(MonetaryCurrency.class);