Search in sources :

Example 26 with Success

use of org.folio.inventory.common.domain.Success 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 27 with Success

use of org.folio.inventory.common.domain.Success in project mod-inventory by folio-org.

the class CreateAuthorityEventHandlerTest method setUp.

@Before
public void setUp() throws IOException {
    MockitoAnnotations.openMocks(this);
    MappingManager.clearReaderFactories();
    MappingMetadataCache mappingMetadataCache = new MappingMetadataCache(vertx, vertx.createHttpClient(), 3600);
    createMarcAuthoritiesEventHandler = new CreateAuthorityEventHandler(storage, mappingMetadataCache, authorityIdStorageService);
    JsonObject mappingRules = new JsonObject(TestUtil.readFileFromPath(MAPPING_RULES_PATH));
    doAnswer(invocationOnMock -> {
        Authority authority = invocationOnMock.getArgument(0);
        Consumer<Success<Authority>> successHandler = invocationOnMock.getArgument(1);
        successHandler.accept(new Success<>(authority));
        return null;
    }).when(authorityCollection).add(any(), any(), any());
    doAnswer(invocationOnMock -> {
        RecordToEntity recordToItem = RecordToEntity.builder().recordId(RECORD_ID).entityId(AUTHORITY_ID).build();
        return Future.succeededFuture(recordToItem);
    }).when(authorityIdStorageService).store(any(), any(), any());
    WireMock.stubFor(get(new UrlPathPattern(new RegexPattern(MAPPING_METADATA_URL + "/.*"), true)).willReturn(WireMock.ok().withBody(Json.encode(new MappingMetadataDto().withMappingParams(Json.encode(new MappingParameters())).withMappingRules(mappingRules.encode())))));
    doAnswer(invocationOnMock -> completedStage(new Response(HttpStatus.SC_CREATED, null, null, null))).when(mockedClient).post(any(URL.class), any(JsonObject.class));
}
Also used : Authority(org.folio.Authority) RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) JsonObject(io.vertx.core.json.JsonObject) MappingMetadataDto(org.folio.MappingMetadataDto) Success(org.folio.inventory.common.domain.Success) RecordToEntity(org.folio.inventory.domain.relationship.RecordToEntity) URL(java.net.URL) Response(org.folio.inventory.support.http.client.Response) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) MappingMetadataCache(org.folio.inventory.dataimport.cache.MappingMetadataCache) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) Before(org.junit.Before)

Example 28 with Success

use of org.folio.inventory.common.domain.Success 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 29 with Success

use of org.folio.inventory.common.domain.Success in project mod-inventory by folio-org.

the class CreateInstanceEventHandlerTest method setUp.

@Before
public void setUp() throws IOException {
    MockitoAnnotations.openMocks(this);
    MappingManager.clearReaderFactories();
    JsonObject mappingRules = new JsonObject(TestUtil.readFileFromPath(MAPPING_RULES_PATH));
    WireMock.stubFor(get(new UrlPathPattern(new RegexPattern(MAPPING_METADATA_URL + "/.*"), true)).willReturn(WireMock.ok().withBody(Json.encode(new MappingMetadataDto().withMappingParams(Json.encode(new MappingParameters())).withMappingRules(mappingRules.toString())))));
    Vertx vertx = Vertx.vertx();
    createInstanceEventHandler = new CreateInstanceEventHandler(storage, new PrecedingSucceedingTitlesHelper(context -> mockedClient), new MappingMetadataCache(vertx, vertx.createHttpClient(new HttpClientOptions().setConnectTimeout(3000)), 3600), instanceIdStorageService);
    doAnswer(invocationOnMock -> {
        Instance instanceRecord = invocationOnMock.getArgument(0);
        Consumer<Success<Instance>> successHandler = invocationOnMock.getArgument(1);
        successHandler.accept(new Success<>(instanceRecord));
        return null;
    }).when(instanceRecordCollection).add(any(), any(Consumer.class), any(Consumer.class));
    doAnswer(invocationOnMock -> completedStage(createdResponse())).when(mockedClient).post(any(URL.class), any(JsonObject.class));
}
Also used : RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) Instance(org.folio.inventory.domain.instances.Instance) JsonObject(io.vertx.core.json.JsonObject) MappingMetadataDto(org.folio.MappingMetadataDto) Vertx(io.vertx.core.Vertx) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Success(org.folio.inventory.common.domain.Success) URL(java.net.URL) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) Consumer(java.util.function.Consumer) MappingMetadataCache(org.folio.inventory.dataimport.cache.MappingMetadataCache) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) Before(org.junit.Before)

Example 30 with Success

use of org.folio.inventory.common.domain.Success 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)

Aggregations

Success (org.folio.inventory.common.domain.Success)67 Consumer (java.util.function.Consumer)51 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)49 MultipleRecords (org.folio.inventory.common.domain.MultipleRecords)48 Test (org.junit.Test)46 DataImportEventPayload (org.folio.DataImportEventPayload)44 JsonObject (io.vertx.core.json.JsonObject)42 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)34 HashMap (java.util.HashMap)33 Async (io.vertx.ext.unit.Async)31 EventHandler (org.folio.processing.events.services.handler.EventHandler)29 HoldingsRecord (org.folio.HoldingsRecord)24 Item (org.folio.inventory.domain.items.Item)23 Instance (org.folio.inventory.domain.instances.Instance)20 Before (org.junit.Before)19 Record (org.folio.rest.jaxrs.model.Record)18 MappingMetadataDto (org.folio.MappingMetadataDto)16 MatchItemEventHandler (org.folio.inventory.dataimport.handlers.matching.MatchItemEventHandler)14 Context (org.folio.inventory.common.Context)12 MappingParameters (org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters)12