Search in sources :

Example 11 with MatchDetail

use of org.folio.MatchDetail in project mod-source-record-storage by folio-org.

the class MarcAuthorityMatchEventHandlerTest method shouldMatchBy010aField.

@Test
public void shouldMatchBy010aField(TestContext context) {
    Async async = context.async();
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(EntityType.MARC_AUTHORITY.value(), Json.encode(incomingRecord));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withContext(payloadContext).withTenant(TENANT_ID).withCurrentNode(new ProfileSnapshotWrapper().withId(UUID.randomUUID().toString()).withContentType(MATCH_PROFILE).withContent(new MatchProfile().withExistingRecordType(EntityType.MARC_AUTHORITY).withIncomingRecordType(EntityType.MARC_AUTHORITY).withMatchDetails(singletonList(new MatchDetail().withMatchCriterion(EXACTLY_MATCHES).withExistingRecordType(EntityType.MARC_AUTHORITY).withExistingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(Lists.newArrayList(new Field().withLabel("field").withValue("010"), new Field().withLabel("indicator1").withValue(""), new Field().withLabel("indicator2").withValue(""), new Field().withLabel("recordSubfield").withValue("a")))).withIncomingRecordType(EntityType.MARC_AUTHORITY).withIncomingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(Lists.newArrayList(new Field().withLabel("field").withValue("010"), new Field().withLabel("indicator1").withValue(""), new Field().withLabel("indicator2").withValue(""), new Field().withLabel("recordSubfield").withValue("a"))))))));
    recordDao.saveRecord(existingRecord, TENANT_ID).onComplete(context.asyncAssertSuccess()).onSuccess(existingSavedRecord -> handler.handle(dataImportEventPayload).whenComplete((updatedEventPayload, throwable) -> {
        context.assertNull(throwable);
        context.assertEquals(1, updatedEventPayload.getEventsChain().size());
        context.assertEquals(updatedEventPayload.getEventType(), DI_SRS_MARC_AUTHORITY_RECORD_MATCHED.value());
        context.assertEquals(new JsonObject(updatedEventPayload.getContext().get(MATCHED_MARC_KEY)).mapTo(Record.class), existingSavedRecord);
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Json(io.vertx.core.json.Json) EventHandler(org.folio.processing.events.services.handler.EventHandler) RecordDaoImpl(org.folio.dao.RecordDaoImpl) BeforeClass(org.junit.BeforeClass) MarcAuthorityMatchEventHandler(org.folio.services.handlers.match.MarcAuthorityMatchEventHandler) Date(java.util.Date) RawRecord(org.folio.rest.jaxrs.model.RawRecord) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) EXACTLY_MATCHES(org.folio.MatchDetail.MatchCriterion.EXACTLY_MATCHES) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) MockitoAnnotations(org.mockito.MockitoAnnotations) MARC_AUTHORITY(org.folio.rest.jaxrs.model.Record.RecordType.MARC_AUTHORITY) Lists(com.google.common.collect.Lists) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) ExternalIdsHolder(org.folio.rest.jaxrs.model.ExternalIdsHolder) TestUtil(org.folio.TestUtil) After(org.junit.After) EntityType(org.folio.rest.jaxrs.model.EntityType) MAPPING_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.MAPPING_PROFILE) DI_SRS_MARC_AUTHORITY_RECORD_CREATED(org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_CREATED) JsonObject(io.vertx.core.json.JsonObject) MatchProfile(org.folio.MatchProfile) MappingProfile(org.folio.MappingProfile) Before(org.junit.Before) MatchDetail(org.folio.MatchDetail) DI_SRS_MARC_AUTHORITY_RECORD_NOT_MATCHED(org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_NOT_MATCHED) RecordDao(org.folio.dao.RecordDao) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) Record(org.folio.rest.jaxrs.model.Record) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) SnapshotDaoUtil(org.folio.dao.util.SnapshotDaoUtil) UUID(java.util.UUID) DI_SRS_MARC_AUTHORITY_RECORD_MATCHED(org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_MATCHED) VALUE_FROM_RECORD(org.folio.rest.jaxrs.model.MatchExpression.DataValueType.VALUE_FROM_RECORD) MATCH_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.MATCH_PROFILE) List(java.util.List) Field(org.folio.rest.jaxrs.model.Field) Assert(org.junit.Assert) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Snapshot(org.folio.rest.jaxrs.model.Snapshot) Field(org.folio.rest.jaxrs.model.Field) MatchProfile(org.folio.MatchProfile) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) MatchDetail(org.folio.MatchDetail) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) DataImportEventPayload(org.folio.DataImportEventPayload) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) Test(org.junit.Test)

