use of org.folio.rest.jaxrs.model.AdditionalInfo in project mod-source-record-storage by folio-org.
the class SourceStorageStreamApiTest method shouldReturnIdOnResponseOnSearchMarcRecordIdsWhenRecordWasSuppressed.
@Test
public void shouldReturnIdOnResponseOnSearchMarcRecordIdsWhenRecordWasSuppressed(TestContext testContext) {
// given
Async async = testContext.async();
Record suppressedRecord = new Record().withId(marc_bib_record_2.getId()).withSnapshotId(snapshot_2.getJobExecutionId()).withRecordType(Record.RecordType.MARC_BIB).withRawRecord(marc_bib_record_2.getRawRecord()).withParsedRecord(marc_bib_record_2.getParsedRecord()).withMatchedId(marc_bib_record_2.getMatchedId()).withState(Record.State.ACTUAL).withAdditionalInfo(new AdditionalInfo().withSuppressDiscovery(true)).withExternalIdsHolder(marc_bib_record_2.getExternalIdsHolder());
postSnapshots(testContext, snapshot_2);
postRecords(testContext, suppressedRecord);
MarcRecordSearchRequest searchRequest = new MarcRecordSearchRequest();
searchRequest.setLeaderSearchExpression("p_05 = 'c' and p_06 = 'c' and p_07 = 'm'");
searchRequest.setFieldsSearchExpression("001.value = '393893' and 005.value ^= '2014110' and 035.ind1 = '#'");
searchRequest.setSuppressFromDiscovery(true);
// when
ExtractableResponse<Response> response = RestAssured.given().spec(spec).body(searchRequest).when().post("/source-storage/stream/marc-record-identifiers").then().extract();
JsonObject responseBody = new JsonObject(response.body().asString());
// then
assertEquals(HttpStatus.SC_OK, response.statusCode());
assertEquals(1, responseBody.getJsonArray("records").size());
assertEquals(1, responseBody.getInteger("totalCount").intValue());
async.complete();
}
use of org.folio.rest.jaxrs.model.AdditionalInfo in project mod-source-record-storage by folio-org.
the class RecordApiTest method returnCreatedMarcRecordWithAdditionalInfoOnGetById.
private void returnCreatedMarcRecordWithAdditionalInfoOnGetById(TestContext testContext, Snapshot snapshot, RecordType marcAuthority) {
postSnapshots(testContext, snapshot);
String matchedId = UUID.randomUUID().toString();
Record newRecord = new Record().withId(matchedId).withSnapshotId(snapshot.getJobExecutionId()).withRecordType(marcAuthority).withRawRecord(rawMarcRecord).withParsedRecord(parsedMarcRecord).withMatchedId(matchedId).withState(Record.State.ACTUAL).withAdditionalInfo(new AdditionalInfo().withSuppressDiscovery(true));
Async async = testContext.async();
Response createResponse = RestAssured.given().spec(spec).body(newRecord).when().post(SOURCE_STORAGE_RECORDS_PATH);
assertThat(createResponse.statusCode(), is(HttpStatus.SC_CREATED));
Record createdRecord = createResponse.body().as(Record.class);
async.complete();
async = testContext.async();
Response getResponse = RestAssured.given().spec(spec).when().get(SOURCE_STORAGE_RECORDS_PATH + "/" + createdRecord.getId());
assertThat(getResponse.statusCode(), is(HttpStatus.SC_OK));
Record getRecord = getResponse.body().as(Record.class);
assertThat(getRecord.getId(), is(createdRecord.getId()));
assertThat(getRecord.getRawRecord().getContent(), is(rawMarcRecord.getContent()));
assertThat(getRecord.getAdditionalInfo().getSuppressDiscovery(), is(newRecord.getAdditionalInfo().getSuppressDiscovery()));
async.complete();
}
use of org.folio.rest.jaxrs.model.AdditionalInfo in project mod-source-record-storage by folio-org.
the class RecordDaoImpl method updateParsedRecords.
@Override
public Future<ParsedRecordsBatchResponse> updateParsedRecords(RecordCollection recordCollection, String tenantId) {
Promise<ParsedRecordsBatchResponse> promise = Promise.promise();
Set<String> recordTypes = new HashSet<>();
List<Record> records = new ArrayList<>();
List<String> errorMessages = new ArrayList<>();
List<UpdateConditionStep<RecordsLbRecord>> recordUpdates = new ArrayList<>();
List<UpdateConditionStep<org.jooq.Record>> parsedRecordUpdates = new ArrayList<>();
Field<UUID> prtId = field(name(ID), UUID.class);
Field<JSONB> prtContent = field(name(CONTENT), JSONB.class);
List<ParsedRecord> parsedRecords = recordCollection.getRecords().stream().map(this::validateParsedRecordId).peek(record -> {
// make sure only one record type
recordTypes.add(record.getRecordType().name());
if (recordTypes.size() > 1) {
throw new BadRequestException("Batch record collection only supports single record type");
}
UpdateSetFirstStep<RecordsLbRecord> updateFirstStep = DSL.update(RECORDS_LB);
UpdateSetMoreStep<RecordsLbRecord> updateStep = null;
// check for external record properties to update
ExternalIdsHolder externalIdsHolder = record.getExternalIdsHolder();
AdditionalInfo additionalInfo = record.getAdditionalInfo();
Metadata metadata = record.getMetadata();
if (Objects.nonNull(externalIdsHolder)) {
var recordType = record.getRecordType();
String externalId = getExternalId(externalIdsHolder, recordType);
String externalHrid = getExternalHrid(externalIdsHolder, recordType);
if (StringUtils.isNotEmpty(externalId)) {
updateStep = updateFirstStep.set(RECORDS_LB.EXTERNAL_ID, UUID.fromString(externalId));
}
if (StringUtils.isNotEmpty(externalHrid)) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.EXTERNAL_HRID, externalHrid);
}
}
if (Objects.nonNull(additionalInfo)) {
if (Objects.nonNull(additionalInfo.getSuppressDiscovery())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.SUPPRESS_DISCOVERY, additionalInfo.getSuppressDiscovery());
}
}
if (Objects.nonNull(metadata)) {
if (StringUtils.isNotEmpty(metadata.getCreatedByUserId())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.CREATED_BY_USER_ID, UUID.fromString(metadata.getCreatedByUserId()));
}
if (Objects.nonNull(metadata.getCreatedDate())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.CREATED_DATE, metadata.getCreatedDate().toInstant().atOffset(ZoneOffset.UTC));
}
if (StringUtils.isNotEmpty(metadata.getUpdatedByUserId())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.UPDATED_BY_USER_ID, UUID.fromString(metadata.getUpdatedByUserId()));
}
if (Objects.nonNull(metadata.getUpdatedDate())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.UPDATED_DATE, metadata.getUpdatedDate().toInstant().atOffset(ZoneOffset.UTC));
}
}
// only attempt update if has id and external values to update
if (Objects.nonNull(updateStep) && Objects.nonNull(record.getId())) {
records.add(record);
recordUpdates.add(updateStep.where(RECORDS_LB.ID.eq(UUID.fromString(record.getId()))));
}
try {
RecordType recordType = toRecordType(record.getRecordType().name());
recordType.formatRecord(record);
parsedRecordUpdates.add(DSL.update(table(name(recordType.getTableName()))).set(prtContent, JSONB.valueOf(ParsedRecordDaoUtil.normalizeContent(record.getParsedRecord()))).where(prtId.eq(UUID.fromString(record.getParsedRecord().getId()))));
} catch (Exception e) {
errorMessages.add(format(INVALID_PARSED_RECORD_MESSAGE_TEMPLATE, record.getId(), e.getMessage()));
// if invalid parsed record, set id to null to filter out
record.getParsedRecord().setId(null);
}
}).map(Record::getParsedRecord).filter(parsedRecord -> Objects.nonNull(parsedRecord.getId())).collect(Collectors.toList());
try (Connection connection = getConnection(tenantId)) {
DSL.using(connection).transaction(ctx -> {
DSLContext dsl = DSL.using(ctx);
// update records
int[] recordUpdateResults = dsl.batch(recordUpdates).execute();
// check record update results
for (int i = 0; i < recordUpdateResults.length; i++) {
int result = recordUpdateResults[i];
if (result == 0) {
errorMessages.add(format("Record with id %s was not updated", records.get(i).getId()));
}
}
// update parsed records
int[] parsedRecordUpdateResults = dsl.batch(parsedRecordUpdates).execute();
// check parsed record update results
List<ParsedRecord> parsedRecordsUpdated = new ArrayList<>();
for (int i = 0; i < parsedRecordUpdateResults.length; i++) {
int result = parsedRecordUpdateResults[i];
ParsedRecord parsedRecord = parsedRecords.get(i);
if (result == 0) {
errorMessages.add(format("Parsed Record with id '%s' was not updated", parsedRecord.getId()));
} else {
parsedRecordsUpdated.add(parsedRecord);
}
}
promise.complete(new ParsedRecordsBatchResponse().withErrorMessages(errorMessages).withParsedRecords(parsedRecordsUpdated).withTotalRecords(parsedRecordsUpdated.size()));
});
} catch (SQLException e) {
LOG.error("Failed to update records", e);
promise.fail(e);
}
return promise.future();
}
use of org.folio.rest.jaxrs.model.AdditionalInfo in project mod-source-record-storage by folio-org.
the class SourceStorageStreamApiTest method shouldReturnEmptyResponseOnSearchMarcRecordIdsWhenRecordWasSuppressed.
@Test
public void shouldReturnEmptyResponseOnSearchMarcRecordIdsWhenRecordWasSuppressed(TestContext testContext) {
// given
Async async = testContext.async();
Record suppressedRecord = new Record().withId(marc_bib_record_2.getId()).withSnapshotId(snapshot_2.getJobExecutionId()).withRecordType(Record.RecordType.MARC_BIB).withRawRecord(marc_bib_record_2.getRawRecord()).withParsedRecord(marc_bib_record_2.getParsedRecord()).withMatchedId(marc_bib_record_2.getMatchedId()).withState(Record.State.ACTUAL).withAdditionalInfo(new AdditionalInfo().withSuppressDiscovery(true));
postSnapshots(testContext, snapshot_2);
postRecords(testContext, suppressedRecord);
MarcRecordSearchRequest searchRequest = new MarcRecordSearchRequest();
searchRequest.setLeaderSearchExpression("p_05 = 'c' and p_06 = 'c' and p_07 = 'm'");
searchRequest.setFieldsSearchExpression("001.value = '393893' and 005.value ^= '2014110' and 035.ind1 = '#'");
// when
ExtractableResponse<Response> response = RestAssured.given().spec(spec).body(searchRequest).when().post("/source-storage/stream/marc-record-identifiers").then().extract();
JsonObject responseBody = new JsonObject(response.body().asString());
// then
assertEquals(HttpStatus.SC_OK, response.statusCode());
assertEquals(0, responseBody.getJsonArray("records").size());
assertEquals(0, responseBody.getInteger("totalCount").intValue());
async.complete();
}
use of org.folio.rest.jaxrs.model.AdditionalInfo in project mod-source-record-storage by folio-org.
the class SourceRecordApiTest method shouldReturnSourceRecordWithAdditionalInfoOnGetBySpecifiedSnapshotId.
@Test
public void shouldReturnSourceRecordWithAdditionalInfoOnGetBySpecifiedSnapshotId(TestContext testContext) {
postSnapshots(testContext, snapshot_2);
String matchedId = UUID.randomUUID().toString();
Record newRecord = new Record().withId(matchedId).withSnapshotId(snapshot_2.getJobExecutionId()).withRecordType(Record.RecordType.MARC_BIB).withRawRecord(rawRecord).withParsedRecord(marcRecord).withMatchedId(matchedId).withAdditionalInfo(new AdditionalInfo().withSuppressDiscovery(true));
Async async = testContext.async();
Response createResponse = RestAssured.given().spec(spec).body(newRecord).when().post(SOURCE_STORAGE_RECORDS_PATH);
assertThat(createResponse.statusCode(), is(HttpStatus.SC_CREATED));
Record createdRecord = createResponse.body().as(Record.class);
async.complete();
async = testContext.async();
Response getResponse = RestAssured.given().spec(spec).when().get(SOURCE_STORAGE_SOURCE_RECORDS_PATH + "?snapshotId=" + newRecord.getSnapshotId() + "&suppressFromDiscovery=true");
assertThat(getResponse.statusCode(), is(HttpStatus.SC_OK));
SourceRecordCollection sourceRecordCollection = getResponse.body().as(SourceRecordCollection.class);
assertThat(sourceRecordCollection.getSourceRecords().size(), is(1));
SourceRecord sourceRecord = sourceRecordCollection.getSourceRecords().get(0);
assertThat(sourceRecord.getRecordId(), is(createdRecord.getId()));
// NOTE: raw record is no longer returned with source records for effeciency
// assertThat(sourceRecord.getRawRecord().getContent(), is(rawRecord.getContent()));
assertThat(sourceRecord.getAdditionalInfo().getSuppressDiscovery(), is(createdRecord.getAdditionalInfo().getSuppressDiscovery()));
async.complete();
}
Aggregations