Search in sources :

Example 6 with ParsedRecord

use of org.folio.ParsedRecord in project mod-invoice by folio-org.

the class CreateInvoiceEventHandlerTest method shouldPublishDiErrorWithInvoiceLineErrorWhenOneOfInvoiceLinesCreationFailed.

@Test
public void shouldPublishDiErrorWithInvoiceLineErrorWhenOneOfInvoiceLinesCreationFailed() throws IOException, 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, mappingProfile);
    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(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 invoiceLines = Json.decodeValue(eventPayload.getContext().get(INVOICE_LINES_KEY), InvoiceLineCollection.class);
    assertEquals(3, invoiceLines.getTotalRecords());
    assertEquals(3, invoiceLines.getInvoiceLines().size());
    invoiceLines.getInvoiceLines().forEach(invLine -> assertEquals(createdInvoice.getId(), invLine.getInvoiceId()));
    assertNotNull(eventPayload.getContext().get(INVOICE_LINES_ERRORS_KEY));
    Map<Integer, String> invoiceLinesErrors = DatabindCodec.mapper().readValue(eventPayload.getContext().get(INVOICE_LINES_ERRORS_KEY), new TypeReference<>() {
    });
    assertEquals(1, invoiceLinesErrors.size());
    assertNull(invoiceLinesErrors.get(1));
    assertNull(invoiceLinesErrors.get(2));
    assertNotNull(invoiceLinesErrors.get(3));
}
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) 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 7 with ParsedRecord

use of org.folio.ParsedRecord in project mod-source-record-manager by folio-org.

the class ImportInvoiceJournalConsumerVerticleMockTest method shouldProcessInvoiceErrorEvent.

@Test
public void shouldProcessInvoiceErrorEvent(TestContext context) throws IOException {
    Async async = context.async();
    // given
    HashMap<String, String> payloadContext = new HashMap<>();
    JsonObject INVOICE_LINES_JSON = new JsonObject().put(FIELD_INVOICE_LINES, new JsonArray().add(new JsonObject().put(FIELD_ID, UUID.randomUUID().toString()).put(FIELD_DESCRIPTION, "Some description").put(FIELD_INVOICE_LINE_NUMBER, "").put("invoiceId", INVOICE_ID)).add(new JsonObject().put(FIELD_ID, UUID.randomUUID().toString()).put(FIELD_DESCRIPTION, "Some description 2").put(FIELD_INVOICE_LINE_NUMBER, null).put("invoiceId", INVOICE_ID))).put("totalRecords", 2);
    Record record = new Record().withId(UUID.randomUUID().toString()).withOrder(0).withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT));
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(INVOICE.value(), Json.encode(INVOICE_JSON));
    payloadContext.put(INVOICE_LINES_KEY, Json.encode(INVOICE_LINES_JSON));
    payloadContext.put(ERROR_KEY, "Some error in invoice.");
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_ERROR.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext).withProfileSnapshot(profileSnapshotWrapper).withEventsChain(List.of(DI_INVOICE_CREATED.value()));
    Mockito.doNothing().when(journalService).saveBatch(ArgumentMatchers.any(JsonArray.class), ArgumentMatchers.any(String.class));
    KafkaConsumerRecord<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(dataImportEventPayload);
    dataImportJournalKafkaHandler.handle(kafkaConsumerRecord);
    Mockito.verify(journalService).saveBatch(invoiceRecordCaptor.capture(), Mockito.anyString());
    JsonArray jsonArray = invoiceRecordCaptor.getValue();
    Assert.assertEquals(3, jsonArray.size());
    Assert.assertTrue(jsonArray.getJsonObject(0).getString(ERROR).length() > 0);
    Assert.assertEquals("Invoice: ", ActionStatus.ERROR.value(), jsonArray.getJsonObject(0).getString(ACTION_STATUS));
    Assert.assertTrue(jsonArray.getJsonObject(1).getString(ERROR).length() > 0);
    Assert.assertEquals("Invoice line 1:", ActionStatus.ERROR.value(), jsonArray.getJsonObject(1).getString(ACTION_STATUS));
    Assert.assertTrue(jsonArray.getJsonObject(2).getString(ERROR).length() > 0);
    Assert.assertEquals("Invoice line 2:", ActionStatus.ERROR.value(), jsonArray.getJsonObject(2).getString(ACTION_STATUS));
    async.complete();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) KafkaConsumerRecord(io.vertx.kafka.client.consumer.KafkaConsumerRecord) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) ParsedRecord(org.folio.ParsedRecord) DataImportEventPayload(org.folio.DataImportEventPayload) AbstractRestTest(org.folio.rest.impl.AbstractRestTest) Test(org.junit.Test)

