Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.formbricks.android.network

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.formbricks.android.model.user.PostUserBody
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class FormbricksApiServiceInstrumentedTest {
private lateinit var context: Context
private lateinit var apiService: FormbricksApiService

@Before
fun setUp() {
context = ApplicationProvider.getApplicationContext()
apiService = FormbricksApiService()
// You may want to initialize with a test server or mock URL
apiService.initialize("https://example.com", isLoggingEnabled = false)
}

@Test
fun testInitialization() {
// This test just verifies that initialization does not throw
try {
apiService.initialize("https://example.com", isLoggingEnabled = false)
} catch (e: Exception) {
fail("Initialization should not throw: ${e.message}")
}
}

@Test
fun testGetEnvironmentStateObject_handlesErrorGracefully() {
val result = apiService.getEnvironmentStateObject("dummy-environment-id")
assertTrue(result.isFailure)
result.exceptionOrNull()?.let { e ->
println("Exception caught as expected: ${e.message}")
}
}

@Test
fun testPostUser_handlesErrorGracefully() {
// This should fail gracefully since the URL is unreachable
val dummyBody = PostUserBody("dummy-user-id", null)
val result = apiService.postUser("dummy-environment-id", dummyBody)
assertTrue(result.isFailure)
}

// Add more integration-style tests as needed, e.g.:
// - testGetEnvironmentStateObject_withMockServer
// - testPostUser_withMockServer
// These would require a running test server or a mock web server
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ open class FormbricksApiService {
}

open fun getEnvironmentStateObject(environmentId: String): Result<EnvironmentDataHolder> {
val result = execute {
retrofit.create(FormbricksService::class.java)
.getEnvironmentState(environmentId)
return try {
val result = execute {
retrofit.create(FormbricksService::class.java)
.getEnvironmentState(environmentId)
}
val json = Json { ignoreUnknownKeys = true }
val resultMap = result.getOrThrow()
val resultJson = mapToJsonElement(resultMap).jsonObject
val environmentResponse = json.decodeFromJsonElement<EnvironmentResponse>(resultJson)
val data = EnvironmentDataHolder(environmentResponse.data, resultMap)
Result.success(data)
} catch (e: Exception) {
Result.failure(e)
}
val json = Json { ignoreUnknownKeys = true }
val resultMap = result.getOrThrow()
val resultJson = mapToJsonElement(resultMap).jsonObject
val environmentResponse = json.decodeFromJsonElement<EnvironmentResponse>(resultJson)
val data = EnvironmentDataHolder(environmentResponse.data, resultMap)
return Result.success(data)
}

open fun postUser(environmentId: String, body: PostUserBody): Result<UserResponse> {
Expand Down
3 changes: 2 additions & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ sonar.coverage.exclusions=**/test/**/*,**/androidTest/**/*,**/*.js,**/*.json,\
**/FormbricksAPIError.kt,\
**/Logger.kt,\
**/Guard.kt,\
**/DateExtensions.kt
**/DateExtensions.kt,\
**/webview/FormbricksFragment.kt

# Debug
sonar.verbose=true
Loading