Skip to content

Commit f203e8f

Browse files
[KTLN-828] Add samples (#1034)
* [KTLN-828] Add samples * [KTLN-828] Add samples * Update kotlin-libraries-data/src/test/kotlin/com/baeldung/retrofit/EnumSerializationUnitTest.kt Co-authored-by: Brandon Ward <[email protected]> --------- Co-authored-by: Brandon Ward <[email protected]>
1 parent 90d8330 commit f203e8f

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed

kotlin-libraries-data/pom.xml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@
1515
</parent>
1616

1717
<dependencies>
18+
<dependency>
19+
<groupId>com.squareup.retrofit2</groupId>
20+
<artifactId>retrofit</artifactId>
21+
<version>${retrofit.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.github.tomakehurst</groupId>
26+
<artifactId>wiremock-jre8</artifactId>
27+
<version>${wiremock-jre8.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.squareup.retrofit2</groupId>
32+
<artifactId>converter-gson</artifactId>
33+
<version>${retrofit.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.squareup.moshi</groupId>
38+
<artifactId>moshi</artifactId>
39+
<version>${moshi.version}</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.squareup.retrofit2</groupId>
44+
<artifactId>converter-moshi</artifactId>
45+
<version>${retrofit.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.squareup.retrofit2</groupId>
49+
<artifactId>converter-jackson</artifactId>
50+
<version>${retrofit.version}</version>
51+
</dependency>
1852
<dependency>
1953
<groupId>org.jetbrains.kotlinx</groupId>
2054
<artifactId>kotlinx-serialization-json-jvm</artifactId>
@@ -55,6 +89,12 @@
5589
<artifactId>jackson-module-kotlin</artifactId>
5690
<version>${jackson-kotlin.version}</version>
5791
</dependency>
92+
<dependency>
93+
<groupId>com.squareup.moshi</groupId>
94+
<artifactId>moshi-kotlin</artifactId>
95+
<version>${moshi.version}</version>
96+
<scope>test</scope>
97+
</dependency>
5898
</dependencies>
5999

60100
<build>
@@ -117,7 +157,10 @@
117157
<kotlinx-serialization-json-jvm.version>1.6.0</kotlinx-serialization-json-jvm.version>
118158
<kotlinx-datetime-jvm.version>0.4.0</kotlinx-datetime-jvm.version>
119159
<mapstruct.version>1.5.3.Final</mapstruct.version>
120-
<jackson-kotlin.version>2.9.8</jackson-kotlin.version>
160+
<jackson-kotlin.version>2.15.4</jackson-kotlin.version>
161+
<retrofit.version>2.9.0</retrofit.version>
162+
<wiremock-jre8.version>2.35.1</wiremock-jre8.version>
163+
<moshi.version>1.15.0</moshi.version>
121164
</properties>
122165

123166
</project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.retrofit
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
5+
import com.github.tomakehurst.wiremock.client.WireMock
6+
import com.github.tomakehurst.wiremock.junit.WireMockRule
7+
import com.google.gson.Gson
8+
import com.google.gson.GsonBuilder
9+
import com.squareup.moshi.Moshi
10+
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
11+
import kotlinx.coroutines.runBlocking
12+
import org.junit.Rule
13+
import org.junit.jupiter.api.AfterEach
14+
import org.junit.jupiter.api.Assertions.assertEquals
15+
import org.junit.jupiter.api.BeforeEach
16+
import org.junit.jupiter.api.Test
17+
import retrofit2.Retrofit
18+
import retrofit2.converter.gson.GsonConverterFactory
19+
import retrofit2.converter.jackson.JacksonConverterFactory
20+
import retrofit2.converter.moshi.MoshiConverterFactory
21+
import retrofit2.http.Body
22+
import retrofit2.http.GET
23+
import retrofit2.http.POST
24+
import retrofit2.http.Path
25+
26+
enum class OrderStatus { PENDING, COMPLETED, CANCELLED }
27+
28+
data class Order(val id: String, val status: OrderStatus)
29+
30+
interface OrderService {
31+
32+
@GET("orders/{id}")
33+
suspend fun getOrder(@Path("id") orderId: String): Order
34+
35+
@POST("orders")
36+
suspend fun createOrder(@Body order: Order): Order
37+
}
38+
39+
val gson: Gson = GsonBuilder().create()
40+
val gsonRetrofit: Retrofit = Retrofit.Builder().baseUrl("http://localhost:8080").addConverterFactory(
41+
GsonConverterFactory.create(gson)
42+
).build()
43+
44+
val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
45+
val moshiRetrofit: Retrofit = Retrofit.Builder().baseUrl("http://localhost:8080").addConverterFactory(
46+
MoshiConverterFactory.create(moshi)
47+
).build()
48+
49+
val objectMapper: ObjectMapper = jacksonObjectMapper()
50+
val jacksonRetrofit: Retrofit = Retrofit.Builder().baseUrl("http://localhost:8080").addConverterFactory(
51+
JacksonConverterFactory.create(objectMapper)
52+
).build()
53+
54+
class EnumSerializationUnitTest {
55+
@Rule
56+
@JvmField
57+
val wireMockRule = WireMockRule(8080)
58+
59+
@BeforeEach
60+
fun setup() {
61+
if(wireMockRule.isRunning) return
62+
wireMockRule.start()
63+
}
64+
65+
@AfterEach
66+
fun tearDown() {
67+
wireMockRule.stop()
68+
}
69+
70+
@Test
71+
fun `test Gson default serialization`() = runBlocking {
72+
val service = gsonRetrofit.create(OrderService::class.java)
73+
val order = Order("1", OrderStatus.PENDING)
74+
wireMockRule.stubFor(
75+
WireMock.post(WireMock.urlEqualTo("/orders"))
76+
.willReturn(WireMock.aResponse().withBody("""{"id":"1","status":"PENDING"}""").withStatus(200))
77+
)
78+
val response = service.createOrder(order)
79+
assertEquals(order, response)
80+
}
81+
82+
@Test
83+
fun `test Moshi default serialization`() = runBlocking {
84+
val service = moshiRetrofit.create(OrderService::class.java)
85+
val order = Order("1", OrderStatus.PENDING)
86+
wireMockRule.stubFor(
87+
WireMock.post(WireMock.urlEqualTo("/orders"))
88+
.willReturn(WireMock.aResponse().withBody("""{"id":"1","status":"PENDING"}""").withStatus(200))
89+
)
90+
val response = service.createOrder(order)
91+
assertEquals(order, response)
92+
}
93+
94+
@Test
95+
fun `test Jackson default serialization`() = runBlocking {
96+
val service = jacksonRetrofit.create(OrderService::class.java)
97+
val order = Order("1", OrderStatus.PENDING)
98+
wireMockRule.stubFor(
99+
WireMock.post(WireMock.urlEqualTo("/orders"))
100+
.willReturn(WireMock.aResponse().withBody("""{"id":"1","status":"PENDING"}""").withStatus(200))
101+
)
102+
val response = service.createOrder(order)
103+
assertEquals(order, response)
104+
}
105+
}

0 commit comments

Comments
 (0)