Example 8 with ParsedRecord

use of org.folio.ParsedRecord in project mod-source-record-manager by folio-org.

the class ImportInvoiceJournalConsumerVerticleMockTest method shouldProcessInvoiceLineErrorEvent.

@Test
public void shouldProcessInvoiceLineErrorEvent(TestContext context) throws IOException {
    Async async = context.async();
    // given
    HashMap<String, String> payloadContext = new HashMap<>();
    Record record = new Record().withId(UUID.randomUUID().toString()).withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT));
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(INVOICE.value(), Json.encode(INVOICE_JSON));
    payloadContext.put(INVOICE_LINES_KEY, Json.encode(INVOICE_LINES_JSON));
    payloadContext.put(INVOICE_LINES_ERRORS_KEY, Json.encode(new JsonObject().put("2", "EventProcessingException: Error during invoice lines creation")));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_ERROR.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext).withProfileSnapshot(profileSnapshotWrapper).withEventsChain(List.of(DI_INVOICE_CREATED.value()));
    Mockito.doNothing().when(journalService).saveBatch(ArgumentMatchers.any(JsonArray.class), ArgumentMatchers.any(String.class));
    KafkaConsumerRecord<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(dataImportEventPayload);
    dataImportJournalKafkaHandler.handle(kafkaConsumerRecord);
    Mockito.verify(journalService).saveBatch(invoiceRecordCaptor.capture(), Mockito.anyString());
    JsonArray jsonArray = invoiceRecordCaptor.getValue();
    Assert.assertEquals(3, jsonArray.size());
    Assert.assertTrue(jsonArray.getJsonObject(2).getString(ERROR).length() > 0);
    Assert.assertEquals("Invoice line 2:", ActionStatus.ERROR.value(), jsonArray.getJsonObject(2).getString(ACTION_STATUS));
    async.complete();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) KafkaConsumerRecord(io.vertx.kafka.client.consumer.KafkaConsumerRecord) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) ParsedRecord(org.folio.ParsedRecord) DataImportEventPayload(org.folio.DataImportEventPayload) AbstractRestTest(org.folio.rest.impl.AbstractRestTest) Test(org.junit.Test)

Example 9 with ParsedRecord

use of org.folio.ParsedRecord in project mod-source-record-manager by folio-org.

the class ImportInvoiceJournalConsumerVerticleTest method testJournalInventoryInstanceCreatedAction.

@Test
public void testJournalInventoryInstanceCreatedAction(TestContext context) throws IOException {
    Async async = context.async();
    // given
    HashMap<String, String> payloadContext = new HashMap<>();
    Record record = new Record().withId(UUID.randomUUID().toString()).withOrder(0).withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT));
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(INVOICE.value(), Json.encode(INVOICE_JSON));
    payloadContext.put(INVOICE_LINES_KEY, Json.encode(INVOICE_LINES_JSON));
    payloadContext.put(INVOICE_LINES_ERRORS_KEY, Json.encode(new JsonObject().put("3", "EventProcessingException: Error during invoice lines creation")));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_ERROR.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withJobExecutionId(jobExecution.getId()).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext).withProfileSnapshot(profileSnapshotWrapper).withEventsChain(List.of(DI_INVOICE_CREATED.value()));
    // when
    KafkaConsumerRecord<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(dataImportEventPayload);
    dataImportJournalKafkaHandler.handle(kafkaConsumerRecord);
    // then
    Future<Optional<JobExecution>> future = jobExecutionDao.getJobExecutionById(jobExecution.getId(), TENANT_ID);
    future.onComplete(ar -> {
        if (ar.succeeded()) {
            context.assertTrue(ar.succeeded());
            Assert.assertNotNull(ar.result());
        }
    });
    async.complete();
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) KafkaConsumerRecord(io.vertx.kafka.client.consumer.KafkaConsumerRecord) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) ParsedRecord(org.folio.ParsedRecord) DataImportEventPayload(org.folio.DataImportEventPayload) AbstractRestTest(org.folio.rest.impl.AbstractRestTest) Test(org.junit.Test)

