Search in sources :

Example 1 with Authority

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

the class CreateAuthorityEventHandler method processAuthority.

@Override
protected Future<Authority> processAuthority(Authority authority, AuthorityRecordCollection authorityCollection, DataImportEventPayload payload) {
    Promise<Authority> promise = Promise.promise();
    createRelationship(promise, authority, payload);
    authorityCollection.add(authority, success -> promise.complete(success.getResult()), failure -> {
        // This is temporary solution (verify by error message). It will be improved via another solution by https://issues.folio.org/browse/RMB-899.
        if (isNotBlank(failure.getReason()) && failure.getReason().contains(UNIQUE_ID_ERROR_MESSAGE)) {
            LOGGER.info("Duplicated event received by AuthorityId: {}. Ignoring...", authority.getId());
            promise.fail(new DuplicateEventException(format("Duplicated event by Authority id: %s", authority.getId())));
        } else {
            LOGGER.error(String.format(FAILED_CREATING_AUTHORITY_MSG_TEMPLATE, failure.getReason(), failure.getStatusCode()));
            promise.fail(failure.getReason());
        }
    });
    return promise.future();
}
Also used : DuplicateEventException(org.folio.kafka.exception.DuplicateEventException) Authority(org.folio.Authority)

Example 2 with Authority

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

the class AbstractAuthorityEventHandler method mapAuthority.

private Future<Authority> mapAuthority(DataImportEventPayload payload, MappingMetadataDto mappingMetadata) {
    try {
        var mappingRules = new JsonObject(mappingMetadata.getMappingRules());
        var mappingParameters = Json.decodeValue(mappingMetadata.getMappingParams(), MappingParameters.class);
        var parsedRecord = new JsonObject((String) new JsonObject(payload.getContext().get(sourceRecordType().value())).mapTo(Record.class).getParsedRecord().getContent());
        RecordMapper<Authority> recordMapper = RecordMapperBuilder.buildMapper(sourceRecordType().value());
        var authority = recordMapper.mapRecord(parsedRecord, mappingParameters, mappingRules);
        authority.setSource(Authority.Source.MARC);
        return Future.succeededFuture(authority);
    } catch (Exception e) {
        var message = format(MAPPING_FAILED_MSG_PATTERN, sourceRecordType(), targetRecordType());
        LOGGER.error(constructMsg(message, payload), e);
        return Future.failedFuture(new JsonMappingException(message, e));
    }
}
Also used : Authority(org.folio.Authority) JsonMappingException(org.folio.inventory.validation.exceptions.JsonMappingException) JsonObject(io.vertx.core.json.JsonObject) JsonMappingException(org.folio.inventory.validation.exceptions.JsonMappingException) EventProcessingException(org.folio.processing.exceptions.EventProcessingException)

Example 3 with Authority

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

the class MatchAuthorityEventHandlerUnitTest method shouldPutMultipleMatchResultToPayloadOnHandleEventPayload.