Example 12 with MatchDetail

use of org.folio.MatchDetail in project mod-source-record-storage by folio-org.

the class AbstractMarcMatchEventHandler method handle.

@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload payload) {
    CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
    HashMap<String, String> context = payload.getContext();
    if (context == null || context.isEmpty() || isEmpty(payload.getContext().get(typeConnection.getMarcType().value())) || Objects.isNull(payload.getCurrentNode()) || Objects.isNull(payload.getEventsChain())) {
        LOG.error(PAYLOAD_HAS_NO_DATA_MSG);
        future.completeExceptionally(new EventProcessingException(PAYLOAD_HAS_NO_DATA_MSG));
        return future;
    }
    payload.getEventsChain().add(payload.getEventType());
    String record = context.get(typeConnection.getMarcType().value());
    MatchDetail matchDetail = retrieveMatchDetail(payload);
    if (isValidMatchDetail(matchDetail)) {
        MatchField matchField = prepareMatchField(record, matchDetail);
        if (matchField.isDefaultField()) {
            processDefaultMatchField(matchField, payload.getTenant()).onSuccess(recordCollection -> processSucceededResult(recordCollection.getRecords(), payload, future)).onFailure(throwable -> future.completeExceptionally(new MatchingException(throwable)));
        } else {
            recordDao.getMatchedRecords(matchField, typeConnection, 0, 2, payload.getTenant()).onSuccess(recordList -> processSucceededResult(recordList, payload, future)).onFailure(throwable -> future.completeExceptionally(new MatchingException(throwable)));
        }
    } else {
        constructError(payload, format(MATCH_DETAIL_IS_NOT_VALID, matchDetail));
        future.complete(payload);
    }
    return future;
}
Also used : MatchField(org.folio.dao.util.MatchField) Json(io.vertx.core.json.Json) StringUtils(org.apache.commons.lang.StringUtils) RecordDaoUtil.filterRecordByRecordId(org.folio.dao.util.RecordDaoUtil.filterRecordByRecordId) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchingException(org.folio.processing.exceptions.MatchingException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) Value(org.folio.processing.value.Value) MatchProfile(org.folio.MatchProfile) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) MatchDetail(org.folio.MatchDetail) RecordDao(org.folio.dao.RecordDao) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) Record(org.folio.rest.jaxrs.model.Record) DataImportEventPayload(org.folio.DataImportEventPayload) RecordDaoUtil.filterRecordByState(org.folio.dao.util.RecordDaoUtil.filterRecordByState) RecordCollection(org.folio.rest.jaxrs.model.RecordCollection) Future(io.vertx.core.Future) VALUE_FROM_RECORD(org.folio.rest.jaxrs.model.MatchExpression.DataValueType.VALUE_FROM_RECORD) MATCH_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.MATCH_PROFILE) String.format(java.lang.String.format) MarcValueReaderUtil(org.folio.processing.matching.reader.util.MarcValueReaderUtil) Objects(java.util.Objects) List(java.util.List) RecordDaoUtil.filterRecordByExternalHrid(org.folio.dao.util.RecordDaoUtil.filterRecordByExternalHrid) Logger(org.apache.logging.log4j.Logger) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) TypeConnection(org.folio.services.util.TypeConnection) DataImportEventTypes(org.folio.rest.jaxrs.model.DataImportEventTypes) Field(org.folio.rest.jaxrs.model.Field) LogManager(org.apache.logging.log4j.LogManager) RecordDaoUtil.filterRecordByExternalId(org.folio.dao.util.RecordDaoUtil.filterRecordByExternalId) CompletableFuture(java.util.concurrent.CompletableFuture) MatchingException(org.folio.processing.exceptions.MatchingException) MatchDetail(org.folio.MatchDetail) DataImportEventPayload(org.folio.DataImportEventPayload) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) MatchField(org.folio.dao.util.MatchField)

