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();
});
}
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);
}
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);
}
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();
});
}
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);
}
Aggregations