Search in sources :

Example 6 with Failure

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

the class MatchHoldingEventHandlerUnitTest method shouldFailOnHandleEventPayloadIfFailedCallToInventoryStorage.

@Test
public void shouldFailOnHandleEventPayloadIfFailedCallToInventoryStorage(TestContext testContext) throws UnsupportedEncodingException {
    Async async = testContext.async();
    doAnswer(ans -> {
        Consumer<Failure> callback = ans.getArgument(3);
        Failure result = new Failure("Internal Server Error", 500);
        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) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Consumer(java.util.function.Consumer) Async(io.vertx.ext.unit.Async) EventHandler(org.folio.processing.events.services.handler.EventHandler) MatchItemEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchItemEventHandler) MatchHoldingEventHandler(org.folio.inventory.dataimport.handlers.matching.MatchHoldingEventHandler) Failure(org.folio.inventory.common.domain.Failure) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test)

Example 7 with Failure

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

the class CreateAuthorityEventHandlerTest method shouldNotProcessEventEvenIfDuplicatedInventoryStorageErrorExists.

@Test(expected = Exception.class)
public void shouldNotProcessEventEvenIfDuplicatedInventoryStorageErrorExists() throws IOException, InterruptedException, ExecutionException, TimeoutException {
    when(storage.getAuthorityRecordCollection(any())).thenReturn(authorityCollection);
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure(UNIQUE_ID_ERROR_MESSAGE, 400));
        return null;
    }).when(authorityCollection).add(any(), any(), any());
    var parsedAuthorityRecord = new JsonObject(TestUtil.readFileFromPath(PARSED_AUTHORITY_RECORD));
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(parsedAuthorityRecord.encode()));
    HashMap<String, String> context = new HashMap<>();
    context.put(MARC_AUTHORITY.value(), Json.encode(record));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_SRS_MARC_AUTHORITY_RECORD_CREATED.value()).withJobExecutionId(UUID.randomUUID().toString()).withOkapiUrl(mockServer.baseUrl()).withContext(context).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    CompletableFuture<DataImportEventPayload> future = createMarcAuthoritiesEventHandler.handle(dataImportEventPayload);
    future.get(5, TimeUnit.SECONDS);
}
Also used : HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Failure(org.folio.inventory.common.domain.Failure) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test)

Example 8 with Failure

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

the class ReplaceInstanceEventHandlerTest method shouldNotProcessEventIfOLErrorExists.

@Test(expected = ExecutionException.class)
public void shouldNotProcessEventIfOLErrorExists() throws InterruptedException, ExecutionException, TimeoutException {
    Reader fakeReader = Mockito.mock(Reader.class);
    String instanceTypeId = UUID.randomUUID().toString();
    String title = "titleValue";
    when(fakeReader.read(any(MappingRule.class))).thenReturn(StringValue.of(instanceTypeId), StringValue.of(title));
    when(fakeReaderFactory.createReader()).thenReturn(fakeReader);
    when(storage.getInstanceCollection(any())).thenReturn(instanceRecordCollection);
    Instance returnedInstance = new Instance(UUID.randomUUID().toString(), String.valueOf(INSTANCE_VERSION), UUID.randomUUID().toString(), "source", "title", instanceTypeId);
    returnedInstance.setTags(List.of("firstTag"));
    when(instanceRecordCollection.findById(anyString())).thenReturn(CompletableFuture.completedFuture(returnedInstance));
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure("Cannot update record 601a8dc4-dee7-48eb-b03f-d02fdf0debd0 because it has been changed (optimistic locking): Stored _version is 2, _version of request is 1", 409));
        return null;
    }).when(instanceRecordCollection).update(any(), any(), any());
    MappingManager.registerReaderFactory(fakeReaderFactory);
    MappingManager.registerWriterFactory(new InstanceWriterFactory());
    HashMap<String, String> context = new HashMap<>();
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(PARSED_CONTENT));
    context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
    context.put(INSTANCE.value(), new JsonObject().put("id", UUID.randomUUID().toString()).put("hrid", UUID.randomUUID().toString()).put("_version", INSTANCE_VERSION).encode());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_INSTANCE_CREATED.value()).withContext(context).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0)).withTenant(TENANT_ID).withOkapiUrl(mockServer.baseUrl()).withToken(TOKEN).withJobExecutionId(UUID.randomUUID().toString());
    CompletableFuture<DataImportEventPayload> future = replaceInstanceEventHandler.handle(dataImportEventPayload);
    future.get(20, TimeUnit.SECONDS);
}
Also used : Instance(org.folio.inventory.domain.instances.Instance) HashMap(java.util.HashMap) Reader(org.folio.processing.mapping.mapper.reader.Reader) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) MappingRule(org.folio.rest.jaxrs.model.MappingRule) DataImportEventPayload(org.folio.DataImportEventPayload) InstanceWriterFactory(org.folio.inventory.dataimport.InstanceWriterFactory) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Example 9 with Failure

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

