Skip to content
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 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ include ":smithy-aws-cloudformation-traits"
include ":smithy-aws-cloudformation"
include ":smithy-validation-model"
include ":smithy-rules-engine"
include ":smithy-smoke-test-traits"
include ":smithy-syntax"
25 changes: 25 additions & 0 deletions smithy-smoke-test-traits/build.gradle
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")
}
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public methods need docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added docs


@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();
}
}
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();
}
}
Loading