From 7e4cfceab4b29c559fddfa2dcf2367fbc58d5ee2 Mon Sep 17 00:00:00 2001 From: raphael Date: Fri, 17 Dec 2021 16:10:29 +0100 Subject: [PATCH] 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 --- .../java/com/example/bump/RestCryptTest.kt | 39 ++++++++++++++++++- .../java/com/example/bump/RestSingleton.kt | 33 +++++++++++----- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/app/src/androidTest/java/com/example/bump/RestCryptTest.kt b/app/src/androidTest/java/com/example/bump/RestCryptTest.kt index b0f54cf..72073d6 100644 --- a/app/src/androidTest/java/com/example/bump/RestCryptTest.kt +++ b/app/src/androidTest/java/com/example/bump/RestCryptTest.kt @@ -2,6 +2,7 @@ package com.example.bump import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.json.JSONArray import org.json.JSONObject import org.junit.Test @@ -38,10 +39,10 @@ class RestCryptTest{ val lock = CountDownLatch(1) - fun messageTester(messageEncrypted: String) { + fun messageTester(messageEncrypted: JSONObject) { val message = MessageProcessor(code) - val messageData: String = JSONObject(messageEncrypted).get("data").toString() + val messageData: String = messageEncrypted.get("data").toString() assertEquals(messageData, encrypted) val data = message.decrypt(messageData) assertEquals(data, testMessage) @@ -55,8 +56,42 @@ class RestCryptTest{ } 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) } } diff --git a/app/src/main/java/com/example/bump/RestSingleton.kt b/app/src/main/java/com/example/bump/RestSingleton.kt index a89d17e..53b39d1 100644 --- a/app/src/main/java/com/example/bump/RestSingleton.kt +++ b/app/src/main/java/com/example/bump/RestSingleton.kt @@ -6,6 +6,7 @@ import com.android.volley.Request import com.android.volley.RequestQueue import com.android.volley.toolbox.* import com.maenle.bump.MainActivity +import org.json.JSONArray import org.json.JSONObject @@ -28,14 +29,31 @@ class RestSingleton constructor(context: Context){ 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 data = JSONObject() data.put("sender", sender) val jsonRequest = JsonObjectRequest(Request.Method.POST, url, data, - { response -> callback(response.toString()) }, - {callback("")}) + { response -> callback(JSONObject(response.toString())) }, + {callback(JSONObject())}) requestQueue.add(jsonRequest) } @@ -54,14 +72,14 @@ class RestSingleton constructor(context: Context){ requestQueue.add(stringRequest) } - fun pop(sender: String,callback: (String) -> Unit){ + fun pop(sender: String,callback: (JSONObject) -> Unit){ val url = URL + "pop/" val data = JSONObject() data.put("sender", sender) val stringRequest = JsonObjectRequest(Request.Method.POST, url, data, - { response -> callback(response.toString()) }, - {callback("")}) + { response -> callback(JSONObject(response.toString())) }, + {callback(JSONObject())}) requestQueue.add(stringRequest) } @@ -72,7 +90,4 @@ class RestSingleton constructor(context: Context){ // Activity or BroadcastReceiver if someone passes one in. Volley.newRequestQueue(context.applicationContext) } - fun addToRequestQueue(req: Request) { - requestQueue.add(req) - } } \ No newline at end of file