Search in sources :

Example 1 with Event

use of org.folio.rest.jaxrs.model.Event in project mod-circulation-storage by folio-org.

the class RequestExpirationApiTest method assertPublishedEvents.

private void assertPublishedEvents(List<JsonObject> events) {
    events.forEach(e -> {
        Event event = e.mapTo(Event.class);
        assertThat(event.getEventType(), is(LOG_RECORD.name()));
        JsonObject payload = new JsonObject(event.getEventPayload()).getJsonObject(PAYLOAD.value());
        Request original = payload.getJsonObject(REQUESTS.value()).getJsonObject(ORIGINAL.value()).mapTo(Request.class);
        Request updated = payload.getJsonObject(REQUESTS.value()).getJsonObject(UPDATED.value()).mapTo(Request.class);
        assertThat(original.getStatus(), not(equalTo(updated.getStatus())));
    });
}
Also used : Request(org.folio.rest.jaxrs.model.Request) Event(org.folio.rest.jaxrs.model.Event) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with Event

use of org.folio.rest.jaxrs.model.Event in project mod-inventory by folio-org.

the class QuickMarcKafkaHandler method handle.

@Override
public Future<String> handle(KafkaConsumerRecord<String, String> record) {
    var params = new OkapiConnectionParams(kafkaHeadersToMap(record.headers()), vertx);
    var context = constructContext(params.getTenantId(), params.getToken(), params.getOkapiUrl());
    Event event = Json.decodeValue(record.value(), Event.class);
    LOGGER.info("Quick marc event payload has been received with event type: {}", event.getEventType());
    return getEventPayload(event).compose(eventPayload -> processPayload(eventPayload, context).compose(recordType -> sendEvent(eventPayload, getReplyEventType(recordType), params)).recover(throwable -> sendErrorEvent(params, eventPayload, throwable)).map(ar -> record.key()), th -> {
        LOGGER.error("Update record state was failed while handle event, {}", th.getMessage());
        return Future.failedFuture(th.getMessage());
    });
}
Also used : QM_INVENTORY_AUTHORITY_UPDATED(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_INVENTORY_AUTHORITY_UPDATED) Json(io.vertx.core.json.Json) Context(org.folio.inventory.common.Context) AbstractQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.AbstractQuickMarcEventHandler) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata) KafkaTopicNameHelper.getDefaultNameSpace(org.folio.kafka.KafkaTopicNameHelper.getDefaultNameSpace) KafkaHeaderUtils.kafkaHeadersFromMap(org.folio.kafka.KafkaHeaderUtils.kafkaHeadersFromMap) HashMap(java.util.HashMap) KafkaProducer(io.vertx.kafka.client.producer.KafkaProducer) EventHandlingUtil.constructContext(org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.constructContext) UpdateHoldingsQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.UpdateHoldingsQuickMarcEventHandler) HoldingsUpdateDelegate(org.folio.inventory.dataimport.handlers.actions.HoldingsUpdateDelegate) Map(java.util.Map) QM_INVENTORY_HOLDINGS_UPDATED(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_INVENTORY_HOLDINGS_UPDATED) KafkaHeaderUtils.kafkaHeadersToMap(org.folio.kafka.KafkaHeaderUtils.kafkaHeadersToMap) QMEventTypes(org.folio.inventory.dataimport.handlers.QMEventTypes) Event(org.folio.rest.jaxrs.model.Event) PrecedingSucceedingTitlesHelper(org.folio.inventory.dataimport.handlers.actions.PrecedingSucceedingTitlesHelper) KafkaTopicNameHelper.formatTopicName(org.folio.kafka.KafkaTopicNameHelper.formatTopicName) Record(org.folio.rest.jaxrs.model.Record) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) UpdateInstanceQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.UpdateInstanceQuickMarcEventHandler) UUID(java.util.UUID) UpdateAuthorityQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.UpdateAuthorityQuickMarcEventHandler) AsyncRecordHandler(org.folio.kafka.AsyncRecordHandler) QM_INVENTORY_INSTANCE_UPDATED(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_INVENTORY_INSTANCE_UPDATED) Future(io.vertx.core.Future) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams) Storage(org.folio.inventory.storage.Storage) InstanceUpdateDelegate(org.folio.inventory.dataimport.handlers.actions.InstanceUpdateDelegate) AtomicLong(java.util.concurrent.atomic.AtomicLong) Logger(org.apache.logging.log4j.Logger) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) KafkaConsumerRecord(io.vertx.kafka.client.consumer.KafkaConsumerRecord) AuthorityUpdateDelegate(org.folio.inventory.dataimport.handlers.actions.AuthorityUpdateDelegate) QM_ERROR(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_ERROR) KafkaProducerRecord(io.vertx.kafka.client.producer.KafkaProducerRecord) LogManager(org.apache.logging.log4j.LogManager) ConsumerWrapperUtil(org.folio.inventory.dataimport.util.ConsumerWrapperUtil) KafkaConfig(org.folio.kafka.KafkaConfig) KafkaProducer.createShared(io.vertx.kafka.client.producer.KafkaProducer.createShared) Event(org.folio.rest.jaxrs.model.Event) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams)

Example 3 with Event

use of org.folio.rest.jaxrs.model.Event in project mod-inventory by folio-org.

the class DataImportKafkaHandlerTest method shouldReturnSucceededFutureWhenProcessingCoreHandlerSucceeded.

