Skip to content

Commit ecab1a0

Browse files
authored
misc: add rules engine codegen tests (#1459)
1 parent eb24838 commit ecab1a0

File tree

4 files changed

+219
-2
lines changed

4 files changed

+219
-2
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ coroutines-version = "1.9.0"
1111
atomicfu-version = "0.25.0"
1212

1313
# smithy-kotlin codegen and runtime are versioned separately
14-
smithy-kotlin-runtime-version = "1.3.29"
15-
smithy-kotlin-codegen-version = "0.33.29"
14+
smithy-kotlin-runtime-version = "1.3.30"
15+
smithy-kotlin-codegen-version = "0.33.30"
1616

1717
# codegen
1818
smithy-version = "1.51.0"

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ include(":hll:hll-mapping-core")
5353
include(":services")
5454
include(":tests")
5555
include(":tests:codegen:event-stream")
56+
include(":tests:codegen:rules-engine")
5657
include(":tests:e2e-test-util")
5758
include(":tests:codegen:smoke-tests")
5859
include(":tests:codegen:smoke-tests:services")
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import aws.sdk.kotlin.gradle.codegen.dsl.generateSmithyProjections
7+
import aws.sdk.kotlin.gradle.codegen.dsl.smithyKotlinPlugin
8+
import aws.sdk.kotlin.gradle.codegen.smithyKotlinProjectionSrcDir
9+
10+
plugins {
11+
alias(libs.plugins.kotlin.jvm)
12+
alias(libs.plugins.aws.kotlin.repo.tools.smithybuild)
13+
}
14+
15+
description = "Smithy rules engine codegen integration test suite"
16+
17+
data class Test(
18+
val projectionName: String,
19+
val protocolName: String,
20+
val modelTemplate: File,
21+
) {
22+
val model: File
23+
get() = layout.buildDirectory.file("$projectionName/model.smithy").get().asFile
24+
}
25+
26+
val tests = listOf(
27+
Test("operationContextParams", "operationContextParams", file("operation-context-params.smithy")),
28+
)
29+
30+
fun fillInModel(output: File, protocolName: String, template: File) {
31+
val input = template.readText()
32+
val opTraits = when (protocolName) {
33+
"restJson1", "restXml" -> """@http(method: "POST", uri: "/test-eventstream", code: 200)"""
34+
else -> ""
35+
}
36+
val replaced = input
37+
.replace("{protocol-name}", protocolName)
38+
.replace("{op-traits}", opTraits)
39+
40+
output.parentFile.mkdirs()
41+
output.writeText(replaced)
42+
}
43+
44+
val testServiceShapeId = "aws.sdk.kotlin.test#TestService"
45+
smithyBuild {
46+
tests.forEach { test ->
47+
48+
projections.register(test.projectionName) {
49+
imports = listOf(test.model.absolutePath)
50+
transforms = listOf(
51+
"""
52+
{
53+
"name": "includeServices",
54+
"args": {
55+
"services": ["$testServiceShapeId"]
56+
}
57+
}
58+
""",
59+
)
60+
61+
smithyKotlinPlugin {
62+
serviceShapeId = testServiceShapeId
63+
packageName = "aws.sdk.kotlin.test.${test.projectionName.lowercase()}"
64+
packageVersion = "1.0"
65+
buildSettings {
66+
generateFullProject = false
67+
generateDefaultBuildFiles = false
68+
optInAnnotations = listOf(
69+
"aws.smithy.kotlin.runtime.InternalApi",
70+
"aws.sdk.kotlin.runtime.InternalSdkApi",
71+
)
72+
}
73+
}
74+
}
75+
}
76+
}
77+
78+
val codegen by configurations.getting
79+
dependencies {
80+
codegen(project(":codegen:aws-sdk-codegen"))
81+
codegen(libs.smithy.cli)
82+
codegen(libs.smithy.model)
83+
}
84+
85+
tasks.generateSmithyBuild {
86+
doFirst {
87+
tests.forEach { test -> fillInModel(test.model, test.protocolName, test.modelTemplate) }
88+
}
89+
}
90+
91+
tasks.generateSmithyProjections {
92+
doFirst {
93+
// ensure the generated tests use the same version of the runtime as the aws aws-runtime
94+
val smithyKotlinRuntimeVersion = libs.versions.smithy.kotlin.runtime.version.get()
95+
System.setProperty("smithy.kotlin.codegen.clientRuntimeVersion", smithyKotlinRuntimeVersion)
96+
}
97+
}
98+
99+
val optinAnnotations = listOf(
100+
"kotlin.RequiresOptIn",
101+
"aws.smithy.kotlin.runtime.InternalApi",
102+
"aws.sdk.kotlin.runtime.InternalSdkApi",
103+
)
104+
105+
kotlin.sourceSets.all {
106+
optinAnnotations.forEach { languageSettings.optIn(it) }
107+
}
108+
109+
kotlin.sourceSets.getByName("test") {
110+
smithyBuild.projections.forEach {
111+
kotlin.srcDir(smithyBuild.smithyKotlinProjectionSrcDir(it.name))
112+
}
113+
}
114+
115+
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
116+
dependsOn(tasks.generateSmithyProjections)
117+
// generated clients have quite a few warnings
118+
kotlinOptions.allWarningsAsErrors = false
119+
}
120+
121+
tasks.test {
122+
useJUnitPlatform()
123+
testLogging {
124+
events("passed", "skipped", "failed")
125+
showStandardStreams = true
126+
showStackTraces = true
127+
showExceptions = true
128+
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
129+
}
130+
}
131+
132+
dependencies {
133+
134+
implementation(libs.kotlinx.coroutines.core)
135+
136+
testImplementation(libs.kotlin.test)
137+
testImplementation(libs.kotlin.test.junit5)
138+
testImplementation(libs.kotlinx.coroutines.test)
139+
140+
testImplementation(libs.smithy.kotlin.smithy.test)
141+
testImplementation(libs.smithy.kotlin.aws.signing.default)
142+
testImplementation(libs.smithy.kotlin.telemetry.api)
143+
144+
// have to manually add all the dependencies of the generated client(s)
145+
// doing it this way (as opposed to doing what we do for protocol-tests) allows
146+
// the tests to work without a publish to maven-local step at the cost of maintaining
147+
// this set of dependencies manually
148+
// <-- BEGIN GENERATED DEPENDENCY LIST -->
149+
implementation(libs.bundles.smithy.kotlin.service.client)
150+
implementation(libs.smithy.kotlin.aws.event.stream)
151+
implementation(project(":aws-runtime:aws-http"))
152+
implementation(libs.smithy.kotlin.aws.json.protocols)
153+
implementation(libs.smithy.kotlin.serde.json)
154+
api(project(":aws-runtime:aws-config"))
155+
api(project(":aws-runtime:aws-core"))
156+
api(project(":aws-runtime:aws-endpoint"))
157+
// <-- END GENERATED DEPENDENCY LIST -->
158+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
$version: "2.0"
2+
namespace aws.sdk.kotlin.test
3+
4+
use aws.protocols#awsJson1_0
5+
use smithy.rules#operationContextParams
6+
use smithy.rules#endpointRuleSet
7+
use aws.api#service
8+
9+
@awsJson1_0
10+
@service(sdkId: "OperationContextParamsTest")
11+
@endpointRuleSet(
12+
version: "1.0",
13+
parameters: {
14+
"ObjectKeys": {
15+
"type": "stringArray",
16+
"documentation": "A string array.",
17+
"required": true
18+
}
19+
},
20+
rules: [
21+
{
22+
"type": "endpoint",
23+
"conditions": [],
24+
"endpoint": {
25+
"url": "https://static.endpoint"
26+
}
27+
}
28+
]
29+
)
30+
service TestService {
31+
operations: [DeleteObjects],
32+
version: "1"
33+
}
34+
35+
@operationContextParams(
36+
ObjectKeys: {
37+
path: "Delete.Objects[*].[Key][]"
38+
}
39+
)
40+
operation DeleteObjects {
41+
input: DeleteObjectsRequest
42+
}
43+
44+
structure DeleteObjectsRequest {
45+
Delete: Delete
46+
}
47+
48+
structure Delete {
49+
Objects: ObjectIdentifierList
50+
}
51+
52+
list ObjectIdentifierList {
53+
member: ObjectIdentifier
54+
}
55+
56+
structure ObjectIdentifier {
57+
Key: String
58+
}

0 commit comments

Comments
 (0)