Skip to content
This repository has been archived by the owner on Aug 4, 2019. It is now read-only.

Commit

Permalink
Validator class edit and test,closed #50
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaT2 committed Aug 3, 2019
1 parent 7cc527c commit 9be955b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 72 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/de/netalic/peacock/common/MyApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package de.netalic.peacock.common

import android.app.Application
import de.netalic.peacock.BuildConfig
import de.netalic.peacock.di.apiModule
import de.netalic.peacock.di.repositoryModule
import de.netalic.peacock.di.viewModelModule
import de.netalic.peacock.di.*
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
Expand All @@ -23,7 +21,8 @@ class MyApplication : Application() {
listOf(
repositoryModule,
viewModelModule,
apiModule
apiModule,
validatorModule
)
)
}
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/de/netalic/peacock/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import de.netalic.peacock.data.webservice.ApiClient
import de.netalic.peacock.ui.emailVerification.EmailVerificationViewModel
import de.netalic.peacock.ui.login.pattern.PatternViewModel
import de.netalic.peacock.ui.registration.RegistrationViewModel
import de.netalic.peacock.util.ValidatorUtils
import org.koin.android.viewmodel.dsl.viewModel
import org.koin.dsl.module

Expand All @@ -17,12 +18,16 @@ val repositoryModule = module {

val viewModelModule = module {
viewModel { PatternViewModel() }
viewModel { RegistrationViewModel(get()) }
viewModel { EmailVerificationViewModel(get()) }
viewModel { RegistrationViewModel(get(), get()) }
viewModel { EmailVerificationViewModel(get(), get()) }


}

val apiModule = module {
single { ApiClient.getService() }
}

val validatorModule = module {
factory { ValidatorUtils() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class EmailVerificationFragment : BaseFragment() {
}

override fun onTextChanged(characters: CharSequence?, p1: Int, p2: Int, p3: Int) {
if (characters != null && ValidatorUtils.emailValidator(characters.toString()))
if (characters != null && ValidatorUtils().emailValidator(characters.toString()))
enableContinueButton()
else
disableContinueButton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import de.netalic.peacock.util.ValidatorUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class EmailVerificationViewModel(private val repository: EmailRepository) : BaseViewModel() {
class EmailVerificationViewModel(private val repository: EmailRepository, private val validatorUtils:ValidatorUtils) : BaseViewModel() {

private val mSetEmailResponseLivaData = MutableLiveData<MyResponse<EmailVerificationModel>>()

Expand All @@ -20,7 +20,7 @@ class EmailVerificationViewModel(private val repository: EmailRepository) : Base
}

fun setEmail(token: String, email: String) {
if (ValidatorUtils.emailValidator(email)) {
if (validatorUtils.emailValidator(email)) {
val disposable = repository.setEmail(token, email)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import de.netalic.peacock.util.ValidatorUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class RegistrationViewModel(private val userRepository: UserRepository) : BaseViewModel() {
class RegistrationViewModel(private val userRepository: UserRepository, private val validatorUtils:ValidatorUtils) : BaseViewModel() {

private val mClaimResponseLiveData = MutableLiveData<MyResponse<UserModel>>()

Expand All @@ -23,7 +23,7 @@ class RegistrationViewModel(private val userRepository: UserRepository) : BaseVi
}

fun claim(phone: String, udid: String) {
if (ValidatorUtils.phoneValidator(phone)) {
if (validatorUtils.phoneValidator(phone)) {
val disposable = userRepository.claim(phone, udid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down
21 changes: 10 additions & 11 deletions app/src/main/java/de/netalic/peacock/util/ValidatorUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package de.netalic.peacock.util

class ValidatorUtils {

companion object {
//ToDo-tina min and max size of phone number
private val mPhonePatternMarcher = "[+0-9 ]{5,15}".toRegex()
//TODO-tina mEmail pattern length
private val mEmailPatternMatcher = "[a-zA-Z0-9._-]{2,30}+@[a-zA-Z0-9-]{2,20}+\\.[a-zA-Z.]{2,10}".toRegex()
//ToDo-tina min and max size of phone number
private val mPhonePatternMarcher = "[+0-9 ]{5,15}".toRegex()
//TODO-tina mEmail pattern length
private val mEmailPatternMatcher = "[a-zA-Z0-9._-]{2,30}+@[a-zA-Z0-9-]{2,20}+\\.[a-zA-Z.]{2,10}".toRegex()

fun phoneValidator(phoneNumber: String): Boolean {
return mPhonePatternMarcher.matches(phoneNumber)
}
fun emailValidator(email: String): Boolean {
return mEmailPatternMatcher.matches(email)
}
fun phoneValidator(phoneNumber: String): Boolean {
return mPhonePatternMarcher.matches(phoneNumber)
}

fun emailValidator(email: String): Boolean {
return mEmailPatternMatcher.matches(email)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import de.netalic.peacock.data.model.EmailVerificationModel
import de.netalic.peacock.data.model.Status
import de.netalic.peacock.data.repository.EmailRepository
import de.netalic.peacock.util.LiveDataTestUtil
import de.netalic.peacock.util.ValidatorUtils
import io.reactivex.Single
import io.reactivex.subjects.PublishSubject
import okhttp3.MediaType
Expand All @@ -20,10 +21,10 @@ import retrofit2.Response
class EmailVerificationViewModelTest : BaseTest() {

companion object {
val sEmail = EmailVerificationModel("[email protected]")
const val token =
private val sEmail = EmailVerificationModel("[email protected]")
private const val TOKEN =
"eyJhbGciOiJIUzI1NiIsImlhdCI6MTU2Mzk2Njk0OCwiZXhwIjoxNTcyNTY2OTQ4fQ.eyJpZCI6NCwiZGV2aWNlX2lkIjoxLCJwaG9uZSI6Iis5ODkzNTkzMjMxNzUiLCJyb2xlcyI6WyJjbGllbnQiXSwic2Vzc2lvbklkIjoiMTk5YmM4ZWItN2ExNC00YjBjLWI2YWMtNzQyZWQ1YTViNTk3IiwiZW1haWwiOiIiLCJpc0FjdGl2ZSI6ZmFsc2V9.sa0z7_2R-u94DlUQ0JEoaCHXi-ULaU5mJFy2KDjm_oM"
val sWrongEmail = EmailVerificationModel("tinat2aq")
private val sWrongEmail = EmailVerificationModel("tinat2aq")
}

@get:Rule
Expand All @@ -32,17 +33,21 @@ class EmailVerificationViewModelTest : BaseTest() {
@Mock
private lateinit var mEmailRepository: EmailRepository

@Mock
private lateinit var mValidatorUtils: ValidatorUtils

private lateinit var mEmailVerificationViewModel: EmailVerificationViewModel

@Before
fun setup() {
MockitoAnnotations.initMocks(this)
mEmailVerificationViewModel = EmailVerificationViewModel(mEmailRepository)
mEmailVerificationViewModel = EmailVerificationViewModel(mEmailRepository, mValidatorUtils)
}

@After
fun tearDown() {
Mockito.reset(mEmailRepository)
Mockito.reset(mValidatorUtils)
}

@Test
Expand All @@ -53,10 +58,11 @@ class EmailVerificationViewModelTest : BaseTest() {
Response.success(200, sEmail)
).delaySubscription(delayer)
Mockito.`when`(
mEmailRepository.setEmail(token, sEmail.mEmail)
mEmailRepository.setEmail(TOKEN, sEmail.mEmail)
).thenReturn(singleResponse)
mEmailVerificationViewModel.setEmail(token, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(token, sEmail.mEmail)
Mockito.`when`(mValidatorUtils.emailValidator(sEmail.mEmail)).thenReturn(true)
mEmailVerificationViewModel.setEmail(TOKEN, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(TOKEN, sEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.LOADING
Expand All @@ -81,16 +87,11 @@ class EmailVerificationViewModelTest : BaseTest() {
val singleResponse = Single.just(
Response.error<EmailVerificationModel>(
400,
ResponseBody.create(MediaType.parse("text/plain"), "")
)
).delaySubscription(delayer)
Mockito.`when`(
mEmailRepository.setEmail(token, sEmail.mEmail)
).thenReturn(singleResponse)
mEmailVerificationViewModel.setEmail(
token, sEmail.mEmail
)
Mockito.verify(mEmailRepository).setEmail(token, sEmail.mEmail)
ResponseBody.create(MediaType.parse("text/plain"), ""))).delaySubscription(delayer)
Mockito.`when`(mEmailRepository.setEmail(TOKEN, sEmail.mEmail)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.emailValidator(sEmail.mEmail)).thenReturn(true)
mEmailVerificationViewModel.setEmail(TOKEN, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(TOKEN, sEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.LOADING
Expand Down Expand Up @@ -118,12 +119,13 @@ class EmailVerificationViewModelTest : BaseTest() {
)
).delaySubscription(delayer)
Mockito.`when`(
mEmailRepository.setEmail(token, sEmail.mEmail)
mEmailRepository.setEmail(TOKEN, sEmail.mEmail)
).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.emailValidator(sEmail.mEmail)).thenReturn(true)
mEmailVerificationViewModel.setEmail(
token, sEmail.mEmail
TOKEN, sEmail.mEmail
)
Mockito.verify(mEmailRepository).setEmail(token, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(TOKEN, sEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.LOADING
Expand All @@ -149,13 +151,12 @@ class EmailVerificationViewModelTest : BaseTest() {
ResponseBody.create(MediaType.parse("text/plain"), "")
)
).delaySubscription(delayer)
Mockito.`when`(
mEmailRepository.setEmail(token, sEmail.mEmail)
).thenReturn(singleResponse)
Mockito.`when`(mEmailRepository.setEmail(TOKEN, sEmail.mEmail)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.emailValidator(sEmail.mEmail)).thenReturn(true)
mEmailVerificationViewModel.setEmail(
token, sEmail.mEmail
TOKEN, sEmail.mEmail
)
Mockito.verify(mEmailRepository).setEmail(token, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(TOKEN, sEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.LOADING
Expand All @@ -181,13 +182,10 @@ class EmailVerificationViewModelTest : BaseTest() {
ResponseBody.create(MediaType.parse("text/plain"), "")
)
).delaySubscription(delayer)
Mockito.`when`(
mEmailRepository.setEmail(token, sEmail.mEmail)
).thenReturn(singleResponse)
mEmailVerificationViewModel.setEmail(
token, sEmail.mEmail
)
Mockito.verify(mEmailRepository).setEmail(token, sEmail.mEmail)
Mockito.`when`(mEmailRepository.setEmail(TOKEN, sEmail.mEmail)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.emailValidator(sEmail.mEmail)).thenReturn(true)
mEmailVerificationViewModel.setEmail(TOKEN, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(TOKEN, sEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.LOADING
Expand All @@ -213,13 +211,10 @@ class EmailVerificationViewModelTest : BaseTest() {
ResponseBody.create(MediaType.parse("text/plain"), "")
)
).delaySubscription(delayer)
Mockito.`when`(
mEmailRepository.setEmail(token, sEmail.mEmail)
).thenReturn(singleResponse)
mEmailVerificationViewModel.setEmail(
token, sEmail.mEmail
)
Mockito.verify(mEmailRepository).setEmail(token, sEmail.mEmail)
Mockito.`when`(mEmailRepository.setEmail(TOKEN, sEmail.mEmail)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.emailValidator(sEmail.mEmail)).thenReturn(true)
mEmailVerificationViewModel.setEmail(TOKEN, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(TOKEN, sEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.LOADING
Expand All @@ -243,13 +238,10 @@ class EmailVerificationViewModelTest : BaseTest() {
val singleResponse = Single
.error<Response<EmailVerificationModel>>(Exception())
.delaySubscription(delayer)
Mockito.`when`(
mEmailRepository.setEmail(token, sEmail.mEmail)
).thenReturn(singleResponse)
mEmailVerificationViewModel.setEmail(
token, sEmail.mEmail
)
Mockito.verify(mEmailRepository).setEmail(token, sEmail.mEmail)
Mockito.`when`(mEmailRepository.setEmail(TOKEN, sEmail.mEmail)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.emailValidator(sEmail.mEmail)).thenReturn(true)
mEmailVerificationViewModel.setEmail(TOKEN, sEmail.mEmail)
Mockito.verify(mEmailRepository).setEmail(TOKEN, sEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.LOADING
Expand All @@ -268,7 +260,8 @@ class EmailVerificationViewModelTest : BaseTest() {
@Test
fun emailValidator_invalidEmail() {

mEmailVerificationViewModel.setEmail(token, sWrongEmail.mEmail)
Mockito.`when`(mValidatorUtils.emailValidator(Mockito.anyString())).thenReturn(false)
mEmailVerificationViewModel.setEmail(TOKEN, sWrongEmail.mEmail)
Assert.assertEquals(
LiveDataTestUtil.getValue(mEmailVerificationViewModel.getSetEmailLiveData()).status,
Status.FAILED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import de.netalic.peacock.data.model.Status
import de.netalic.peacock.data.model.UserModel
import de.netalic.peacock.data.repository.UserRepository
import de.netalic.peacock.util.LiveDataTestUtil
import de.netalic.peacock.util.ValidatorUtils
import io.reactivex.Single
import io.reactivex.subjects.PublishSubject
import okhttp3.MediaType
Expand All @@ -24,8 +25,8 @@ class PhoneInputViewModelTest : BaseTest() {


companion object {
val sUser = UserModel(mPhone = "989359323175", mUdid = "123456")
val wrongUserPhone = UserModel(mPhone = "a359323175", mUdid = "123456")
private val sUser = UserModel(mPhone = "989359323175", mUdid = "123456")
private val wrongUserPhone = UserModel(mPhone = "a359323175", mUdid = "123456")
}

@get:Rule
Expand All @@ -34,17 +35,21 @@ class PhoneInputViewModelTest : BaseTest() {
@Mock
private lateinit var mUserRepository: UserRepository

@Mock
private lateinit var mValidatorUtils: ValidatorUtils

private lateinit var mRegistrationViewModel: RegistrationViewModel

@Before
fun setup() {
MockitoAnnotations.initMocks(this)
mRegistrationViewModel = RegistrationViewModel(mUserRepository)
mRegistrationViewModel = RegistrationViewModel(mUserRepository,mValidatorUtils)
}

@After
fun tearDown() {
Mockito.reset(mUserRepository)
Mockito.reset(mValidatorUtils)
}

@Test
Expand All @@ -55,6 +60,7 @@ class PhoneInputViewModelTest : BaseTest() {
Response.success(200, sUser)
).delaySubscription(delayer)
Mockito.`when`(mUserRepository.claim(sUser.mPhone, sUser.mUdid)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.phoneValidator(sUser.mPhone)).thenReturn(true)
mRegistrationViewModel.claim(sUser.mPhone, sUser.mUdid)
Mockito.verify(mUserRepository).claim(sUser.mPhone, sUser.mUdid)
Assert.assertEquals(
Expand All @@ -80,6 +86,7 @@ class PhoneInputViewModelTest : BaseTest() {
)
.delaySubscription(delayer)
Mockito.`when`(mUserRepository.claim(sUser.mPhone, sUser.mUdid)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.phoneValidator(sUser.mPhone)).thenReturn(true)
mRegistrationViewModel.claim(sUser.mPhone, sUser.mUdid)
Mockito.verify(mUserRepository).claim(sUser.mPhone, sUser.mUdid)
Assert.assertEquals(
Expand Down Expand Up @@ -107,6 +114,7 @@ class PhoneInputViewModelTest : BaseTest() {
)
.delaySubscription(delayer)
Mockito.`when`(mUserRepository.claim(sUser.mPhone, sUser.mUdid)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.phoneValidator(sUser.mPhone)).thenReturn(true)
mRegistrationViewModel.claim(sUser.mPhone, sUser.mUdid)
Mockito.verify(mUserRepository).claim(sUser.mPhone, sUser.mUdid)
Assert.assertEquals(
Expand Down Expand Up @@ -135,6 +143,7 @@ class PhoneInputViewModelTest : BaseTest() {
)
.delaySubscription(delayer)
Mockito.`when`(mUserRepository.claim(sUser.mPhone, sUser.mUdid)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.phoneValidator(sUser.mPhone)).thenReturn(true)
mRegistrationViewModel.claim(sUser.mPhone, sUser.mUdid)
Mockito.verify(mUserRepository).claim(sUser.mPhone, sUser.mUdid)
Assert.assertEquals(
Expand All @@ -158,6 +167,7 @@ class PhoneInputViewModelTest : BaseTest() {
.delaySubscription(delayer)

Mockito.`when`(mUserRepository.claim(sUser.mPhone, sUser.mUdid)).thenReturn(singleResponse)
Mockito.`when`(mValidatorUtils.phoneValidator(sUser.mPhone)).thenReturn(true)
mRegistrationViewModel.claim(sUser.mPhone, sUser.mUdid)
Mockito.verify(mUserRepository).claim(sUser.mPhone, sUser.mUdid)
Assert.assertEquals(
Expand All @@ -177,6 +187,7 @@ class PhoneInputViewModelTest : BaseTest() {
@Test
fun phoneValidator_invalidPhoneNumber() {

Mockito.`when`(mValidatorUtils.phoneValidator(wrongUserPhone.mPhone)).thenReturn(false)
mRegistrationViewModel.claim(wrongUserPhone.mPhone, wrongUserPhone.mUdid)
Assert.assertEquals(LiveDataTestUtil.getValue(mRegistrationViewModel.getClaimLiveData()).status, Status.FAILED)
Assert.assertEquals(
Expand Down
Loading

0 comments on commit 9be955b

Please sign in to comment.