@Test
public void shouldPutMultipleMatchResultToPayloadOnHandleEventPayload(TestContext testContext) throws UnsupportedEncodingException {
    Async async = testContext.async();
    List<Authority> matchedAuthorities = List.of(new Authority().withId(AUTHORITY_ID), new Authority().withId(UUID.randomUUID().toString()));
    MatchDetail personalNameMatchDetail = new MatchDetail().withMatchCriterion(EXACTLY_MATCHES).withExistingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(singletonList(new Field().withLabel("personalName").withValue("authority.personalName"))));
    doAnswer(invocation -> {
        Consumer<Success<MultipleRecords<Authority>>> successHandler = invocation.getArgument(2);
        Success<MultipleRecords<Authority>> result = new Success<>(new MultipleRecords<>(matchedAuthorities, 2));
        successHandler.accept(result);
        return null;
    }).when(collection).findByCql(eq(format("personalName == \"%s\"", PERSONAL_NAME)), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    EventHandler eventHandler = new MatchAuthorityEventHandler(mappingMetadataCache);
    HashMap<String, String> context = new HashMap<>();
    context.put("MAPPING_PARAMS", new JsonObject().encode());
    DataImportEventPayload eventPayload = createEventPayload(personalNameMatchDetail).withContext(context);
    eventPayload.getCurrentNode().setChildSnapshotWrappers(List.of(new ProfileSnapshotWrapper().withContent(new MatchProfile().withExistingRecordType(AUTHORITY).withIncomingRecordType(MARC_BIBLIOGRAPHIC)).withContentType(MATCH_PROFILE).withReactTo(MATCH)));
    eventHandler.handle(eventPayload).whenComplete((processedPayload, throwable) -> testContext.verify(v -> {
        testContext.assertNull(throwable);
        testContext.assertEquals(1, processedPayload.getEventsChain().size());
        testContext.assertEquals(processedPayload.getEventsChain(), List.of(DI_SRS_MARC_AUTHORITY_RECORD_CREATED.value()));
        testContext.assertEquals(DI_INVENTORY_AUTHORITY_MATCHED.value(), processedPayload.getEventType());
        assertThat(new JsonArray(processedPayload.getContext().get(MULTI_MATCH_IDS)), hasItems(matchedAuthorities.get(0).getId(), matchedAuthorities.get(1).getId()));
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) MappingMetadataDto(org.folio.MappingMetadataDto) EventHandler(org.folio.processing.events.services.handler.EventHandler) AuthorityRecordCollection(org.folio.inventory.domain.AuthorityRecordCollection) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Failure(org.folio.inventory.common.domain.Failure) AuthorityLoader(org.folio.inventory.dataimport.handlers.matching.loaders.AuthorityLoader) Matchers.hasItems(org.hamcrest.Matchers.hasItems) MarcValueReaderImpl(org.folio.processing.matching.reader.MarcValueReaderImpl) Collections.singletonList(java.util.Collections.singletonList) MockitoAnnotations(org.mockito.MockitoAnnotations) Mockito.doThrow(org.mockito.Mockito.doThrow) MULTI_MATCH_IDS(org.folio.inventory.dataimport.handlers.matching.loaders.AbstractLoader.MULTI_MATCH_IDS) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) Arrays.asList(java.util.Arrays.asList) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JsonObject(io.vertx.core.json.JsonObject) MATCH(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ReactTo.MATCH) MatcherAssert.assertThat(org.hamcrest.junit.MatcherAssert.assertThat) MissingValue(org.folio.processing.value.MissingValue) AUTHORITY(org.folio.rest.jaxrs.model.EntityType.AUTHORITY) DataImportEventPayload(org.folio.DataImportEventPayload) UUID(java.util.UUID) DI_INVENTORY_AUTHORITY_MATCHED(org.folio.DataImportEventTypes.DI_INVENTORY_AUTHORITY_MATCHED) Future(io.vertx.core.Future) String.format(java.lang.String.format) Storage(org.folio.inventory.storage.Storage) List(java.util.List) StringValue(org.folio.processing.value.StringValue) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DI_SRS_MARC_AUTHORITY_RECORD_CREATED(org.folio.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_CREATED) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Async(io.vertx.ext.unit.Async) Json(io.vertx.core.json.Json) Context(org.folio.inventory.common.Context) DI_INVENTORY_AUTHORITY_NOT_MATCHED(org.folio.DataImportEventTypes.DI_INVENTORY_AUTHORITY_NOT_MATCHED) MARC_AUTHORITY(org.folio.rest.jaxrs.model.EntityType.MARC_AUTHORITY) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) EXACTLY_MATCHES(org.folio.MatchDetail.MatchCriterion.EXACTLY_MATCHES) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) ArrayList(java.util.ArrayList) MARC_BIBLIOGRAPHIC(org.folio.rest.jaxrs.model.EntityType.MARC_BIBLIOGRAPHIC) MAPPING_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.MAPPING_PROFILE) MatchProfile(org.folio.MatchProfile) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) ListValue(org.folio.processing.value.ListValue) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) MatchValueReaderFactory(org.folio.processing.matching.reader.MatchValueReaderFactory) MatchDetail(org.folio.MatchDetail) Identifier___(org.folio.Identifier___) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) Vertx(io.vertx.core.Vertx) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Mockito.when(org.mockito.Mockito.when) VALUE_FROM_RECORD(org.folio.rest.jaxrs.model.MatchExpression.DataValueType.VALUE_FROM_RECORD) MATCH_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.MATCH_PROFILE) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) Mockito(org.mockito.Mockito) Authority(org.folio.Authority) Field(org.folio.rest.jaxrs.model.Field) Success(org.folio.inventory.common.domain.Success) MatchValueLoaderFactory(org.folio.processing.matching.loader.MatchValueLoaderFactory) MappingMetadataCache(org.folio.inventory.dataimport.cache.MappingMetadataCache) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Authority(org.folio.Authority) HashMap(java.util.HashMap) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) JsonArray(io.vertx.core.json.JsonArray) Field(org.folio.rest.jaxrs.model.Field) MatchProfile(org.folio.MatchProfile) 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 4 with Authority

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

