Skip to content

Commit

Permalink
Polish tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Apr 15, 2022
1 parent 9332a8f commit c4c4bb8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@ import io.github.nstdio.http.ext.GSON
import io.github.nstdio.http.ext.JACKSON
import io.github.nstdio.http.ext.jupiter.DisabledIfOnClasspath
import io.github.nstdio.http.ext.jupiter.EnabledIfOnClasspath
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.assertj.core.api.Assumptions.assumeThat
import io.kotest.assertions.throwables.shouldThrowExactly
import io.kotest.matchers.types.shouldBeTypeOf
import org.junit.jupiter.api.Assumptions.assumeTrue
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

internal class JsonMappingProviderSpiTest {
@Test
fun shouldGetDefaultProviderByName() {
assumeThat(ALL_JSON)
.anyMatch { Classpath.isPresent(it) }
assumeTrue { ALL_JSON.any { Classpath.isPresent(it) } }

//given
val providerName = CompositeJsonMappingProvider::class.java.name

//when + then
//when
val provider = JsonMappingProvider.provider(providerName)

//then
assertThat(provider)
.isExactlyInstanceOf(CompositeJsonMappingProvider::class.java)
provider.shouldBeTypeOf<CompositeJsonMappingProvider>()
}

@Nested
Expand All @@ -54,8 +52,8 @@ internal class JsonMappingProviderSpiTest {
val jsonMapping = provider.get()

//then
assertThat(provider).isExactlyInstanceOf(CompositeJsonMappingProvider::class.java)
assertThat(jsonMapping).isExactlyInstanceOf(GsonJsonMapping::class.java)
provider.shouldBeTypeOf<CompositeJsonMappingProvider>()
jsonMapping.shouldBeTypeOf<GsonJsonMapping>()
}
}

Expand All @@ -70,8 +68,8 @@ internal class JsonMappingProviderSpiTest {
val jsonMapping = provider.get()

//then
assertThat(provider).isExactlyInstanceOf(CompositeJsonMappingProvider::class.java)
assertThat(jsonMapping).isExactlyInstanceOf(JacksonJsonMapping::class.java)
provider.shouldBeTypeOf<CompositeJsonMappingProvider>()
jsonMapping.shouldBeTypeOf<JacksonJsonMapping>()
}
}

Expand All @@ -81,8 +79,7 @@ internal class JsonMappingProviderSpiTest {
@Test
fun shouldThrowWhenNothingIsPresent() {
//when + then
assertThatExceptionOfType(JsonMappingProviderNotFoundException::class.java)
.isThrownBy { JsonMappingProvider.provider() }
shouldThrowExactly<JsonMappingProviderNotFoundException> { JsonMappingProvider.provider() }
}

@Test
Expand All @@ -91,8 +88,7 @@ internal class JsonMappingProviderSpiTest {
val providerName = CompositeJsonMappingProvider::class.java.name

//when + then
assertThatExceptionOfType(JsonMappingProviderNotFoundException::class.java)
.isThrownBy { JsonMappingProvider.provider(providerName) }
shouldThrowExactly<JsonMappingProviderNotFoundException> { JsonMappingProvider.provider(providerName) }
}
}

Expand All @@ -106,8 +102,8 @@ internal class JsonMappingProviderSpiTest {
val jsonMapping = provider.get()

//then
assertThat(provider).isExactlyInstanceOf(CompositeJsonMappingProvider::class.java)
assertThat(jsonMapping).isExactlyInstanceOf(JacksonJsonMapping::class.java)
provider.shouldBeTypeOf<CompositeJsonMappingProvider>()
jsonMapping.shouldBeTypeOf<JacksonJsonMapping>()
}
}
}
76 changes: 55 additions & 21 deletions src/test/kotlin/io/github/nstdio/http/ext/BodyHandlersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
*/
package io.github.nstdio.http.ext

import org.assertj.core.api.Assertions
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.ok
import com.github.tomakehurst.wiremock.client.WireMock.stubFor
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import io.kotest.assertions.throwables.shouldThrowExactly
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.throwable.shouldHaveCause
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import java.io.UncheckedIOException
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.util.function.Supplier

