-
Notifications
You must be signed in to change notification settings - Fork 179
/
EndpointsDecoratorTest.kt
161 lines (151 loc) · 6.62 KB
/
EndpointsDecoratorTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.client.endpoint
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.string.shouldContain
import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.client.testutil.clientIntegrationTest
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.testutil.TokioTest
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.testutil.integrationTest
import software.amazon.smithy.rust.codegen.core.testutil.runWithWarnings
import software.amazon.smithy.rust.codegen.core.util.CommandFailed
/**
* End-to-end test of endpoint resolvers, attaching a real resolver to a fully generated service
*/
class EndpointsDecoratorTest {
val model = """
namespace test
use smithy.rules#endpointRuleSet
use smithy.rules#endpointTests
use smithy.rules#clientContextParams
use smithy.rules#staticContextParams
use smithy.rules#contextParam
use aws.protocols#awsJson1_1
@awsJson1_1
@endpointRuleSet({
"version": "1.0",
"rules": [{
"conditions": [
{"fn": "isSet", "argv": [{"ref":"Region"}]},
{"fn": "isSet", "argv": [{"ref":"ABoolParam"}]},
{"fn": "booleanEquals", "argv": [{"ref": "ABoolParam"}, false]}
],
"type": "endpoint",
"endpoint": {
"url": "https://www.{Region}.example.com",
"properties": {
"first-properties": {
"z-first": "zazz",
"y-second": "bar",
"x-third": "baz"
},
"second-properties": [1,2,3]
}
}
}],
"parameters": {
"Bucket": { "required": false, "type": "String" },
"Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
"BuiltInWithDefault": { "required": true, "type": "String", "builtIn": "AWS::DefaultBuiltIn", "default": "some-default" },
"BoolBuiltInWithDefault": { "required": true, "type": "Boolean", "builtIn": "AWS::FooBar", "default": true },
"AStringParam": { "required": false, "type": "String" },
"ABoolParam": { "required": false, "type": "Boolean" }
}
})
@clientContextParams(
AStringParam: {
documentation: "string docs",
type: "string"
},
aBoolParam: {
documentation: "bool docs",
type: "boolean"
}
)
@endpointTests({
"version": "1.0",
"testCases": [
{
"documentation": "uriEncode when the string has nothing to encode returns the input",
"params": {
"Region": "test-region"
},
"expect": {
"endpoint": {
"url": "https://failingtest.com"
"properties": {
"first-properties": {
"a-first": "zazz",
"b-second": "bar",
"c-third": "baz"
},
"second-properties": [1,2,3]
}
}
}
}
]
})
service TestService {
operations: [TestOperation]
}
@staticContextParams(Region: { value: "us-east-2" })
operation TestOperation {
input: TestOperationInput
}
structure TestOperationInput {
@contextParam(name: "Bucket")
bucket: String
}
""".asSmithyModel()
@Test
fun `set an endpoint in the property bag`() {
val testDir = clientIntegrationTest(
model,
// just run integration tests
command = { "cargo test --test *".runWithWarnings(it) },
) { clientCodegenContext, rustCrate ->
rustCrate.integrationTest("endpoint_params_test") {
val moduleName = clientCodegenContext.moduleUseName()
TokioTest.render(this)
rust(
"""
async fn endpoint_params_are_set() {
let conf = $moduleName::Config::builder().a_string_param("hello").a_bool_param(false).build();
let operation = $moduleName::operation::TestOperation::builder()
.bucket("bucket-name").build().expect("input is valid")
.make_operation(&conf).await.expect("valid operation");
use $moduleName::endpoint::{Params};
use aws_smithy_http::endpoint::Result;
let props = operation.properties();
let endpoint_result = dbg!(props.get::<Result>().expect("endpoint result in the bag"));
let endpoint_params = props.get::<Params>().expect("endpoint params in the bag");
let endpoint = endpoint_result.as_ref().expect("endpoint resolved properly");
assert_eq!(
endpoint_params,
&Params::builder()
.bucket("bucket-name".to_string())
.built_in_with_default("some-default")
.bool_built_in_with_default(true)
.a_bool_param(false)
.a_string_param("hello".to_string())
.region("us-east-2".to_string())
.build().unwrap()
);
assert_eq!(endpoint.url(), "https://www.us-east-2.example.com");
}
""",
)
}
}
// the model has an intentionally failing test—ensure it fails
val failure = shouldThrow<CommandFailed> { "cargo test".runWithWarnings(testDir) }
failure.output shouldContain "endpoint::test::test_1"
failure.output shouldContain "https://failingtest.com"
"cargo clippy".runWithWarnings(testDir)
}
}