Skip to content

Commit

Permalink
Detekt/checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
PattaFeuFeu committed Oct 12, 2023
1 parent 42ae9f3 commit 2fe0cdd
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import social.bigbone.MastodonClient

object MockClient {

val json: Json = Json {
val JSON: Json = Json {
encodeDefaults = true
ignoreUnknownKeys = true
coerceInputValues = true
Expand Down Expand Up @@ -46,7 +46,7 @@ object MockClient {
every { client.get(ofType<String>(), any()) } returns response

// mocking function that is internal in MastodonClient
every { client["getSerializer"]() } returns json
every { client["getSerializer"]() } returns JSON

return client
}
Expand Down
2 changes: 1 addition & 1 deletion bigbone/src/main/kotlin/social/bigbone/JsonSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package social.bigbone

import kotlinx.serialization.json.Json

internal val JsonSerializer: Json = Json {
internal val JSON_SERIALIZER: Json = Json {
encodeDefaults = true
ignoreUnknownKeys = true
coerceInputValues = true
Expand Down
35 changes: 31 additions & 4 deletions bigbone/src/main/kotlin/social/bigbone/MastodonClient.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
package social.bigbone

import kotlinx.serialization.json.Json
import okhttp3.*
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import social.bigbone.api.Pageable
import social.bigbone.api.entity.data.InstanceVersion
import social.bigbone.api.exception.BigBoneRequestException
import social.bigbone.api.method.*
import social.bigbone.api.method.AccountMethods
import social.bigbone.api.method.AppMethods
import social.bigbone.api.method.BlockMethods
import social.bigbone.api.method.BookmarkMethods
import social.bigbone.api.method.ConversationMethods
import social.bigbone.api.method.DirectoryMethods
import social.bigbone.api.method.FavouriteMethods
import social.bigbone.api.method.FilterMethods
import social.bigbone.api.method.FollowRequestMethods
import social.bigbone.api.method.InstanceMethods
import social.bigbone.api.method.ListMethods
import social.bigbone.api.method.MarkerMethods
import social.bigbone.api.method.MediaMethods
import social.bigbone.api.method.MuteMethods
import social.bigbone.api.method.NotificationMethods
import social.bigbone.api.method.OAuthMethods
import social.bigbone.api.method.PollMethods
import social.bigbone.api.method.ReportMethods
import social.bigbone.api.method.SearchMethods
import social.bigbone.api.method.StatusMethods
import social.bigbone.api.method.StreamingMethods
import social.bigbone.api.method.TagMethods
import social.bigbone.api.method.TimelineMethods
import social.bigbone.extension.emptyRequestBody
import social.bigbone.nodeinfo.NodeInfoClient
import java.io.IOException
Expand Down Expand Up @@ -206,7 +233,7 @@ private constructor(
}

@PublishedApi
internal fun getSerializer(): Json = JsonSerializer
internal fun getSerializer(): Json = JSON_SERIALIZER

fun getInstanceName() = instanceName

Expand Down Expand Up @@ -615,7 +642,7 @@ private constructor(
val response = versionedInstanceRequest(apiVersion)
if (response.isSuccessful) {
val instanceVersion: InstanceVersion? = response.body?.string()?.let { responseBody: String ->
JsonSerializer.decodeFromString(responseBody)
JSON_SERIALIZER.decodeFromString(responseBody)
}
instanceVersion?.version
} else {
Expand Down
2 changes: 1 addition & 1 deletion bigbone/src/main/kotlin/social/bigbone/MastodonRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MastodonRequest<T>(
val body: String? = response.body?.string()
requireNotNull(body)

val element: JsonElement = JsonSerializer.parseToJsonElement(body)
val element: JsonElement = JSON_SERIALIZER.parseToJsonElement(body)
if (element is JsonObject) {
action(body)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package social.bigbone.nodeinfo

import okhttp3.OkHttpClient
import okhttp3.Request
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.api.exception.BigBoneRequestException
import social.bigbone.nodeinfo.entity.NodeInfo
import social.bigbone.nodeinfo.entity.Server
Expand Down Expand Up @@ -38,7 +38,7 @@ object NodeInfoClient {
throw BigBoneRequestException("request for NodeInfo URL unsuccessful")
}

return response.body?.string()?.let { JsonSerializer.decodeFromString(it) }
return response.body?.string()?.let { JSON_SERIALIZER.decodeFromString(it) }
} catch (e: Exception) {
throw BigBoneRequestException("invalid NodeInfo response")
}
Expand All @@ -62,7 +62,7 @@ object NodeInfoClient {
throw BigBoneRequestException("request for well-known NodeInfo URL unsuccessful")
}

val nodeInfo: NodeInfo? = response.body?.string()?.let { JsonSerializer.decodeFromString(it) }
val nodeInfo: NodeInfo? = response.body?.string()?.let { JSON_SERIALIZER.decodeFromString(it) }
if (nodeInfo == null || nodeInfo.links.isEmpty()) {
throw BigBoneRequestException("empty link list in well-known NodeInfo location")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package social.bigbone.api.entity
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldBeNull
import org.junit.jupiter.api.Test
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.testtool.AssetsUtil

class ErrorTest {

@Test
fun deserialize() {
val json = AssetsUtil.readFromAssets("error.json")
val error: Error = JsonSerializer.decodeFromString(json)
val error: Error = JSON_SERIALIZER.decodeFromString(json)
error.error shouldBeEqualTo "invalid_grant"
error.errorDescription shouldBeEqualTo "Here comes the more detailed error description."
}
Expand All @@ -26,7 +26,7 @@ class ErrorTest {
@Test
fun deserialize_no_description() {
val json = AssetsUtil.readFromAssets("error_no_description.json")
val error: Error = JsonSerializer.decodeFromString(json)
val error: Error = JSON_SERIALIZER.decodeFromString(json)
error.error shouldBeEqualTo "invalid_grant"
error.errorDescription.shouldBeNull()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package social.bigbone.api.entity
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldNotBe
import org.junit.jupiter.api.Test
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.testtool.AssetsUtil

class MediaAttachmentTest {
@Test
fun deserialize() {
val json = AssetsUtil.readFromAssets("attachment.json")
val status: MediaAttachment = JsonSerializer.decodeFromString(json)
val status: MediaAttachment = JSON_SERIALIZER.decodeFromString(json)
status.id shouldBeEqualTo "10"
status.url shouldBeEqualTo "youtube"
status.remoteUrl shouldNotBe null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package social.bigbone.api.entity

import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.testtool.AssetsUtil

class StatusTest {

@Test
fun deserialize() {
val json = AssetsUtil.readFromAssets("status.json")
val status: Status = JsonSerializer.decodeFromString(json)
val status: Status = JSON_SERIALIZER.decodeFromString(json)
status.id shouldBeEqualTo "11111"
status.visibility shouldBeEqualTo Status.Visibility.Public.value
status.content shouldBeEqualTo "Test Status"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package social.bigbone.api.entity

import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.testtool.AssetsUtil

class TokenTest {

@Test
fun deserialize() {
val json = AssetsUtil.readFromAssets("access_token.json")
val accessToken: Token = JsonSerializer.decodeFromString(json)
val accessToken: Token = JSON_SERIALIZER.decodeFromString(json)
accessToken.accessToken shouldBeEqualTo "test"
accessToken.tokenType shouldBeEqualTo "bearer"
accessToken.scope shouldBeEqualTo "read write follow"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package social.bigbone.nodeinfo.entity

import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.testtool.AssetsUtil

class NodeInfoTest {
Expand All @@ -13,7 +13,7 @@ class NodeInfoTest {
val json = AssetsUtil.readFromAssets("nodeinfo.json")

// when
val nodeInfo: NodeInfo = JsonSerializer.decodeFromString(json)
val nodeInfo: NodeInfo = JSON_SERIALIZER.decodeFromString(json)

// when/then
nodeInfo.links.size shouldBeEqualTo 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package social.bigbone.nodeinfo.entity

import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.testtool.AssetsUtil

class ServerTest {
Expand All @@ -13,7 +13,7 @@ class ServerTest {
val json = AssetsUtil.readFromAssets("nodeinfo_server_info.json")

// when
val server: Server = JsonSerializer.decodeFromString(json)
val server: Server = JSON_SERIALIZER.decodeFromString(json)

// then
server.schemaVersion shouldBeEqualTo "2.0"
Expand Down
6 changes: 3 additions & 3 deletions bigbone/src/test/kotlin/social/bigbone/testtool/MockClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.ResponseBody.Companion.toResponseBody
import social.bigbone.JsonSerializer
import social.bigbone.JSON_SERIALIZER
import social.bigbone.MastodonClient
import social.bigbone.api.exception.BigBoneRequestException
import java.net.SocketTimeoutException
Expand Down Expand Up @@ -49,7 +49,7 @@ object MockClient {
every { clientMock.post(ofType<String>(), any(), any()) } returns response
every { clientMock.postRequestBody(ofType<String>(), any()) } returns response
every { clientMock.put(ofType<String>(), any()) } returns response
every { clientMock.getSerializer() } returns JsonSerializer
every { clientMock.getSerializer() } returns JSON_SERIALIZER
return clientMock
}

Expand All @@ -74,7 +74,7 @@ object MockClient {
every { clientMock.postRequestBody(ofType<String>(), any()) } returns response
every { clientMock.put(ofType<String>(), any()) } returns response
every { clientMock.performAction(ofType<String>(), any()) } throws BigBoneRequestException("mock")
every { clientMock.getSerializer() } returns JsonSerializer
every { clientMock.getSerializer() } returns JSON_SERIALIZER
return clientMock
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@SuppressWarnings("PMD.SystemPrintln")
public class GetInstanceInfo {

private static final Json jsonSerializer = JsonKt.Json(Json.Default, jsonBuilder -> {
private static final Json JSON_SERIALIZER = JsonKt.Json(Json.Default, jsonBuilder -> {
jsonBuilder.setEncodeDefaults(true);
jsonBuilder.setIgnoreUnknownKeys(true);
jsonBuilder.setCoerceInputValues(true);
Expand All @@ -28,6 +28,6 @@ public static void main(final String[] args) throws BigBoneRequestException {
// Get instance info and dump it to the console as JSON
final Instance instanceInfo = client.instances().getInstance().execute();

System.out.println(jsonSerializer.encodeToString(serializer(Instance.class), instanceInfo));
System.out.println(JSON_SERIALIZER.encodeToString(serializer(Instance.class), instanceInfo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import social.bigbone.api.entity.Instance

object GetInstanceInfo {

private val json: Json = Json {
private val JSON_SERIALIZER: Json = Json {
encodeDefaults = true
ignoreUnknownKeys = true
coerceInputValues = true
Expand All @@ -22,6 +22,6 @@ object GetInstanceInfo {

// Get instance info and dump it to the console as JSON
val instanceInfo = client.instances.getInstance().execute()
println(json.encodeToString(Instance.serializer(), instanceInfo))
println(JSON_SERIALIZER.encodeToString(Instance.serializer(), instanceInfo))
}
}

0 comments on commit 2fe0cdd

Please sign in to comment.