-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#3229) feat: implement validator for QuerySpecDto and CatalogRequestDto
- Loading branch information
Showing
37 changed files
with
724 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...mmon/api/api-core/src/main/java/org/eclipse/edc/api/validation/CriterionDtoValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.api.validation; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.validator.jsonobject.JsonLdPath; | ||
import org.eclipse.edc.validator.jsonobject.JsonObjectValidator; | ||
import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
import org.eclipse.edc.validator.spi.Violation; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.lang.String.format; | ||
import static org.eclipse.edc.api.model.CriterionDto.CRITERION_OPERAND_LEFT; | ||
import static org.eclipse.edc.api.model.CriterionDto.CRITERION_OPERAND_RIGHT; | ||
import static org.eclipse.edc.api.model.CriterionDto.CRITERION_OPERATOR; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE; | ||
|
||
public class CriterionDtoValidator { | ||
|
||
public static Validator<JsonObject> instance() { | ||
return instance(JsonObjectValidator.newValidator()).build(); | ||
} | ||
|
||
public static JsonObjectValidator.Builder instance(JsonObjectValidator.Builder builder) { | ||
return builder | ||
.verify(CRITERION_OPERAND_LEFT, MandatoryValue::new) | ||
.verify(CRITERION_OPERATOR, MandatoryValue::new) | ||
.verify(OperandRightValidator::new); | ||
} | ||
|
||
private record OperandRightValidator(JsonLdPath path) implements Validator<JsonObject> { | ||
|
||
@Override | ||
public ValidationResult validate(JsonObject input) { | ||
var operator = Optional.ofNullable(input.getJsonArray(CRITERION_OPERATOR)) | ||
.map(it -> it.getJsonObject(0)) | ||
.map(it -> it.getString(VALUE)) | ||
.orElse(null); | ||
|
||
if (operator == null || "in".equals(operator)) { | ||
return ValidationResult.success(); | ||
} | ||
|
||
return Optional.ofNullable(input.getJsonArray(CRITERION_OPERAND_RIGHT)) | ||
.filter(it -> it.size() == 1) | ||
.map(it -> ValidationResult.success()) | ||
.orElse(ValidationResult.failure(Violation.violation(format("%s cannot contain multiple values as the operator is not 'in'", path.toString()), CRITERION_OPERAND_RIGHT))); | ||
} | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
...mmon/api/api-core/src/main/java/org/eclipse/edc/api/validation/QuerySpecDtoValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.api.validation; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.spi.query.SortOrder; | ||
import org.eclipse.edc.validator.jsonobject.JsonLdPath; | ||
import org.eclipse.edc.validator.jsonobject.JsonObjectValidator; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Collections.emptyList; | ||
import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_FILTER_EXPRESSION; | ||
import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_LIMIT; | ||
import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_OFFSET; | ||
import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_SORT_FIELD; | ||
import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_SORT_ORDER; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE; | ||
import static org.eclipse.edc.validator.spi.Violation.violation; | ||
|
||
public class QuerySpecDtoValidator { | ||
|
||
public static Validator<JsonObject> instance() { | ||
return instance(JsonObjectValidator.newValidator()).build(); | ||
} | ||
|
||
public static JsonObjectValidator.Builder instance(JsonObjectValidator.Builder builder) { | ||
return builder | ||
.verify(EDC_QUERY_SPEC_OFFSET, OptionalValueGreaterEqualZero::new) | ||
.verify(EDC_QUERY_SPEC_LIMIT, OptionalValueGreaterZero::new) | ||
.verify(EDC_QUERY_SPEC_SORT_ORDER, OptionalValueSortField::new) | ||
.verify(EDC_QUERY_SPEC_SORT_FIELD, OptionalValueNotBlank::new) | ||
.verifyObject(EDC_QUERY_SPEC_FILTER_EXPRESSION, CriterionDtoValidator::instance); | ||
} | ||
|
||
private record OptionalValueGreaterEqualZero(JsonLdPath path) implements Validator<JsonObject> { | ||
|
||
@Override | ||
public ValidationResult validate(JsonObject input) { | ||
var value = Optional.ofNullable(input.getJsonArray(path.last())) | ||
.map(it -> it.getJsonObject(0)) | ||
.map(it -> it.getInt(VALUE)) | ||
.orElse(0); | ||
|
||
if (value < 0) { | ||
return ValidationResult.failure(violation(format("optional value '%s' must be greater or equal to zero", path), path.toString(), value)); | ||
} | ||
|
||
return ValidationResult.success(); | ||
} | ||
} | ||
|
||
private record OptionalValueGreaterZero(JsonLdPath path) implements Validator<JsonObject> { | ||
|
||
@Override | ||
public ValidationResult validate(JsonObject input) { | ||
var value = Optional.ofNullable(input.getJsonArray(path.last())) | ||
.map(it -> it.getJsonObject(0)) | ||
.map(it -> it.getInt(VALUE)) | ||
.orElse(1); | ||
|
||
if (value < 1) { | ||
return ValidationResult.failure(violation(format("optional value '%s' must be greater than zero", path), path.toString(), value)); | ||
} | ||
|
||
return ValidationResult.success(); | ||
} | ||
} | ||
|
||
private record OptionalValueSortField(JsonLdPath path) implements Validator<JsonObject> { | ||
|
||
@Override | ||
public ValidationResult validate(JsonObject input) { | ||
var values = Optional.ofNullable(input.getJsonArray(path.last())) | ||
.map(array -> array.stream().map(it -> it.asJsonObject().getString(VALUE)).toList()) | ||
.orElse(emptyList()); | ||
|
||
if (values.size() > 0 && !Arrays.stream(SortOrder.values()).map(Enum::name).toList().contains(values.get(0))) { | ||
var message = format("optional value '%s' must be one of %s", path, Arrays.toString(SortOrder.values())); | ||
return ValidationResult.failure(violation(message, path.toString(), values.get(0))); | ||
} | ||
|
||
return ValidationResult.success(); | ||
} | ||
} | ||
|
||
private record OptionalValueNotBlank(JsonLdPath path) implements Validator<JsonObject> { | ||
|
||
@Override | ||
public ValidationResult validate(JsonObject input) { | ||
var optional = Optional.ofNullable(input.getJsonArray(path.last())) | ||
.map(it -> it.getJsonObject(0)) | ||
.map(it -> it.getString(VALUE)); | ||
|
||
if (optional.isEmpty()) { | ||
return ValidationResult.success(); | ||
} | ||
|
||
return optional | ||
.filter(it -> !it.isBlank()) | ||
.map(it -> ValidationResult.success()) | ||
.orElseGet(() -> ValidationResult.failure(violation(format("optional value '%s' is blank", path), path.toString()))); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/ApiCoreExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.api; | ||
|
||
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_TYPE; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(DependencyInjectionExtension.class) | ||
class ApiCoreExtensionTest { | ||
|
||
private final JsonObjectValidatorRegistry validatorRegistry = mock(); | ||
|
||
@BeforeEach | ||
void setUp(ServiceExtensionContext context) { | ||
context.registerService(JsonObjectValidatorRegistry.class, validatorRegistry); | ||
} | ||
|
||
@Test | ||
void initialize_shouldRegisterValidators(ApiCoreExtension extension, ServiceExtensionContext context) { | ||
extension.initialize(context); | ||
|
||
verify(validatorRegistry).register(eq(EDC_QUERY_SPEC_TYPE), any()); | ||
} | ||
} |
Oops, something went wrong.