-
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.
feat: validate DSP catalog messages (#3365)
* feat: validate DSP catalog messages * PR remarks
- Loading branch information
Showing
21 changed files
with
452 additions
and
430 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
.../validator-core/src/main/java/org/eclipse/edc/validator/jsonobject/validators/TypeIs.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,67 @@ | ||
/* | ||
* 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.validator.jsonobject.validators; | ||
|
||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonString; | ||
import jakarta.json.JsonValue; | ||
import org.eclipse.edc.validator.jsonobject.JsonLdPath; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
import static org.eclipse.edc.validator.spi.Violation.violation; | ||
|
||
/** | ||
* Verify that the @value node has a certain value | ||
*/ | ||
public class TypeIs implements Validator<JsonObject> { | ||
|
||
private final JsonLdPath path; | ||
private final String expectedType; | ||
|
||
public TypeIs(JsonLdPath path, String expectedType) { | ||
this.path = path; | ||
this.expectedType = expectedType; | ||
} | ||
|
||
@Override | ||
public ValidationResult validate(JsonObject input) { | ||
var newPath = path.append(TYPE); | ||
var typeStream = Optional.of(input) | ||
.map(it -> it.getJsonArray(TYPE)) | ||
.stream().flatMap(Collection::stream); | ||
|
||
var result = typeStream | ||
.filter(it -> it.getValueType() == JsonValue.ValueType.STRING) | ||
.map(JsonString.class::cast) | ||
.map(JsonString::getString) | ||
.filter(it -> it.equals(expectedType)) | ||
.findFirst(); | ||
|
||
if (result.isPresent()) { | ||
return ValidationResult.success(); | ||
} else { | ||
var violation = violation( | ||
"%s was expected to be %s but it was not".formatted(newPath, expectedType), | ||
newPath.toString(), typeStream | ||
); | ||
return ValidationResult.failure(violation); | ||
} | ||
} | ||
} |
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
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
36 changes: 36 additions & 0 deletions
36
...a/org/eclipse/edc/protocol/dsp/catalog/api/validation/CatalogRequestMessageValidator.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,36 @@ | ||
/* | ||
* 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.protocol.dsp.catalog.api.validation; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.validator.jsonobject.JsonObjectValidator; | ||
import org.eclipse.edc.validator.jsonobject.validators.TypeIs; | ||
import org.eclipse.edc.validator.jsonobject.validators.model.QuerySpecValidator; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
|
||
import static org.eclipse.edc.protocol.dsp.type.DspCatalogPropertyAndTypeNames.DSPACE_PROPERTY_FILTER; | ||
import static org.eclipse.edc.protocol.dsp.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE; | ||
|
||
/** | ||
* Validator for {@link CatalogRequestMessageValidator} Json-LD representation | ||
*/ | ||
public class CatalogRequestMessageValidator { | ||
public static Validator<JsonObject> instance() { | ||
return JsonObjectValidator.newValidator() | ||
.verify(path -> new TypeIs(path, DSPACE_TYPE_CATALOG_REQUEST_MESSAGE)) | ||
.verifyObject(DSPACE_PROPERTY_FILTER, QuerySpecValidator::instance) | ||
.build(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...pi/src/test/java/org/eclipse/edc/protocol/dsp/catalog/api/DspCatalogApiExtensionTest.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.protocol.dsp.catalog.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.protocol.dsp.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE; | ||
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 DspCatalogApiExtensionTest { | ||
|
||
private final JsonObjectValidatorRegistry validatorRegistry = mock(); | ||
|
||
@BeforeEach | ||
void setUp(ServiceExtensionContext context) { | ||
context.registerService(JsonObjectValidatorRegistry.class, validatorRegistry); | ||
} | ||
|
||
@Test | ||
void shouldRegisterMessageValidator(DspCatalogApiExtension extension, ServiceExtensionContext context) { | ||
extension.initialize(context); | ||
|
||
verify(validatorRegistry).register(eq(DSPACE_TYPE_CATALOG_REQUEST_MESSAGE), any()); | ||
} | ||
} |
Oops, something went wrong.