adds rest communication

- test cases for push and pop
- rest singleton instantiated in MainApplication
  access through context
- pop and peek work, push needs encryption process
  still
This commit is contained in:
2021-12-16 12:21:17 +01:00
parent 3d42f0ca2e
commit 10ba4cf31b
8 changed files with 283 additions and 57 deletions

View File

@ -1,12 +1,17 @@
package com.maenle.bump
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.bump.MessageProcessor
import com.example.bump.RestSingleton
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/**
* Instrumented test, which will execute on an Android device.
@ -21,4 +26,83 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.maenle.bump", appContext.packageName)
}
}
}
@RunWith(AndroidJUnit4::class)
class MessageProcessorTest {
@Test
fun decryptMessages() {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
val messageRaw = "M1dEAxKZ5HUHCJoRkgGOvAABhqCAAAAAAGG2eKTSlKXWLDQx5B_wssZsNwsanzQID2UyUm4KKuKYKgfwH5MG2N-qzt6K4mg3pfZmWPaiDB9PiqlX236k6zo9Yvvq"
val data = decryptMessage(code, messageRaw)
assertEquals(data, "hello")
}
companion object {
@JvmStatic
fun decryptMessage(code: String, messageRaw: String): String {
val message = MessageProcessor(code)
val data = message.decrypt(messageRaw)
return data
}
}
}
@RunWith(AndroidJUnit4::class)
class RestTest{
@Test
fun getPeek() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val sender = "dydoes-unknowledgeable-indiscretion-househusbands"
RestSingleton.getInstance(context).peek(sender) { i -> assertEquals(i, "hello") }
}
}
class IntegrationTest{
@Test
fun pushPopAndDecrypt() {
val testMessage = "Hi There Sir"
val sender = "dydoes-unknowledgeable-indiscretion-househusbands"
val context = InstrumentationRegistry.getInstrumentation().targetContext
val lock = CountDownLatch(1);
fun messageTester(encrypted: String) {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
Log.d("TEST", encrypted)
val message = MessageProcessor(code)
val data = message.decrypt(encrypted)
Log.d("TEST", "data")
assertEquals(data, testMessage)
lock.countDown()
}
RestSingleton.getInstance(context).push(sender, testMessage) {
RestSingleton.getInstance(context).pop(sender) { i -> messageTester(i) }
}
Log.d("TEST", "waiting")
lock.await(20000, TimeUnit.MILLISECONDS)
Log.d("TEST", "done")
}
@Test
fun popAndDectypt() {
val testMessage = "Hi There Sir"
val sender = "raphael"
val context = InstrumentationRegistry.getInstrumentation().targetContext
val lock = CountDownLatch(1);
fun messageTester(encrypted: String) {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
val message = MessageProcessor(code)
val data = message.decrypt(encrypted)
assertEquals(data, testMessage)
lock.countDown()
}
RestSingleton.getInstance(context).pop(sender) { i -> messageTester(i) }
lock.await(20000, TimeUnit.MILLISECONDS)
}
}