Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
fix: navigation crashing when open a invalid route and poptoview not …
Browse files Browse the repository at this point in the history
…working to first route. (#1027)

* refactor: remove deprecated from constructor

* fix: adjust crash when has invalid uri and adjust path to navigation

* adjust in navigation

* create method

* adjust to internal

* adjust internal

* change method
  • Loading branch information
uziasferreirazup authored Oct 13, 2020
1 parent 5e229f9 commit 19b3500
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ internal class HttpClientDefault : HttpClient, CoroutineScope {
private fun doHttpRequest(
request: RequestData
): ResponseData {
val urlConnection = request.uri.toURL().openConnection() as HttpURLConnection
val urlConnection: HttpURLConnection

try {
urlConnection = request.uri.toURL().openConnection() as HttpURLConnection
} catch (e: Exception) {
throw BeagleApiException(ResponseData(-1, data = byteArrayOf()), request)
}

request.headers.forEach {
urlConnection.setRequestProperty(it.key, it.value)
Expand Down Expand Up @@ -98,7 +104,6 @@ internal class HttpClientDefault : HttpClient, CoroutineScope {
return BeagleApiException(responseData, request)
}


private fun addRequestMethod(urlConnection: HttpURLConnection, method: HttpMethod) {
val methodValue = method.toString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package br.com.zup.beagle.android.utils

import br.com.zup.beagle.android.logger.BeagleMessageLogs
import br.com.zup.beagle.android.setup.BeagleEnvironment

internal fun String.toAndroidColor(): Int? = try {
ColorUtils.hexColor(this)
Expand All @@ -36,3 +37,5 @@ fun String.getExpressions(): List<String> {
}
return expressions
}

internal fun String.removeBaseUrl(): String = this.removePrefix(BeagleEnvironment.beagleSdk.config.baseUrl)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.fragment.app.FragmentActivity
import br.com.zup.beagle.android.action.Route
import br.com.zup.beagle.android.logger.BeagleLoggerProxy
import br.com.zup.beagle.android.setup.BeagleEnvironment
import br.com.zup.beagle.android.utils.removeBaseUrl
import br.com.zup.beagle.android.view.BeagleActivity
import br.com.zup.beagle.android.view.ScreenRequest
import br.com.zup.beagle.android.widget.RootView
Expand Down Expand Up @@ -86,7 +87,8 @@ internal object BeagleNavigator {

fun popToView(context: Context, route: String) {
if (context is AppCompatActivity) {
context.supportFragmentManager.popBackStack(route, 0)
val relativePath = route.removeBaseUrl()
context.supportFragmentManager.popBackStack(relativePath, 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import br.com.zup.beagle.android.components.layout.ScreenComponent
import br.com.zup.beagle.android.data.ComponentRequester
import br.com.zup.beagle.android.exception.BeagleException
import br.com.zup.beagle.android.logger.BeagleLoggerProxy
import br.com.zup.beagle.android.setup.BeagleEnvironment
import br.com.zup.beagle.android.utils.BeagleRetry
import br.com.zup.beagle.android.utils.CoroutineDispatchers
import br.com.zup.beagle.android.utils.removeBaseUrl
import br.com.zup.beagle.android.view.ScreenRequest
import br.com.zup.beagle.core.IdentifierComponent
import br.com.zup.beagle.core.ServerDrivenComponent
Expand Down Expand Up @@ -80,7 +82,8 @@ internal class BeagleViewModel(
try {
setLoading(true)
val component = componentRequester.fetchComponent(screenRequest)
postLiveDataResponse(ViewState.DoRender(screenRequest.url, component))
val relativePath = screenRequest.url.removeBaseUrl()
postLiveDataResponse(ViewState.DoRender(relativePath, component))
} catch (exception: BeagleException) {
if (screen != null) {
postLiveDataResponse(ViewState.DoRender(identifier, screen))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ class HttpClientDefaultTest {
})
}

@Test
fun `GIVEN request data with invalid uri when execute request THEN it should throw exception`() = runBlockingTest {
// Given
every { uri.toURL() } throws IllegalArgumentException("URI is not absolute")
val method = HttpMethod.GET
val requestData = RequestData(
uri = uri,
body = RandomData.string(),
method = method
)

// When
urlRequestDispatchingDefault.execute(requestData, onSuccess = {}, onError = {
// Then
assertEquals(-1, it.statusCode)
assertEquals(0, it.data.size)
assertEquals(0, it.headers.size)
})
}

@Test
fun execute_should_throw_IllegalStateException_when_data_is_set_for_HttpMethod_DELETE() = runBlockingTest {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ class BeagleNavigatorTest {
verify(exactly = once()) { context.supportFragmentManager.popBackStack(url, 0) }
}

@Test
fun `GIVEN pop to view with full url WHEN call pop to view THEN format url to relative()`() {
// Given
every { context.supportFragmentManager.popBackStack("/test", 0) } just Runs
every { BeagleEnvironment.beagleSdk.config.baseUrl } returns url
val url = "$url/test"

// When
BeagleNavigator.popToView(context, url)

// Then
verify(exactly = once()) { context.supportFragmentManager.popBackStack("/test", 0) }
}


@Test
fun pushStack_should_start_BeagleActivity() {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package br.com.zup.beagle.android.view.viewmodel

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import br.com.zup.beagle.android.BaseTest
import br.com.zup.beagle.android.action.Action
import br.com.zup.beagle.android.components.layout.ScreenComponent
import br.com.zup.beagle.android.data.ActionRequester
Expand Down Expand Up @@ -45,7 +46,7 @@ import org.junit.Rule
import org.junit.Test

@ExperimentalCoroutinesApi
class BeagleViewModelTest {
class BeagleViewModelTest : BaseTest() {

@get:Rule
val rule = InstantTaskExecutorRule()
Expand All @@ -70,11 +71,10 @@ class BeagleViewModelTest {

private lateinit var beagleUIViewModel: BeagleViewModel

private val slotViewState = mutableListOf<ViewState>()
private val slotViewState = mutableListOf<ViewState>()

@Before
fun setUp() {
MockKAnnotations.init(this)
override fun setUp() {
super.setUp();

beagleUIViewModel = BeagleViewModel(componentRequester = componentRequester)

Expand Down Expand Up @@ -144,7 +144,7 @@ class BeagleViewModelTest {
}

@Test
fun `GIVEN a ServerDrivenComponent WHEN fetchComponents called SHOULD post ViewState doRender `(){
fun `GIVEN a ServerDrivenComponent WHEN fetchComponents called SHOULD post ViewState doRender `() {
//GIVEN
val screenRequest = ScreenRequest("")

Expand All @@ -156,12 +156,12 @@ class BeagleViewModelTest {
}

@Test
fun `GIVEN a IdentifierComponent WHEN fetchComponents called SHOULD post ViewState doRender `(){
fun `GIVEN a IdentifierComponent WHEN fetchComponents called SHOULD post ViewState doRender `() {
//GIVEN
val screenRequest = ScreenRequest("")
val component : IdentifierComponent = mockk()
val component: IdentifierComponent = mockk()
val id = "id"
every {component.id} returns id
every { component.id } returns id

//WHEN
beagleUIViewModel.fetchComponent(screenRequest, component).observeForever(observer)
Expand All @@ -171,7 +171,7 @@ class BeagleViewModelTest {
}

@Test
fun `GIVEN a NULL ScreenComponent WHEN fetchComponents called SHOULD post ViewState doRender `(){
fun `GIVEN a NULL ScreenComponent WHEN fetchComponents called SHOULD post ViewState doRender `() {
//GIVEN
val screenRequest = ScreenRequest("url")

Expand All @@ -184,15 +184,29 @@ class BeagleViewModelTest {
}

@Test
fun `GIIVEN a ScreenComponent WHEN fetchComponent called SHOULD use identifier as screenId on ViewState doRender`(){
fun `GIVEN screen with full path WHEN fetchComponents called SHOULD post ViewState doRender with correct screen id`() {
//GIVEN
every { beagleSdk.config.baseUrl } returns "http://localhost:2020/"

val screenRequest = ScreenRequest("http://localhost:2020/test")

//WHEN
beagleUIViewModel.fetchComponent(screenRequest, null).observeForever(observer)

//THEN
verify(exactly = once()) { observer.onChanged(ViewState.DoRender("test", component)) }
}

@Test
fun `GIIVEN a ScreenComponent WHEN fetchComponent called SHOULD use identifier as screenId on ViewState doRender`() {
//Given
val screenRequest = ScreenRequest("")
val component : ScreenComponent = mockk()
val component: ScreenComponent = mockk()
val id = "id"
val identifier = "identifier"

every {component.id} returns id
every {component.identifier} returns identifier
every { component.id } returns id
every { component.identifier } returns identifier

//WHEN
beagleUIViewModel.fetchComponent(screenRequest, component).observeForever(observer)
Expand All @@ -201,4 +215,5 @@ class BeagleViewModelTest {
verify(exactly = once()) { observer.onChanged(ViewState.DoRender(identifier, component)) }

}

}

0 comments on commit 19b3500

Please sign in to comment.