Skip to content

Commit

Permalink
Rename Api in JniApi and restructure packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ivnsch committed Jun 25, 2020
1 parent 97b8473 commit 153d2b9
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.coepi.api
import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.coepi.core.jni.JniApi
import org.coepi.core.jni.JniLogCallback
import org.coepi.core.jni.JniVoidResult
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
Expand All @@ -27,8 +30,10 @@ class JNIInterfaceBootstrappedTests {
// we need to pass the db directory (without file name)
.absolutePath.removeSuffix("/remove")

val n = Api()
val result = n.bootstrapCore(dbPath, "debug", true, JniLogCallback())
val n = JniApi()
val result = n.bootstrapCore(dbPath, "debug", true,
JniLogCallback()
)
// Double check
assertEquals(JniVoidResult(1, ""), result)
}
Expand All @@ -43,178 +48,184 @@ class JNIInterfaceBootstrappedTests {

@Test
fun recordTcn() {
val value = Api().recordTcn("2485a64b57addcaea3ed1b538d07dbce")
val value = JniApi().recordTcn("2485a64b57addcaea3ed1b538d07dbce")
assertEquals(JniVoidResult(1, ""), value)
}

@Test
fun generateTcn() {
val value = Api().generateTcn()
val value = JniApi().generateTcn()
assertEquals(value.length, 32)
}

@Test
fun setSymptomIds() {
// NOTE: JSON format
val value =
Api().setSymptomIds("""["breathlessness", "muscle_aches", "runny_nose"]""")
JniApi().setSymptomIds("""["breathlessness", "muscle_aches", "runny_nose"]""")
assertEquals(JniVoidResult(1, ""), value)
}

@Test
fun setInvalidSymptomIdReturnsError() {
// NOTE: JSON format
val value = Api().setSymptomIds("""["not_supported", "muscle_aches", "runny_nose"]""")
val value = JniApi().setSymptomIds("""["not_supported", "muscle_aches", "runny_nose"]""")
// TODO https://github.com/Co-Epi/app-backend-rust/issues/79 shouldn't return 1
assertEquals(JniVoidResult(1, ""), value)
}

@Test
fun setInvalidSymptomIdsJsonReturnsError() {
val value = Api().setSymptomIds("sdjfhskdf")
val value = JniApi().setSymptomIds("sdjfhskdf")
// TODO https://github.com/Co-Epi/app-backend-rust/issues/79 shouldn't return 1
assertEquals(JniVoidResult(1, ""), value)
}

@Test
fun setCoughTypeNone() {
val result = Api().setCoughType("none")
val result = JniApi().setCoughType("none")
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setCoughTypeWet() {
val result = Api().setCoughType("wet")
val result = JniApi().setCoughType("wet")
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setCoughTypeDry() {
val result = Api().setCoughType("dry")
val result = JniApi().setCoughType("dry")
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setInvalidCoughTypeReturnsError() {
val result = Api().setCoughType("invalid")
val result = JniApi().setCoughType("invalid")
// TODO https://github.com/Co-Epi/app-backend-rust/issues/79 shouldn't return 1
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setCoughDaysIsSet() {
val result = Api().setCoughDays(1, 3)
val result = JniApi().setCoughDays(1, 3)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setCoughDaysIsNotSet() {
// Note: days is ignored
val result = Api().setCoughDays(0, 123)
val result = JniApi().setCoughDays(0, 123)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setCoughStatus() {
val result = Api().setCoughStatus("better_and_worse")
val result = JniApi().setCoughStatus("better_and_worse")
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setInvalidCoughStatusReturnsError() {
val result = Api().setCoughStatus("invalid")
val result = JniApi().setCoughStatus("invalid")
// TODO https://github.com/Co-Epi/app-backend-rust/issues/79 shouldn't return 1
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setBreathlessnessCause() {
val result = Api().setCoughStatus("leaving_house_or_dressing")
val result = JniApi().setCoughStatus("leaving_house_or_dressing")
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setInvalidBreathlessnessCauseReturnsError() {
val result = Api().setCoughStatus("invalid")
val result = JniApi().setCoughStatus("invalid")
// TODO https://github.com/Co-Epi/app-backend-rust/issues/79 shouldn't return 1
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setFeverDaysIsSet() {
val result = Api().setFeverDays(1, 3)
val result = JniApi().setFeverDays(1, 3)
// TODO https://github.com/Co-Epi/app-backend-rust/issues/79 shouldn't return 1
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setFeverDaysNone() {
// Note: days is ignored
val result = Api().setFeverDays(0, 3)
val result = JniApi().setFeverDays(0, 3)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setFeverTakenTemperatureToday() {
val result = Api().setFeverTakenTemperatureToday(1, 3)
val result = JniApi()
.setFeverTakenTemperatureToday(1, 3)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setFeverTakenTemperatureTodayNone() {
// Note: days is ignored
val result = Api().setFeverTakenTemperatureToday(0, 3)
val result = JniApi()
.setFeverTakenTemperatureToday(0, 3)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setFeverTakenTemperatureSpot() {
val result = Api().setFeverTakenTemperatureSpot("armpit")
val result = JniApi().setFeverTakenTemperatureSpot("armpit")
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setInvalidFeverTakenTemperatureSpot() {
val result = Api().setFeverTakenTemperatureSpot("invalid")
val result = JniApi().setFeverTakenTemperatureSpot("invalid")
// TODO https://github.com/Co-Epi/app-backend-rust/issues/79 shouldn't return 1
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setHigherFeverTemperatureTaken() {
val result = Api().setFeverHighestTemperatureTaken(1, 100f)
val result = JniApi()
.setFeverHighestTemperatureTaken(1, 100f)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setHigherFeverTemperatureTakenNone() {
// Note: temp is ignored
val result = Api().setFeverHighestTemperatureTaken(0, 100f)
val result = JniApi()
.setFeverHighestTemperatureTaken(0, 100f)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setEarliestSymptomStartedDaysAgo() {
val result = Api().setEarliestSymptomStartedDaysAgo(1, 10)
val result = JniApi()
.setEarliestSymptomStartedDaysAgo(1, 10)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun setEarliestSymptomStartedDaysAgoNone() {
// Note: days is ignored
val result = Api().setEarliestSymptomStartedDaysAgo(0, 10)
val result = JniApi()
.setEarliestSymptomStartedDaysAgo(0, 10)
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun clearSymptoms() {
val result = Api().clearSymptoms()
val result = JniApi().clearSymptoms()
assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun submitSymptoms() {
val result = Api().submitSymptoms()
val result = JniApi().submitSymptoms()
assertEquals(JniVoidResult(1, ""), result)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package org.coepi.api
import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.coepi.core.jni.JniApi
import org.coepi.core.jni.JniAlert
import org.coepi.core.jni.JniAlertsArrayResult
import org.coepi.core.jni.JniLogCallback
import org.coepi.core.jni.JniOneAlertResult
import org.coepi.core.jni.JniPublicReport
import org.coepi.core.jni.JniVoidResult
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -30,15 +37,17 @@ class JNIInterfaceTests {
// we need to pass the db directory (without file name)
.absolutePath.removeSuffix("/remove")

val n = Api()
val result = n.bootstrapCore(dbPath, "debug", true, JniLogCallback())
val n = JniApi()
val result = n.bootstrapCore(dbPath, "debug", true,
JniLogCallback()
)

assertEquals(JniVoidResult(1, ""), result)
}

@Test
fun testFetchAReport() {
val n = Api()
val n = JniApi()
val value = n.testReturnAnAlert()
assertEquals(
JniOneAlertResult(
Expand All @@ -64,7 +73,7 @@ class JNIInterfaceTests {

@Test
fun testFetchNewReports() {
val n = Api()
val n = JniApi()
val value = n.testReturnMultipleAlerts()
assertEquals(
JniAlertsArrayResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.test.runBlockingTest
import org.coepi.core.jni.JniApi
import org.coepi.core.jni.Callback
import org.coepi.core.jni.FFINestedParameterStruct
import org.coepi.core.jni.FFIParameterStruct
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -20,14 +24,14 @@ class JniBasicTests {

@Test
fun testSendReceiveString() {
val n = Api()
val n = JniApi()
val value = n.sendReceiveString("world")
assertEquals("Hello world!", value)
}

@Test
fun testSendStruct() {
val n = Api()
val n = JniApi()
val myStruct = FFIParameterStruct(
123,
"hi from Android",
Expand All @@ -39,7 +43,7 @@ class JniBasicTests {

@Test
fun testReturnStruct() {
val n = Api()
val n = JniApi()
val value = n.returnStruct()
assertEquals(
value,
Expand All @@ -53,7 +57,7 @@ class JniBasicTests {
@ExperimentalCoroutinesApi
@Test
fun testCallCallback() = runBlockingTest {
val n = Api()
val n = JniApi()
val result = suspendCancellableCoroutine<String> { continuation ->
n.callCallback(object : Callback() {
override fun call(string: String) {
Expand All @@ -66,7 +70,7 @@ class JniBasicTests {

@Test
fun testRegisterCallback() = runBlocking {
val n = Api()
val n = JniApi()
val result = suspendCancellableCoroutine<String> { continuation ->
n.registerCallback(object : Callback() {
override fun call(string: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.coepi.api
package org.coepi.core.jni

import android.content.Context
import org.coepi.core.domain.common.Result
import org.coepi.core.domain.common.Result.Failure
import org.coepi.core.domain.common.Result.Success

class Api {
class JniApi {

init {
System.loadLibrary("coepi_core")
Expand All @@ -23,6 +23,7 @@ class Api {

external fun recordTcn(tcn: String): JniVoidResult

// TODO test:
external fun setBreathlessnessCause(cause: String): JniVoidResult

external fun setCoughDays(isSet: Int, days: Int): JniVoidResult
Expand Down Expand Up @@ -162,15 +163,16 @@ private fun statusDescription(status: Int, message: String): String =
"Status: $status Message: $message"

fun bootstrap(applicationContext: Context) {
val nativeApi = Api()
val nativeApi = JniApi()

// getDatabasePath requires a db name, but we use need the directory
// (to initialize multiple databases), so adding and removing a suffix.
val dbPath = applicationContext.getDatabasePath("remove")
.absolutePath.removeSuffix("/remove")

val result = nativeApi.bootstrapCore(dbPath, "debug", true,
JniLogCallback())
JniLogCallback()
)
if (result.status != 1) {
error("Couldn't bootstrap core: status: ${result.status}, message: ${result.message}")
}
Expand Down
Loading

0 comments on commit 153d2b9

Please sign in to comment.