Search in sources :

Example 16 with KeyValue

use of net.mguenther.kafka.junit.KeyValue in project mod-invoice by folio-org.

the class CreateInvoiceEventHandlerTest method shouldPublishDiErrorWhenMappingProfileHasInvalidMappingSyntax.

@Test
public void shouldPublishDiErrorWhenMappingProfileHasInvalidMappingSyntax() throws InterruptedException {
    // given
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(PARSED_CONTENT_INVOICE_LINE_3_HAS_NO_SUBTOTAL)).withId(UUID.randomUUID().toString());
    ProfileSnapshotWrapper profileSnapshotWrapper = buildProfileSnapshotWrapper(jobProfile, actionProfile, mappingProfileWithInvalidMappingSyntax);
    addMockEntry(JOB_PROFILE_SNAPSHOTS_MOCK, profileSnapshotWrapper);
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext);
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> kafkaRecord = new KeyValue<>("test-key", Json.encode(event));
    kafkaRecord.addHeader(RECORD_ID_HEADER, record.getId(), UTF_8);
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(kafkaRecord)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, DI_ERROR.value());
    List<String> observedValues = kafkaCluster.observeValues(ObserveKeyValues.on(topicToObserve, 1).with(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID).observeFor(60, TimeUnit.SECONDS).build());
    Event obtainedEvent = Json.decodeValue(observedValues.get(0), Event.class);
    DataImportEventPayload eventPayload = Json.decodeValue(obtainedEvent.getEventPayload(), DataImportEventPayload.class);
    assertEquals(DI_INVOICE_CREATED.value(), eventPayload.getEventsChain().get(eventPayload.getEventsChain().size() - 1));
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) HashMap(java.util.HashMap) Event(org.folio.rest.jaxrs.model.Event) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) ParsedRecord(org.folio.ParsedRecord) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.jupiter.api.Test)

Example 17 with KeyValue

use of net.mguenther.kafka.junit.KeyValue in project mod-invoice by folio-org.

the class CreateInvoiceEventHandlerTest method shouldMatchPoLineByPoLineNumberAndLeaveEmptyInvoiceLineFundDistributionExpenseClassIdWhenMatchedPoLineHasDifferentExpenseClasses.

