Search in sources :

Example 11 with Authority

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

the class ExternalStorageModuleAuthorityRecordCollectionExamples method shouldRetrieveId.

@Test
public void shouldRetrieveId() {
    String authorityId = UUID.randomUUID().toString();
    Authority authority = new Authority().withId(authorityId);
    assertEquals(authorityId, storage.getId(authority));
}
Also used : Authority(org.folio.Authority) Test(org.junit.Test)

Example 12 with Authority

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

the class UpdateAuthorityQuickMarcEventHandlerTest method shouldProcessEvent.

@Test
public void shouldProcessEvent() {
    HashMap<String, String> eventPayload = new HashMap<>();
    eventPayload.put("RECORD_TYPE", "MARC_AUTHORITY");
    eventPayload.put("MARC_AUTHORITY", record.encode());
    eventPayload.put("MAPPING_RULES", mappingRules.encode());
    eventPayload.put("MAPPING_PARAMS", new JsonObject().encode());
    eventPayload.put("RELATED_RECORD_VERSION", AUTHORITY_VERSION.toString());
    Future<Authority> future = updateAuthorityQuickMarcEventHandler.handle(eventPayload);
    Authority updatedAuthority = future.result();
    Assert.assertNotNull(updatedAuthority);
    Assert.assertEquals(AUTHORITY_ID, updatedAuthority.getId());
    Assert.assertEquals(AUTHORITY_VERSION, updatedAuthority.getVersion());
    Assert.assertNotNull(updatedAuthority.getIdentifiers());
    Assert.assertEquals(4, updatedAuthority.getIdentifiers().size());
    Assert.assertNotNull(updatedAuthority.getNotes());
    ArgumentCaptor<Context> argument = ArgumentCaptor.forClass(Context.class);
    verify(authorityUpdateDelegate).handle(any(), any(), argument.capture());
    Assert.assertEquals("token", argument.getValue().getToken());
    Assert.assertEquals("dummy", argument.getValue().getTenantId());
    Assert.assertEquals("http://localhost", argument.getValue().getOkapiLocation());
}
Also used : Context(org.folio.inventory.common.Context) HashMap(java.util.HashMap) Authority(org.folio.Authority) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 13 with Authority

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

the class UpdateAuthorityQuickMarcEventHandlerTest method setUp.

@Before
public void setUp() throws IOException {
    existingAuthority = new JsonObject(TestUtil.readFileFromPath(AUTHORITY_PATH)).mapTo(Authority.class);
    authorityUpdateDelegate = Mockito.spy(new AuthorityUpdateDelegate(storage));
    updateAuthorityQuickMarcEventHandler = new UpdateAuthorityQuickMarcEventHandler(authorityUpdateDelegate, context);
    when(storage.getAuthorityRecordCollection(any())).thenReturn(authorityRecordCollection);
    doAnswer(invocationOnMock -> {
        Consumer<Success<Authority>> successHandler = invocationOnMock.getArgument(1);
        successHandler.accept(new Success<>(existingAuthority));
        return null;
    }).when(authorityRecordCollection).findById(anyString(), any(), any());
    doAnswer(invocationOnMock -> {
        Authority authority = invocationOnMock.getArgument(0);
        Consumer<Success<Authority>> successHandler = invocationOnMock.getArgument(1);
        successHandler.accept(new Success<>(authority));
        return null;
    }).when(authorityRecordCollection).update(any(), any(), any());
    when(context.getTenantId()).thenReturn("dummy");
    when(context.getToken()).thenReturn("token");
    when(context.getOkapiLocation()).thenReturn("http://localhost");
    mappingRules = new JsonObject(TestUtil.readFileFromPath(MAPPING_RULES_PATH));
    record = new JsonObject(TestUtil.readFileFromPath(RECORD_PATH));
}
Also used : Authority(org.folio.Authority) JsonObject(io.vertx.core.json.JsonObject) UpdateAuthorityQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.UpdateAuthorityQuickMarcEventHandler) AuthorityUpdateDelegate(org.folio.inventory.dataimport.handlers.actions.AuthorityUpdateDelegate) Success(org.folio.inventory.common.domain.Success) Before(org.junit.Before)

Example 14 with Authority

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

the class AuthorityUpdateDelegate method mergeRecords.

private Future<Authority> mergeRecords(Authority existingRecord, Authority mappedRecord) {
    try {
        mappedRecord.setId(existingRecord.getId());
        JsonObject existing = JsonObject.mapFrom(existingRecord);
        JsonObject mapped = JsonObject.mapFrom(mappedRecord);
        JsonObject merged = existing.mergeIn(mapped);
        Authority mergedAuthorityRecord = merged.mapTo(Authority.class);
        return Future.succeededFuture(mergedAuthorityRecord);
    } catch (Exception e) {
        LOGGER.error("Error updating authority", e);
        return Future.failedFuture(e);
    }
}
Also used : Authority(org.folio.Authority) JsonObject(io.vertx.core.json.JsonObject) DataImportException(org.folio.inventory.dataimport.exceptions.DataImportException)

Example 15 with Authority

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

the class AuthorityUpdateDelegate method handle.

public Future<Authority> handle(Map<String, String> eventPayload, Record marcRecord, Context context) {
    try {
        JsonObject mappingRules = new JsonObject(eventPayload.get(MAPPING_RULES_KEY));
        MappingParameters mappingParameters = new JsonObject(eventPayload.get(MAPPING_PARAMS_KEY)).mapTo(MappingParameters.class);
        JsonObject parsedRecord = retrieveParsedContent(marcRecord.getParsedRecord());
        String authorityId = marcRecord.getExternalIdsHolder().getAuthorityId();
        RecordMapper<Authority> recordMapper = RecordMapperBuilder.buildMapper(MARC_FORMAT);
        var mappedAuthority = recordMapper.mapRecord(parsedRecord, mappingParameters, mappingRules);
        AuthorityRecordCollection authorityRecordCollection = storage.getAuthorityRecordCollection(context);
        return getAuthorityRecordById(authorityId, authorityRecordCollection).onSuccess(existingAuthorityRecord -> fillVersion(existingAuthorityRecord, eventPayload)).compose(existingAuthorityRecord -> mergeRecords(existingAuthorityRecord, mappedAuthority)).compose(updatedAuthorityRecord -> updateAuthorityRecord(updatedAuthorityRecord, authorityRecordCollection));
    } catch (Exception e) {
        LOGGER.error("Error updating Authority", e);
        return Future.failedFuture(e);
    }
}
Also used : Context(org.folio.inventory.common.Context) AuthorityRecordCollection(org.folio.inventory.domain.AuthorityRecordCollection) Record(org.folio.rest.jaxrs.model.Record) Promise(io.vertx.core.Promise) Future(io.vertx.core.Future) String.format(java.lang.String.format) RecordMapperBuilder(org.folio.processing.mapping.defaultmapper.RecordMapperBuilder) Storage(org.folio.inventory.storage.Storage) Logger(org.apache.logging.log4j.Logger) Authority(org.folio.Authority) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) DataImportException(org.folio.inventory.dataimport.exceptions.DataImportException) LogManager(org.apache.logging.log4j.LogManager) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) RecordMapper(org.folio.processing.mapping.defaultmapper.RecordMapper) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) Authority(org.folio.Authority) JsonObject(io.vertx.core.json.JsonObject) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) AuthorityRecordCollection(org.folio.inventory.domain.AuthorityRecordCollection) DataImportException(org.folio.inventory.dataimport.exceptions.DataImportException)

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