the class MatchAuthorityEventHandlerUnitTest method shouldFailOnHandleEventPayloadIfMatchedMultipleAuthorityRecords.

@Test
public void shouldFailOnHandleEventPayloadIfMatchedMultipleAuthorityRecords(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);
    doAnswer(ans -> {
        Consumer<Success<MultipleRecords<Authority>>> callback = ans.getArgument(2);
        Success<MultipleRecords<Authority>> result = new Success<>(new MultipleRecords<>(asList(createAuthority(), createAuthority()), 2));
        callback.accept(result);
        return null;
    }).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 : 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 5 with Authority

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

the class MatchAuthorityEventHandlerUnitTest method shouldMatchWithSubMatchByAuthorityOnHandleEventPayload.

@Test
public void shouldMatchWithSubMatchByAuthorityOnHandleEventPayload(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"))));
    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("personalName == \"%s\" AND id == \"%s\"", PERSONAL_NAME, AUTHORITY_ID)), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    EventHandler eventHandler = new MatchAuthorityEventHandler(mappingMetadataCache);
    HashMap<String, String> context = new HashMap<>();
    context.put(AUTHORITY.value(), JsonObject.mapFrom(createAuthority()).encode());
    context.put("MAPPING_PARAMS", new JsonObject().encode());
    DataImportEventPayload eventPayload = createEventPayload(personalNameMatchDetail).withContext(context);
    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 : PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Authority(org.folio.Authority) HashMap(java.util.HashMap) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchAuthorityEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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

Authority (org.folio.Authority)20 JsonObject (io.vertx.core.json.JsonObject)13 Test (org.junit.Test)13 Success (org.folio.inventory.common.domain.Success)11 Async (io.vertx.ext.unit.Async)8 Consumer (java.util.function.Consumer)8 DataImportEventPayload (org.folio.DataImportEventPayload)8 MatchDetail (org.folio.MatchDetail)8 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)8 MultipleRecords (org.folio.inventory.common.domain.MultipleRecords)8 MatchAuthorityEventHandler (org.folio.inventory.dataimport.handlers.matching.MatchAuthorityEventHandler)8 EventHandler (org.folio.processing.events.services.handler.EventHandler)8 Field (org.folio.rest.jaxrs.model.Field)8 MatchExpression (org.folio.rest.jaxrs.model.MatchExpression)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 HashMap (java.util.HashMap)5 Context (org.folio.inventory.common.Context)4 Before (org.junit.Before)4 Future (io.vertx.core.Future)2 TestContext (io.vertx.ext.unit.TestContext)2