the class MarcBibInstanceHridSetKafkaHandlerTest method shouldReturnFailedIfOLErrorExist.

@Test
public void shouldReturnFailedIfOLErrorExist(TestContext context) {
    // given
    Async async = context.async();
    Map<String, String> payload = new HashMap<>();
    payload.put("MARC_BIB", Json.encode(record));
    payload.put(JOB_EXECUTION_ID_KEY, UUID.randomUUID().toString());
    payload.put("CURRENT_RETRY_NUMBER", "1");
    Event event = new Event().withId("01").withEventPayload(Json.encode(payload));
    String expectedKafkaRecordKey = "test_key";
    when(kafkaRecord.key()).thenReturn(expectedKafkaRecordKey);
    when(kafkaRecord.value()).thenReturn(Json.encode(event));
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure("Cannot update record 601a8dc4-dee7-48eb-b03f-d02fdf0debd0 because it has been changed (optimistic locking): Stored _version is 2, _version of request is 1", 409));
        return null;
    }).when(mockedInstanceCollection).update(any(), any(), any());
    // when
    Future<String> future = marcBibInstanceHridSetKafkaHandler.handle(kafkaRecord);
    // then
    future.onComplete(ar -> {
        context.assertTrue(ar.failed());
        async.complete();
    });
}
Also used : HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) Event(org.folio.rest.jaxrs.model.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Example 10 with Failure

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

the class UpdateItemEventHandlerTest method shouldReturnFailedWhenOLError.

@Test(expected = ExecutionException.class)
public void shouldReturnFailedWhenOLError() throws UnsupportedEncodingException, InterruptedException, ExecutionException, TimeoutException {
    // given
    doAnswer(invocationOnMock -> {
        MultipleRecords<Item> result = new MultipleRecords<>(new ArrayList<>(), 0);
        Consumer<Success<MultipleRecords<Item>>> successHandler = invocationOnMock.getArgument(2);
        successHandler.accept(new Success<>(result));
        return null;
    }).when(mockedItemCollection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure("Cannot update record 601a8dc4-dee7-48eb-b03f-d02fdf0debd0 because it has been changed (optimistic locking): Stored _version is 2, _version of request is 1", 409));
        return null;
    }).when(mockedItemCollection).update(any(), any(), any());
    Item returnedItem = new Item(existingItemJson.getString("id"), "2", UUID.randomUUID().toString(), "test", new Status(AVAILABLE), UUID.randomUUID().toString(), UUID.randomUUID().toString(), new JsonObject());
    when(mockedItemCollection.findById(anyString())).thenReturn(CompletableFuture.completedFuture(returnedItem));
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record()));
    payloadContext.put(ITEM.value(), existingItemJson.encode());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_ITEM_MATCHED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    // when
    CompletableFuture<DataImportEventPayload> future = updateItemHandler.handle(dataImportEventPayload);
    // then
    future.get(5, TimeUnit.SECONDS);
}
Also used : Status(org.folio.inventory.domain.items.Status) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) Item(org.folio.inventory.domain.items.Item) Consumer(java.util.function.Consumer) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) HoldingsRecord(org.folio.HoldingsRecord) Record(org.folio.rest.jaxrs.model.Record) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Aggregations

Failure (org.folio.inventory.common.domain.Failure)50 Test (org.junit.Test)44 CompletableFuture (java.util.concurrent.CompletableFuture)26 HashMap (java.util.HashMap)21 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 DataImportEventPayload (org.folio.DataImportEventPayload)17 JsonObject (io.vertx.core.json.JsonObject)16 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)10 Record (org.folio.rest.jaxrs.model.Record)10 ParsedRecord (org.folio.rest.jaxrs.model.ParsedRecord)9 Async (io.vertx.ext.unit.Async)8 Consumer (java.util.function.Consumer)7 HoldingsRecord (org.folio.HoldingsRecord)7 AuthorityRecordCollection (org.folio.inventory.domain.AuthorityRecordCollection)7 InstanceCollection (org.folio.inventory.domain.instances.InstanceCollection)7 ItemCollection (org.folio.inventory.domain.items.ItemCollection)6 EventHandler (org.folio.processing.events.services.handler.EventHandler)6 MultipleRecords (org.folio.inventory.common.domain.MultipleRecords)4 Instance (org.folio.inventory.domain.instances.Instance)4 Reader (org.folio.processing.mapping.mapper.reader.Reader)4