-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implements new DataAddress validation
- Loading branch information
Showing
13 changed files
with
352 additions
and
111 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
44 changes: 44 additions & 0 deletions
44
...aws/aws-s3-core/src/main/java/org/eclipse/edc/aws/s3/S3DataAddressValidatorExtension.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 implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.aws.s3; | ||
|
||
import org.eclipse.edc.aws.s3.validation.S3DataAddressValidator; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Extension; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Inject; | ||
import org.eclipse.edc.spi.system.ServiceExtension; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.eclipse.edc.validator.spi.DataAddressValidatorRegistry; | ||
|
||
import static org.eclipse.edc.aws.s3.S3DataAddressValidatorExtension.NAME; | ||
|
||
@Extension(NAME) | ||
public class S3DataAddressValidatorExtension implements ServiceExtension { | ||
public static final String NAME = "DataAddress S3 Validator"; | ||
|
||
@Inject | ||
private DataAddressValidatorRegistry validatorRegistry; | ||
|
||
@Override | ||
public String name() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public void initialize(ServiceExtensionContext context) { | ||
var validator = new S3DataAddressValidator(); | ||
validatorRegistry.registerDestinationValidator(S3BucketSchema.TYPE, validator); | ||
validatorRegistry.registerSourceValidator(S3BucketSchema.TYPE, validator); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...re/src/main/java/org/eclipse/edc/aws/s3/validation/S3DataAddressCredentialsValidator.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,51 @@ | ||
/* | ||
* 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 implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.aws.s3.validation; | ||
|
||
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.aws.s3.S3BucketSchema.ACCESS_KEY_ID; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.SECRET_ACCESS_KEY; | ||
import static org.eclipse.edc.validator.spi.Violation.violation; | ||
|
||
/** | ||
* Validator for AmazonS3 DataAddress type when credentials are in the data address (Optional) | ||
*/ | ||
public class S3DataAddressCredentialsValidator implements Validator<DataAddress> { | ||
@Override | ||
public ValidationResult validate(DataAddress dataAddress) { | ||
var violations = Stream.of(ACCESS_KEY_ID, SECRET_ACCESS_KEY) | ||
.map(it -> { | ||
var value = dataAddress.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); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...s/aws-s3-core/src/main/java/org/eclipse/edc/aws/s3/validation/S3DataAddressValidator.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,53 @@ | ||
/* | ||
* 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 implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.aws.s3.validation; | ||
|
||
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.aws.s3.S3BucketSchema.BUCKET_NAME; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.REGION; | ||
import static org.eclipse.edc.validator.spi.Violation.violation; | ||
|
||
/** | ||
* Validator for AmazonS3 DataAddress type | ||
*/ | ||
public class S3DataAddressValidator implements Validator<DataAddress> { | ||
|
||
@Override | ||
public ValidationResult validate(DataAddress dataAddress) { | ||
var violations = Stream.of(BUCKET_NAME, REGION) | ||
.map(it -> { | ||
var value = dataAddress.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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ | |
# | ||
|
||
org.eclipse.edc.aws.s3.S3CoreExtension | ||
|
||
org.eclipse.edc.aws.s3.S3DataAddressValidatorExtension |
51 changes: 51 additions & 0 deletions
51
...aws-s3-core/src/test/java/org/eclipse/edc/aws/s3/S3DataAddressValidatorExtensionTest.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,51 @@ | ||
/* | ||
* 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 implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.aws.s3; | ||
|
||
|
||
import org.eclipse.edc.aws.s3.validation.S3DataAddressValidator; | ||
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.eclipse.edc.validator.spi.DataAddressValidatorRegistry; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isA; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(DependencyInjectionExtension.class) | ||
public class S3DataAddressValidatorExtensionTest { | ||
|
||
private final DataAddressValidatorRegistry registry = mock(); | ||
|
||
@BeforeEach | ||
void setup(ServiceExtensionContext context) { | ||
context.registerService(DataAddressValidatorRegistry.class, registry); | ||
} | ||
|
||
@Test | ||
void initialize(S3DataAddressValidatorExtension extension, ServiceExtensionContext context) { | ||
extension.initialize(context); | ||
|
||
assertThat(extension.name()).isEqualTo(S3DataAddressValidatorExtension.NAME); | ||
|
||
verify(registry).registerDestinationValidator(eq(S3BucketSchema.TYPE), isA(S3DataAddressValidator.class)); | ||
verify(registry).registerSourceValidator(eq(S3BucketSchema.TYPE), isA(S3DataAddressValidator.class)); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...rc/test/java/org/eclipse/edc/aws/s3/validation/S3DataAddressCredentialsValidatorTest.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,59 @@ | ||
/* | ||
* 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 implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.aws.s3.validation; | ||
|
||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.validator.spi.ValidationFailure; | ||
import org.eclipse.edc.validator.spi.Violation; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.ACCESS_KEY_ID; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.SECRET_ACCESS_KEY; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.TYPE; | ||
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | ||
|
||
public class S3DataAddressCredentialsValidatorTest { | ||
|
||
private final S3DataAddressCredentialsValidator validator = new S3DataAddressCredentialsValidator(); | ||
|
||
@Test | ||
void shouldPass_whenDataAddressIsValid() { | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.type(TYPE) | ||
.property(ACCESS_KEY_ID, "ak") | ||
.property(SECRET_ACCESS_KEY, "sk") | ||
.build(); | ||
|
||
var result = validator.validate(dataAddress); | ||
|
||
assertThat(result).isSucceeded(); | ||
} | ||
|
||
@Test | ||
void shouldFail_whenRequiredFieldsAreMissing() { | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.type(TYPE) | ||
.build(); | ||
|
||
var result = validator.validate(dataAddress); | ||
|
||
assertThat(result).isFailed() | ||
.extracting(ValidationFailure::getViolations) | ||
.satisfies(violations -> assertThat(violations) | ||
.extracting(Violation::path) | ||
.containsExactlyInAnyOrder(ACCESS_KEY_ID, SECRET_ACCESS_KEY)); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...s-s3-core/src/test/java/org/eclipse/edc/aws/s3/validation/S3DataAddressValidatorTest.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,60 @@ | ||
/* | ||
* 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 implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.aws.s3.validation; | ||
|
||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.validator.spi.ValidationFailure; | ||
import org.eclipse.edc.validator.spi.Violation; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.BUCKET_NAME; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.REGION; | ||
import static org.eclipse.edc.aws.s3.S3BucketSchema.TYPE; | ||
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | ||
|
||
public class S3DataAddressValidatorTest { | ||
|
||
private final S3DataAddressValidator validator = new S3DataAddressValidator(); | ||
|
||
@Test | ||
void shouldPass_whenDataAddressIsValid() { | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.type(TYPE) | ||
.property(BUCKET_NAME, "bucketName") | ||
.property(REGION, "region") | ||
.build(); | ||
|
||
var result = validator.validate(dataAddress); | ||
|
||
assertThat(result).isSucceeded(); | ||
} | ||
|
||
@Test | ||
void shouldFail_whenRequiredFieldsAreMissing() { | ||
var dataAddress = DataAddress.Builder.newInstance() | ||
.type(TYPE) | ||
.build(); | ||
|
||
var result = validator.validate(dataAddress); | ||
|
||
|
||
assertThat(result).isFailed() | ||
.extracting(ValidationFailure::getViolations) | ||
.satisfies(violations -> assertThat(violations) | ||
.extracting(Violation::path) | ||
.containsExactlyInAnyOrder(BUCKET_NAME, REGION)); | ||
} | ||
} |
Oops, something went wrong.