-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
data-management-api: validate query parameters #1258
Merged
bscholtes1A
merged 3 commits into
eclipse-edc:main
from
ndr-brt:feature/1235-query-param-validation
May 10, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
120 changes: 120 additions & 0 deletions
120
...ons/api/api-core/src/main/java/org/eclipse/dataspaceconnector/api/query/QuerySpecDto.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,120 @@ | ||
/* | ||
* Copyright (c) 2022 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.dataspaceconnector.api.query; | ||
|
||
import jakarta.validation.constraints.AssertTrue; | ||
import jakarta.validation.constraints.Positive; | ||
import jakarta.validation.constraints.PositiveOrZero; | ||
import jakarta.ws.rs.QueryParam; | ||
import org.eclipse.dataspaceconnector.spi.query.SortOrder; | ||
|
||
public class QuerySpecDto { | ||
|
||
@QueryParam("offset") | ||
@PositiveOrZero | ||
private Integer offset = 0; | ||
|
||
@QueryParam("limit") | ||
@Positive | ||
private Integer limit = 50; | ||
|
||
@QueryParam("filter") | ||
private String filter; | ||
|
||
@QueryParam("sort") | ||
private SortOrder sortOrder = SortOrder.ASC; | ||
|
||
@QueryParam("sortField") | ||
private String sortField; | ||
|
||
public QuerySpecDto() { | ||
|
||
} | ||
|
||
public Integer getOffset() { | ||
return offset; | ||
} | ||
|
||
public Integer getLimit() { | ||
return limit; | ||
} | ||
|
||
public String getFilter() { | ||
return filter; | ||
} | ||
|
||
public SortOrder getSortOrder() { | ||
return sortOrder; | ||
} | ||
|
||
public String getSortField() { | ||
return sortField; | ||
} | ||
|
||
@AssertTrue | ||
public boolean isValid() { | ||
if (filter != null && filter.isBlank()) { | ||
return false; | ||
} | ||
|
||
if (sortField != null && sortField.isBlank()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static final class Builder { | ||
private final QuerySpecDto querySpec; | ||
|
||
private Builder() { | ||
querySpec = new QuerySpecDto(); | ||
} | ||
|
||
public static Builder newInstance() { | ||
return new Builder(); | ||
} | ||
|
||
public Builder offset(Integer offset) { | ||
querySpec.offset = offset; | ||
return this; | ||
} | ||
|
||
public Builder limit(Integer limit) { | ||
querySpec.limit = limit; | ||
return this; | ||
} | ||
|
||
public Builder sortOrder(SortOrder sortOrder) { | ||
querySpec.sortOrder = sortOrder; | ||
return this; | ||
} | ||
|
||
public Builder sortField(String sortField) { | ||
querySpec.sortField = sortField; | ||
return this; | ||
} | ||
|
||
public Builder filter(String filter) { | ||
querySpec.filter = filter; | ||
return this; | ||
} | ||
|
||
public QuerySpecDto build() { | ||
return querySpec; | ||
} | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...va/org/eclipse/dataspaceconnector/api/transformer/QuerySpecDtoToQuerySpecTransformer.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,45 @@ | ||
/* | ||
* Copyright (c) 2022 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.dataspaceconnector.api.transformer; | ||
|
||
import org.eclipse.dataspaceconnector.api.query.QuerySpecDto; | ||
import org.eclipse.dataspaceconnector.spi.query.QuerySpec; | ||
import org.eclipse.dataspaceconnector.spi.transformer.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class QuerySpecDtoToQuerySpecTransformer implements DtoTransformer<QuerySpecDto, QuerySpec> { | ||
|
||
@Override | ||
public Class<QuerySpecDto> getInputType() { | ||
return QuerySpecDto.class; | ||
} | ||
|
||
@Override | ||
public Class<QuerySpec> getOutputType() { | ||
return QuerySpec.class; | ||
} | ||
|
||
@Override | ||
public @Nullable QuerySpec transform(@Nullable QuerySpecDto object, @NotNull TransformerContext context) { | ||
return QuerySpec.Builder.newInstance() | ||
.limit(object.getLimit()) | ||
.offset(object.getOffset()) | ||
.filter(object.getFilter()) | ||
.sortField(object.getSortField()) | ||
.sortOrder(object.getSortOrder()) | ||
.build(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...re/src/test/java/org/eclipse/dataspaceconnector/api/query/QuerySpecDtoValidationTest.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,80 @@ | ||
/* | ||
* Copyright (c) 2022 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.dataspaceconnector.api.query; | ||
|
||
import jakarta.validation.Validation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.ValidatorFactory; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class QuerySpecDtoValidationTest { | ||
|
||
private Validator validator; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { | ||
validator = factory.getValidator(); | ||
} | ||
} | ||
|
||
@Test | ||
void defaultQuerySpecIsValid() { | ||
var querySpec = QuerySpecDto.Builder.newInstance().build(); | ||
|
||
var result = validator.validate(querySpec); | ||
|
||
assertThat(result).isEmpty(); | ||
} | ||
|
||
@Test | ||
void limitShouldBeGreaterThanZero() { | ||
var querySpec = QuerySpecDto.Builder.newInstance().limit(0).build(); | ||
|
||
var result = validator.validate(querySpec); | ||
|
||
assertThat(result).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void offsetShouldBeGreaterOrEqualThanZero() { | ||
var querySpec = QuerySpecDto.Builder.newInstance().offset(-1).build(); | ||
|
||
var result = validator.validate(querySpec); | ||
|
||
assertThat(result).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void filterShouldNotBeBlank() { | ||
var querySpec = QuerySpecDto.Builder.newInstance().filter(" ").build(); | ||
|
||
var result = validator.validate(querySpec); | ||
|
||
assertThat(result).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void sortFieldShouldNotBeBlank() { | ||
var querySpec = QuerySpecDto.Builder.newInstance().sortField(" ").build(); | ||
|
||
var result = validator.validate(querySpec); | ||
|
||
assertThat(result).isNotEmpty(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...rg/eclipse/dataspaceconnector/api/transformer/QuerySpecDtoToQuerySpecTransformerTest.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,71 @@ | ||
/* | ||
* Copyright (c) 2022 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.dataspaceconnector.api.transformer; | ||
|
||
import org.eclipse.dataspaceconnector.api.query.QuerySpecDto; | ||
import org.eclipse.dataspaceconnector.spi.query.Criterion; | ||
import org.eclipse.dataspaceconnector.spi.query.SortOrder; | ||
import org.eclipse.dataspaceconnector.spi.transformer.TransformerContext; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class QuerySpecDtoToQuerySpecTransformerTest { | ||
|
||
private final QuerySpecDtoToQuerySpecTransformer transformer = new QuerySpecDtoToQuerySpecTransformer(); | ||
|
||
@Test | ||
void inputOutputType() { | ||
assertThat(transformer.getInputType()).isNotNull(); | ||
assertThat(transformer.getOutputType()).isNotNull(); | ||
} | ||
|
||
@Test | ||
void transform() { | ||
var context = mock(TransformerContext.class); | ||
var querySpecDto = QuerySpecDto.Builder.newInstance() | ||
.offset(10) | ||
.limit(20) | ||
.filter("field=value") | ||
.sortOrder(SortOrder.DESC) | ||
.sortField("field") | ||
.build(); | ||
|
||
var spec = transformer.transform(querySpecDto, context); | ||
|
||
assertThat(spec.getOffset()).isEqualTo(10); | ||
assertThat(spec.getLimit()).isEqualTo(20); | ||
assertThat(spec.getFilterExpression()).hasSize(1) | ||
.containsExactly(new Criterion("field", "=", "value")); | ||
assertThat(spec.getSortOrder()).isEqualTo(SortOrder.DESC); | ||
assertThat(spec.getSortField()).isEqualTo("field"); | ||
} | ||
|
||
@Test | ||
void transform_defaultValues() { | ||
var context = mock(TransformerContext.class); | ||
var querySpecDto = QuerySpecDto.Builder.newInstance().build(); | ||
|
||
var spec = transformer.transform(querySpecDto, context); | ||
|
||
assertThat(spec.getOffset()).isEqualTo(0); | ||
assertThat(spec.getLimit()).isEqualTo(50); | ||
assertThat(spec.getFilterExpression()).hasSize(0); | ||
assertThat(spec.getSortOrder()).isEqualTo(SortOrder.ASC); | ||
assertThat(spec.getSortField()).isNull(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to have the
@JsonIgnore
annotation, otherwise Jackson will look for avalid
field. A serialization test would have surfaced that. Mind adding one?For example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is never serialized to/deserialized from json, so it's not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't checked, but what about tests (integration, e2e)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests are working just fine without touching them, I just add a "failing query" test for every controller
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, that'll work, but as far as I'm aware @algattik has plans to generate a java API client from our own OpenAPI spec and use that in tests. If that is the case we'd indeed have to serialize the
QuerySpecDto
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paullatzelsperger I'm not sure about that, however I would leave this for that issue, if it's needed it will be done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, fine for me.