Skip to content

Commit

Permalink
apply ktlint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
iNoles committed Jun 29, 2024
1 parent 1502f66 commit 099f158
Show file tree
Hide file tree
Showing 33 changed files with 832 additions and 730 deletions.
28 changes: 15 additions & 13 deletions fuel-forge-jvm/src/test/kotlin/fuel/forge/FuelForgeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ class FuelForgeTest {

@OptIn(ExperimentalOkHttpApi::class)
@Test
fun testForgeResponse() = runBlocking {
val mockWebServer = MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\", \"status\": \"OK\"}"))
start()
}
fun testForgeResponse() =
runBlocking {
val mockWebServer =
MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\", \"status\": \"OK\"}"))
start()
}

val binUserAgentModel = HttpBinUserAgentModel("Fuel", "OK")
val response = Fuel.get(mockWebServer.url("user-agent").toString())
when (val forge = response.toForge(httpBinUserDeserializer)) {
is Result.Success -> assertEquals(binUserAgentModel, forge.value)
is Result.Failure -> fail(forge.error.localizedMessage)
}
val binUserAgentModel = HttpBinUserAgentModel("Fuel", "OK")
val response = Fuel.get(mockWebServer.url("user-agent").toString())
when (val forge = response.toForge(httpBinUserDeserializer)) {
is Result.Success -> assertEquals(binUserAgentModel, forge.value)
is Result.Failure -> fail(forge.error.localizedMessage)
}

mockWebServer.shutdown()
}
mockWebServer.shutdown()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import com.github.kittinunf.result.runCatching
import fuel.HttpResponse
import kotlinx.io.readString

public inline fun <reified T : Any> HttpResponse.toJackson(
mapper: ObjectMapper = jacksonObjectMapper()
): Result<T?, Throwable> =
public inline fun <reified T : Any> HttpResponse.toJackson(mapper: ObjectMapper = jacksonObjectMapper()): Result<T?, Throwable> =
runCatching {
mapper.readValue(source.readString())
}
74 changes: 39 additions & 35 deletions fuel-jackson-jvm/src/test/kotlin/fuel/jackson/FuelJacksonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,55 @@ import org.junit.Assert.fail
import org.junit.Test

class FuelJacksonTest {

private val createCustomMapper: ObjectMapper = ObjectMapper().registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).apply {
propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE
}
private val createCustomMapper: ObjectMapper =
ObjectMapper().registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).apply {
propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE
}

data class HttpBinUserAgentModel(
val userAgent: String = "",
val http_status: String = ""
val http_status: String = "",
)

@Test
fun jacksonTestResponseObject() = runBlocking {
val mockWebServer = MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\"}"))
start()
}
fun jacksonTestResponseObject() =
runBlocking {
val mockWebServer =
MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\"}"))
start()
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val jackson = response.toJackson<HttpBinUserAgentModel>()
jackson.fold({
assertEquals("Fuel", it?.userAgent)
}, {
fail(it.localizedMessage)
})
val response = Fuel.get(mockWebServer.url("user-agent").toString())
val jackson = response.toJackson<HttpBinUserAgentModel>()
jackson.fold({
assertEquals("Fuel", it?.userAgent)
}, {
fail(it.localizedMessage)
})

mockWebServer.shutdown()
}
mockWebServer.shutdown()
}

@Test
fun jacksonTestResponseObjectWithCustomMapper() = runBlocking {
val mockWebServer = MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\", \"http_status\": \"OK\"}"))
start()
}
fun jacksonTestResponseObjectWithCustomMapper() =
runBlocking {
val mockWebServer =
MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\", \"http_status\": \"OK\"}"))
start()
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val jackson = response.toJackson<HttpBinUserAgentModel>(createCustomMapper)
jackson.fold({
assertEquals("", it?.userAgent)
assertEquals("OK", it?.http_status)
}, {
fail(it.localizedMessage)
})
val response = Fuel.get(mockWebServer.url("user-agent").toString())
val jackson = response.toJackson<HttpBinUserAgentModel>(createCustomMapper)
jackson.fold({
assertEquals("", it?.userAgent)
assertEquals("OK", it?.http_status)
}, {
fail(it.localizedMessage)
})

mockWebServer.shutdown()
}
mockWebServer.shutdown()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fuel.serialization

