Skip to content

Commit b12f0b4

Browse files
committed
Add JUnitTests covering the Polymorphic support
1 parent 7c2ca28 commit b12f0b4

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package com.yelp.codegen.generatecodesamples
2+
3+
import com.squareup.moshi.JsonDataException
4+
import com.yelp.codegen.generatecodesamples.apis.PolymorphismApi
5+
import com.yelp.codegen.generatecodesamples.models.Animal
6+
import com.yelp.codegen.generatecodesamples.models.Cat
7+
import com.yelp.codegen.generatecodesamples.models.Dog
8+
import com.yelp.codegen.generatecodesamples.tools.MockServerApiRule
9+
import okhttp3.mockwebserver.MockResponse
10+
import org.junit.Assert.assertEquals
11+
import org.junit.Assert.assertTrue
12+
import org.junit.Rule
13+
import org.junit.Test
14+
15+
class PolymorphismEndpointTest {
16+
17+
@get:Rule
18+
val rule = MockServerApiRule()
19+
20+
@Test
21+
fun checkPolymorphicReturnsBaseModel() {
22+
rule.server.enqueue(
23+
MockResponse().setBody(
24+
"""
25+
{
26+
"name": "a name",
27+
"type": "animal"
28+
}
29+
""".trimIndent()
30+
)
31+
)
32+
33+
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet()
34+
assertEquals(
35+
Animal(
36+
name = "a name",
37+
type = "animal",
38+
classification = null
39+
),
40+
returned
41+
)
42+
assertTrue(returned is Animal)
43+
assertTrue(returned !is Cat)
44+
assertTrue(returned !is Dog)
45+
}
46+
47+
@Test
48+
fun checkPolymorphicReturnsBaseModelWhenUnknownDiscriminator() {
49+
rule.server.enqueue(
50+
MockResponse().setBody(
51+
"""
52+
{
53+
"name": "a name",
54+
"type": "snake",
55+
"classification": "reptile"
56+
}
57+
""".trimIndent()
58+
)
59+
)
60+
61+
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet()
62+
assertEquals(
63+
Animal(
64+
name = "a name",
65+
type = "snake",
66+
classification = Animal.ClassificationEnum.REPTILE
67+
),
68+
returned
69+
)
70+
assertTrue(returned is Animal)
71+
assertTrue(returned !is Cat)
72+
assertTrue(returned !is Dog)
73+
}
74+
75+
@Test
76+
fun checkPolymorphicReturnsDiscriminatedModelWhenRecognized() {
77+
rule.server.enqueue(
78+
MockResponse().setBody(
79+
"""
80+
{
81+
"name": "a name",
82+
"type": "Cat",
83+
"classification": "mammal",
84+
"age": 42
85+
}
86+
""".trimIndent()
87+
)
88+
)
89+
90+
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet()
91+
assertEquals(
92+
Animal(
93+
name = "a name",
94+
type = "Cat",
95+
classification = Animal.ClassificationEnum.MAMMAL
96+
),
97+
returned
98+
)
99+
assertEquals(
100+
Cat(
101+
name = "a name",
102+
type = "Cat",
103+
classification = Animal.ClassificationEnum.MAMMAL,
104+
age = 42.toBigDecimal()
105+
),
106+
returned
107+
)
108+
assertTrue(returned is Animal)
109+
assertTrue(returned is Cat)
110+
assertTrue(returned !is Dog)
111+
}
112+
113+
@Test
114+
fun checkPolymorphicReturnsDiscriminatedModelWhenRecognizedAndInnerModelHasEnum() {
115+
rule.server.enqueue(
116+
MockResponse().setBody(
117+
"""
118+
{
119+
"name": "a name",
120+
"type": "dog",
121+
"classification": "mammal",
122+
"size": "small"
123+
}
124+
""".trimIndent()
125+
)
126+
)
127+
128+
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet()
129+
assertEquals(
130+
Animal(
131+
name = "a name",
132+
type = "dog",
133+
classification = Animal.ClassificationEnum.MAMMAL
134+
),
135+
returned
136+
)
137+
assertEquals(
138+
Dog(
139+
name = "a name",
140+
type = "dog",
141+
classification = Animal.ClassificationEnum.MAMMAL,
142+
size = Dog.SizeEnum.SMALL
143+
),
144+
returned
145+
)
146+
assertTrue(returned is Animal)
147+
assertTrue(returned !is Cat)
148+
assertTrue(returned is Dog)
149+
}
150+
151+
@Test(expected = JsonDataException::class)
152+
fun checkPolymorphicDoesNotReturnBaseModelIfDiscriminatedModelIsIdentifiedAnFailedToParse() {
153+
rule.server.enqueue(
154+
MockResponse().setBody(
155+
"""
156+
{
157+
"name": "a name",
158+
"type": "dog",
159+
"classification": "mammal"
160+
}
161+
""".trimIndent()
162+
)
163+
)
164+
165+
rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet()
166+
}
167+
}

0 commit comments

Comments
 (0)