use of org.folio.inventory.common.domain.Success in project mod-inventory by folio-org.
the class CreateItemEventHandlerTest method shouldCreateItemAndFillInHoldingsRecordIdFromHoldingsEntity.
@Test
public void shouldCreateItemAndFillInHoldingsRecordIdFromHoldingsEntity() throws UnsupportedEncodingException, InterruptedException, ExecutionException, TimeoutException {
// 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));
Mockito.doAnswer(invocationOnMock -> {
Item item = invocationOnMock.getArgument(0);
Consumer<Success<Item>> successHandler = invocationOnMock.getArgument(1);
successHandler.accept(new Success<>(item));
return null;
}).when(mockedItemCollection).add(any(), any(Consumer.class), any(Consumer.class));
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);
// then
DataImportEventPayload eventPayload = future.get(5, TimeUnit.SECONDS);
Assert.assertEquals(DI_INVENTORY_ITEM_CREATED.value(), eventPayload.getEventType());
Assert.assertNotNull(eventPayload.getContext().get(ITEM.value()));
JsonObject createdItem = new JsonObject(eventPayload.getContext().get(ITEM.value()));
Assert.assertNotNull(createdItem.getJsonObject("status").getString("name"));
Assert.assertNotNull(createdItem.getString("permanentLoanTypeId"));
Assert.assertNotNull(createdItem.getString("materialTypeId"));
Assert.assertEquals(expectedHoldingId, createdItem.getString("holdingId"));
}
use of org.folio.inventory.common.domain.Success in project mod-inventory by folio-org.
the class CreateMarcHoldingsEventHandlerTest method shouldProcessEventIfInstanceFoundButTotalRecordsIsNotEqualOne.
@Test
public void shouldProcessEventIfInstanceFoundButTotalRecordsIsNotEqualOne() throws InterruptedException, ExecutionException, TimeoutException, IOException {
doAnswer(invocationOnMock -> {
instanceId = String.valueOf(UUID.randomUUID());
Instance instance = new Instance(instanceId, "2", String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
MultipleRecords<Instance> result = new MultipleRecords<>(Collections.singletonList(instance), 0);
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()));
record.setId("a0eb738a-c631-48cb-b36e-41cdcc83e2a4");
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);
DataImportEventPayload actualDataImportEventPayload = future.get(10, TimeUnit.SECONDS);
Assert.assertEquals(DI_INVENTORY_HOLDING_CREATED.value(), actualDataImportEventPayload.getEventType());
assertNotNull(actualDataImportEventPayload.getContext().get(HOLDINGS.value()));
assertNotNull(new JsonObject(actualDataImportEventPayload.getContext().get(HOLDINGS.value())).getString("id"));
Assert.assertNull(new JsonObject(actualDataImportEventPayload.getContext().get(HOLDINGS.value())).getString("instanceId"));
Assert.assertEquals(PERMANENT_LOCATION_ID, new JsonObject(actualDataImportEventPayload.getContext().get(HOLDINGS.value())).getString("permanentLocationId"));
}
use of org.folio.inventory.common.domain.Success in project mod-inventory by folio-org.
the class CreateMarcHoldingsEventHandlerTest method setUp.
@Before
public void setUp() throws IOException {
MockitoAnnotations.openMocks(this);
MappingManager.clearReaderFactories();
MappingMetadataCache mappingMetadataCache = new MappingMetadataCache(vertx, vertx.createHttpClient(), 3600);
createMarcHoldingsEventHandler = new CreateMarcHoldingsEventHandler(storage, mappingMetadataCache, holdingsIdStorageService);
mappingRules = new JsonObject(TestUtil.readFileFromPath(MAPPING_RULES_PATH));
doAnswer(invocationOnMock -> {
instanceId = String.valueOf(UUID.randomUUID());
Instance instance = new Instance(instanceId, "2", String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
List<Instance> instanceList = Collections.singletonList(instance);
MultipleRecords<Instance> result = new MultipleRecords<>(instanceList, 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));
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 -> {
recordId = String.valueOf(UUID.randomUUID());
holdingsId = String.valueOf(UUID.randomUUID());
RecordToEntity recordToHoldings = RecordToEntity.builder().recordId(recordId).entityId(holdingsId).build();
return Future.succeededFuture(recordToHoldings);
}).when(holdingsIdStorageService).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())))));
}
use of org.folio.inventory.common.domain.Success in project mod-inventory by folio-org.
the class MatchAuthorityEventHandlerUnitTest method shouldPutMultipleMatchResultToPayloadOnHandleEventPayload.
@Test
public void shouldPutMultipleMatchResultToPayloadOnHandleEventPayload(TestContext testContext) throws UnsupportedEncodingException {
Async async = testContext.async();
List<Authority> matchedAuthorities = List.of(new Authority().withId(AUTHORITY_ID), new Authority().withId(UUID.randomUUID().toString()));
MatchDetail personalNameMatchDetail = new MatchDetail().withMatchCriterion(EXACTLY_MATCHES).withExistingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(singletonList(new Field().withLabel("personalName").withValue("authority.personalName"))));
doAnswer(invocation -> {
Consumer<Success<MultipleRecords<Authority>>> successHandler = invocation.getArgument(2);
Success<MultipleRecords<Authority>> result = new Success<>(new MultipleRecords<>(matchedAuthorities, 2));
successHandler.accept(result);
return null;
}).when(collection).findByCql(eq(format("personalName == \"%s\"", PERSONAL_NAME)), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
EventHandler eventHandler = new MatchAuthorityEventHandler(mappingMetadataCache);
HashMap<String, String> context = new HashMap<>();
context.put("MAPPING_PARAMS", new JsonObject().encode());
DataImportEventPayload eventPayload = createEventPayload(personalNameMatchDetail).withContext(context);
eventPayload.getCurrentNode().setChildSnapshotWrappers(List.of(new ProfileSnapshotWrapper().withContent(new MatchProfile().withExistingRecordType(AUTHORITY).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(processedPayload.getEventsChain(), List.of(DI_SRS_MARC_AUTHORITY_RECORD_CREATED.value()));
testContext.assertEquals(DI_INVENTORY_AUTHORITY_MATCHED.value(), processedPayload.getEventType());
assertThat(new JsonArray(processedPayload.getContext().get(MULTI_MATCH_IDS)), hasItems(matchedAuthorities.get(0).getId(), matchedAuthorities.get(1).getId()));
async.complete();
}));
}
use of org.folio.inventory.common.domain.Success in project mod-inventory by folio-org.
the class MatchAuthorityEventHandlerUnitTest method shouldFailOnHandleEventPayloadIfMatchedMultipleAuthorityRecords.
@Test
public void shouldFailOnHandleEventPayloadIfMatchedMultipleAuthorityRecords(TestContext testContext) throws UnsupportedEncodingException {
Async async = testContext.async();
MatchDetail personalNameMatchDetail = new MatchDetail().withMatchCriterion(EXACTLY_MATCHES).withExistingMatchExpression(new MatchExpression().withDataValueType(VALUE_FROM_RECORD).withFields(singletonList(new Field().withLabel("personalName").withValue("authority.personalName"))));
DataImportEventPayload eventPayload = createEventPayload(personalNameMatchDetail);
doAnswer(ans -> {
Consumer<Success<MultipleRecords<Authority>>> callback = ans.getArgument(2);
Success<MultipleRecords<Authority>> result = new Success<>(new MultipleRecords<>(asList(createAuthority(), createAuthority()), 2));
callback.accept(result);
return null;
}).when(collection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
EventHandler eventHandler = new MatchAuthorityEventHandler(mappingMetadataCache);
eventHandler.handle(eventPayload).whenComplete((updatedEventPayload, throwable) -> {
testContext.assertNotNull(throwable);
async.complete();
});
}
Aggregations