Search in sources :

Example 6 with ParsedRecordDto

use of org.folio.rest.jaxrs.model.ParsedRecordDto in project mod-source-record-manager by folio-org.

the class ParsedRecordServiceImpl method getRecordByExternalId.

@Override
public Future<ParsedRecordDto> getRecordByExternalId(String externalId, OkapiConnectionParams params) {
    Promise<ParsedRecordDto> promise = Promise.promise();
    var client = new SourceStorageSourceRecordsClient(params.getOkapiUrl(), params.getTenantId(), params.getToken());
    try {
        client.getSourceStorageSourceRecordsById(externalId, "EXTERNAL", response -> {
            if (HTTP_OK.toInt() == response.result().statusCode()) {
                Buffer bodyAsBuffer = response.result().bodyAsBuffer();
                Try.itGet(() -> mapSourceRecordToParsedRecordDto(bodyAsBuffer)).compose(parsedRecordDto -> sourceRecordStateService.get(parsedRecordDto.getId(), params.getTenantId()).map(sourceRecordStateOptional -> sourceRecordStateOptional.orElse(new SourceRecordState().withRecordState(SourceRecordState.RecordState.ACTUAL))).compose(sourceRecordState -> Future.succeededFuture(parsedRecordDto.withRecordState(ParsedRecordDto.RecordState.valueOf(sourceRecordState.getRecordState().name()))))).onComplete(parsedRecordDtoAsyncResult -> {
                    if (parsedRecordDtoAsyncResult.succeeded()) {
                        promise.complete(parsedRecordDtoAsyncResult.result());
                    } else {
                        promise.fail(parsedRecordDtoAsyncResult.cause());
                    }
                });
            } else {
                String message = format("Error retrieving Record by externalId: '%s', response code %s, %s", externalId, response.result().statusCode(), response.result().statusMessage());
                if (HTTP_NOT_FOUND.toInt() == response.result().statusCode()) {
                    promise.fail(new NotFoundException(message));
                } else {
                    promise.fail(message);
                }
            }
        });
    } catch (Exception e) {
        log.error("Failed to GET Record from SRS", e);
        promise.fail(e);
    }
    return promise.future();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) MappingParametersProvider(org.folio.services.mappers.processor.MappingParametersProvider) Json(io.vertx.core.json.Json) HashMap(java.util.HashMap) Value(org.springframework.beans.factory.annotation.Value) QM_RECORD_UPDATED(org.folio.verticle.consumers.util.QMEventTypes.QM_RECORD_UPDATED) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) MappingRuleCacheKey(org.folio.services.entity.MappingRuleCacheKey) JsonObject(io.vertx.core.json.JsonObject) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) SourceRecord(org.folio.rest.jaxrs.model.SourceRecord) Promise(io.vertx.core.Promise) HTTP_NOT_FOUND(org.folio.HttpStatus.HTTP_NOT_FOUND) Future.failedFuture(io.vertx.core.Future.failedFuture) UUID(java.util.UUID) Future(io.vertx.core.Future) Try(org.folio.dataimport.util.Try) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) String.format(java.lang.String.format) NotFoundException(javax.ws.rs.NotFoundException) OkapiConnectionParams(org.folio.dataimport.util.OkapiConnectionParams) HTTP_OK(org.folio.HttpStatus.HTTP_OK) Buffer(io.vertx.core.buffer.Buffer) SourceStorageSourceRecordsClient(org.folio.rest.client.SourceStorageSourceRecordsClient) Log4j2(lombok.extern.log4j.Log4j2) KafkaHeaderUtils(org.folio.kafka.KafkaHeaderUtils) SourceRecordState(org.folio.rest.jaxrs.model.SourceRecordState) SourceRecordState(org.folio.rest.jaxrs.model.SourceRecordState) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) SourceStorageSourceRecordsClient(org.folio.rest.client.SourceStorageSourceRecordsClient) NotFoundException(javax.ws.rs.NotFoundException) NotFoundException(javax.ws.rs.NotFoundException)

Example 7 with ParsedRecordDto

use of org.folio.rest.jaxrs.model.ParsedRecordDto in project mod-source-record-manager by folio-org.

the class ChangeManagerParsedRecordsAPITest method shouldUpdateParsedRecordOnPut.

