Search in sources :

Example 16 with HoldingsRecord

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

the class MatchHoldingEventHandlerUnitTest method shouldFailOnHandleEventPayloadIfMatchedMultipleHoldings.

@Test
public void shouldFailOnHandleEventPayloadIfMatchedMultipleHoldings(TestContext testContext) throws UnsupportedEncodingException {
    Async async = testContext.async();
    doAnswer(ans -> {
        Consumer<Success<MultipleRecords<HoldingsRecord>>> callback = ans.getArgument(2);
        Success<MultipleRecords<HoldingsRecord>> result = new Success<>(new MultipleRecords<>(asList(createHolding(), createHolding()), 2));
        callback.accept(result);
        return null;
    }).when(holdingCollection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    EventHandler eventHandler = new MatchHoldingEventHandler(mappingMetadataCache);
    DataImportEventPayload eventPayload = createEventPayload();
    eventHandler.handle(eventPayload).whenComplete((updatedEventPayload, throwable) -> {
        testContext.assertNotNull(throwable);
        async.complete();
    });
}
Also used : MatchHoldingEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchHoldingEventHandler) HoldingsRecord(org.folio.HoldingsRecord) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Consumer(java.util.function.Consumer) Async(io.vertx.ext.unit.Async) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchItemEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchItemEventHandler) MatchHoldingEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchHoldingEventHandler) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test)

Example 17 with HoldingsRecord

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

the class MatchHoldingEventHandlerUnitTest method shouldPutMultipleMatchResultToPayloadOnHandleEventPayload.

@Test
public void shouldPutMultipleMatchResultToPayloadOnHandleEventPayload(TestContext testContext) throws UnsupportedEncodingException {
    Async async = testContext.async();
    List<HoldingsRecord> matchedHoldings = List.of(new HoldingsRecord().withId(HOLDING_ID), new HoldingsRecord().withId(UUID.randomUUID().toString()));
    doAnswer(invocation -> {
        Consumer<Success<MultipleRecords<HoldingsRecord>>> successHandler = invocation.getArgument(2);
        Success<MultipleRecords<HoldingsRecord>> result = new Success<>(new MultipleRecords<>(matchedHoldings, 2));
        successHandler.accept(result);
        return null;
    }).when(holdingCollection).findByCql(eq(format("hrid == \"%s\"", HOLDING_HRID)), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    EventHandler eventHandler = new MatchHoldingEventHandler(mappingMetadataCache);
    HashMap<String, String> context = new HashMap<>();
    context.put(MAPPING_PARAMS, LOCATIONS_PARAMS);
    context.put(RELATIONS, MATCHING_RELATIONS);
    DataImportEventPayload eventPayload = createEventPayload().withContext(context);
    eventPayload.getCurrentNode().setChildSnapshotWrappers(List.of(new ProfileSnapshotWrapper().withContent(new MatchProfile().withExistingRecordType(HOLDINGS).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(DI_INVENTORY_HOLDING_MATCHED.value(), processedPayload.getEventType());
        assertThat(new JsonArray(processedPayload.getContext().get(MULTI_MATCH_IDS)), hasItems(matchedHoldings.get(0).getId(), matchedHoldings.get(1).getId()));
        async.complete();
    }));
}
Also used : MatchHoldingEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchHoldingEventHandler) TestContext(io.vertx.ext.unit.TestContext) MappingMetadataDto(org.folio.MappingMetadataDto) EventHandler(org.folio.processing.events.services.handler.EventHandler) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Failure(org.folio.inventory.common.domain.Failure) 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) ITEM(org.folio.rest.jaxrs.model.EntityType.ITEM) HoldingsRecordCollection(org.folio.inventory.domain.HoldingsRecordCollection) 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) Instance(org.folio.Instance) DataImportEventPayload(org.folio.DataImportEventPayload) UUID(java.util.UUID) Future(io.vertx.core.Future) HoldingsRecord(org.folio.HoldingsRecord) 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) MatchItemEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchItemEventHandler) Optional(java.util.Optional) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Async(io.vertx.ext.unit.Async) Json(io.vertx.core.json.Json) Context(org.folio.inventory.common.Context) 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) DI_INVENTORY_HOLDING_NOT_MATCHED(org.folio.DataImportEventTypes.DI_INVENTORY_HOLDING_NOT_MATCHED) MARC_BIBLIOGRAPHIC(org.folio.rest.jaxrs.model.EntityType.MARC_BIBLIOGRAPHIC) MatchHoldingEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchHoldingEventHandler) EntityType(org.folio.rest.jaxrs.model.EntityType) MAPPING_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.MAPPING_PROFILE) MatchProfile(org.folio.MatchProfile) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) MatchValueReaderFactory(org.folio.processing.matching.reader.MatchValueReaderFactory) MatchDetail(org.folio.MatchDetail) HOLDINGS(org.folio.rest.jaxrs.model.EntityType.HOLDINGS) MatchExpression(org.folio.rest.jaxrs.model.MatchExpression) Vertx(io.vertx.core.Vertx) DI_INVENTORY_HOLDING_MATCHED(org.folio.DataImportEventTypes.DI_INVENTORY_HOLDING_MATCHED) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Mockito.when(org.mockito.Mockito.when) DI_SRS_MARC_BIB_RECORD_CREATED(org.folio.DataImportEventTypes.DI_SRS_MARC_BIB_RECORD_CREATED) 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) HoldingLoader(org.folio.inventory.dataimport.handlers.matching.loaders.HoldingLoader) 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) HashMap(java.util.HashMap) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchItemEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchItemEventHandler) MatchHoldingEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchHoldingEventHandler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) JsonArray(io.vertx.core.json.JsonArray) HoldingsRecord(org.folio.HoldingsRecord) MatchProfile(org.folio.MatchProfile) Consumer(java.util.function.Consumer) Async(io.vertx.ext.unit.Async) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) Test(org.junit.Test)

