use of org.folio.inventory.common.domain.MultipleRecords 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();
}));
}
use of org.folio.inventory.common.domain.MultipleRecords 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())))));
}
use of org.folio.inventory.common.domain.MultipleRecords 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());
}
use of org.folio.inventory.common.domain.MultipleRecords in project mod-inventory by folio-org.
the class UpdateItemEventHandlerTest method shouldUpdateItemWithNewStatusIfHoldingAlreadyInPayload.
@Test
public void shouldUpdateItemWithNewStatusIfHoldingAlreadyInPayload() 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));
HashMap<String, String> payloadContext = new HashMap<>();
payloadContext.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record()));
payloadContext.put(ITEM.value(), existingItemJson.encode());
payloadContext.put(HOLDINGS.value(), "{notEmptyValue}");
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
DataImportEventPayload eventPayload = future.get(5, TimeUnit.SECONDS);
Assert.assertEquals(DI_INVENTORY_ITEM_UPDATED, DataImportEventTypes.fromValue(eventPayload.getEventType()));
Assert.assertNotNull(eventPayload.getContext().get(ITEM.value()));
JsonObject updatedItem = new JsonObject(eventPayload.getContext().get(ITEM.value()));
Assert.assertEquals(existingItemJson.getString("id"), updatedItem.getString("id"));
Assert.assertEquals(existingItemJson.getString(HRID_KEY), updatedItem.getString(HRID_KEY));
Assert.assertEquals(getNestedProperty(existingItemJson, "permanentLoanType", "id"), updatedItem.getString("permanentLoanTypeId"));
Assert.assertEquals(getNestedProperty(existingItemJson, "materialType", "id"), updatedItem.getString("materialTypeId"));
Assert.assertEquals(existingItemJson.getString("holdingsRecordId"), updatedItem.getString("holdingsRecordId"));
Assert.assertEquals(IN_PROCESS.value(), updatedItem.getJsonObject(STATUS_KEY).getString("name"));
Assert.assertNotNull(eventPayload.getContext().get(HOLDINGS.value()));
}
use of org.folio.inventory.common.domain.MultipleRecords in project mod-inventory by folio-org.
the class UpdateItemEventHandlerTest method shouldUpdateItemWithNewStatus.
@Test
public void shouldUpdateItemWithNewStatus() 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));
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
DataImportEventPayload eventPayload = future.get(5, TimeUnit.SECONDS);
Assert.assertEquals(DI_INVENTORY_ITEM_UPDATED, DataImportEventTypes.fromValue(eventPayload.getEventType()));
Assert.assertNotNull(eventPayload.getContext().get(ITEM.value()));
JsonObject updatedItem = new JsonObject(eventPayload.getContext().get(ITEM.value()));
Assert.assertEquals(existingItemJson.getString("id"), updatedItem.getString("id"));
Assert.assertEquals(existingItemJson.getString(HRID_KEY), updatedItem.getString(HRID_KEY));
Assert.assertEquals(getNestedProperty(existingItemJson, "permanentLoanType", "id"), updatedItem.getString("permanentLoanTypeId"));
Assert.assertEquals(getNestedProperty(existingItemJson, "materialType", "id"), updatedItem.getString("materialTypeId"));
Assert.assertEquals(existingItemJson.getString("holdingsRecordId"), updatedItem.getString("holdingsRecordId"));
Assert.assertEquals(IN_PROCESS.value(), updatedItem.getJsonObject(STATUS_KEY).getString("name"));
Assert.assertNotNull(eventPayload.getContext().get(HOLDINGS.value()));
}
Aggregations