Example 13 with MatchDetail

use of org.folio.MatchDetail in project mod-inventory by folio-org.

the class MatchAuthorityEventHandlerUnitTest method shouldMatchOnHandleEventPayloadFor999s.

@Test
public void shouldMatchOnHandleEventPayloadFor999s(TestContext testContext) throws UnsupportedEncodingException {
    Async async = testContext.async();
    MatchValueReaderFactory.clearReaderFactory();
    MatchDetail matchDetail999s = new MatchDetail().withMatchCriterion(EXACTLY_MATCHES).withExistingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(singletonList(new Field().withLabel("idField").withValue("authority.id"))));
    DataImportEventPayload eventPayload = createEventPayload(matchDetail999s);
    MarcValueReaderImpl marcValueReaderMock = Mockito.mock(MarcValueReaderImpl.class);
    when(marcValueReaderMock.isEligibleForEntityType(MARC_AUTHORITY)).thenReturn(true);
    when(marcValueReaderMock.read(eventPayload, matchDetail999s)).thenReturn(StringValue.of(AUTHORITY_ID));
    MatchValueReaderFactory.register(marcValueReaderMock);
    doAnswer(ans -> {
        Consumer<Success<MultipleRecords<Authority>>> callback = ans.getArgument(2);
        Success<MultipleRecords<Authority>> result = new Success<>(new MultipleRecords<>(singletonList(createAuthority()), 1));
        callback.accept(result);
        return null;
    }).when(collection).findByCql(eq(format("id == \"%s\"", AUTHORITY_ID)), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    EventHandler eventHandler = new MatchAuthorityEventHandler(mappingMetadataCache);
    eventHandler.handle(eventPayload).whenComplete((updatedEventPayload, throwable) -> {
        testContext.assertNull(throwable);
        testContext.assertEquals(1, updatedEventPayload.getEventsChain().size());
        testContext.assertEquals(updatedEventPayload.getEventsChain(), singletonList(DI_SRS_MARC_AUTHORITY_RECORD_CREATED.value()));
        testContext.assertEquals(DI_INVENTORY_AUTHORITY_MATCHED.value(), updatedEventPayload.getEventType());
        async.complete();
    });
}
Also used : MarcValueReaderImpl(org.folio.processing.matching.reader.MarcValueReaderImpl) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Authority(org.folio.Authority) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) Field(org.folio.rest.jaxrs.model.Field) Consumer(java.util.function.Consumer) Async(io.vertx.ext.unit.Async) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) MatchDetail(org.folio.MatchDetail) Test(org.junit.Test)

Example 14 with MatchDetail

use of org.folio.MatchDetail in project mod-inventory by folio-org.

the class MatchAuthorityEventHandlerUnitTest method shouldFailOnHandleEventPayloadIfExceptionThrown.

@Test
public void shouldFailOnHandleEventPayloadIfExceptionThrown(TestContext testContext) throws UnsupportedEncodingException {
    Async async = testContext.async();
    MatchDetail personalNameMatchDetail = new MatchDetail().withMatchCriterion(EXACTLY_MATCHES).withExistingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(singletonList(new Field().withLabel("personalName").withValue("authority.personalName"))));
    DataImportEventPayload eventPayload = createEventPayload(personalNameMatchDetail);
    doThrow(new UnsupportedEncodingException()).when(collection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    EventHandler eventHandler = new MatchAuthorityEventHandler(mappingMetadataCache);
    eventHandler.handle(eventPayload).whenComplete((updatedEventPayload, throwable) -> {
        testContext.assertNotNull(throwable);
        async.complete();
    });
}
Also used : Field(org.folio.rest.jaxrs.model.Field) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Consumer(java.util.function.Consumer) Async(io.vertx.ext.unit.Async) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) MatchDetail(org.folio.MatchDetail) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test)