internal class BodyHandlersTest {
@Nested
Expand All @@ -33,37 +39,65 @@ internal class BodyHandlersTest {
@Test
fun shouldProperlyReadJson() {
//given
val request = HttpRequest.newBuilder(URI.create("https://httpbin.org/get")).build()
val request = HttpRequest.newBuilder(URI.create("${wm.baseUrl()}/get")).build()

//when
val body1 = client.sendAsync(
request, BodyHandlers.ofJson(
Any::class.java
stubFor(
get("/get").willReturn(
ok().withBody(
"""
{
"args": {},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Host": "httpbin.org",
"Referer": "https://httpbin.org/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0",
"X-Amzn-Trace-Id": "Root=1-6259c64f-002692993e9394f43151cfd7"
},
"origin": "37.252.83.253",
"url": "https://httpbin.org/get"
}
""".trimIndent()
)
)
)
.thenApply { obj: HttpResponse<Supplier<Any>> -> obj.body() }
.thenApply { obj: Supplier<Any> -> obj.get() }
//when
val body1 = client.sendAsync(request, BodyHandlers.ofJson(Any::class.java))
.thenApply { it.body().get() }
.join()

//then
Assertions.assertThat(body1).isNotNull
body1.shouldNotBeNull()
}

@Test
fun shouldThrowUncheckedExceptionIfCannotRead() {
//given
val request = HttpRequest.newBuilder(URI.create("https://httpbin.org/html")).build()
val request = HttpRequest.newBuilder(URI.create("${wm.baseUrl()}/get")).build()

stubFor(get("/get").willReturn(ok().withBody("<html></html>")))

//when
Assertions.assertThatExceptionOfType(UncheckedIOException::class.java)
.isThrownBy {
client.send(
request, BodyHandlers.ofJson(
Any::class.java
)
).body().get()
}
.havingRootCause()
shouldThrowExactly<UncheckedIOException> {
client.send(request, BodyHandlers.ofJson(Any::class.java)).body().get()
}.shouldHaveCause()
}
}

companion object {
@RegisterExtension
@JvmStatic
var wm = WireMockExtension.newInstance()
.configureStaticDsl(true)
.failOnUnmatchedRequests(true)
.options(
WireMockConfiguration.wireMockConfig().dynamicPort()
)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package io.github.nstdio.http.ext

import org.assertj.core.api.Assertions
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.RepeatedTest
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import java.time.Duration
import java.time.Instant
import java.time.ZoneOffset
Expand All @@ -32,7 +34,7 @@ import java.util.stream.IntStream
import kotlin.streams.toList

internal class FixedRateTickClockTest {
@RepeatedTest(512)
@RepeatedTest(16)
fun shouldTickAtFixedRate() {
//given
val baseInstant = Instant.ofEpochSecond(0)
Expand All @@ -56,14 +58,14 @@ internal class FixedRateTickClockTest {
for (j in 0 until n) {
val expectedTick = tick.multipliedBy((j - i).toLong())
val actualDuration = Duration.between(actual[i], actual[j])
Assertions.assertThat(actualDuration).isEqualTo(expectedTick)
actualDuration shouldBe expectedTick
}
i++
}
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestInstance(PER_CLASS)
internal inner class ThreadSafetyTest {
private val baseInstant = Instant.ofEpochSecond(0)
private val tick = Duration.ofSeconds(1)
Expand All @@ -77,7 +79,7 @@ internal class FixedRateTickClockTest {
executor.shutdown()
}

@RepeatedTest(512)
@RepeatedTest(16)
fun shouldBeSafe() {
//when
val futures = IntStream.range(0, nTasks)
Expand All @@ -97,7 +99,7 @@ internal class FixedRateTickClockTest {
//then

// Implicitly checking clock to not return same instant more than once
Assertions.assertThat(actual).hasSize(nTasks)
actual shouldHaveSize nTasks
assertEvenlyDistributed(actual.toTypedArray(), tick)
}
}
Expand Down

0 comments on commit c4c4bb8

Please sign in to comment.