From a60a843edf507b06f5dfa865ea4558332ab7113d Mon Sep 17 00:00:00 2001 From: tzugen Date: Wed, 9 Jun 2021 17:00:25 +0200 Subject: [PATCH] Fix tests --- .../moire/ultrasonic/api/subsonic/SubsonicAPIClient.kt | 10 ++++++++++ .../ultrasonic/imageloader/AvatarRequestHandler.kt | 7 ++++--- .../ultrasonic/imageloader/CoverArtRequestHandler.kt | 3 ++- .../ultrasonic/imageloader/AvatarRequestHandlerTest.kt | 9 ++++++--- .../imageloader/CoverArtRequestHandlerTest.kt | 9 ++++----- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/core/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIClient.kt b/core/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIClient.kt index c111716b..e51085a9 100644 --- a/core/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIClient.kt +++ b/core/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIClient.kt @@ -9,12 +9,15 @@ import java.util.concurrent.TimeUnit.MILLISECONDS import javax.net.ssl.SSLContext import javax.net.ssl.X509TrustManager import okhttp3.OkHttpClient +import okhttp3.ResponseBody import okhttp3.logging.HttpLoggingInterceptor import org.moire.ultrasonic.api.subsonic.interceptors.PasswordHexInterceptor import org.moire.ultrasonic.api.subsonic.interceptors.PasswordMD5Interceptor import org.moire.ultrasonic.api.subsonic.interceptors.ProxyPasswordInterceptor import org.moire.ultrasonic.api.subsonic.interceptors.RangeHeaderInterceptor import org.moire.ultrasonic.api.subsonic.interceptors.VersionInterceptor +import org.moire.ultrasonic.api.subsonic.response.StreamResponse +import retrofit2.Response import retrofit2.Retrofit private const val READ_TIMEOUT = 60_000L @@ -124,6 +127,13 @@ class SubsonicAPIClient( hostnameVerifier { _, _ -> true } } + /** + * This function is necessary because Mockito has problems with stubbing chained calls + */ + fun toStreamResponse(call: Response): StreamResponse { + return call.toStreamResponse() + } + companion object { val jacksonMapper: ObjectMapper = ObjectMapper() .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandler.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandler.kt index 0c32e2d3..b63549fb 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandler.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandler.kt @@ -6,13 +6,12 @@ import com.squareup.picasso.RequestHandler import java.io.IOException import okio.Okio import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient -import org.moire.ultrasonic.api.subsonic.toStreamResponse /** * Loads avatars from subsonic api. */ class AvatarRequestHandler( - private val apiClient: SubsonicAPIClient + private val client: SubsonicAPIClient ) : RequestHandler() { override fun canHandleRequest(data: Request): Boolean { return with(data.uri) { @@ -24,7 +23,9 @@ class AvatarRequestHandler( val username = request.uri.getQueryParameter(QUERY_USERNAME) ?: throw IllegalArgumentException("Nullable username") - val response = apiClient.api.getAvatar(username).execute().toStreamResponse() + // Inverted call order, because Mockito has problems with chained calls. + val response = client.toStreamResponse(client.api.getAvatar(username).execute()) + if (response.hasError() || response.stream == null) { throw IOException("${response.apiError}") } else { diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandler.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandler.kt index fbfad62f..3cdc2039 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandler.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandler.kt @@ -39,7 +39,8 @@ class CoverArtRequestHandler(private val client: SubsonicAPIClient) : RequestHan } // Try to fetch the image from the API - val response = client.api.getCoverArt(id, size).execute().toStreamResponse() + // Inverted call order, because Mockito has problems with chained calls. + val response = client.toStreamResponse(client.api.getCoverArt(id, size).execute()) // Handle the response if (!response.hasError() && response.stream != null) { diff --git a/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandlerTest.kt b/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandlerTest.kt index 5ad84cff..ddf283bb 100644 --- a/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandlerTest.kt +++ b/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/AvatarRequestHandlerTest.kt @@ -9,6 +9,7 @@ import org.amshove.kluent.`should throw` import org.amshove.kluent.shouldBeEqualTo import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Answers import org.mockito.kotlin.any import org.mockito.kotlin.mock import org.mockito.kotlin.whenever @@ -21,7 +22,7 @@ import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(manifest = Config.NONE) class AvatarRequestHandlerTest { - private val mockApiClient: SubsonicAPIClient = mock() + private val mockApiClient: SubsonicAPIClient = mock(defaultAnswer = Answers.RETURNS_DEEP_STUBS) private val handler = AvatarRequestHandler(mockApiClient) @Test @@ -60,8 +61,10 @@ class AvatarRequestHandlerTest { apiError = null, responseHttpCode = 200 ) - whenever(mockApiClient.api.getAvatar(any()).execute().toStreamResponse()) - .thenReturn(streamResponse) + + whenever( + mockApiClient.toStreamResponse(any()) + ).thenReturn(streamResponse) val response = handler.load( createLoadAvatarRequest("some-username").buildRequest(), 0 diff --git a/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandlerTest.kt b/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandlerTest.kt index 442eba3c..fdef0b0c 100644 --- a/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandlerTest.kt +++ b/ultrasonic/src/test/kotlin/org/moire/ultrasonic/imageloader/CoverArtRequestHandlerTest.kt @@ -10,18 +10,17 @@ import org.amshove.kluent.`should throw` import org.amshove.kluent.shouldBeEqualTo import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Answers import org.mockito.kotlin.any -import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.mock import org.mockito.kotlin.whenever import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient import org.moire.ultrasonic.api.subsonic.response.StreamResponse -import org.moire.ultrasonic.api.subsonic.toStreamResponse import org.robolectric.RobolectricTestRunner @RunWith(RobolectricTestRunner::class) class CoverArtRequestHandlerTest { - private val mockApiClient: SubsonicAPIClient = mock() + private val mockApiClient: SubsonicAPIClient = mock(defaultAnswer = Answers.RETURNS_DEEP_STUBS) private val handler = CoverArtRequestHandler(mockApiClient) @Test @@ -58,7 +57,7 @@ class CoverArtRequestHandlerTest { val streamResponse = StreamResponse(null, null, 500) whenever( - mockApiClient.api.getCoverArt(any(), anyOrNull()).execute().toStreamResponse() + mockApiClient.toStreamResponse(any()) ).thenReturn(streamResponse) val fail = { @@ -77,7 +76,7 @@ class CoverArtRequestHandlerTest { ) whenever( - mockApiClient.api.getCoverArt(any(), anyOrNull()).execute().toStreamResponse() + mockApiClient.toStreamResponse(any()) ).thenReturn(streamResponse) val response = handler.load(