Fix tests

This commit is contained in:
tzugen 2021-06-09 17:00:25 +02:00
parent d9a7fa2413
commit a60a843edf
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
5 changed files with 26 additions and 12 deletions

View File

@ -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<ResponseBody>): StreamResponse {
return call.toStreamResponse()
}
companion object {
val jacksonMapper: ObjectMapper = ObjectMapper()
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)

View File

@ -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 {

View File

@ -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) {

View File

@ -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

View File

@ -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(