Example 15 with MatchDetail

use of org.folio.MatchDetail in project mod-inventory by folio-org.

the class MatchAuthorityEventHandlerUnitTest method shouldMatchOnHandleEventPayloadFor001.

@Test
public void shouldMatchOnHandleEventPayloadFor001(TestContext testContext) throws UnsupportedEncodingException {
    Async async = testContext.async();
    MatchValueReaderFactory.clearReaderFactory();
    MatchDetail matchDetail001 = new MatchDetail().withMatchCriterion(EXACTLY_MATCHES).withExistingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(singletonList(new Field().withLabel("identifiersField").withValue("authority.identifiers[]"))));
    DataImportEventPayload eventPayload = createEventPayload(matchDetail001);
    MarcValueReaderImpl marcValueReaderMock = Mockito.mock(MarcValueReaderImpl.class);
    when(marcValueReaderMock.isEligibleForEntityType(MARC_AUTHORITY)).thenReturn(true);
    when(marcValueReaderMock.read(eventPayload, matchDetail001)).thenReturn(ListValue.of(singletonList(JsonObject.mapFrom(IDENTIFIER).toString())));
    MatchValueReaderFactory.register(marcValueReaderMock);
    doAnswer(ans -> {
        Consumer<Success<MultipleRecords<Authority>>> callback = ans.getArgument(2);
        Success<MultipleRecords<Authority>> result = new Success<>(new MultipleRecords<>(singletonList(createAuthority()), 1));
        callback.accept(result);
        return null;
    }).when(collection).findByCql(eq("identifiers=\\\"[{\\\"value\\\":\\\"955335\\\",\\\"identifierTypeId\\\":\\\"11bf5f7c-30e1-4308-8170-1fbb5b817cf2\\\"}]\\\""), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    EventHandler eventHandler = new MatchAuthorityEventHandler(mappingMetadataCache);
    eventHandler.handle(eventPayload).whenComplete((updatedEventPayload, throwable) -> {
        testContext.assertNull(throwable);
        testContext.assertEquals(1, updatedEventPayload.getEventsChain().size());
        testContext.assertEquals(updatedEventPayload.getEventsChain(), singletonList(DI_SRS_MARC_AUTHORITY_RECORD_CREATED.value()));
        testContext.assertEquals(DI_INVENTORY_AUTHORITY_MATCHED.value(), updatedEventPayload.getEventType());
        async.complete();
    });
}
Also used : MarcValueReaderImpl(org.folio.processing.matching.reader.MarcValueReaderImpl) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Authority(org.folio.Authority) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) Field(org.folio.rest.jaxrs.model.Field) Consumer(java.util.function.Consumer) Async(io.vertx.ext.unit.Async) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) MatchDetail(org.folio.MatchDetail) Test(org.junit.Test)

Aggregations

DataImportEventPayload (org.folio.DataImportEventPayload)22 MatchDetail (org.folio.MatchDetail)22 Field (org.folio.rest.jaxrs.model.Field)22 MatchExpression (org.folio.rest.jaxrs.model.MatchExpression)22 Async (io.vertx.ext.unit.Async)21 Test (org.junit.Test)21 EventHandler (org.folio.processing.events.services.handler.EventHandler)17 JsonObject (io.vertx.core.json.JsonObject)14 HashMap (java.util.HashMap)14 ArrayList (java.util.ArrayList)13 Json (io.vertx.core.json.Json)12 List (java.util.List)12 MatchProfile (org.folio.MatchProfile)12 VALUE_FROM_RECORD (org.folio.rest.jaxrs.model.MatchExpression.DataValueType.VALUE_FROM_RECORD)12 ProfileSnapshotWrapper (org.folio.rest.jaxrs.model.ProfileSnapshotWrapper)12 MATCH_PROFILE (org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.MATCH_PROFILE)12 TestContext (io.vertx.ext.unit.TestContext)11 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)11 Collections.singletonList (java.util.Collections.singletonList)11 UUID (java.util.UUID)11