use of org.folio.services.entity.MappingRuleCacheKey in project mod-source-record-manager by folio-org.
the class MappingRuleCache method loadMappingRules.
private CompletableFuture<Optional<JsonObject>> loadMappingRules(MappingRuleCacheKey key, Executor executor, MappingRuleDao mappingRuleDao) {
CompletableFuture<Optional<JsonObject>> future = new CompletableFuture<>();
executor.execute(() -> mappingRuleDao.get(key.getRecordType(), key.getTenantId()).map(optional -> optional.isPresent() ? optional : Optional.of(new JsonObject())).onComplete(ar -> {
if (ar.failed()) {
LOGGER.error("Failed to load mapping rules for tenant '{}' from data base", key.getTenantId(), ar.cause());
future.completeExceptionally(ar.cause());
return;
}
future.complete(ar.result());
}));
return future;
}
use of org.folio.services.entity.MappingRuleCacheKey in project mod-source-record-manager by folio-org.
the class MappingRuleServiceImpl method updateRules.
private Future<JsonObject> updateRules(String rules, Record.RecordType recordType, String tenantId) {
Promise<JsonObject> promise = Promise.promise();
if (isValidJson(rules)) {
MappingRuleCacheKey cacheKey = new MappingRuleCacheKey(tenantId, recordType);
mappingRuleDao.update(new JsonObject(rules), recordType, tenantId).onSuccess(updatedRules -> mappingRuleCache.put(cacheKey, updatedRules)).onComplete(promise);
} else {
String errorMessage = "Can not update rules in non-JSON format";
LOGGER.error(errorMessage);
promise.fail(new BadRequestException(errorMessage));
}
return promise.future();
}
use of org.folio.services.entity.MappingRuleCacheKey in project mod-source-record-manager by folio-org.
the class StoredRecordChunksKafkaHandlerTest method shouldReturnFailedFutureWhenMappingRuleCacheReturnFailed.
@Test
public void shouldReturnFailedFutureWhenMappingRuleCacheReturnFailed() throws IOException {
Record record = Json.decodeValue(TestUtil.readFileFromPath(EDIFACT_RECORD_PATH), Record.class);
RecordsBatchResponse savedRecordsBatch = new RecordsBatchResponse().withRecords(List.of(record)).withTotalRecords(1);
Event event = new Event().withId(UUID.randomUUID().toString()).withEventPayload(Json.encode(savedRecordsBatch));
when(kafkaRecord.value()).thenReturn(Json.encode(event));
when(kafkaRecord.headers()).thenReturn(List.of(KafkaHeader.header(OKAPI_HEADER_TENANT, TENANT_ID)));
when(eventProcessedService.collectData(STORED_RECORD_CHUNKS_KAFKA_HANDLER_UUID, event.getId(), TENANT_ID)).thenReturn(Future.succeededFuture());
when(mappingRuleCache.get(new MappingRuleCacheKey(TENANT_ID, EntityType.EDIFACT))).thenReturn(Future.failedFuture(new Exception()));
// when
Future<String> future = storedRecordChunksKafkaHandler.handle(kafkaRecord);
// then
assertTrue(future.failed());
}
use of org.folio.services.entity.MappingRuleCacheKey in project mod-source-record-manager by folio-org.
the class MarcBibErrorPayloadBuilderTest method shouldBuildPayload.
@Test
public void shouldBuildPayload(TestContext context) throws IOException {
Async async = context.async();
Record record = getRecordFromFile();
when(mappingRuleCache.get(new MappingRuleCacheKey(TENANT_ID, record.getRecordType()))).thenReturn(Future.succeededFuture(Optional.of(new JsonObject(TestUtil.readFileFromPath(MAPPING_RULES_PATH)))));
Future<DataImportEventPayload> payloadFuture = payloadBuilder.buildEventPayload(new RecordTooLargeException(LARGE_PAYLOAD_ERROR_MESSAGE), getOkapiParams(), JOB_EXECUTION_ID, record);
payloadFuture.onComplete(ar -> {
DataImportEventPayload result = ar.result();
assertEquals(DI_ERROR.value(), result.getEventType());
assertTrue(result.getContext().containsKey(ERROR_KEY));
Record resRecordWithTitle = getRecordFromContext(result);
assertNotNull(resRecordWithTitle.getParsedRecord());
async.complete();
});
}
use of org.folio.services.entity.MappingRuleCacheKey in project mod-source-record-manager by folio-org.
the class MarcBibErrorPayloadBuilderTest method shouldBuildPayloadWhenNoMappingRulesFound.
@Test
public void shouldBuildPayloadWhenNoMappingRulesFound(TestContext context) throws IOException {
Async async = context.async();
Record record = getRecordFromFile();
when(mappingRuleCache.get(new MappingRuleCacheKey(TENANT_ID, record.getRecordType()))).thenReturn(Future.succeededFuture(Optional.empty()));
Future<DataImportEventPayload> payloadFuture = payloadBuilder.buildEventPayload(new RecordTooLargeException(LARGE_PAYLOAD_ERROR_MESSAGE), getOkapiParams(), JOB_EXECUTION_ID, record);
payloadFuture.onComplete(ar -> {
DataImportEventPayload result = ar.result();
assertEquals(DI_ERROR.value(), result.getEventType());
assertTrue(result.getContext().containsKey(ERROR_KEY));
Record resRecordWithNoTitle = getRecordFromContext(result);
assertNull(resRecordWithNoTitle.getParsedRecord());
async.complete();
});
}
Aggregations