@Test
public void shouldMatchPoLineByPoLineNumberAndLeaveEmptyInvoiceLineFundDistributionExpenseClassIdWhenMatchedPoLineHasDifferentExpenseClasses() throws IOException, InterruptedException {
    // given
    PoLine poLine3 = Json.decodeValue(getMockData(String.format(MOCK_DATA_PATH_PATTERN, PO_LINES_MOCK_DATA_PATH, PO_LINE_ID_3)), PoLine.class);
    PoLineCollection poLineCollection = new PoLineCollection().withPoLines(List.of(poLine3));
    when(mockOrderLinesRestClient.get(any(), any(RequestContext.class), eq(PoLineCollection.class))).thenReturn(CompletableFuture.completedFuture(poLineCollection)).thenReturn(CompletableFuture.completedFuture(new PoLineCollection()));
    ProfileSnapshotWrapper profileSnapshotWrapper = buildProfileSnapshotWrapper(jobProfile, actionProfile, mappingProfileWithMixedFundDistributionMapping);
    addMockEntry(JOB_PROFILE_SNAPSHOTS_MOCK, profileSnapshotWrapper);
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT)).withId(UUID.randomUUID().toString());
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext);
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> kafkaRecord = new KeyValue<>("test-key", Json.encode(event));
    kafkaRecord.addHeader(RECORD_ID_HEADER, record.getId(), UTF_8);
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(kafkaRecord)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, DI_COMPLETED.value());
    List<String> observedValues = kafkaCluster.observeValues(ObserveKeyValues.on(topicToObserve, 1).with(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID).observeFor(30, TimeUnit.SECONDS).build());
    Event obtainedEvent = Json.decodeValue(observedValues.get(0), Event.class);
    DataImportEventPayload eventPayload = Json.decodeValue(obtainedEvent.getEventPayload(), DataImportEventPayload.class);
    assertEquals(DI_INVOICE_CREATED.value(), eventPayload.getEventsChain().get(eventPayload.getEventsChain().size() - 1));
    assertNotNull(eventPayload.getContext().get(INVOICE.value()));
    Invoice createdInvoice = Json.decodeValue(eventPayload.getContext().get(INVOICE.value()), Invoice.class);
    assertNotNull(eventPayload.getContext().get(INVOICE_LINES_KEY));
    InvoiceLineCollection createdInvoiceLines = Json.decodeValue(eventPayload.getContext().get(INVOICE_LINES_KEY), InvoiceLineCollection.class);
    assertEquals(3, createdInvoiceLines.getTotalRecords());
    assertEquals(3, createdInvoiceLines.getInvoiceLines().size());
    createdInvoiceLines.getInvoiceLines().forEach(invLine -> assertEquals(createdInvoice.getId(), invLine.getInvoiceId()));
    assertNull(createdInvoiceLines.getInvoiceLines().get(0).getPoLineId());
    assertNull(createdInvoiceLines.getInvoiceLines().get(1).getPoLineId());
    assertEquals(poLine3.getId(), createdInvoiceLines.getInvoiceLines().get(2).getPoLineId());
    assertEquals(1, createdInvoiceLines.getInvoiceLines().get(2).getFundDistributions().size());
    assertNotNull(createdInvoiceLines.getInvoiceLines().get(2).getFundDistributions().get(0).getFundId());
    assertNotNull(createdInvoiceLines.getInvoiceLines().get(2).getFundDistributions().get(0).getValue());
    assertEquals(2, poLine3.getFundDistribution().size());
    assertNotEquals(poLine3.getFundDistribution().get(0).getExpenseClassId(), poLine3.getFundDistribution().get(1).getExpenseClassId());
    assertNull(createdInvoiceLines.getInvoiceLines().get(2).getFundDistributions().get(0).getExpenseClassId());
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) Invoice(org.folio.rest.jaxrs.model.Invoice) HashMap(java.util.HashMap) ParsedRecord(org.folio.ParsedRecord) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) DataImportEventPayload(org.folio.DataImportEventPayload) PoLineCollection(org.folio.rest.acq.model.orders.PoLineCollection) PoLine(org.folio.rest.acq.model.orders.PoLine) Event(org.folio.rest.jaxrs.model.Event) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Test(org.junit.jupiter.api.Test)

Example 18 with KeyValue

use of net.mguenther.kafka.junit.KeyValue in project mod-invoice by folio-org.

the class CreateInvoiceEventHandlerTest method shouldPublishDiErrorEventWhenHasNoSourceRecord.

@Test
public void shouldPublishDiErrorEventWhenHasNoSourceRecord() throws InterruptedException {
    // given
    ProfileSnapshotWrapper profileSnapshotWrapper = buildProfileSnapshotWrapper(jobProfile, actionProfile, mappingProfile);
    addMockEntry(JOB_PROFILE_SNAPSHOTS_MOCK, profileSnapshotWrapper);
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(TENANT_ID).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(new HashMap<>() {

        {
            put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
        }
    });
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), TENANT_ID, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> kafkaRecord = new KeyValue<>("test-key", Json.encode(event));
    kafkaRecord.addHeader(RECORD_ID_HEADER, UUID.randomUUID().toString(), UTF_8);
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(kafkaRecord)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), TENANT_ID, DI_ERROR.value());
    List<String> observedValues = kafkaCluster.observeValues(ObserveKeyValues.on(topicToObserve, 1).with(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID).observeFor(30, TimeUnit.SECONDS).build());
    Event publishedEvent = Json.decodeValue(observedValues.get(0), Event.class);
    DataImportEventPayload eventPayload = Json.decodeValue(publishedEvent.getEventPayload(), DataImportEventPayload.class);
    assertEquals(DI_ERROR.value(), eventPayload.getEventType());
    assertEquals(DI_INVOICE_CREATED.value(), eventPayload.getEventsChain().get(eventPayload.getEventsChain().size() - 1));
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) Event(org.folio.rest.jaxrs.model.Event) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.jupiter.api.Test)

Example 19 with KeyValue

use of net.mguenther.kafka.junit.KeyValue in project mod-invoice by folio-org.

the class DataImportConsumerVerticleTest method shouldPublishDiCompletedEventWhenProcessingCoreHandlerSucceeded.

