mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-19 21:20:52 +01:00
Fix tests
This commit is contained in:
parent
d9a7fa2413
commit
a60a843edf
@ -9,12 +9,15 @@ import java.util.concurrent.TimeUnit.MILLISECONDS
|
|||||||
import javax.net.ssl.SSLContext
|
import javax.net.ssl.SSLContext
|
||||||
import javax.net.ssl.X509TrustManager
|
import javax.net.ssl.X509TrustManager
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.ResponseBody
|
||||||
import okhttp3.logging.HttpLoggingInterceptor
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
import org.moire.ultrasonic.api.subsonic.interceptors.PasswordHexInterceptor
|
import org.moire.ultrasonic.api.subsonic.interceptors.PasswordHexInterceptor
|
||||||
import org.moire.ultrasonic.api.subsonic.interceptors.PasswordMD5Interceptor
|
import org.moire.ultrasonic.api.subsonic.interceptors.PasswordMD5Interceptor
|
||||||
import org.moire.ultrasonic.api.subsonic.interceptors.ProxyPasswordInterceptor
|
import org.moire.ultrasonic.api.subsonic.interceptors.ProxyPasswordInterceptor
|
||||||
import org.moire.ultrasonic.api.subsonic.interceptors.RangeHeaderInterceptor
|
import org.moire.ultrasonic.api.subsonic.interceptors.RangeHeaderInterceptor
|
||||||
import org.moire.ultrasonic.api.subsonic.interceptors.VersionInterceptor
|
import org.moire.ultrasonic.api.subsonic.interceptors.VersionInterceptor
|
||||||
|
import org.moire.ultrasonic.api.subsonic.response.StreamResponse
|
||||||
|
import retrofit2.Response
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
|
|
||||||
private const val READ_TIMEOUT = 60_000L
|
private const val READ_TIMEOUT = 60_000L
|
||||||
@ -124,6 +127,13 @@ class SubsonicAPIClient(
|
|||||||
hostnameVerifier { _, _ -> true }
|
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 {
|
companion object {
|
||||||
val jacksonMapper: ObjectMapper = ObjectMapper()
|
val jacksonMapper: ObjectMapper = ObjectMapper()
|
||||||
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
|
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
|
||||||
|
@ -6,13 +6,12 @@ import com.squareup.picasso.RequestHandler
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import okio.Okio
|
import okio.Okio
|
||||||
import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient
|
import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient
|
||||||
import org.moire.ultrasonic.api.subsonic.toStreamResponse
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads avatars from subsonic api.
|
* Loads avatars from subsonic api.
|
||||||
*/
|
*/
|
||||||
class AvatarRequestHandler(
|
class AvatarRequestHandler(
|
||||||
private val apiClient: SubsonicAPIClient
|
private val client: SubsonicAPIClient
|
||||||
) : RequestHandler() {
|
) : RequestHandler() {
|
||||||
override fun canHandleRequest(data: Request): Boolean {
|
override fun canHandleRequest(data: Request): Boolean {
|
||||||
return with(data.uri) {
|
return with(data.uri) {
|
||||||
@ -24,7 +23,9 @@ class AvatarRequestHandler(
|
|||||||
val username = request.uri.getQueryParameter(QUERY_USERNAME)
|
val username = request.uri.getQueryParameter(QUERY_USERNAME)
|
||||||
?: throw IllegalArgumentException("Nullable 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) {
|
if (response.hasError() || response.stream == null) {
|
||||||
throw IOException("${response.apiError}")
|
throw IOException("${response.apiError}")
|
||||||
} else {
|
} else {
|
||||||
|
@ -39,7 +39,8 @@ class CoverArtRequestHandler(private val client: SubsonicAPIClient) : RequestHan
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to fetch the image from the API
|
// 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
|
// Handle the response
|
||||||
if (!response.hasError() && response.stream != null) {
|
if (!response.hasError() && response.stream != null) {
|
||||||
|
@ -9,6 +9,7 @@ import org.amshove.kluent.`should throw`
|
|||||||
import org.amshove.kluent.shouldBeEqualTo
|
import org.amshove.kluent.shouldBeEqualTo
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.Answers
|
||||||
import org.mockito.kotlin.any
|
import org.mockito.kotlin.any
|
||||||
import org.mockito.kotlin.mock
|
import org.mockito.kotlin.mock
|
||||||
import org.mockito.kotlin.whenever
|
import org.mockito.kotlin.whenever
|
||||||
@ -21,7 +22,7 @@ import org.robolectric.annotation.Config
|
|||||||
@RunWith(RobolectricTestRunner::class)
|
@RunWith(RobolectricTestRunner::class)
|
||||||
@Config(manifest = Config.NONE)
|
@Config(manifest = Config.NONE)
|
||||||
class AvatarRequestHandlerTest {
|
class AvatarRequestHandlerTest {
|
||||||
private val mockApiClient: SubsonicAPIClient = mock()
|
private val mockApiClient: SubsonicAPIClient = mock(defaultAnswer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private val handler = AvatarRequestHandler(mockApiClient)
|
private val handler = AvatarRequestHandler(mockApiClient)
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -60,8 +61,10 @@ class AvatarRequestHandlerTest {
|
|||||||
apiError = null,
|
apiError = null,
|
||||||
responseHttpCode = 200
|
responseHttpCode = 200
|
||||||
)
|
)
|
||||||
whenever(mockApiClient.api.getAvatar(any()).execute().toStreamResponse())
|
|
||||||
.thenReturn(streamResponse)
|
whenever(
|
||||||
|
mockApiClient.toStreamResponse(any())
|
||||||
|
).thenReturn(streamResponse)
|
||||||
|
|
||||||
val response = handler.load(
|
val response = handler.load(
|
||||||
createLoadAvatarRequest("some-username").buildRequest(), 0
|
createLoadAvatarRequest("some-username").buildRequest(), 0
|
||||||
|
@ -10,18 +10,17 @@ import org.amshove.kluent.`should throw`
|
|||||||
import org.amshove.kluent.shouldBeEqualTo
|
import org.amshove.kluent.shouldBeEqualTo
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.Answers
|
||||||
import org.mockito.kotlin.any
|
import org.mockito.kotlin.any
|
||||||
import org.mockito.kotlin.anyOrNull
|
|
||||||
import org.mockito.kotlin.mock
|
import org.mockito.kotlin.mock
|
||||||
import org.mockito.kotlin.whenever
|
import org.mockito.kotlin.whenever
|
||||||
import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient
|
import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient
|
||||||
import org.moire.ultrasonic.api.subsonic.response.StreamResponse
|
import org.moire.ultrasonic.api.subsonic.response.StreamResponse
|
||||||
import org.moire.ultrasonic.api.subsonic.toStreamResponse
|
|
||||||
import org.robolectric.RobolectricTestRunner
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner::class)
|
@RunWith(RobolectricTestRunner::class)
|
||||||
class CoverArtRequestHandlerTest {
|
class CoverArtRequestHandlerTest {
|
||||||
private val mockApiClient: SubsonicAPIClient = mock()
|
private val mockApiClient: SubsonicAPIClient = mock(defaultAnswer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private val handler = CoverArtRequestHandler(mockApiClient)
|
private val handler = CoverArtRequestHandler(mockApiClient)
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -58,7 +57,7 @@ class CoverArtRequestHandlerTest {
|
|||||||
val streamResponse = StreamResponse(null, null, 500)
|
val streamResponse = StreamResponse(null, null, 500)
|
||||||
|
|
||||||
whenever(
|
whenever(
|
||||||
mockApiClient.api.getCoverArt(any(), anyOrNull()).execute().toStreamResponse()
|
mockApiClient.toStreamResponse(any())
|
||||||
).thenReturn(streamResponse)
|
).thenReturn(streamResponse)
|
||||||
|
|
||||||
val fail = {
|
val fail = {
|
||||||
@ -77,7 +76,7 @@ class CoverArtRequestHandlerTest {
|
|||||||
)
|
)
|
||||||
|
|
||||||
whenever(
|
whenever(
|
||||||
mockApiClient.api.getCoverArt(any(), anyOrNull()).execute().toStreamResponse()
|
mockApiClient.toStreamResponse(any())
|
||||||
).thenReturn(streamResponse)
|
).thenReturn(streamResponse)
|
||||||
|
|
||||||
val response = handler.load(
|
val response = handler.load(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user