Skip to content

Commit

Permalink
Fixed failing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek-kommunicate committed Dec 28, 2024
1 parent f32e7be commit ea7381c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class ConversationTest {
onView(withId(R.id.conversation_send))
.perform(click())

onView(isRoot())
.perform(waitFor(3000))

verifyMessagesOnTheDashboard(groupId.toString(), listOf(randomPrefillMessage), tempUser.userId)
}

Expand All @@ -212,7 +215,7 @@ class ConversationTest {
val latch = CountDownLatch(1)
val conversationTitle = getRandomString()
var groupId: Int = 0
val botIds = listOf("inline-code-34rpc", "kk-3s8r3")
val botIds = listOf("inline-code-34rpc", "kk-3s8r3", "richmessagetest-apbah")
val teamId = "103785933"
val agentIds = listOf("[email protected]", "[email protected]")

Expand Down Expand Up @@ -284,7 +287,7 @@ class ConversationTest {
}

onView(isRoot())
.perform(waitForLatch(latch))
.perform(waitForLatch(latch, waitAfterLatch = 5000))

sendMessageAsUser(getRandomString())

Expand Down Expand Up @@ -332,7 +335,7 @@ class ConversationTest {
val tempUser = getRandomKmUser()
val latch = CountDownLatch(1)
var groupId = 0
val botIds = listOf("inline-code-34rpc")
val botIds = listOf("inline-code-34rpc", "richmessagetest-apbah")
val initialTeamId = "103785933"
val updateTeamId= "106336264"
val agentIds = listOf("[email protected]")
Expand Down Expand Up @@ -464,7 +467,7 @@ class ConversationTest {
.perform(waitFor(2500))

onView(withId(R.id.toolbar_title))
.check(matches(withText("Prateek Singh")))
.check(matches(withText("trgfdgfd")))
}

private fun validateBotMessageReply(groupId: Int, bots: List<String>) = runBlocking {
Expand Down Expand Up @@ -507,7 +510,7 @@ class ConversationTest {
}
}
if (tempMessageList.isNotEmpty()) {
fail("unable to see the sent messages from SDK on dashboard $tempMessageList")
fail("unable to see the sent messages from SDK on dashboard $tempMessageList, groupId: $groupId")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ package kommunicate.io.sample
import android.app.ProgressDialog
import android.content.Context
import android.os.ResultReceiver
import android.util.Log
import android.view.View
import android.widget.Spinner
import androidx.lifecycle.lifecycleScope
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.closeSoftKeyboard
import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.FlakyTest
import com.applozic.mobicomkit.uiwidgets.kommunicate.activities.LeadCollectionActivity.EMAIL_VALIDATION_REGEX
import com.applozic.mobicomkit.uiwidgets.kommunicate.activities.LeadCollectionActivity.PHONE_NUMBER_VALIDATION_REGEX
import com.applozic.mobicomkit.uiwidgets.kommunicate.adapters.KmPrechatInputAdapter
Expand All @@ -39,30 +35,24 @@ import kommunicate.io.sample.utils.getRandomString
import kommunicate.io.sample.utils.sendMessageAsUser
import kommunicate.io.sample.utils.waitFor
import kommunicate.io.sample.utils.waitForLatch
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeout
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.instanceOf
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.Matcher
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import com.applozic.mobicomkit.uiwidgets.R as Rui
Expand Down Expand Up @@ -170,6 +160,9 @@ class ConversationWithPreChatTest {
sendMessageAsUser(it)
}

onView(isRoot())
.perform(waitFor(5000))

val groupId = getConversationGroupIdFromUserEmail(tempUserMail)
verifyMessagesOnTheDashboard(groupId, messageList, tempUserMail)
}
Expand Down Expand Up @@ -332,26 +325,30 @@ class ConversationWithPreChatTest {
val tempGender = "Female"
val latch = CountDownLatch(1)

assertThrows(NullPointerException::class.java) {
mActivityRule.onActivity {
val user = KMUser().apply {
email = tempUserMail
displayName = tempName
contactNumber = tempUserPhone
metadata = mapOf("gender" to tempGender)
}
it.lifecycleScope.launch {
val isSuccess = buildAndLaunchConversationWithUser(it, user)
assertFalse("Conversation built with no group id:", isSuccess)
fail("the user object is created even when the userid is not passed")
}.invokeOnCompletion {
mActivityRule.onActivity { activity ->
val user = KMUser().apply {
email = tempUserMail
displayName = tempName
contactNumber = tempUserPhone
metadata = mapOf("gender" to tempGender)
}

activity.lifecycleScope.launch {
var exceptionThrown = false
try {
buildAndLaunchConversationWithUser(activity, user)
} catch (e: Exception) {
exceptionThrown = true
assertEquals("userId cannot be empty", e.message) // Verify exception message
} finally {
assertTrue("Expected exception was not thrown", exceptionThrown)
latch.countDown()
}
}

onView(isRoot())
.perform(waitForLatch(latch,100))
}

onView(isRoot())
.perform(waitForLatch(latch))
}

@Test
Expand Down Expand Up @@ -379,6 +376,8 @@ class ConversationWithPreChatTest {

onView(isRoot())
.perform(waitForLatch(latch,100))
onView(isRoot())
.perform(waitFor(5000))

val user = getUserFromDashboardWithEmail(tempUserMail)
assertNotNull("user not found on dashboard", user)
Expand All @@ -395,7 +394,7 @@ class ConversationWithPreChatTest {
continuation.resume(true)
}
override fun onFailure(error: Any) {
continuation.resumeWithException(error as NullPointerException)
continuation.resumeWithException(error as Exception)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ import kommunicate.io.sample.network.RetrofitClient
import kommunicate.io.sample.utils.getAuthToken
import kommunicate.io.sample.utils.getRandomKmUser
import kommunicate.io.sample.utils.getRandomString
import kommunicate.io.sample.utils.waitFor
import kommunicate.io.sample.utils.waitForLatch
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
Expand Down Expand Up @@ -97,19 +94,22 @@ class KMUserDataValidationTest {
val kmTempUser = getTempKmUser(isWrongMetadata = true)
val latch = CountDownLatch(1)

assertThrows("Expected Exception when keys with more that 30 char passed in metadata ${kmTempUser.metadata}", NullPointerException::class.java) {
mActivityRule.onActivity {
it.lifecycleScope.launch {
val isSuccess = buildAndLaunchConversationWithUser(it, kmTempUser)
assertFalse("conversation created with invalid metadata", isSuccess)
}.invokeOnCompletion {
mActivityRule.onActivity { activity ->
activity.lifecycleScope.launch {
var exceptionThrown = false
try {
buildAndLaunchConversationWithUser(activity, kmTempUser)
} catch (e: Exception) {
exceptionThrown = true
} finally {
assertTrue("Expected exception was not thrown", exceptionThrown)
latch.countDown()
}
}

onView(isRoot())
.perform(waitForLatch(latch, 100))
}

onView(isRoot())
.perform(waitForLatch(latch, 100))
}

private fun getUserFromDashboardWithEmail(email: String) = runBlocking {
Expand Down Expand Up @@ -147,7 +147,7 @@ class KMUserDataValidationTest {
continuation.resume(true)
}
override fun onFailure(error: Any) {
continuation.resumeWithException(error as NullPointerException)
continuation.resumeWithException(error as Exception)
}
})
}
Expand Down
68 changes: 66 additions & 2 deletions app/src/androidTest/java/kommunicate/io/sample/PseudoNameTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package kommunicate.io.sample

import android.content.Context
import androidx.lifecycle.lifecycleScope
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.applozic.mobicomkit.api.account.register.RegistrationResponse
import io.kommunicate.Kommunicate
Expand All @@ -10,41 +14,77 @@ import okhttp3.RequestBody.Companion.toRequestBody
import kommunicate.io.sample.network.KommunicateChatAPI
import kommunicate.io.sample.network.KommunicateDashboardAPI
import kommunicate.io.sample.network.RetrofitClient
import kommunicate.io.sample.utils.KmTestHelper.launchConversation
import kommunicate.io.sample.utils.getAuthToken
import kommunicate.io.sample.utils.getRandomString
import kommunicate.io.sample.utils.sendMessageAsUser
import kommunicate.io.sample.utils.waitFor
import kommunicate.io.sample.utils.waitForLatch
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import org.junit.Assert.assertNotNull
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.Exception
import java.util.concurrent.CountDownLatch
import kotlin.coroutines.resume
import kotlin.math.exp

@RunWith(AndroidJUnit4::class)
class PseudoNameTest {

private val mActivityRule = ActivityScenario.launch(MainActivity::class.java)
private lateinit var authToken: String
private lateinit var dashboardAPI: KommunicateDashboardAPI
private lateinit var chatAPI: KommunicateChatAPI
private lateinit var chatAuthToken: String
private lateinit var context: Context

@Before
fun setup() {
context = ApplicationProvider.getApplicationContext()
dashboardAPI = RetrofitClient.apiClient.create(KommunicateDashboardAPI::class.java)
authToken = getAuthToken(dashboardAPI)[1]
Kommunicate.init(context, "d6cbc2322c608519ad65ab3bcb09fe78", false)
chatAPI = RetrofitClient.chatClient.create(KommunicateChatAPI::class.java)
getAuthToken(dashboardAPI).let {
chatAuthToken = it[0]
authToken = it[1]
}
mActivityRule.onActivity {
Kommunicate.init(context, "d6cbc2322c608519ad65ab3bcb09fe78", false)
}
}

@Test
fun testPseudoNameIsCorrectWhenSettingsIsEnabled() {
val latch = CountDownLatch(1)

setPseudoNameOnDashboard(true)
val randomUserName = createRandomUser()
val correctDisplayNameRegex = "^([a-zA-Z]{2,}\\s[a-zA-Z]{1,}'?-?[a-zA-Z]{2,}\\s?([a-zA-Z]{1,})?)".toRegex()
if (!correctDisplayNameRegex.matches(randomUserName)) {
fail("random user name is not as per the pseudo name")
}

mActivityRule.onActivity {
it.lifecycleScope.launch {
launchConversation(it)
}.invokeOnCompletion {
latch.countDown()
}
}

onView(isRoot())
.perform(waitForLatch(latch))
sendMessageAsUser(getRandomString())
onView(isRoot())
.perform(waitFor(3000))

val userName = getUserFromDashboardWithUserName(randomUserName)
assertNotNull("Expected the same username from dashboard.", userName)
}

@Test
Expand Down Expand Up @@ -94,4 +134,28 @@ class PseudoNameTest {
})
}
}

private fun getUserFromDashboardWithUserName(userName: String) = runBlocking {
val dashboardDataResponse = chatAPI.getUsers(
token = chatAuthToken,
startIndex = 0,
pageSize = 10,
orderBy = 1,
roleNameList = "USER",
inactiveUser = true
)
val tempCreatedUser = if (dashboardDataResponse.isSuccessful && dashboardDataResponse.body() != null) {
dashboardDataResponse.body()!!.response.users.firstOrNull {
it.userName == userName
}
} else {
fail("unable to communicate with users api: ${dashboardDataResponse.errorBody()}")
return@runBlocking null
}
if (tempCreatedUser == null) {
fail("no user found on the dashboard with email $userName")
return@runBlocking null
}
return@runBlocking tempCreatedUser
}
}
Loading

0 comments on commit ea7381c

Please sign in to comment.