@Test
public void shouldReturnSucceededFutureWhenProcessingCoreHandlerSucceeded(TestContext context) {
    // given
    Async async = context.async();
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withTenant(TENANT_ID).withOkapiUrl(mockServer.baseUrl()).withToken("test-token").withContext(new HashMap<>(Map.of("JOB_PROFILE_SNAPSHOT_ID", profileSnapshotWrapper.getId())));
    Event event = new Event().withId("01").withEventPayload(Json.encode(dataImportEventPayload));
    String expectedKafkaRecordKey = "test_key";
    when(kafkaRecord.key()).thenReturn(expectedKafkaRecordKey);
    when(kafkaRecord.value()).thenReturn(Json.encode(event));
    when(kafkaRecord.headers()).thenReturn(List.of(KafkaHeader.header(RECORD_ID_HEADER, UUID.randomUUID().toString()), KafkaHeader.header(CHUNK_ID_HEADER, UUID.randomUUID().toString())));
    EventHandler mockedEventHandler = mock(EventHandler.class);
    when(mockedEventHandler.isEligible(any(DataImportEventPayload.class))).thenReturn(true);
    when(mockedEventHandler.handle(any(DataImportEventPayload.class))).thenReturn(CompletableFuture.completedFuture(new DataImportEventPayload()));
    EventManager.registerEventHandler(mockedEventHandler);
    // when
    Future<String> future = dataImportKafkaHandler.handle(kafkaRecord);
    // then
    future.onComplete(ar -> {
        context.assertTrue(ar.succeeded());
        context.assertEquals(expectedKafkaRecordKey, ar.result());
        async.complete();
    });
}
Also used : Async(io.vertx.ext.unit.Async) Event(org.folio.rest.jaxrs.model.Event) EventHandler(org.folio.processing.events.services.handler.EventHandler) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test)

Example 4 with Event

use of org.folio.rest.jaxrs.model.Event in project mod-inventory by folio-org.

the class MarcBibInstanceHridSetKafkaHandlerTest method shouldReturnFailedFutureWhenPayloadHasNoMarcRecord.

@Test
public void shouldReturnFailedFutureWhenPayloadHasNoMarcRecord(TestContext context) throws IOException {
    // given
    Async async = context.async();
    Map<String, String> payload = new HashMap<>();
    payload.put(JOB_EXECUTION_ID_KEY, UUID.randomUUID().toString());
    Event event = new Event().withId("01").withEventPayload(Json.encode(payload));
    when(kafkaRecord.value()).thenReturn(Json.encode(event));
    // when
    Future<String> future = marcBibInstanceHridSetKafkaHandler.handle(kafkaRecord);
    // then
    future.onComplete(ar -> {
        context.assertTrue(ar.failed());
        async.complete();
    });
}
Also used : HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) Event(org.folio.rest.jaxrs.model.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with Event

use of org.folio.rest.jaxrs.model.Event in project mod-inventory by folio-org.

the class MarcBibInstanceHridSetKafkaHandlerTest method shouldReturnFailedIfOLErrorExist.

@Test
public void shouldReturnFailedIfOLErrorExist(TestContext context) {
    // given
    Async async = context.async();
    Map<String, String> payload = new HashMap<>();
    payload.put("MARC_BIB", Json.encode(record));
    payload.put(JOB_EXECUTION_ID_KEY, UUID.randomUUID().toString());
    payload.put("CURRENT_RETRY_NUMBER", "1");
    Event event = new Event().withId("01").withEventPayload(Json.encode(payload));
    String expectedKafkaRecordKey = "test_key";
    when(kafkaRecord.key()).thenReturn(expectedKafkaRecordKey);
    when(kafkaRecord.value()).thenReturn(Json.encode(event));
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure("Cannot update record 601a8dc4-dee7-48eb-b03f-d02fdf0debd0 because it has been changed (optimistic locking): Stored _version is 2, _version of request is 1", 409));
        return null;
    }).when(mockedInstanceCollection).update(any(), any(), any());
    // when
    Future<String> future = marcBibInstanceHridSetKafkaHandler.handle(kafkaRecord);
    // then
    future.onComplete(ar -> {
        context.assertTrue(ar.failed());
        async.complete();
    });
}
Also used : HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) Event(org.folio.rest.jaxrs.model.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Aggregations

Event (org.folio.rest.jaxrs.model.Event)90 Test (org.junit.Test)41 HashMap (java.util.HashMap)32 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)28 DataImportEventPayload (org.folio.DataImportEventPayload)27 KeyValue (net.mguenther.kafka.junit.KeyValue)22 Async (io.vertx.ext.unit.Async)21 KafkaConsumerRecord (io.vertx.kafka.client.consumer.KafkaConsumerRecord)16 OkapiConnectionParams (org.folio.dataimport.util.OkapiConnectionParams)16 Record (org.folio.rest.jaxrs.model.Record)14 LogManager (org.apache.logging.log4j.LogManager)12 Logger (org.apache.logging.log4j.Logger)12 ProfileSnapshotWrapper (org.folio.rest.jaxrs.model.ProfileSnapshotWrapper)12 Test (org.junit.jupiter.api.Test)12 KafkaHeader (io.vertx.kafka.client.producer.KafkaHeader)11 DuplicateEventException (org.folio.kafka.exception.DuplicateEventException)10 AbstractRestTest (org.folio.rest.impl.AbstractRestTest)10 RecordCollection (org.folio.rest.jaxrs.model.RecordCollection)10 RecordsBatchResponse (org.folio.rest.jaxrs.model.RecordsBatchResponse)10 Future (io.vertx.core.Future)9