Example 10 with ParsedRecord

use of org.folio.ParsedRecord in project mod-source-record-manager by folio-org.

the class ImportInvoiceJournalConsumerVerticleMockTest method shouldProcessEvent.

@Test
public void shouldProcessEvent(TestContext context) throws IOException {
    Async async = context.async();
    // given
    HashMap<String, String> payloadContext = new HashMap<>();
    Record record = new Record().withId(UUID.randomUUID().toString()).withOrder(0).withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT));
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(INVOICE.value(), Json.encode(INVOICE_JSON));
    payloadContext.put(INVOICE_LINES_KEY, Json.encode(INVOICE_LINES_JSON));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVOICE_CREATED.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext).withProfileSnapshot(profileSnapshotWrapper);
    Mockito.doNothing().when(journalService).saveBatch(ArgumentMatchers.any(JsonArray.class), ArgumentMatchers.any(String.class));
    KafkaConsumerRecord<String, String> kafkaConsumerRecord = buildKafkaConsumerRecord(dataImportEventPayload);
    dataImportJournalKafkaHandler.handle(kafkaConsumerRecord);
    Mockito.verify(journalService).saveBatch(invoiceRecordCaptor.capture(), Mockito.anyString());
    JsonArray jsonArray = invoiceRecordCaptor.getValue();
    Assert.assertEquals(3, jsonArray.size());
    Assert.assertEquals("Invoice title:", INVOICE_TITLE, jsonArray.getJsonObject(0).getString("title"));
    Assert.assertEquals("Invoice line 1 -> title:", "Some description", jsonArray.getJsonObject(1).getString("title"));
    Assert.assertEquals("Invoice line 2 -> title:", "Some description 2", jsonArray.getJsonObject(2).getString("title"));
    async.complete();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) KafkaConsumerRecord(io.vertx.kafka.client.consumer.KafkaConsumerRecord) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) ParsedRecord(org.folio.ParsedRecord) DataImportEventPayload(org.folio.DataImportEventPayload) AbstractRestTest(org.folio.rest.impl.AbstractRestTest) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)15 DataImportEventPayload (org.folio.DataImportEventPayload)15 ParsedRecord (org.folio.ParsedRecord)15 Record (org.folio.Record)15 KeyValue (net.mguenther.kafka.junit.KeyValue)9 Event (org.folio.rest.jaxrs.model.Event)9 InvoiceLineCollection (org.folio.rest.jaxrs.model.InvoiceLineCollection)9 ProfileSnapshotWrapper (org.folio.rest.jaxrs.model.ProfileSnapshotWrapper)9 Test (org.junit.jupiter.api.Test)9 Invoice (org.folio.rest.jaxrs.model.Invoice)7 JsonArray (io.vertx.core.json.JsonArray)6 PoLine (org.folio.rest.acq.model.orders.PoLine)6 PoLineCollection (org.folio.rest.acq.model.orders.PoLineCollection)6 JsonObject (io.vertx.core.json.JsonObject)5 Async (io.vertx.ext.unit.Async)5 KafkaConsumerRecord (io.vertx.kafka.client.consumer.KafkaConsumerRecord)5 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)5 AbstractRestTest (org.folio.rest.impl.AbstractRestTest)5 Test (org.junit.Test)5 RequestContext (org.folio.rest.core.models.RequestContext)3