-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make DataAddressValidator extensible
- Loading branch information
Showing
69 changed files
with
660 additions
and
542 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
62 changes: 62 additions & 0 deletions
62
.../main/java/org/eclipse/edc/connector/core/validator/DataAddressValidatorRegistryImpl.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,62 @@ | ||
/* | ||
* 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.connector.core.validator; | ||
|
||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.validator.spi.DataAddressValidatorRegistry; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class DataAddressValidatorRegistryImpl implements DataAddressValidatorRegistry { | ||
|
||
private final Map<String, Validator<DataAddress>> sourceValidators = new HashMap<>(); | ||
private final Map<String, Validator<DataAddress>> destinationValidators = new HashMap<>(); | ||
private final Monitor monitor; | ||
|
||
public DataAddressValidatorRegistryImpl(Monitor monitor) { | ||
this.monitor = monitor; | ||
} | ||
|
||
@Override | ||
public void registerSourceValidator(String type, Validator<DataAddress> validator) { | ||
sourceValidators.put(type, validator); | ||
} | ||
|
||
@Override | ||
public void registerDestinationValidator(String type, Validator<DataAddress> validator) { | ||
destinationValidators.put(type, validator); | ||
} | ||
|
||
@Override | ||
public ValidationResult validateSource(DataAddress dataAddress) { | ||
return sourceValidators.getOrDefault(dataAddress.getType(), d -> warning("source")).validate(dataAddress); | ||
} | ||
|
||
@Override | ||
public ValidationResult validateDestination(DataAddress dataAddress) { | ||
return destinationValidators.getOrDefault(dataAddress.getType(), d -> warning("destination")).validate(dataAddress); | ||
} | ||
|
||
@NotNull | ||
private ValidationResult warning(String type) { | ||
monitor.warning("No %s DataAddress validator has been registered, please register one as it is strongly recommended.".formatted(type)); | ||
return ValidationResult.success(); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...t/java/org/eclipse/edc/connector/core/validator/DataAddressValidatorRegistryImplTest.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,97 @@ | ||
/* | ||
* 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.connector.core.validator; | ||
|
||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.validator.spi.DataAddressValidatorRegistry; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class DataAddressValidatorRegistryImplTest { | ||
|
||
private final Monitor monitor = mock(); | ||
private final DataAddressValidatorRegistry validator = new DataAddressValidatorRegistryImpl(monitor); | ||
|
||
@Nested | ||
class Source { | ||
@Test | ||
void shouldCallRegisteredValidator() { | ||
Validator<DataAddress> typeValidator = mock(); | ||
when(typeValidator.validate(any())).thenReturn(ValidationResult.success()); | ||
validator.registerSourceValidator("type", typeValidator); | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.property("type", "type") | ||
.build(); | ||
|
||
var result = validator.validateSource(dataAddress); | ||
|
||
assertThat(result).isSucceeded(); | ||
verify(typeValidator).validate(dataAddress); | ||
} | ||
|
||
@Test | ||
void shouldSucceedWithWarning_whenTypeIsNotRegistered() { | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.property("type", "not-registered") | ||
.build(); | ||
|
||
var result = validator.validateSource(dataAddress); | ||
|
||
assertThat(result).isSucceeded(); | ||
verify(monitor).warning(anyString()); | ||
} | ||
} | ||
|
||
@Nested | ||
class Destination { | ||
@Test | ||
void shouldCallRegisteredValidator() { | ||
Validator<DataAddress> typeValidator = mock(); | ||
when(typeValidator.validate(any())).thenReturn(ValidationResult.success()); | ||
validator.registerDestinationValidator("type", typeValidator); | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.property("type", "type") | ||
.build(); | ||
|
||
var result = validator.validateDestination(dataAddress); | ||
|
||
assertThat(result).isSucceeded(); | ||
verify(typeValidator).validate(dataAddress); | ||
} | ||
|
||
@Test | ||
void shouldSucceedWithWarning_whenTypeIsNotRegistered() { | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.property("type", "not-registered") | ||
.build(); | ||
|
||
var result = validator.validateDestination(dataAddress); | ||
|
||
assertThat(result).isSucceeded(); | ||
verify(monitor).warning(anyString()); | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ore/src/main/java/org/eclipse/edc/validator/dataaddress/HttpDataDataAddressValidator.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,44 @@ | ||
/* | ||
* 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.dataaddress; | ||
|
||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import static org.eclipse.edc.spi.dataaddress.HttpDataAddressSchema.BASE_URL; | ||
import static org.eclipse.edc.spi.dataaddress.HttpDataAddressSchema.HTTP_DATA_TYPE; | ||
import static org.eclipse.edc.validator.spi.Violation.violation; | ||
|
||
/** | ||
* Validator for HttpData type | ||
*/ | ||
public class HttpDataDataAddressValidator implements Validator<DataAddress> { | ||
|
||
@Override | ||
public ValidationResult validate(DataAddress dataAddress) { | ||
var baseUrl = dataAddress.getStringProperty(BASE_URL); | ||
try { | ||
new URL(baseUrl); | ||
return ValidationResult.success(); | ||
} catch (MalformedURLException e) { | ||
var violation = violation("DataAddress of type %s must contain a valid baseUrl.".formatted(HTTP_DATA_TYPE), BASE_URL, baseUrl); | ||
return ValidationResult.failure(violation); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...r-core/src/main/java/org/eclipse/edc/validator/dataaddress/KafkaDataAddressValidator.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,50 @@ | ||
/* | ||
* 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.dataaddress; | ||
|
||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
|
||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
import static org.eclipse.edc.spi.dataaddress.KafkaDataAddressSchema.BOOTSTRAP_SERVERS; | ||
import static org.eclipse.edc.spi.dataaddress.KafkaDataAddressSchema.TOPIC; | ||
import static org.eclipse.edc.validator.spi.Violation.violation; | ||
|
||
public class KafkaDataAddressValidator implements Validator<DataAddress> { | ||
|
||
@Override | ||
public ValidationResult validate(DataAddress input) { | ||
var violations = Stream.of(TOPIC, BOOTSTRAP_SERVERS) | ||
.map(it -> { | ||
var value = input.getStringProperty(it); | ||
if (value == null || value.isBlank()) { | ||
return violation("'%s' is a mandatory attribute".formatted(it), it, value); | ||
} | ||
return null; | ||
}) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
|
||
if (violations.isEmpty()) { | ||
return ValidationResult.success(); | ||
} | ||
|
||
return ValidationResult.failure(violations); | ||
} | ||
|
||
} |
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
Oops, something went wrong.