use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.
the class CreateItemEventHandlerTest method shouldReturnFailedFutureIfDuplicatedErrorExists.
@Test(expected = ExecutionException.class)
public void shouldReturnFailedFutureIfDuplicatedErrorExists() throws InterruptedException, ExecutionException, TimeoutException, UnsupportedEncodingException {
// given
RecordToEntity recordToItem = RecordToEntity.builder().recordId(RECORD_ID).entityId(ITEM_ID).build();
when(itemIdStorageService.store(any(), any(), any())).thenReturn(Future.succeededFuture(recordToItem));
Mockito.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(UNIQUE_ID_ERROR_MESSAGE, 400));
return null;
}).when(mockedItemCollection).add(any(), any(), any());
MappingManager.registerReaderFactory(fakeReaderFactory);
MappingManager.registerWriterFactory(new ItemWriterFactory());
String expectedHoldingId = UUID.randomUUID().toString();
JsonObject holdingAsJson = new JsonObject().put("id", expectedHoldingId);
HashMap<String, String> payloadContext = new HashMap<>();
payloadContext.put(EntityType.MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record()));
payloadContext.put(EntityType.HOLDINGS.value(), holdingAsJson.encode());
DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_SRS_MARC_BIB_RECORD_CREATED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
// when
CompletableFuture<DataImportEventPayload> future = createItemHandler.handle(dataImportEventPayload);
future.get(5, TimeUnit.SECONDS);
}
use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.
the class CreateMarcHoldingsEventHandlerTest method shouldNotProcessEventEvenIfDuplicatedInventoryStorageErrorExists.
@Test(expected = Exception.class)
public void shouldNotProcessEventEvenIfDuplicatedInventoryStorageErrorExists() throws IOException, InterruptedException, ExecutionException, TimeoutException {
when(storage.getHoldingsRecordCollection(any())).thenReturn(holdingsRecordsCollection);
when(storage.getInstanceCollection(any())).thenReturn(instanceRecordCollection);
doAnswer(invocationOnMock -> {
Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
failureHandler.accept(new Failure(UNIQUE_ID_ERROR_MESSAGE, 400));
return null;
}).when(holdingsRecordsCollection).add(any(), any(), any());
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()));
record.setId(recordId);
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).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
CompletableFuture<DataImportEventPayload> future = createMarcHoldingsEventHandler.handle(dataImportEventPayload);
future.get(5, TimeUnit.SECONDS);
}
use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.
the class MarcBibModifiedPostProcessingEventHandlerTest method shouldNotUpdateInstanceIfOLErrorExist.
@Test(expected = ExecutionException.class)
public void shouldNotUpdateInstanceIfOLErrorExist() throws InterruptedException, ExecutionException, TimeoutException {
HashMap<String, String> payloadContext = new HashMap<>();
payloadContext.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_SRS_MARC_BIB_RECORD_MODIFIED_READY_FOR_POST_PROCESSING.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withOkapiUrl(OKAPI_URL).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
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());
CompletableFuture<DataImportEventPayload> future = marcBibModifiedEventHandler.handle(dataImportEventPayload);
future.get(5, TimeUnit.SECONDS);
}
use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.
the class UpdateHoldingEventHandlerTest method shouldNotProcessEventIfOLErrorExist.
@Test(expected = ExecutionException.class)
public void shouldNotProcessEventIfOLErrorExist() throws InterruptedException, ExecutionException, TimeoutException {
Reader fakeReader = mock(Reader.class);
String permanentLocationId = UUID.randomUUID().toString();
String instanceId = String.valueOf(UUID.randomUUID());
String holdingId = UUID.randomUUID().toString();
String hrid = UUID.randomUUID().toString();
HoldingsRecord holdingsRecord = new HoldingsRecord().withId(holdingId).withInstanceId(instanceId).withHrid(hrid).withPermanentLocationId(permanentLocationId);
when(fakeReader.read(any(MappingRule.class))).thenReturn(StringValue.of(permanentLocationId));
HoldingsRecord returnedHoldings = new HoldingsRecord().withId(holdingId).withHrid(hrid).withInstanceId(instanceId).withPermanentLocationId(permanentLocationId).withVersion(1);
when(holdingsRecordsCollection.findById(anyString())).thenReturn(CompletableFuture.completedFuture(returnedHoldings));
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(holdingsRecordsCollection).update(any(), any(), any());
when(fakeReaderFactory.createReader()).thenReturn(fakeReader);
when(storage.getHoldingsRecordCollection(any())).thenReturn(holdingsRecordsCollection);
MappingManager.registerReaderFactory(fakeReaderFactory);
MappingManager.registerWriterFactory(new HoldingWriterFactory());
Record record = new Record().withParsedRecord(new ParsedRecord().withContent(PARSED_CONTENT_WITH_INSTANCE_ID));
HashMap<String, String> context = new HashMap<>();
context.put(HOLDINGS.value(), Json.encode(holdingsRecord));
context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_HOLDING_UPDATED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(context).withProfileSnapshot(profileSnapshotWrapper).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
CompletableFuture<DataImportEventPayload> future = updateHoldingEventHandler.handle(dataImportEventPayload);
future.get(5, TimeUnit.MILLISECONDS);
}
use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.
the class CreateInstanceEventHandlerTest method shouldNotProcessEventEvenIfInventoryStorageErrorExists.
@Test(expected = Exception.class)
public void shouldNotProcessEventEvenIfInventoryStorageErrorExists() throws InterruptedException, ExecutionException, TimeoutException {
Reader fakeReader = Mockito.mock(Reader.class);
String instanceTypeId = "fe19bae4-da28-472b-be90-d442e2428ead";
String recordId = "567859ad-505a-400d-a699-0028a1fdbf84";
String instanceId = "4d4545df-b5ba-4031-a031-70b1c1b2fc5d";
String title = "titleValue";
RecordToEntity recordToInstance = RecordToEntity.builder().recordId(recordId).entityId(instanceId).build();
when(fakeReader.read(any(MappingRule.class))).thenReturn(StringValue.of(instanceTypeId), StringValue.of(title));
when(fakeReaderFactory.createReader()).thenReturn(fakeReader);
when(storage.getInstanceCollection(any())).thenReturn(instanceRecordCollection);
when(instanceIdStorageService.store(any(), any(), any())).thenReturn(Future.succeededFuture(recordToInstance));
doAnswer(invocationOnMock -> {
Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
failureHandler.accept(new Failure("Smth error", 400));
return null;
}).when(instanceRecordCollection).add(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));
record.setId(recordId);
context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
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()).withOkapiUrl(mockServer.baseUrl());
CompletableFuture<DataImportEventPayload> future = createInstanceEventHandler.handle(dataImportEventPayload);
future.get(5, TimeUnit.SECONDS);
}
Aggregations