-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
6 - add ktor, kotlinx.serialization, and add mock network test
- Loading branch information
1 parent
8c2bbe3
commit 8e5766b
Showing
11 changed files
with
249 additions
and
44 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
92 changes: 92 additions & 0 deletions
92
app/src/androidTest/java/com/plusmobileapps/kotlinopenespresso/test/MockNetworkLoginTest.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,92 @@ | ||
package com.plusmobileapps.kotlinopenespresso.test | ||
|
||
import androidx.test.core.app.launchActivity | ||
import com.plusmobileapps.kotlinopenespresso.data.model.LoginError | ||
import com.plusmobileapps.kotlinopenespresso.data.model.LoginResponse | ||
import com.plusmobileapps.kotlinopenespresso.di.HttpClientModule | ||
import com.plusmobileapps.kotlinopenespresso.di.HttpEngineModule | ||
import com.plusmobileapps.kotlinopenespresso.extensions.click | ||
import com.plusmobileapps.kotlinopenespresso.extensions.startOnPage | ||
import com.plusmobileapps.kotlinopenespresso.extensions.verifyText | ||
import com.plusmobileapps.kotlinopenespresso.extensions.verifyVisible | ||
import com.plusmobileapps.kotlinopenespresso.page.LoginPage | ||
import com.plusmobileapps.kotlinopenespresso.ui.login.LoginActivity | ||
import com.plusmobileapps.kotlinopenespresso.util.MockNetworkTestHelper | ||
import dagger.hilt.android.testing.BindValue | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import dagger.hilt.android.testing.UninstallModules | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.* | ||
import io.ktor.client.engine.mock.* | ||
import io.ktor.http.* | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.junit.After | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
@UninstallModules(HttpEngineModule::class) | ||
@HiltAndroidTest | ||
class MockNetworkLoginTest { | ||
|
||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
private val networkHelper = MockNetworkTestHelper() | ||
|
||
@BindValue | ||
@JvmField | ||
val mockClient: HttpClientEngine = networkHelper.httpClientEngine | ||
|
||
private val username = "andrew" | ||
private val password = "password123" | ||
private val displayName = "Buzz Killington" | ||
|
||
@After | ||
fun tearDown() { | ||
networkHelper.destroy() | ||
} | ||
|
||
@Test | ||
fun successfulLogin() { | ||
networkHelper.everyLoginReturns { | ||
respond( | ||
Json.encodeToString(LoginResponse("first-id", displayName)), | ||
HttpStatusCode.OK, | ||
headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString()) | ||
) | ||
} | ||
|
||
val activityScenario = launchActivity<LoginActivity>() | ||
|
||
startOnPage<LoginPage> { | ||
enterInfo(username, password) | ||
}.goToLoggedInPage { | ||
onWelcomeGreeting().verifyText("Welcome $displayName!") | ||
}.goToSettings() | ||
|
||
activityScenario.close() | ||
} | ||
|
||
@Test | ||
fun errorLogin() { | ||
val expectedError = LoginError(1, "There was an error") | ||
networkHelper.everyLoginReturns { | ||
respond( | ||
Json.encodeToString(LoginError.serializer(), expectedError), | ||
HttpStatusCode.BadRequest | ||
) | ||
} | ||
|
||
val activityScenario = launchActivity<LoginActivity>() | ||
|
||
startOnPage<LoginPage> { | ||
enterInfo(username, password) | ||
onSignInOrRegisterButton().click() | ||
onErrorMessage().verifyText(expectedError.message).verifyVisible() | ||
} | ||
|
||
activityScenario.close() | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
app/src/androidTest/java/com/plusmobileapps/kotlinopenespresso/util/NetworkTestHelper.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,46 @@ | ||
package com.plusmobileapps.kotlinopenespresso.util | ||
|
||
import com.plusmobileapps.kotlinopenespresso.data.LoginDataSource | ||
import com.plusmobileapps.kotlinopenespresso.data.model.LoginResponse | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.* | ||
import io.ktor.client.engine.mock.* | ||
import io.ktor.client.features.json.* | ||
import io.ktor.client.features.json.serializer.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
|
||
class MockNetworkTestHelper { | ||
|
||
val httpClientEngine: HttpClientEngine = MockEngine { request -> | ||
when (request.url.fullUrl) { | ||
LoginDataSource.LOGIN_URL -> getLoginResponse.invoke(this, request) | ||
else -> error("Unhandled ${request.url.fullUrl}") | ||
} | ||
} | ||
|
||
private var getLoginResponse: MockRequestHandler = defaultLoginResponseHandler | ||
|
||
fun everyLoginReturns(response: MockRequestHandler) { | ||
this.getLoginResponse = response | ||
} | ||
|
||
fun destroy() { | ||
getLoginResponse = defaultLoginResponseHandler | ||
} | ||
|
||
companion object { | ||
val defaultLoginResponseHandler: MockRequestHandler = { | ||
respond( | ||
Json.encodeToString(LoginResponse("default-id", "Buzz Killington")), | ||
HttpStatusCode.OK, | ||
headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString()) | ||
) | ||
} | ||
} | ||
} | ||
|
||
val Url.hostWithPortIfRequired: String get() = if (port == protocol.defaultPort) host else hostWithPort | ||
val Url.fullUrl: String get() = "${protocol.name}://$hostWithPortIfRequired$fullPath" |
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
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/plusmobileapps/kotlinopenespresso/data/model/LoginResponse.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,12 @@ | ||
package com.plusmobileapps.kotlinopenespresso.data.model | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class LoginRequest(val username: String, val password: String) | ||
|
||
@Serializable | ||
data class LoginResponse(val id: String, val displayName: String) | ||
|
||
@Serializable | ||
data class LoginError(val errorCode: Int, val message: String) |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/plusmobileapps/kotlinopenespresso/di/HttpClientModule.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,35 @@ | ||
package com.plusmobileapps.kotlinopenespresso.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.* | ||
import io.ktor.client.engine.android.* | ||
import io.ktor.client.features.json.* | ||
import io.ktor.client.features.json.serializer.* | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object HttpEngineModule { | ||
@Provides | ||
@Singleton | ||
fun providesHttpEngine(): HttpClientEngine = Android.create() | ||
} | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object HttpClientModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun providesHttpClient(httpClientEngine: HttpClientEngine): HttpClient = | ||
HttpClient(httpClientEngine) { | ||
install(JsonFeature) { | ||
serializer = KotlinxSerializer() | ||
} | ||
} | ||
|
||
} |
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
25 changes: 0 additions & 25 deletions
25
app/src/main/java/com/plusmobileapps/kotlinopenespresso/ui/login/LoginViewModelFactory.kt
This file was deleted.
Oops, something went wrong.
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