Skip to content

Commit

Permalink
[BasicRxJavaSampleKotlin] Updated to use Completable (#539)
Browse files Browse the repository at this point in the history
* Updates versions

* Updates insertUser to return Completable. Removes Completable creation in ViewModel

* Fixes the UserViewModelTest
  • Loading branch information
florina-muntenescu authored Jan 18, 2019
1 parent 903bc64 commit 9da853b
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 100 deletions.
28 changes: 14 additions & 14 deletions BasicRxJavaSampleKotlin/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ dependencies {
testImplementation deps.kotlin.test

// Android Testing Support Library's runner and rules
androidTestCompile deps.atsl.runner
androidTestCompile deps.atsl.rules
androidTestCompile deps.room.testing
androidTestCompile deps.arch_core.testing
androidTestImplementation deps.atsl.runner
androidTestImplementation deps.atsl.rules
androidTestImplementation deps.room.testing
androidTestImplementation deps.arch_core.testing

// Dependencies for Android unit tests
androidTestCompile deps.junit
androidTestCompile deps.mockito.core, { exclude group: 'net.bytebuddy' }
androidTestCompile deps.dexmaker
androidTestImplementation deps.junit
androidTestImplementation deps.mockito.core, { exclude group: 'net.bytebuddy' }
androidTestImplementation deps.dexmaker

// Espresso UI Testing
androidTestCompile deps.espresso.core
androidTestCompile deps.espresso.contrib
androidTestCompile deps.espresso.intents
androidTestImplementation deps.espresso.core
androidTestImplementation deps.espresso.contrib
androidTestImplementation deps.espresso.intents

// Resolve conflicts between main and test APK:
androidTestCompile deps.support.annotations
androidTestCompile deps.support.v4
androidTestCompile deps.support.app_compat
androidTestCompile deps.support.design
androidTestImplementation deps.support.annotations
androidTestImplementation deps.support.v4
androidTestImplementation deps.support.app_compat
androidTestImplementation deps.support.design
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class UserDaoTest {

@Test fun insertAndGetUser() {
// When inserting a new user in the data source
database.userDao().insertUser(USER)
database.userDao().insertUser(USER).blockingAwait()

// When subscribing to the emissions of the user
database.userDao().getUserById(USER.id)
Expand All @@ -68,11 +68,11 @@ class UserDaoTest {

@Test fun updateAndGetUser() {
// Given that we have a user in the data source
database.userDao().insertUser(USER)
database.userDao().insertUser(USER).blockingAwait()

// When we are updating the name of the user
val updatedUser = User(USER.id, "new username")
database.userDao().insertUser(updatedUser)
database.userDao().insertUser(updatedUser).blockingAwait()

// When subscribing to the emissions of the user
database.userDao().getUserById(USER.id)
Expand All @@ -83,7 +83,7 @@ class UserDaoTest {

@Test fun deleteAndGetUser() {
// Given that we have a user in the data source
database.userDao().insertUser(USER)
database.userDao().insertUser(USER).blockingAwait()

//When we are deleting all users
database.userDao().deleteAllUsers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import io.reactivex.Completable

import io.reactivex.Flowable

Expand All @@ -43,7 +44,7 @@ interface UserDao {
* @param user the user to be inserted.
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUser(user: User)
fun insertUser(user: User): Completable

/**
* Delete all users.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ class UserViewModel(private val dataSource: UserDao) : ViewModel() {
* @return a [Completable] that completes when the user name is updated
*/
fun updateUserName(userName: String): Completable {
return Completable.fromAction {
val user = User(USER_ID, userName)
dataSource.insertUser(user)
}
val user = User(USER_ID, userName)
return dataSource.insertUser(user)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,43 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.example.android.observability.persistence.User
import com.example.android.observability.persistence.UserDao
import com.example.android.observability.ui.UserViewModel
import io.reactivex.Completable
import io.reactivex.Flowable
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations


/**
* Unit test for [UserViewModel]
*/
class UserViewModelTest {

@get:Rule var instantTaskExecutorRule = InstantTaskExecutorRule()
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()

@Mock private lateinit var dataSource: UserDao
@Mock
private lateinit var dataSource: UserDao

@Captor private lateinit var userArgumentCaptor: ArgumentCaptor<User>
@Captor
private lateinit var userArgumentCaptor: ArgumentCaptor<User>

private lateinit var viewModel: UserViewModel

@Before fun setUp() {
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)

viewModel = UserViewModel(dataSource)
}

@Test fun getUserName_whenNoUserSaved() {
@Test
fun getUserName_whenNoUserSaved() {
// Given that the UserDataSource returns an empty list of users
`when`(dataSource.getUserById(UserViewModel.USER_ID)).thenReturn(Flowable.empty<User>())

Expand All @@ -63,7 +67,8 @@ class UserViewModelTest {
.assertNoValues()
}

@Test fun getUserName_whenUserSaved() {
@Test
fun getUserName_whenUserSaved() {
// Given that the UserDataSource returns a user
val user = User(userName = "user name")
`when`(dataSource.getUserById(UserViewModel.USER_ID)).thenReturn(Flowable.just(user))
Expand All @@ -75,17 +80,20 @@ class UserViewModelTest {
.assertValue("user name")
}

@Test fun updateUserName_updatesNameInDataSource() {
@Test
fun updateUserName_updatesNameInDataSource() {
// Given that a user is already inserted
dataSource.insertUser(User(UserViewModel.USER_ID, "name"))

// And a specific user is expected when inserting
val userName = "new user name"
val expectedUser = User(UserViewModel.USER_ID, userName)
`when`(dataSource.insertUser(expectedUser)).thenReturn(Completable.complete())

// When updating the user name
viewModel.updateUserName("new user name")
viewModel.updateUserName(userName)
.test()
.assertComplete()

// The user name is updated in the data source
// using ?: User("someUser") because otherwise, we get
// "IllegalStateException: userArgumentCaptor.capture() must not be null"
verify<UserDao>(dataSource).insertUser(capture(userArgumentCaptor))
assertThat(userArgumentCaptor.value.userName, Matchers.`is`("new user name"))
}

}
3 changes: 3 additions & 0 deletions BasicRxJavaSampleKotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ buildscript {
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
repositories {
google()
}
}

allprojects {
Expand Down
Binary file modified BasicRxJavaSampleKotlin/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
19 changes: 1 addition & 18 deletions BasicRxJavaSampleKotlin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
#
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#Thu Feb 22 10:53:38 GMT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1-milestone-1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip
72 changes: 42 additions & 30 deletions BasicRxJavaSampleKotlin/gradlew
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
warn () {
echo "$*"
}

die ( ) {
die () {
echo
echo "$*"
echo
Expand All @@ -30,6 +48,7 @@ die ( ) {
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
Expand All @@ -40,26 +59,11 @@ case "`uname`" in
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

# Determine the Java command to use to start the JVM.
Expand All @@ -85,7 +89,7 @@ location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
Expand Down Expand Up @@ -150,11 +154,19 @@ if $cygwin ; then
esac
fi

# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
APP_ARGS=$(save "$@")

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
exec "$JAVACMD" "$@"
14 changes: 4 additions & 10 deletions BasicRxJavaSampleKotlin/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

Expand Down Expand Up @@ -46,10 +46,9 @@ echo location of your Java installation.
goto fail

:init
@rem Get command-line arguments, handling Windowz variants
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args

:win9xME_args
@rem Slurp the command line arguments.
Expand All @@ -60,11 +59,6 @@ set _SKIP=2
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*
goto execute

:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$

:execute
@rem Setup the command line
Expand Down
Loading

0 comments on commit 9da853b

Please sign in to comment.