-
Notifications
You must be signed in to change notification settings - Fork 218
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
Add smoke test traits #2005
Merged
Merged
Add smoke test traits #2005
Changes from 1 commit
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
description = "Defines smoke test traits." | ||
|
||
ext { | ||
displayName = "Smithy :: Smoke Test Traits" | ||
moduleName = "software.amazon.smithy.smoketest.traits" | ||
} | ||
|
||
dependencies { | ||
api project(":smithy-model") | ||
} |
103 changes: 103 additions & 0 deletions
103
...smoke-test-traits/src/main/java/software/amazon/smithy/smoketests/traits/Expectation.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,103 @@ | ||
/* | ||
* 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.smoketests.traits; | ||
|
||
import java.util.Optional; | ||
import software.amazon.smithy.model.node.ExpectationNotMetException; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.node.ToNode; | ||
import software.amazon.smithy.model.validation.ValidationUtils; | ||
import software.amazon.smithy.utils.ListUtils; | ||
|
||
/** | ||
* Defines the expected result of a smoke test case. | ||
*/ | ||
public final class Expectation implements ToNode { | ||
private static final String SUCCESS = "success"; | ||
private static final String FAILURE = "failure"; | ||
|
||
private final FailureExpectation failure; | ||
|
||
private Expectation(FailureExpectation failure) { | ||
this.failure = failure; | ||
} | ||
|
||
public static Expectation success() { | ||
return new Expectation(null); | ||
} | ||
|
||
public static Expectation failure(FailureExpectation failure) { | ||
return new Expectation(failure); | ||
} | ||
|
||
public boolean isSuccess() { | ||
return failure == null; | ||
} | ||
|
||
public boolean isFailure() { | ||
return failure != null; | ||
} | ||
|
||
public Optional<FailureExpectation> getFailure() { | ||
return Optional.ofNullable(failure); | ||
} | ||
|
||
@Override | ||
public Node toNode() { | ||
ObjectNode.Builder builder = Node.objectNodeBuilder(); | ||
if (this.isSuccess()) { | ||
builder.withMember(SUCCESS, Node.objectNode()); | ||
} else { | ||
Node failureNode = this.getFailure() | ||
.map(FailureExpectation::toNode) | ||
.orElse(Node.objectNode()); | ||
builder.withMember(FAILURE, failureNode); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
public static Expectation fromNode(Node node) { | ||
ObjectNode o = node.expectObjectNode(); | ||
if (o.containsMember(SUCCESS)) { | ||
o.expectNoAdditionalProperties(ListUtils.of(SUCCESS)); | ||
return Expectation.success(); | ||
} else if (o.containsMember(FAILURE)) { | ||
o.expectNoAdditionalProperties(ListUtils.of(FAILURE)); | ||
FailureExpectation failure = FailureExpectation.fromNode(o.expectObjectMember(FAILURE)); | ||
return Expectation.failure(failure); | ||
} else { | ||
throw new ExpectationNotMetException("Expected an object with exactly one `" + SUCCESS + "` or `" + FAILURE | ||
+ "` property, but found properties: " + ValidationUtils.tickedList(o.getStringMap().keySet()), o); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} else if (o == null || o.getClass() != getClass()) { | ||
return false; | ||
} else { | ||
return toNode().equals(((Expectation) o).toNode()); | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return toNode().hashCode(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...est-traits/src/main/java/software/amazon/smithy/smoketests/traits/FailureExpectation.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,86 @@ | ||
/* | ||
* 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.smoketests.traits; | ||
|
||
import java.util.Optional; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.node.StringNode; | ||
import software.amazon.smithy.model.node.ToNode; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
|
||
/** | ||
* Defines a failure expectation of a smoke test case. | ||
*/ | ||
public final class FailureExpectation implements ToNode { | ||
private static final String ERROR_ID = "errorId"; | ||
|
||
private final ShapeId errorId; | ||
|
||
private FailureExpectation(ShapeId errorId) { | ||
this.errorId = errorId; | ||
} | ||
|
||
public static FailureExpectation anyError() { | ||
return new FailureExpectation(null); | ||
} | ||
|
||
public static FailureExpectation errorWithId(ShapeId errorId) { | ||
return new FailureExpectation(errorId); | ||
} | ||
|
||
/** | ||
* Gets the ID of the expected error shape. If present, it indicates | ||
* the call should throw a matching error. Otherwise, the call should | ||
* throw any error. | ||
* | ||
* @return The ID of the expected error shape. | ||
*/ | ||
public Optional<ShapeId> getErrorId() { | ||
return Optional.ofNullable(errorId); | ||
} | ||
|
||
@Override | ||
public Node toNode() { | ||
return Node.objectNodeBuilder() | ||
.withOptionalMember(ERROR_ID, this.getErrorId().map(ShapeId::toString).map(StringNode::from)) | ||
.build(); | ||
} | ||
|
||
public static FailureExpectation fromNode(Node node) { | ||
ObjectNode o = node.expectObjectNode(); | ||
return o.getStringMember(ERROR_ID) | ||
.map(ShapeId::fromNode) | ||
.map(FailureExpectation::errorWithId) | ||
.orElse(FailureExpectation.anyError()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} else if (o == null || o.getClass() != getClass()) { | ||
return false; | ||
} else { | ||
return toNode().equals(((FailureExpectation) o).toNode()); | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return toNode().hashCode(); | ||
} | ||
} |
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.
public methods need docs
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.
Added docs