-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from gravatar/hamorillo/14-manual-depency-inje…
…ctor DI - Adding basic manual injection
- Loading branch information
Showing
5 changed files
with
107 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
gravatar/src/main/java/com/gravatar/di/container/GravatarSdkContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.gravatar.di.container | ||
|
||
import com.gravatar.GravatarApiService | ||
import com.gravatar.GravatarConstants.GRAVATAR_API_BASE_URL | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
|
||
class GravatarSdkContainer private constructor() { | ||
companion object { | ||
val instance: GravatarSdkContainer by lazy { | ||
GravatarSdkContainer() | ||
} | ||
} | ||
|
||
private fun getRetrofitBuilder() = Retrofit.Builder().baseUrl(GRAVATAR_API_BASE_URL) | ||
|
||
fun getGravatarApiService(okHttpClient: OkHttpClient? = null): GravatarApiService { | ||
return getRetrofitBuilder().apply { | ||
okHttpClient?.let { client(it) } | ||
}.build().create(GravatarApiService::class.java) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.gravatar | ||
|
||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import junit.framework.TestCase.assertTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class GravatarApiTest { | ||
@get:Rule | ||
var gravatarSdkTest = GravatarSdkContainerRule() | ||
|
||
private lateinit var gravatarApi: GravatarApi | ||
|
||
@Before | ||
fun setUp() { | ||
gravatarApi = GravatarApi() | ||
} | ||
|
||
@Test | ||
fun `given an file, email and accessToken when uploading avatar then Gravatar service is invoked`() { | ||
gravatarApi.uploadGravatar(File("avatarFile"), "email", "accessToken", mockk()) | ||
verify(exactly = 1) { | ||
gravatarSdkTest.gravatarApiServiceMock.uploadImage( | ||
"Bearer accessToken", | ||
withArg { | ||
assertTrue( | ||
it.headers?.values("Content-Disposition").toString().contains("account"), | ||
) | ||
}, | ||
withArg { | ||
assertTrue( | ||
with(it.headers?.values("Content-Disposition").toString()) { | ||
contains("filedata") && contains("avatarFile") | ||
}, | ||
) | ||
}, | ||
) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
gravatar/src/test/java/com/gravatar/GravatarSdkContainerRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.gravatar | ||
|
||
import com.gravatar.di.container.GravatarSdkContainer | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.mockkObject | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
class GravatarSdkContainerRule : TestRule { | ||
var gravatarSdkContainerMock = mockk<GravatarSdkContainer>() | ||
var gravatarApiServiceMock = mockk<GravatarApiService>() | ||
|
||
override fun apply( | ||
base: Statement, | ||
description: Description, | ||
): Statement { | ||
return object : Statement() { | ||
override fun evaluate() { | ||
gravatarSdkContainerMock = mockk<GravatarSdkContainer>() | ||
gravatarApiServiceMock = mockk<GravatarApiService>(relaxed = true) | ||
mockkObject(GravatarSdkContainer) | ||
every { GravatarSdkContainer.instance } returns gravatarSdkContainerMock | ||
every { gravatarSdkContainerMock.getGravatarApiService(any()) } returns gravatarApiServiceMock | ||
|
||
base.evaluate() | ||
} | ||
} | ||
} | ||
} |