-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Rpcv2TraitValidator and tests (#1613)
* Implement Rpcv2TraitValidator and tests * Add missing license * Update smithy-protocols-traits/src/main/java/software/amazon/smithy/protocols/traits/Rpcv2TraitValidator.java Co-authored-by: Kevin Stich <[email protected]> * Address comments from PR * Make sure format is always set * Fix typo * Fix comment * Fix comment * Fix trailing whitespace --------- Co-authored-by: Kevin Stich <[email protected]>
- Loading branch information
Showing
9 changed files
with
196 additions
and
5 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
80 changes: 80 additions & 0 deletions
80
...ols-traits/src/main/java/software/amazon/smithy/protocols/traits/Rpcv2TraitValidator.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 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except | ||
* in compliance with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.protocols.traits; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.utils.ListUtils; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Validates models implementing the {@code Rpcv2Trait} against its constraints by: | ||
* | ||
* - Ensuring that every entry in {@code eventStreamHttp} also appears in the {@code http} property | ||
* of a protocol trait. | ||
* - Ensuring that there is at least one value for the {@code format} property. | ||
* - Ensuring all the {@code format} property values are valid and supported. | ||
*/ | ||
@SmithyInternalApi | ||
public final class Rpcv2TraitValidator extends AbstractValidator { | ||
|
||
private static final List<String> VALID_FORMATS = ListUtils.of("cbor"); | ||
|
||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
for (ServiceShape serviceShape : model.getServiceShapesWithTrait(Rpcv2Trait.class)) { | ||
events.addAll(validateService(serviceShape)); | ||
} | ||
return events; | ||
} | ||
|
||
private List<ValidationEvent> validateService(ServiceShape service) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
|
||
Rpcv2Trait protocolTrait = service.expectTrait(Rpcv2Trait.class); | ||
|
||
List<String> invalid = new ArrayList<>(protocolTrait.getEventStreamHttp()); | ||
invalid.removeAll(protocolTrait.getHttp()); | ||
if (!invalid.isEmpty()) { | ||
events.add(error(service, protocolTrait, | ||
String.format("The following values of the `eventStreamHttp` property do " | ||
+ "not also appear in the `http` property of the %s protocol " | ||
+ "trait: %s", protocolTrait.toShapeId(), invalid))); | ||
} | ||
|
||
List<String> formats = new ArrayList<>(protocolTrait.getFormat()); | ||
// Validate there is at least one element in the format property. | ||
if (formats.size() == 0) { | ||
events.add(error(service, protocolTrait, String.format( | ||
"The `format` property for the %s protocol must have at least 1 element", | ||
protocolTrait.toShapeId()))); | ||
} | ||
// All the user specified wire formats must be valid. | ||
formats.removeAll(VALID_FORMATS); | ||
if (!formats.isEmpty()) { | ||
events.add(error(service, protocolTrait, | ||
String.format( | ||
"The following values of the `format` property for the %s protocol " | ||
+ "are not supported: %s", | ||
protocolTrait.toShapeId(), formats)));; | ||
} | ||
|
||
return events; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ts/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator
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 @@ | ||
software.amazon.smithy.protocols.traits.Rpcv2TraitValidator |
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
34 changes: 34 additions & 0 deletions
34
...rotocols-traits/src/test/java/software/amazon/smithy/protocols/traits/TestRunnerTest.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,34 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except | ||
* in compliance with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.protocols.traits; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import software.amazon.smithy.model.validation.testrunner.SmithyTestCase; | ||
import software.amazon.smithy.model.validation.testrunner.SmithyTestSuite; | ||
|
||
public class TestRunnerTest { | ||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("source") | ||
public void testRunner(String filename, Callable<SmithyTestCase.Result> callable) | ||
throws Exception { | ||
callable.call(); | ||
} | ||
|
||
public static Stream<?> source() { | ||
return SmithyTestSuite.defaultParameterizedTestSource(TestRunnerTest.class); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...test/resources/software/amazon/smithy/protocols/traits/errorfiles/cbor-wire-format.errors
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,4 @@ | ||
[ERROR] smithy.example#InvalidService1: The `format` property for the smithy.protocols#rpcv2 protocol must have at least 1 element | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService2: The following values of the `format` property for the smithy.protocols#rpcv2 protocol are not supported: [invalid-wire-format] | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService3: The following values of the `format` property for the smithy.protocols#rpcv2 protocol are not supported: [invalid-wire-format] | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService4: The following values of the `format` property for the smithy.protocols#rpcv2 protocol are not supported: [invalid-wire-format1, invalid-wire-format2] | Rpcv2Trait |
30 changes: 30 additions & 0 deletions
30
...test/resources/software/amazon/smithy/protocols/traits/errorfiles/cbor-wire-format.smithy
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,30 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
use smithy.protocols#rpcv2 | ||
|
||
@rpcv2(format: ["cbor"]) | ||
service ValidService1 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(format: []) | ||
service InvalidService1 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(format: ["invalid-wire-format"]) | ||
service InvalidService2 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(format: ["cbor", "invalid-wire-format"]) | ||
service InvalidService3 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(format: ["invalid-wire-format1", "invalid-wire-format2"]) | ||
service InvalidService4 { | ||
version: "2023-02-10" | ||
} |
3 changes: 3 additions & 0 deletions
3
...es/software/amazon/smithy/protocols/traits/errorfiles/eventStreamHttp-matches-http.errors
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,3 @@ | ||
[ERROR] smithy.example#InvalidService1: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2 protocol trait: [http/1.1] | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService2: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2 protocol trait: [http/1.1] | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService3: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2 protocol trait: [http/1.1, h2c] | Rpcv2Trait |
40 changes: 40 additions & 0 deletions
40
...es/software/amazon/smithy/protocols/traits/errorfiles/eventStreamHttp-matches-http.smithy
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,40 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
use smithy.protocols#rpcv2 | ||
|
||
@rpcv2(http: ["h2", "http/1.1"], eventStreamHttp: ["h2"], format: ["cbor"]) | ||
service ValidService1 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(http: ["h2"], eventStreamHttp: ["h2"], format: ["cbor"]) | ||
service ValidService2 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(http: [], eventStreamHttp: [], format: ["cbor"]) | ||
service ValidService3 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(http: ["http/1.1"], eventStreamHttp: [], format: ["cbor"]) | ||
service ValidService4 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(eventStreamHttp: ["http/1.1"], format: ["cbor"]) | ||
service InvalidService1 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(http: ["h2"], eventStreamHttp: ["http/1.1"], format: ["cbor"]) | ||
service InvalidService2 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2(http: ["h2"], eventStreamHttp: ["h2", "http/1.1", "h2c"], format: ["cbor"]) | ||
service InvalidService3 { | ||
version: "2023-02-10" | ||
} |