@Test
public void shouldUpdateParsedRecordOnPut(TestContext testContext) throws InterruptedException {
    Async async = testContext.async();
    ParsedRecordDto parsedRecordDto = new ParsedRecordDto().withId(UUID.randomUUID().toString()).withParsedRecord(new ParsedRecord().withId(UUID.randomUUID().toString()).withContent("{\"leader\":\"01240cas a2200397   4500\",\"fields\":[]}")).withRecordType(ParsedRecordDto.RecordType.MARC_HOLDING).withRelatedRecordVersion("1").withExternalIdsHolder(new ExternalIdsHolder().withInstanceId(UUID.randomUUID().toString()));
    RestAssured.given().spec(spec).body(parsedRecordDto).when().put(PARSED_RECORDS_URL + "/" + parsedRecordDto.getId()).then().statusCode(HttpStatus.SC_ACCEPTED);
    String observeTopic = formatTopicName(kafkaConfig.getEnvId(), getDefaultNameSpace(), TENANT_ID, QMEventTypes.QM_RECORD_UPDATED.name());
    kafkaCluster.observeValues(ObserveKeyValues.on(observeTopic, 1).observeFor(30, TimeUnit.SECONDS).build());
    async.complete();
}
Also used : ExternalIdsHolder(org.folio.rest.jaxrs.model.ExternalIdsHolder) Async(io.vertx.ext.unit.Async) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Test(org.junit.Test) AbstractRestTest(org.folio.rest.impl.AbstractRestTest)

Example 8 with ParsedRecordDto

use of org.folio.rest.jaxrs.model.ParsedRecordDto in project mod-source-record-manager by folio-org.

the class ParsedRecordServiceImpl method updateRecord.

@Override
public Future<Boolean> updateRecord(ParsedRecordDto parsedRecordDto, OkapiConnectionParams params) {
    String snapshotId = UUID.randomUUID().toString();
    MappingRuleCacheKey cacheKey = new MappingRuleCacheKey(params.getTenantId(), parsedRecordDto.getRecordType());
    return mappingParametersProvider.get(snapshotId, params).compose(mappingParameters -> mappingRuleCache.get(cacheKey).compose(rulesOptional -> {
        if (rulesOptional.isPresent()) {
            return updateRecord(parsedRecordDto, snapshotId, mappingParameters, rulesOptional.get(), params);
        } else {
            var message = format("Can not send %s event, no mapping rules found for tenant %s", QM_RECORD_UPDATED.name(), params.getTenantId());
            log.error(message);
            return failedFuture(message);
        }
    }));
}
Also used : MappingParametersProvider(org.folio.services.mappers.processor.MappingParametersProvider) Json(io.vertx.core.json.Json) HashMap(java.util.HashMap) Value(org.springframework.beans.factory.annotation.Value) QM_RECORD_UPDATED(org.folio.verticle.consumers.util.QMEventTypes.QM_RECORD_UPDATED) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) MappingRuleCacheKey(org.folio.services.entity.MappingRuleCacheKey) JsonObject(io.vertx.core.json.JsonObject) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) SourceRecord(org.folio.rest.jaxrs.model.SourceRecord) Promise(io.vertx.core.Promise) HTTP_NOT_FOUND(org.folio.HttpStatus.HTTP_NOT_FOUND) Future.failedFuture(io.vertx.core.Future.failedFuture) UUID(java.util.UUID) Future(io.vertx.core.Future) Try(org.folio.dataimport.util.Try) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) String.format(java.lang.String.format) NotFoundException(javax.ws.rs.NotFoundException) OkapiConnectionParams(org.folio.dataimport.util.OkapiConnectionParams) HTTP_OK(org.folio.HttpStatus.HTTP_OK) Buffer(io.vertx.core.buffer.Buffer) SourceStorageSourceRecordsClient(org.folio.rest.client.SourceStorageSourceRecordsClient) Log4j2(lombok.extern.log4j.Log4j2) KafkaHeaderUtils(org.folio.kafka.KafkaHeaderUtils) SourceRecordState(org.folio.rest.jaxrs.model.SourceRecordState) MappingRuleCacheKey(org.folio.services.entity.MappingRuleCacheKey)

Example 9 with ParsedRecordDto

use of org.folio.rest.jaxrs.model.ParsedRecordDto in project mod-inventory by folio-org.

the class QuickMarcKafkaHandlerTest method shouldSendErrorEventWhenFailedToFetchRecordFromStorage.