Example 18 with HoldingsRecord

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

the class CreateHoldingEventHandlerTest method setUp.

@Before
public void setUp() throws UnsupportedEncodingException {
    MockitoAnnotations.initMocks(this);
    MappingManager.clearReaderFactories();
    createHoldingEventHandler = new CreateHoldingEventHandler(storage, mappingMetadataCache, holdingsIdStorageService);
    doAnswer(invocationOnMock -> {
        MultipleRecords result = new MultipleRecords<>(new ArrayList<>(), 0);
        Consumer<Success<MultipleRecords>> successHandler = invocationOnMock.getArgument(2);
        successHandler.accept(new Success<>(result));
        return null;
    }).when(holdingsRecordsCollection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    doAnswer(invocationOnMock -> {
        HoldingsRecord holdingsRecord = invocationOnMock.getArgument(0);
        Consumer<Success<HoldingsRecord>> successHandler = invocationOnMock.getArgument(1);
        successHandler.accept(new Success<>(holdingsRecord));
        return null;
    }).when(holdingsRecordsCollection).add(any(), any(Consumer.class), any(Consumer.class));
    doAnswer(invocationOnMock -> {
        RecordToEntity recordToItem = RecordToEntity.builder().recordId(RECORD_ID).entityId(ITEM_ID).build();
        return Future.succeededFuture(recordToItem);
    }).when(holdingsIdStorageService).store(any(), any(), any());
    when(mappingMetadataCache.get(anyString(), any(Context.class))).thenReturn(Future.succeededFuture(Optional.of(new MappingMetadataDto().withMappingRules(new JsonObject().encode()).withMappingParams(Json.encode(new MappingParameters())))));
}
Also used : Context(org.folio.inventory.common.Context) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) JsonObject(io.vertx.core.json.JsonObject) MappingMetadataDto(org.folio.MappingMetadataDto) Success(org.folio.inventory.common.domain.Success) RecordToEntity(org.folio.inventory.domain.relationship.RecordToEntity) HoldingsRecord(org.folio.HoldingsRecord) Consumer(java.util.function.Consumer) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) Before(org.junit.Before)

Example 19 with HoldingsRecord

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

the class CreateMarcHoldingsEventHandlerTest method shouldNotProcessEventIfInstanceNotFoundByMarcHoldingsHrid.