@Test
public void shouldPublishDiCompletedEventWhenProcessingCoreHandlerSucceeded() throws InterruptedException, UnsupportedEncodingException {
    // given
    EventHandler mockedEventHandler = mock(EventHandler.class);
    when(mockedEventHandler.isEligible(any(DataImportEventPayload.class))).thenReturn(true);
    doAnswer(invocationOnMock -> CompletableFuture.completedFuture(invocationOnMock.getArgument(0))).when(mockedEventHandler).handle(any(DataImportEventPayload.class));
    EventManager.registerEventHandler(mockedEventHandler);
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(TENANT_ID).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(new HashMap<>() {

        {
            put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
        }
    }).withProfileSnapshot(profileSnapshotWrapper);
    String recordId = UUID.randomUUID().toString();
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), TENANT_ID, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> record = new KeyValue<>("test-key", Json.encode(event));
    record.addHeader(RECORD_ID_HEADER, recordId, UTF_8);
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(record)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), TENANT_ID, DI_COMPLETED.value());
    List<KeyValue<String, String>> observedRecords = kafkaCluster.observe(ObserveKeyValues.on(topicToObserve, 1).observeFor(30, TimeUnit.SECONDS).build());
    assertEquals(recordId, new String(observedRecords.get(0).getHeaders().lastHeader(RECORD_ID_HEADER).value(), UTF_8.name()));
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) HashMap(java.util.HashMap) EventHandler(org.folio.processing.events.services.handler.EventHandler) Event(org.folio.rest.jaxrs.model.Event) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.jupiter.api.Test)

Example 20 with KeyValue

use of net.mguenther.kafka.junit.KeyValue in project mod-invoice by folio-org.

the class DataImportConsumerVerticleTest method shouldPublishDiErrorEventWhenProcessingCoreHandlerFailed.

@Test
public void shouldPublishDiErrorEventWhenProcessingCoreHandlerFailed() throws InterruptedException {
    // given
    EventHandler mockedEventHandler = mock(EventHandler.class);
    when(mockedEventHandler.isEligible(any(DataImportEventPayload.class))).thenReturn(true);
    doAnswer(invocationOnMock -> CompletableFuture.failedFuture(new EventProcessingException("error msg"))).when(mockedEventHandler).handle(any(DataImportEventPayload.class));
    EventManager.registerEventHandler(mockedEventHandler);
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(TENANT_ID).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(new HashMap<>() {

        {
            put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
        }
    }).withProfileSnapshot(profileSnapshotWrapper);
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), TENANT_ID, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> record = new KeyValue<>("test-key", Json.encode(event));
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(record)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), TENANT_ID, DI_ERROR.value());
    kafkaCluster.observeValues(ObserveKeyValues.on(topicToObserve, 1).observeFor(30, TimeUnit.SECONDS).build());
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) HashMap(java.util.HashMap) EventHandler(org.folio.processing.events.services.handler.EventHandler) Event(org.folio.rest.jaxrs.model.Event) DataImportEventPayload(org.folio.DataImportEventPayload) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) Test(org.junit.jupiter.api.Test)

Aggregations

KeyValue (net.mguenther.kafka.junit.KeyValue)24 Event (org.folio.rest.jaxrs.model.Event)22 DataImportEventPayload (org.folio.DataImportEventPayload)13 HashMap (java.util.HashMap)12 ProfileSnapshotWrapper (org.folio.rest.jaxrs.model.ProfileSnapshotWrapper)12 Test (org.junit.jupiter.api.Test)12 ParsedRecord (org.folio.ParsedRecord)9 Record (org.folio.Record)9 InvoiceLineCollection (org.folio.rest.jaxrs.model.InvoiceLineCollection)8 Invoice (org.folio.rest.jaxrs.model.Invoice)6 PoLine (org.folio.rest.acq.model.orders.PoLine)5 PoLineCollection (org.folio.rest.acq.model.orders.PoLineCollection)5 Test (org.junit.Test)4 KafkaConfig (com.bakdata.quick.common.config.KafkaConfig)2 QuickTopicData (com.bakdata.quick.common.type.QuickTopicData)2 KafkaIngestService (com.bakdata.quick.gateway.ingest.KafkaIngestService)2 RegexPattern (com.github.tomakehurst.wiremock.matching.RegexPattern)2 UrlPathPattern (com.github.tomakehurst.wiremock.matching.UrlPathPattern)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 ArrayList (java.util.ArrayList)2