Search in sources :

Example 1 with BAD_ARGUMENT

use of org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_ARGUMENT 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 BAD_ARGUMENT

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

the class VerbValidator method validateExclusiveParam.

/**
 * In case of resumption token param presence verifies if there any other parameters were specified too. If they were then error
 * will be added to error list.
 *
 * @param verb          - verb
 * @param requestParams - request parameters
 * @param request       - oai-pmh request
 * @param errors        - list of errors
 */
private void validateExclusiveParam(Verb verb, Map<String, String> requestParams, Request request, List<OAIPMHerrorType> errors) {
    String resumptionToken = requestParams.get(verb.getExclusiveParam());
    if (verb.getExclusiveParam() != null && resumptionToken != null) {
        requestParams.keySet().stream().filter(p -> !verb.getExcludedParams().contains(p)).filter(p -> !verb.getExclusiveParam().equals(p)).findAny().ifPresent(param -> {
            if (!param.equals(VERB_PARAM)) {
                errors.add(new OAIPMHerrorType().withCode(BAD_ARGUMENT).withValue(format(EXCLUSIVE_PARAM_ERROR_MESSAGE, verb.name(), verb.getExclusiveParam())));
            }
        });
        if (!request.isResumptionTokenParsableAndValid()) {
            OAIPMHerrorType error = new OAIPMHerrorType().withCode(BAD_RESUMPTION_TOKEN).withValue(format(INVALID_RESUMPTION_TOKEN, verb.name()));
            errors.add(error);
            return;
        }
        if (request.isResumptionTokenTimeExpired()) {
            OAIPMHerrorType errorByExpiredTime = new OAIPMHerrorType().withCode(BAD_RESUMPTION_TOKEN).withValue(EXPIRED_RESUMPTION_TOKEN);
            errors.add(errorByExpiredTime);
        }
    }
}
Also used : INVALID_RESUMPTION_TOKEN(org.folio.oaipmh.Constants.INVALID_RESUMPTION_TOKEN) EXPIRED_RESUMPTION_TOKEN(org.folio.oaipmh.Constants.EXPIRED_RESUMPTION_TOKEN) Set(java.util.Set) VERB_PARAM(org.folio.oaipmh.Constants.VERB_PARAM) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) BAD_ARGUMENT(org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_ARGUMENT) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) Component(org.springframework.stereotype.Component) List(java.util.List) Request(org.folio.oaipmh.Request) Verb(org.folio.oaipmh.domain.Verb) Map(java.util.Map) BAD_RESUMPTION_TOKEN(org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_RESUMPTION_TOKEN) BAD_VERB(org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_VERB) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType)

Example 3 with BAD_ARGUMENT

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

the class OaiPmhImplTest method getOaiGetRecordVerbWithoutMetadataPrefix.

@Test
void getOaiGetRecordVerbWithoutMetadataPrefix(VertxTestContext testContext) {
    String identifier = IDENTIFIER_PREFIX + OkapiMockServer.EXISTING_IDENTIFIER;
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, GET_RECORD.value()).param(IDENTIFIER_PARAM, identifier);
    OAIPMH oaipmh = verifyResponseWithErrors(request, GET_RECORD, 400, 1);
    assertThat(oaipmh.getGetRecord(), is(nullValue()));
    assertThat(oaipmh.getErrors().get(0).getCode(), equalTo(BAD_ARGUMENT));
    testContext.completeNow();
}
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) Test(org.junit.jupiter.api.Test)

Example 4 with BAD_ARGUMENT

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

the class OaiPmhImplTest method getOaiListVerbWithoutParams.

@ParameterizedTest
@EnumSource(value = VerbType.class, names = { "LIST_IDENTIFIERS", "LIST_RECORDS" })
void getOaiListVerbWithoutParams(VerbType verb) {
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, verb.value());
    List<OAIPMHerrorType> errors = verifyResponseWithErrors(request, verb, 400, 1).getErrors();
    OAIPMHerrorType error = errors.get(0);
    assertThat(error.getCode(), equalTo(BAD_ARGUMENT));
    assertThat(error.getValue(), equalTo(LIST_NO_REQUIRED_PARAM_ERROR));
}
Also used : OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) RequestSpecification(io.restassured.specification.RequestSpecification) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with BAD_ARGUMENT

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

the class OaiPmhImplTest method shouldRespondWithBadArgument_whenGetOaiRecordsMarc21WithHoldingsWithInvalidDateRange.

@Test
void shouldRespondWithBadArgument_whenGetOaiRecordsMarc21WithHoldingsWithInvalidDateRange() {
    final String currentValue = System.getProperty(REPOSITORY_MAX_RECORDS_PER_RESPONSE);
    System.setProperty(REPOSITORY_MAX_RECORDS_PER_RESPONSE, "50");
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, LIST_RECORDS.value()).param(FROM_PARAM, INVALID_FROM_PARAM).param(UNTIL_PARAM, INVALID_UNTIL_PARAM).param(METADATA_PREFIX_PARAM, MetadataPrefix.MARC21WITHHOLDINGS.getName());
    OAIPMH oaipmh = verifyResponseWithErrors(request, LIST_RECORDS, 400, 1);
    assertThat(oaipmh.getRequest().getMetadataPrefix(), equalTo(MetadataPrefix.MARC21WITHHOLDINGS.getName()));
    OAIPMHerrorType error = oaipmh.getErrors().get(0);
    assertThat(error.getCode(), equalTo(BAD_ARGUMENT));
    System.setProperty(REPOSITORY_MAX_RECORDS_PER_RESPONSE, currentValue);
}
Also used : OAIPMH(org.openarchives.oai._2.OAIPMH) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) RequestSpecification(io.restassured.specification.RequestSpecification) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

RequestSpecification (io.restassured.specification.RequestSpecification)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 OAIPMH (org.openarchives.oai._2.OAIPMH)9 OAIPMHerrorType (org.openarchives.oai._2.OAIPMHerrorType)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)7 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 Test (org.junit.jupiter.api.Test)4 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 EXPIRED_RESUMPTION_TOKEN (org.folio.oaipmh.Constants.EXPIRED_RESUMPTION_TOKEN)2 INVALID_RESUMPTION_TOKEN (org.folio.oaipmh.Constants.INVALID_RESUMPTION_TOKEN)2 VERB_PARAM (org.folio.oaipmh.Constants.VERB_PARAM)2