Fix sourceLocation propagation in RetryableTrait

This commit is contained in:
Jaykumar Gosar 2021-07-14 11:30:36 -07:00 committed by Jordon Phillips
parent 4a6814471b
commit 9b4de14a5c
3 changed files with 72 additions and 4 deletions

View File

@ -80,7 +80,7 @@ public final class RangeTrait extends AbstractTrait implements ToSmithyBuilder<R
}
/**
* Builder used to create a LongTrait.
* Builder used to create a RangeTrait.
*/
public static final class Builder extends AbstractTraitBuilder<RangeTrait, Builder> {
private BigDecimal min;

View File

@ -52,12 +52,16 @@ public final class RetryableTrait extends AbstractTrait implements ToSmithyBuild
@Override
public Builder toBuilder() {
return builder().throttling(throttling);
return builder().sourceLocation(getSourceLocation()).throttling(throttling);
}
@Override
protected Node createNode() {
return throttling ? Node.objectNode().withMember(THROTTLING, true) : Node.objectNode();
ObjectNode.Builder nodeBuilder = Node.objectNodeBuilder().sourceLocation(getSourceLocation());
if (throttling) {
nodeBuilder.withMember(THROTTLING, true);
}
return nodeBuilder.build();
}
public static final class Provider implements TraitService {
@ -69,7 +73,8 @@ public final class RetryableTrait extends AbstractTrait implements ToSmithyBuild
@Override
public RetryableTrait createTrait(ShapeId target, Node value) {
ObjectNode node = value.expectObjectNode();
return builder().throttling(node.getBooleanMemberOrDefault(THROTTLING)).build();
Builder builder = builder().sourceLocation(value.getSourceLocation());
return builder.throttling(node.getBooleanMemberOrDefault(THROTTLING)).build();
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 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.model.traits;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ShapeId;
public class RetryableTraitTest {
@Test
public void loadsTraitWithDefault() {
TraitFactory provider = TraitFactory.createServiceFactory();
SourceLocation sourceLocation = new SourceLocation("fileA");
Node node = Node.objectNodeBuilder().sourceLocation(sourceLocation).build();
Optional<Trait> trait = provider.createTrait(
ShapeId.from("smithy.api#retryable"), ShapeId.from("ns.qux#foo"), node);
assertTrue(trait.isPresent());
assertThat(trait.get(), instanceOf(RetryableTrait.class));
RetryableTrait retryableTrait = (RetryableTrait) trait.get();
assertThat(retryableTrait.getSourceLocation(), equalTo(sourceLocation));
assertFalse(retryableTrait.getThrottling());
assertThat(retryableTrait.toNode(), equalTo(node));
assertThat(retryableTrait.toNode().getSourceLocation(), equalTo(sourceLocation));
assertThat(retryableTrait.toBuilder().build(), equalTo(retryableTrait));
assertThat(retryableTrait.toBuilder().build().getSourceLocation(), equalTo(sourceLocation));
}
@Test
public void loadsTraitWithThrottling() {
TraitFactory provider = TraitFactory.createServiceFactory();
Node node = Node.objectNodeBuilder().withMember("throttling", true).build();
Optional<Trait> trait = provider.createTrait(
ShapeId.from("smithy.api#retryable"), ShapeId.from("ns.qux#foo"), node);
assertTrue(trait.isPresent());
assertThat(trait.get(), instanceOf(RetryableTrait.class));
RetryableTrait retryableTrait = (RetryableTrait) trait.get();
assertTrue(retryableTrait.getThrottling());
assertThat(retryableTrait.toNode(), equalTo(node));
assertThat(retryableTrait.toBuilder().build(), equalTo(retryableTrait));
}
}