@Test
public void shouldSendErrorEventWhenFailedToFetchRecordFromStorage(TestContext context) throws InterruptedException {
    // given
    Async async = context.async();
    Map<String, String> payload = new HashMap<>();
    payload.put("RECORD_TYPE", "MARC_AUTHORITY");
    payload.put("MARC_AUTHORITY", Json.encode(authorityRecord));
    payload.put("MAPPING_RULES", authorityMappingRules.encode());
    payload.put("MAPPING_PARAMS", new JsonObject().encode());
    payload.put("PARSED_RECORD_DTO", Json.encode(new ParsedRecordDto().withRecordType(ParsedRecordDto.RecordType.MARC_AUTHORITY).withRelatedRecordVersion("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("Unexpected failure", 500));
        return null;
    }).when(mockedAuthorityRecordCollection).findById(anyString(), any(), any());
    // when
    Future<String> future = handler.handle(kafkaRecord);
    // then
    String observeTopic = formatTopicName(kafkaConfig.getEnvId(), getDefaultNameSpace(), TENANT_ID, QM_ERROR.name());
    cluster.observeValues(ObserveKeyValues.on(observeTopic, 1).observeFor(30, TimeUnit.SECONDS).build());
    future.onComplete(ar -> {
        context.assertTrue(ar.succeeded());
        context.assertEquals(expectedKafkaRecordKey, ar.result());
        async.complete();
    });
}
Also used : HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) JsonObject(io.vertx.core.json.JsonObject) Event(org.folio.rest.jaxrs.model.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Example 10 with ParsedRecordDto

use of org.folio.rest.jaxrs.model.ParsedRecordDto in project mod-inventory by folio-org.

the class QuickMarcKafkaHandlerTest method shouldSendErrorEventWhenRecordIsNotExistInStorage.

@Test
public void shouldSendErrorEventWhenRecordIsNotExistInStorage(TestContext context) throws InterruptedException {
    // given
    Async async = context.async();
    Map<String, String> payload = new HashMap<>();
    payload.put("RECORD_TYPE", "MARC_AUTHORITY");
    payload.put("MARC_AUTHORITY", Json.encode(authorityRecord));
    payload.put("MAPPING_RULES", authorityMappingRules.encode());
    payload.put("MAPPING_PARAMS", new JsonObject().encode());
    payload.put("PARSED_RECORD_DTO", Json.encode(new ParsedRecordDto().withRecordType(ParsedRecordDto.RecordType.MARC_AUTHORITY).withRelatedRecordVersion("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<Success<Authority>> successHandler = invocationOnMock.getArgument(1);
        successHandler.accept(new Success<>(null));
        return null;
    }).when(mockedAuthorityRecordCollection).findById(anyString(), any(), any());
    // when
    Future<String> future = handler.handle(kafkaRecord);
    // then
    String observeTopic = formatTopicName(kafkaConfig.getEnvId(), getDefaultNameSpace(), TENANT_ID, QM_ERROR.name());
    cluster.observeValues(ObserveKeyValues.on(observeTopic, 1).observeFor(30, TimeUnit.SECONDS).build());
    future.onComplete(ar -> {
        context.assertTrue(ar.succeeded());
        context.assertEquals(expectedKafkaRecordKey, ar.result());
        async.complete();
    });
}
Also used : HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) JsonObject(io.vertx.core.json.JsonObject) Event(org.folio.rest.jaxrs.model.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Success(org.folio.inventory.common.domain.Success) Test(org.junit.Test)

Aggregations

ParsedRecordDto (org.folio.rest.jaxrs.model.ParsedRecordDto)11 Async (io.vertx.ext.unit.Async)9 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 JsonObject (io.vertx.core.json.JsonObject)6 Event (org.folio.rest.jaxrs.model.Event)5 SourceRecord (org.folio.rest.jaxrs.model.SourceRecord)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ParsedRecord (org.folio.rest.jaxrs.model.ParsedRecord)4 Future (io.vertx.core.Future)3 UUID (java.util.UUID)3 MappingParameters (org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters)3 RawRecord (org.folio.rest.jaxrs.model.RawRecord)3 Record (org.folio.rest.jaxrs.model.Record)3 Future.failedFuture (io.vertx.core.Future.failedFuture)2 Promise (io.vertx.core.Promise)2 Buffer (io.vertx.core.buffer.Buffer)2 Json (io.vertx.core.json.Json)2 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2