-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JUnitTests covering the Polymorphic support
- Loading branch information
1 parent
ed34c5b
commit eb252e7
Showing
1 changed file
with
167 additions
and
0 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
...unit-tests/src/test/java/com/yelp/codegen/generatecodesamples/PolymorphismEndpointTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package com.yelp.codegen.generatecodesamples | ||
|
||
import com.squareup.moshi.JsonDataException | ||
import com.yelp.codegen.generatecodesamples.apis.PolymorphismApi | ||
import com.yelp.codegen.generatecodesamples.models.Animal | ||
import com.yelp.codegen.generatecodesamples.models.Cat | ||
import com.yelp.codegen.generatecodesamples.models.Dog | ||
import com.yelp.codegen.generatecodesamples.tools.MockServerApiRule | ||
import okhttp3.mockwebserver.MockResponse | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class PolymorphismEndpointTest { | ||
|
||
@get:Rule | ||
val rule = MockServerApiRule() | ||
|
||
@Test | ||
fun checkPolymorphicReturnsBaseModel() { | ||
rule.server.enqueue( | ||
MockResponse().setBody( | ||
""" | ||
{ | ||
"name": "a name", | ||
"type": "animal" | ||
} | ||
""".trimIndent() | ||
) | ||
) | ||
|
||
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet() | ||
assertEquals( | ||
Animal( | ||
name = "a name", | ||
type = "animal", | ||
classification = null | ||
), | ||
returned | ||
) | ||
assertTrue(returned is Animal) | ||
assertTrue(returned !is Cat) | ||
assertTrue(returned !is Dog) | ||
} | ||
|
||
@Test | ||
fun checkPolymorphicReturnsBaseModelWhenUnknownDiscriminator() { | ||
rule.server.enqueue( | ||
MockResponse().setBody( | ||
""" | ||
{ | ||
"name": "a name", | ||
"type": "snake", | ||
"classification": "reptile" | ||
} | ||
""".trimIndent() | ||
) | ||
) | ||
|
||
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet() | ||
assertEquals( | ||
Animal( | ||
name = "a name", | ||
type = "snake", | ||
classification = Animal.ClassificationEnum.REPTILE | ||
), | ||
returned | ||
) | ||
assertTrue(returned is Animal) | ||
assertTrue(returned !is Cat) | ||
assertTrue(returned !is Dog) | ||
} | ||
|
||
@Test | ||
fun checkPolymorphicReturnsDiscriminatedModelWhenRecognized() { | ||
rule.server.enqueue( | ||
MockResponse().setBody( | ||
""" | ||
{ | ||
"name": "a name", | ||
"type": "Cat", | ||
"classification": "mammal", | ||
"age": 42 | ||
} | ||
""".trimIndent() | ||
) | ||
) | ||
|
||
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet() | ||
assertEquals( | ||
Animal( | ||
name = "a name", | ||
type = "Cat", | ||
classification = Animal.ClassificationEnum.MAMMAL | ||
), | ||
returned | ||
) | ||
assertEquals( | ||
Cat( | ||
name = "a name", | ||
type = "Cat", | ||
classification = Animal.ClassificationEnum.MAMMAL, | ||
age = 42.toBigDecimal() | ||
), | ||
returned | ||
) | ||
assertTrue(returned is Animal) | ||
assertTrue(returned is Cat) | ||
assertTrue(returned !is Dog) | ||
} | ||
|
||
@Test | ||
fun checkPolymorphicReturnsDiscriminatedModelWhenRecognizedAndInnerModelHasEnum() { | ||
rule.server.enqueue( | ||
MockResponse().setBody( | ||
""" | ||
{ | ||
"name": "a name", | ||
"type": "dog", | ||
"classification": "mammal", | ||
"size": "small" | ||
} | ||
""".trimIndent() | ||
) | ||
) | ||
|
||
val returned = rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet() | ||
assertEquals( | ||
Animal( | ||
name = "a name", | ||
type = "dog", | ||
classification = Animal.ClassificationEnum.MAMMAL | ||
), | ||
returned | ||
) | ||
assertEquals( | ||
Dog( | ||
name = "a name", | ||
type = "dog", | ||
classification = Animal.ClassificationEnum.MAMMAL, | ||
size = Dog.SizeEnum.SMALL | ||
), | ||
returned | ||
) | ||
assertTrue(returned is Animal) | ||
assertTrue(returned !is Cat) | ||
assertTrue(returned is Dog) | ||
} | ||
|
||
@Test(expected = JsonDataException::class) | ||
fun checkPolymorphicDoesNotReturnBaseModelIfDiscriminatedModelIsIdentifiedAnFailedToParse() { | ||
rule.server.enqueue( | ||
MockResponse().setBody( | ||
""" | ||
{ | ||
"name": "a name", | ||
"type": "dog", | ||
"classification": "mammal" | ||
} | ||
""".trimIndent() | ||
) | ||
) | ||
|
||
rule.getApi<PolymorphismApi>().getCheckPolymorphism().blockingGet() | ||
} | ||
} |