import fuel.HttpResponse
import kotlinx.io.*
import kotlinx.io.bytestring.ByteString
import kotlinx.io.Buffer
import kotlinx.io.writeString
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.Test
Expand All @@ -19,10 +19,11 @@ class FuelKotlinxSerializationTest {
@Test
fun testSerializableResponse() {
val jsonBuffer = Buffer().also { it.writeString("{\"userAgent\": \"Fuel\"}") }
val httpResponse = HttpResponse().apply {
statusCode = 200
source = jsonBuffer
}
val httpResponse =
HttpResponse().apply {
statusCode = 200
source = jsonBuffer
}
val json = httpResponse.toJson(Json.Default, HttpBinUserAgentModel.serializer())
json.fold({
assertEquals("Fuel", it?.userAgent)
Expand All @@ -33,14 +34,23 @@ class FuelKotlinxSerializationTest {

@Test
fun testSpaceXDetail() {
val jsonBuffer = Buffer().also {it.writeString("{\"rocket\":\"5e9d0d95eda69973a809d1ec\", \"success\":true,\"details\":\"Second GTO launch for Falcon 9. The USAF evaluated launch data from this flight as part of a separate certification program for SpaceX to qualify to fly U.S. military payloads and found that the Thaicom 6 launch had \"unacceptable fuel reserves at engine cutoff of the stage 2 second burnoff\"}") }
val httpResponse = HttpResponse().apply {
statusCode = 200
source = jsonBuffer
}
val jsonBuffer =
Buffer().also {
it.writeString(
"{\"rocket\":\"5e9d0d95eda69973a809d1ec\", \"success\":true,\"details\":\"Second GTO launch for Falcon 9. The USAF evaluated launch data from this flight as part of a separate certification program for SpaceX to qualify to fly U.S. military payloads and found that the Thaicom 6 launch had \"unacceptable fuel reserves at engine cutoff of the stage 2 second burnoff\"}",
)
}
val httpResponse =
HttpResponse().apply {
statusCode = 200
source = jsonBuffer
}
val json = httpResponse.toJson(Json.Default, RocketLaunch.serializer())
json.fold({
assertEquals("Second GTO launch for Falcon 9. The USAF evaluated launch data from this flight as part of a separate certification program for SpaceX to qualify to fly U.S. military payloads and found that the Thaicom 6 launch had \"unacceptable fuel reserves at engine cutoff of the stage 2 second burnoff\"", it?.details)
assertEquals(
"Second GTO launch for Falcon 9. The USAF evaluated launch data from this flight as part of a separate certification program for SpaceX to qualify to fly U.S. military payloads and found that the Thaicom 6 launch had \"unacceptable fuel reserves at engine cutoff of the stage 2 second burnoff\"",
it?.details,
)
}, {
fail(it.message)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import kotlinx.serialization.json.io.decodeFromSource
@OptIn(ExperimentalSerializationApi::class)
public fun <T : Any> HttpResponse.toJson(
json: Json = Json,
deserializationStrategy: DeserializationStrategy<T>
): Result<T?, Throwable> = runCatching {
json.decodeFromSource(source = source, deserializer = deserializationStrategy)
}
deserializationStrategy: DeserializationStrategy<T>,
): Result<T?, Throwable> =
runCatching {
json.decodeFromSource(source = source, deserializer = deserializationStrategy)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,42 @@ class FuelKotlinxSerializationTest {
data class HttpBinUserAgentModel(var userAgent: String = "")

@Test
fun testSerializableResponse() = runBlocking {
val mockWebServer = MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\"}"))
start()
fun testSerializableResponse() =
runBlocking {
val mockWebServer =
MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel\"}"))
start()
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val json = response.toJson(Json.Default, HttpBinUserAgentModel.serializer())
json.fold({
assertEquals("Fuel", it?.userAgent)
}, {
fail(it.message)
})

mockWebServer.shutdown()
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val json = response.toJson(Json.Default, HttpBinUserAgentModel.serializer())
json.fold({
assertEquals("Fuel", it?.userAgent)
}, {
fail(it.message)
})

mockWebServer.shutdown()
}

@Test
fun testSerializableResponseWithDefaultJson() = runBlocking {
val mockWebServer = MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel2\"}"))
start()
fun testSerializableResponseWithDefaultJson() =
runBlocking {
val mockWebServer =
MockWebServer().apply {
enqueue(MockResponse(body = "{\"userAgent\": \"Fuel2\"}"))
start()
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val json = response.toJson(deserializationStrategy = HttpBinUserAgentModel.serializer())
json.fold({
assertEquals("Fuel2", it?.userAgent)
}, {
fail(it.message)
})

mockWebServer.shutdown()
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val json = response.toJson(deserializationStrategy = HttpBinUserAgentModel.serializer())
json.fold({
assertEquals("Fuel2", it?.userAgent)
}, {
fail(it.message)
})

mockWebServer.shutdown()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ public val defaultMoshi: Moshi.Builder = Moshi.Builder()

public inline fun <reified T : Any> HttpResponse.toMoshi(): Result<T?, Throwable> = toMoshi(T::class.java)

public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, Throwable> =
toMoshi(defaultMoshi.build().adapter(clazz))
public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, Throwable> = toMoshi(defaultMoshi.build().adapter(clazz))

public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, Throwable> =
toMoshi(defaultMoshi.build().adapter(type))
public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, Throwable> = toMoshi(defaultMoshi.build().adapter(type))

public fun <T : Any> HttpResponse.toMoshi(jsonAdapter: JsonAdapter<T>): Result<T?, Throwable> {
val buffer = Buffer().apply { write(source.readByteArray()) }
Expand Down
Loading

0 comments on commit 099f158

Please sign in to comment.