@Test
public void shouldNotProcessEventIfInstanceNotFoundByMarcHoldingsHrid() throws IOException {
    doAnswer(invocationOnMock -> {
        MultipleRecords<Instance> result = new MultipleRecords<>(new ArrayList<>(), 1);
        Consumer<Success<MultipleRecords<Instance>>> successHandler = invocationOnMock.getArgument(2);
        successHandler.accept(new Success<>(result));
        return null;
    }).when(instanceRecordCollection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    when(storage.getHoldingsRecordCollection(any())).thenReturn(holdingsRecordsCollection);
    when(storage.getInstanceCollection(any())).thenReturn(instanceRecordCollection);
    HoldingsRecord holdings = new HoldingsRecord().withId(String.valueOf(UUID.randomUUID())).withHrid(String.valueOf(UUID.randomUUID())).withInstanceId(String.valueOf(UUID.randomUUID())).withSourceId(String.valueOf(UUID.randomUUID())).withHoldingsTypeId(String.valueOf(UUID.randomUUID())).withPermanentLocationId(PERMANENT_LOCATION_ID);
    var parsedHoldingsRecord = new JsonObject(TestUtil.readFileFromPath(PARSED_HOLDINGS_RECORD));
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(parsedHoldingsRecord.encode()));
    HashMap<String, String> context = new HashMap<>();
    context.put("HOLDINGS", new JsonObject(new ObjectMapper().writer().withDefaultPrettyPrinter().writeValueAsString(holdings)).encode());
    context.put(MARC_HOLDINGS.value(), Json.encode(record));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_SRS_MARC_HOLDING_RECORD_CREATED.value()).withJobExecutionId(UUID.randomUUID().toString()).withOkapiUrl(mockServer.baseUrl()).withContext(context).withProfileSnapshot(profileSnapshotWrapper).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    CompletableFuture<DataImportEventPayload> future = createMarcHoldingsEventHandler.handle(dataImportEventPayload);
    ExecutionException exception = Assert.assertThrows(ExecutionException.class, future::get);
    Assert.assertEquals("No instance id found for marc holdings with hrid: in00000000315", exception.getCause().getMessage());
}
Also used : PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Instance(org.folio.inventory.domain.instances.Instance) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) HoldingsRecord(org.folio.HoldingsRecord) Consumer(java.util.function.Consumer) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) HoldingsRecord(org.folio.HoldingsRecord) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) ExecutionException(java.util.concurrent.ExecutionException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 20 with HoldingsRecord

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

the class UpdateHoldingEventHandlerTest method shouldNotProcessEventIfHoldingIdIsNotExistsInContext.

@Test(expected = ExecutionException.class)
public void shouldNotProcessEventIfHoldingIdIsNotExistsInContext() throws InterruptedException, ExecutionException, TimeoutException {
    Reader fakeReader = Mockito.mock(Reader.class);
    String permanentLocationId = UUID.randomUUID().toString();
    when(fakeReader.read(any(MappingRule.class))).thenReturn(StringValue.of(permanentLocationId));
    when(fakeReaderFactory.createReader()).thenReturn(fakeReader);
    when(storage.getHoldingsRecordCollection(any())).thenReturn(holdingsRecordsCollection);
    MappingManager.registerReaderFactory(fakeReaderFactory);
    MappingManager.registerWriterFactory(new HoldingWriterFactory());
    String hrid = UUID.randomUUID().toString();
    String instanceId = String.valueOf(UUID.randomUUID());
    HoldingsRecord holdingsRecord = new HoldingsRecord().withInstanceId(instanceId).withHrid(hrid).withPermanentLocationId(permanentLocationId);
    HashMap<String, String> context = new HashMap<>();
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(PARSED_CONTENT_WITH_INSTANCE_ID));
    context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
    context.put(HOLDINGS.value(), Json.encode(holdingsRecord));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_HOLDING_UPDATED.value()).withContext(context).withProfileSnapshot(profileSnapshotWrapper).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    CompletableFuture<DataImportEventPayload> future = updateHoldingEventHandler.handle(dataImportEventPayload);
    future.get(5, TimeUnit.MILLISECONDS);
}
Also used : HoldingsRecord(org.folio.HoldingsRecord) HashMap(java.util.HashMap) HoldingWriterFactory(org.folio.inventory.dataimport.HoldingWriterFactory) Reader(org.folio.processing.mapping.mapper.reader.Reader) HoldingsRecord(org.folio.HoldingsRecord) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) MappingRule(org.folio.rest.jaxrs.model.MappingRule) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test)

Aggregations

HoldingsRecord (org.folio.HoldingsRecord)44 JsonObject (io.vertx.core.json.JsonObject)27 Test (org.junit.Test)26 DataImportEventPayload (org.folio.DataImportEventPayload)24 HashMap (java.util.HashMap)23 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)22 Record (org.folio.rest.jaxrs.model.Record)18 Success (org.folio.inventory.common.domain.Success)17 Consumer (java.util.function.Consumer)14 ParsedRecord (org.folio.rest.jaxrs.model.ParsedRecord)14 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)13 Context (org.folio.inventory.common.Context)12 MultipleRecords (org.folio.inventory.common.domain.MultipleRecords)12 EventHandler (org.folio.processing.events.services.handler.EventHandler)11 MappingParameters (org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters)11 Async (io.vertx.ext.unit.Async)8 MappingMetadataDto (org.folio.MappingMetadataDto)8 MatchItemEventHandler (org.folio.inventory.dataimport.handlers.matching.MatchItemEventHandler)8 HoldingsRecordCollection (org.folio.inventory.domain.HoldingsRecordCollection)8 Before (org.junit.Before)8