Skip to content

Commit

Permalink
cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaThorne committed Mar 24, 2024
1 parent b0d38df commit f3c9842
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
14 changes: 12 additions & 2 deletions sdk/src/main/java/com/thorne/sdk/MedSdkImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ class MedSdkImpl private constructor() : MedSdk {
companion object {
// Private reference to the singleton instance

// Suppress warning because we're only ever storing application context
// Suppress warning because we're only ever keeping application context,
// which doesn't change, so we're not creating a memory leak
@SuppressLint("StaticFieldLeak")

@Volatile
private var INSTANCE: MedSdkImpl? = null

private var isInitialized = false

// Buffer for storage - in-memory storage,
// loaded from disk on initialization
// and saved to disk on any changes
private var medicationList = ArrayList<Medication>()
private val storageManager = MedicationStorageManagerImpl()

// Storage manager
private var storageManager = MedicationStorageManagerImpl()

// Function to get or create the singleton instance
fun getInstance(): MedSdkImpl {
Expand Down Expand Up @@ -124,4 +130,8 @@ class MedSdkImpl private constructor() : MedSdk {
medicationList = storageManager.loadFromStorage(context!!)
}
}

internal fun setCustomStorageManager(customStorageManager: MedicationStorageManagerImpl) {
storageManager = customStorageManager
}
}
60 changes: 35 additions & 25 deletions sdk/src/test/java/com/thorne/sdk/MedSdkImplTest.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
package com.thorne.sdk

import android.content.Context
import android.test.mock.MockContext
import com.thorne.sdk.meds.Medication
import com.thorne.sdk.meds.MedicationImpl
import com.thorne.sdk.storage.MedicationStorageManagerImpl
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.any
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`



class MedSdkImplTest {

private lateinit var medSdkImpl: MedSdkImpl
private lateinit var mockMedication: Medication
private lateinit var mockMedication: MedicationImpl
private lateinit var mockStorageManager: MedicationStorageManagerImpl
private lateinit var mockContext: Context

@Before
fun setUp() {
mockMedication = mockk<MedicationImpl>()
mockStorageManager = mock(MedicationStorageManagerImpl::class.java)
mockContext = mock(Context::class.java)
medSdkImpl = MedSdkImpl.getInstance()
medSdkImpl.initialize(mockContext)
}
@Before
fun setUp() {
// Mock Medication
mockMedication = mockk<MedicationImpl>()
every { mockMedication.getId() } returns "testId"

// Mock StorageManager
mockStorageManager = mockk<MedicationStorageManagerImpl>()
every { mockStorageManager.isEmpty(any()) } returns true
every { mockStorageManager.loadFromStorage(any()) } returns ArrayList()
every { mockStorageManager.saveToStorage(any(), any()) } returns Unit

// Mock Context
mockContext = mockk<MockContext>()
every { mockContext.applicationContext } returns mockContext

// Create MedSdkImpl instance
medSdkImpl = spyk(MedSdkImpl.getInstance())
medSdkImpl.setCustomStorageManager(mockStorageManager)
medSdkImpl.initialize(mockContext)
}

@Test
fun testIsInitialized() {
Expand All @@ -41,34 +52,33 @@ fun setUp() {
@Test
fun testAddMedication() {
medSdkImpl.addMedication(mockMedication)
verify(mockStorageManager).saveToStorage(any(), any())
verify(exactly = 1) { mockStorageManager.saveToStorage(any(), any()) }
}

@Test
fun testRemoveMedication() {
medSdkImpl.removeMedication(mockMedication)
verify(mockStorageManager).saveToStorage(any(), any())
verify(exactly = 1) { mockStorageManager.saveToStorage(any(), any()) }
}

@Test
fun testRemoveMedicationById() {
`when`(mockMedication.getId()).thenReturn("testId")
medSdkImpl.addMedication(mockMedication)
medSdkImpl.removeMedication("testId")
verify(mockStorageManager, times(2)).saveToStorage(any(), any())
verify(exactly = 1) { mockStorageManager.saveToStorage(any(), any()) }
}

@Test
fun testGetMedicationById() {
`when`(mockMedication.getId()).thenReturn("testId")
medSdkImpl.addMedication(mockMedication)
assertEquals(mockMedication, medSdkImpl.getMedicationById("testId"))
// get medication from in-memory buffer
assertTrue(medSdkImpl.getMedicationById("testId") is Medication)
verify(exactly = 0) { mockStorageManager.saveToStorage(any(), any()) }
}

@Test
fun testGetMedicationList() {
medSdkImpl.addMedication(mockMedication)
assertEquals(1, medSdkImpl.getMedicationList().size)
// get medication list from in-memory buffer
assertEquals(ArrayList<Medication>()::class, medSdkImpl.getMedicationList()::class)
verify(exactly = 0) { mockStorageManager.saveToStorage(any(), any()) }
}

@Test
Expand Down

0 comments on commit f3c9842

Please sign in to comment.