Search in sources :

Example 1 with GranularityType

use of org.openarchives.oai._2.GranularityType in project mod-oai-pmh by folio-org.

the class AbstractHelper method validateDateRange.

private void validateDateRange(Request request, List<OAIPMHerrorType> errors) {
    Pair<GranularityType, LocalDateTime> from = null;
    Pair<GranularityType, LocalDateTime> until = null;
    // Get repository supported granularity
    boolean isDateOnly = isDateOnlyGranularity(request);
    if (request.getFrom() != null) {
        ImmutablePair<String, String> date = new ImmutablePair<>(FROM_PARAM, request.getFrom());
        from = isDateOnly ? parseDate(date, errors) : parseDateTime(date, errors);
        if (from == null) {
            // In case the 'from' date is invalid, it cannot be sent in OAI-PMH/request@from because it contradicts schema definition
            request.getOaiRequest().setFrom(null);
        }
    }
    if (request.getUntil() != null) {
        ImmutablePair<String, String> date = new ImmutablePair<>(UNTIL_PARAM, request.getUntil());
        until = isDateOnly ? parseDate(date, errors) : parseDateTime(date, errors);
        if (until == null) {
            // In case the 'until' date is invalid, it cannot be sent in OAI-PMH/request@until because it contradicts schema definition
            request.getOaiRequest().setUntil(null);
        }
    }
    if (from != null && until != null) {
        // Both arguments must have the same granularity.
        if (from.getLeft() != until.getLeft()) {
            errors.add(new OAIPMHerrorType().withCode(BAD_ARGUMENT).withValue("Invalid date range: 'from' must have the same granularity as 'until'."));
        } else if (from.getRight().isAfter(until.getRight())) {
            // The from argument must be less than or equal to the until argument.
            errors.add(new OAIPMHerrorType().withCode(BAD_ARGUMENT).withValue("Invalid date range: 'from' must be less than or equal to 'until'."));
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) GranularityType(org.openarchives.oai._2.GranularityType)

Example 2 with GranularityType

use of org.openarchives.oai._2.GranularityType in project mod-oai-pmh by folio-org.

the class OaiPmhImplTest method shouldReturnCorrectHeaderDate_whenGetListRecords.

@ParameterizedTest
@MethodSource("metadataPrefixAndVerbAndGranularityType")
void shouldReturnCorrectHeaderDate_whenGetListRecords(MetadataPrefix metadataPrefix, VerbType verb, GranularityType granularityType) {
    String timeGranularity = System.getProperty(REPOSITORY_TIME_GRANULARITY);
    System.setProperty(REPOSITORY_TIME_GRANULARITY, granularityType.value());
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, verb.value()).param(FROM_PARAM, DEFAULT_RECORD_DATE).param(METADATA_PREFIX_PARAM, metadataPrefix.getName());
    OAIPMH oaipmh = verify200WithXml(request, verb);
    String expectedDate = granularityType.equals(GranularityType.YYYY_MM_DD) ? "2021-03-31" : "2021-03-31T07:23:11Z";
    verifyHeaderDate(expectedDate, oaipmh, verb);
    System.setProperty(REPOSITORY_TIME_GRANULARITY, timeGranularity);
}
Also used : OAIPMH(org.openarchives.oai._2.OAIPMH) RequestSpecification(io.restassured.specification.RequestSpecification) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with GranularityType

use of org.openarchives.oai._2.GranularityType in project mod-oai-pmh by folio-org.

the class OaiPmhImplTest method headerDatestampOfGetRecordVerbShouldCorrespondToGranularitySetting.

@ParameterizedTest
@MethodSource("allMetadataPrefixesAndGranularityTypesProvider")
void headerDatestampOfGetRecordVerbShouldCorrespondToGranularitySetting(MetadataPrefix prefix, GranularityType granularityType) {
    String timeGranularity = System.getProperty(REPOSITORY_TIME_GRANULARITY);
    System.setProperty(REPOSITORY_TIME_GRANULARITY, granularityType.value());
    String identifier = IDENTIFIER_PREFIX + OkapiMockServer.EXISTING_IDENTIFIER;
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, GET_RECORD.value()).param(IDENTIFIER_PARAM, identifier).param(METADATA_PREFIX_PARAM, prefix.getName());
    OAIPMH oaipmh = verify200WithXml(request, GET_RECORD);
    verifyHeaderDateStamp(oaipmh, GET_RECORD, granularityType.value());
    System.setProperty(REPOSITORY_TIME_GRANULARITY, timeGranularity);
}
Also used : OAIPMH(org.openarchives.oai._2.OAIPMH) RequestSpecification(io.restassured.specification.RequestSpecification) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

RequestSpecification (io.restassured.specification.RequestSpecification)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 OAIPMH (org.openarchives.oai._2.OAIPMH)2 LocalDateTime (java.time.LocalDateTime)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 GranularityType (org.openarchives.oai._2.GranularityType)1 OAIPMHerrorType (org.openarchives.oai._2.OAIPMHerrorType)1