Skip to content

Commit 64e93b7

Browse files
author
luigi
committed
json unit test
1 parent 4eae182 commit 64e93b7

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/service/utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fun renderCastingPrimitiveFromShapeType(
3232
writer.write("$variable.let{ #T.fromIso8601(it) }", RuntimeTypes.Core.Instant)
3333
TimestampFormatTrait.Format.HTTP_DATE ->
3434
writer.write("$variable.let{ #T.fromRfc5322(it) }", RuntimeTypes.Core.Instant)
35-
else -> writer.write("$variable.let{ #T.fromIso8601(it) }", RuntimeTypes.Core.Instant)
35+
else -> writer.write("$variable.let{ #T.fromEpochSeconds(it) }", RuntimeTypes.Core.Instant)
3636
}
3737
else -> throw IllegalStateException(errorMessage ?: "Unable to render casting primitive for $type")
3838
}

tests/codegen/service-codegen-tests/src/test/kotlin/com/test/JsonServiceTest.kt

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ class JsonServiceTest {
143143
"correctToken",
144144
)
145145
assertIs<HttpResponse<String>>(response)
146-
println(response.body())
147146
assertEquals(201, response.statusCode(), "Expected 201")
148147
val body = json.decodeFromString(
149148
HttpStructurePayloadTestStructure.serializer(),
@@ -154,5 +153,63 @@ class JsonServiceTest {
154153
assertEquals(456.toFloat(), body.content3)
155154
}
156155

156+
@Test
157+
fun `checks timestamp`() {
158+
val json = Json { }
159+
160+
val requestJson = json.encodeToJsonElement(
161+
TimestampTestRequestResponse.serializer(),
162+
TimestampTestRequestResponse(
163+
1515531081.123,
164+
"1985-04-12T23:20:50.520Z",
165+
"Tue, 29 Apr 2014 18:30:38 GMT",
166+
1234567890.123,
167+
),
168+
)
169+
170+
val response = sendRequest(
171+
"$baseUrl/timestamp",
172+
"POST",
173+
requestJson,
174+
"application/json",
175+
"application/json",
176+
"correctToken",
177+
)
178+
assertIs<HttpResponse<String>>(response)
179+
assertEquals(201, response.statusCode(), "Expected 201")
180+
val body = json.decodeFromString(
181+
TimestampTestRequestResponse.serializer(),
182+
response.body(),
183+
)
184+
assertEquals(1515531081.123, body.default)
185+
assertEquals("1985-04-12T23:20:50.520Z", body.dateTime)
186+
assertEquals("Tue, 29 Apr 2014 18:30:38 GMT", body.httpDate)
187+
assertEquals(1234567890.123, body.epochSeconds)
188+
}
157189

190+
@Test
191+
fun `checks json name`() {
192+
val json = Json { }
193+
194+
val requestJson = json.encodeToJsonElement(
195+
JsonNameTestRequest.serializer(),
196+
JsonNameTestRequest("Hello Kotlin Team"),
197+
)
198+
199+
val response = sendRequest(
200+
"$baseUrl/json-name",
201+
"POST",
202+
requestJson,
203+
"application/json",
204+
"application/json",
205+
"correctToken",
206+
)
207+
assertIs<HttpResponse<String>>(response)
208+
assertEquals(201, response.statusCode(), "Expected 201")
209+
val body = json.decodeFromString(
210+
JsonNameTestResponse.serializer(),
211+
response.body(),
212+
)
213+
assertEquals("Hello Kotlin Team", body.responseName)
214+
}
158215
}

tests/codegen/service-codegen-tests/src/test/kotlin/com/test/ServiceDataClasses.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,17 @@ data class HttpStructurePayloadTestStructure(
6060
val content2: Int,
6161
val content3: Float,
6262
)
63+
64+
@Serializable
65+
data class TimestampTestRequestResponse(
66+
val default: Double,
67+
val dateTime: String,
68+
val httpDate: String,
69+
val epochSeconds: Double,
70+
)
71+
72+
@Serializable
73+
data class JsonNameTestRequest(val requestName: String)
74+
75+
@Serializable
76+
data class JsonNameTestResponse(val responseName: String)

0 commit comments

Comments
 (0)