adds list functionality

- adds rest connection with gets a JSON Object
  with a "messages" list inside it.
- test case, which pushes a new message
  and gets an entire list. checking that the
  first element is "hello" and the last element
  to be the new element just pushed
This commit is contained in:
Raphael Maenle 2021-12-17 16:10:29 +01:00
parent c46c664cdb
commit 7e4cfceab4
2 changed files with 61 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package com.example.bump
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import org.junit.Test import org.junit.Test
@ -38,10 +39,10 @@ class RestCryptTest{
val lock = CountDownLatch(1) val lock = CountDownLatch(1)
fun messageTester(messageEncrypted: String) { fun messageTester(messageEncrypted: JSONObject) {
val message = MessageProcessor(code) val message = MessageProcessor(code)
val messageData: String = JSONObject(messageEncrypted).get("data").toString() val messageData: String = messageEncrypted.get("data").toString()
assertEquals(messageData, encrypted) assertEquals(messageData, encrypted)
val data = message.decrypt(messageData) val data = message.decrypt(messageData)
assertEquals(data, testMessage) assertEquals(data, testMessage)
@ -55,8 +56,42 @@ class RestCryptTest{
} }
lock.await(200000, TimeUnit.MILLISECONDS) lock.await(200000, TimeUnit.MILLISECONDS)
}
@Test
fun pushListAndDecrypt() {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
val testMessage = getRandomString(32)
val context = InstrumentationRegistry.getInstrumentation().targetContext
var encrypted = ""
val lock = CountDownLatch(1)
fun listTester(resultList: JSONObject) {
val message = MessageProcessor(code)
val messages: JSONArray = resultList.get("messages") as JSONArray
var messageEncoded = messages[0] as JSONObject
var messageData = messageEncoded.get("data").toString()
var data = message.decrypt(messageData)
assertEquals(data, "hello")
messageEncoded = messages[messages.length()-1] as JSONObject
messageData = messageEncoded.get("data").toString()
data = message.decrypt(messageData)
assertEquals(data, testMessage)
lock.countDown()
}
val message = MessageProcessor(code)
encrypted = message.encrypt(testMessage)
RestSingleton.getInstance(context).push(message.sender, encrypted) {
RestSingleton.getInstance(context).list(message.sender) { i -> listTester(i) }
}
lock.await(200000, TimeUnit.MILLISECONDS)
} }
} }

View File

@ -6,6 +6,7 @@ import com.android.volley.Request
import com.android.volley.RequestQueue import com.android.volley.RequestQueue
import com.android.volley.toolbox.* import com.android.volley.toolbox.*
import com.maenle.bump.MainActivity import com.maenle.bump.MainActivity
import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
@ -28,14 +29,31 @@ class RestSingleton constructor(context: Context){
private const val URL = "http://192.168.68.127:4000/api/" private const val URL = "http://192.168.68.127:4000/api/"
} }
fun peek(sender: String, callback: (String) -> Unit){ fun list(sender: String, callback: (JSONObject) -> Unit){
val url = URL + "list/"
val data = JSONObject()
data.put("sender", sender)
data.put("minutes", (60*24*365*100).toString())
val jsonRequest = JsonObjectRequest(Request.Method.POST, url,
data,
{ response ->
run {
callback(response)
}
} ,
{callback(JSONObject())})
requestQueue.add(jsonRequest)
}
fun peek(sender: String, callback: (JSONObject) -> Unit){
val url = URL + "peek/" val url = URL + "peek/"
val data = JSONObject() val data = JSONObject()
data.put("sender", sender) data.put("sender", sender)
val jsonRequest = JsonObjectRequest(Request.Method.POST, url, val jsonRequest = JsonObjectRequest(Request.Method.POST, url,
data, data,
{ response -> callback(response.toString()) }, { response -> callback(JSONObject(response.toString())) },
{callback("")}) {callback(JSONObject())})
requestQueue.add(jsonRequest) requestQueue.add(jsonRequest)
} }
@ -54,14 +72,14 @@ class RestSingleton constructor(context: Context){
requestQueue.add(stringRequest) requestQueue.add(stringRequest)
} }
fun pop(sender: String,callback: (String) -> Unit){ fun pop(sender: String,callback: (JSONObject) -> Unit){
val url = URL + "pop/" val url = URL + "pop/"
val data = JSONObject() val data = JSONObject()
data.put("sender", sender) data.put("sender", sender)
val stringRequest = JsonObjectRequest(Request.Method.POST, url, val stringRequest = JsonObjectRequest(Request.Method.POST, url,
data, data,
{ response -> callback(response.toString()) }, { response -> callback(JSONObject(response.toString())) },
{callback("")}) {callback(JSONObject())})
requestQueue.add(stringRequest) requestQueue.add(stringRequest)
} }
@ -72,7 +90,4 @@ class RestSingleton constructor(context: Context){
// Activity or BroadcastReceiver if someone passes one in. // Activity or BroadcastReceiver if someone passes one in.
Volley.newRequestQueue(context.applicationContext) Volley.newRequestQueue(context.applicationContext)
} }
fun <T> addToRequestQueue(req: Request<T>) {
requestQueue.add(req)
}
} }