From d7a28bf71314fb39e1d27a86fe3dbd4b9f304736 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 14 Jul 2019 16:11:27 +0100 Subject: [PATCH] Session test --- .../plugins/general/tidepool/comm/Session.kt | 6 +-- .../general/tidepool/comm/TidepoolUploader.kt | 9 ++++- .../general/tidepool/comm/SessionTest.kt | 37 +++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/tidepool/comm/SessionTest.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/Session.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/Session.kt index 684f66f543..7067ddd073 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/Session.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/Session.kt @@ -4,9 +4,9 @@ import info.nightscout.androidaps.plugins.general.tidepool.messages.AuthReplyMes import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReplyMessage import okhttp3.Headers -class Session(var authHeader: String?, private var sessionTokenHeader: String) { - - val service = TidepoolUploader.getRetrofitInstance()?.create(TidepoolApiService::class.java) +class Session(val authHeader: String?, + private val sessionTokenHeader: String, + val service: TidepoolApiService?) { internal var token: String? = null internal var authReply: AuthReplyMessage? = null diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt index 32f2f1e00b..891e091574 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt @@ -63,6 +63,11 @@ object TidepoolUploader { return retrofit } + fun createSession(): Session { + val service = getRetrofitInstance()?.create(TidepoolApiService::class.java) + return Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER, service) + } + // TODO: call on preference change fun resetInstance() { retrofit = null @@ -80,7 +85,7 @@ object TidepoolUploader { } // TODO failure backoff extendWakeLock(30000) - session = Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER) + session = createSession() val authHeader = session?.authHeader if (authHeader != null) { connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTING @@ -104,7 +109,7 @@ object TidepoolUploader { } fun testLogin(rootContext: Context) { - val session = Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER) + val session = createSession() session.authHeader?.let { val call = session.service?.getLogin(it) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/tidepool/comm/SessionTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/tidepool/comm/SessionTest.kt new file mode 100644 index 0000000000..b172447cec --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/tidepool/comm/SessionTest.kt @@ -0,0 +1,37 @@ +package info.nightscout.androidaps.plugins.general.tidepool.comm + +import info.nightscout.androidaps.plugins.general.tidepool.messages.AuthReplyMessage +import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReplyMessage +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +class SessionTest { + @Test + fun populateBody() { + val session = Session("", "", null) + assertNull(session.authReply) + + // test authReply + val authReplyMessage = AuthReplyMessage() + session.populateBody(authReplyMessage) + assertEquals(authReplyMessage, session.authReply) + + // test datasetReply + val datasetReplyMessage = DatasetReplyMessage() + assertNull(session.datasetReply) + session.populateBody(datasetReplyMessage) + assertEquals(datasetReplyMessage, session.datasetReply) + + // test datasetReply as array + val list: List = listOf(datasetReplyMessage) + session.datasetReply = null + assertNull(session.datasetReply) + session.populateBody(list) + assertEquals(datasetReplyMessage, session.datasetReply) + + } +} \ No newline at end of file