Skip to content

Commit 4faf444

Browse files
author
crypticminds
committed
"[Test] Added test dependencies and unit test for cache"
1 parent 5a9f964 commit 4faf444

File tree

6 files changed

+145
-23
lines changed

6 files changed

+145
-23
lines changed

app/src/main/java/com/arcane/coldstorage/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.arcane.coldstorage.cache.ImageCache
1111
import com.arcane.coldstoragecache.callback.OnValueFetchedCallback
1212
import com.arcane.coldstoragecache.converter.impl.StringToBitmapConverter
1313

14-
class MainActivity : AppCompatActivity(), OnValueFetchedCallback<Any?> {
14+
class MainActivity : AppCompatActivity(), OnValueFetchedCallback<Bitmap?> {
1515

1616
companion object {
1717
val URLS = arrayListOf(
@@ -81,7 +81,7 @@ class MainActivity : AppCompatActivity(), OnValueFetchedCallback<Any?> {
8181
* When the image is downloaded , adding the image to
8282
* the image view.
8383
*/
84-
override fun valueFetched(output: Any?) {
84+
override fun valueFetched(output: Bitmap?) {
8585
imageCache.commitToSharedPref(applicationContext)
8686
runOnUiThread {
8787
val outputAsBitmap = output as Bitmap

coldstoragecache/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ dependencies {
3131
implementation 'androidx.appcompat:appcompat:1.1.0'
3232
implementation 'androidx.core:core-ktx:1.1.0'
3333
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.10.1"
34+
// Required -- JUnit 4 framework
3435
testImplementation 'junit:junit:4.12'
36+
// Optional -- Robolectric environment
37+
testImplementation 'androidx.test:core:1.2.0'
38+
// Optional -- Mockito framework
39+
testImplementation 'org.mockito:mockito-core:2.19.0'
40+
//mockk library
41+
testImplementation "io.mockk:mockk:1.9.3"
42+
3543
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3644
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
3745
androidTestImplementation 'org.mockito:mockito-core:2.19.0'

coldstoragecache/src/androidTest/java/com/arcane/coldstoragecache/cache/CacheTest.kt

Lines changed: 0 additions & 16 deletions
This file was deleted.

coldstoragecache/src/main/java/com/arcane/coldstoragecache/cache/Cache.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ abstract class Cache {
211211
* needs to be considered stale.(In milliseconds)
212212
*
213213
*/
214-
fun get(
214+
fun <Output> get(
215215
key: String,
216-
onValueFetchedCallback: OnValueFetchedCallback<Any?>,
217-
converter: IConverter<Any?>,
216+
onValueFetchedCallback: OnValueFetchedCallback<Output?>,
217+
converter: IConverter<Output?>,
218218
timeToLive: Long? = null
219219
) {
220220
thread {
@@ -275,7 +275,7 @@ abstract class Cache {
275275
*
276276
* @param timeToLive the time after which the object will be considered stale.
277277
*/
278-
fun addToCache(key: String, objectToCache: Any, timeToLive: Long?) {
278+
fun addToCache(key: String, objectToCache: Any, timeToLive: Long? = null) {
279279
val objectAsString = objectMapper.writeValueAsString(objectToCache)
280280
cache[key] = CachedDataModel(
281281
objectAsString,

coldstoragecache/src/main/java/com/arcane/coldstoragecache/converter/impl/StringToBitmapConverter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.arcane.coldstoragecache.converter.IConverter
1212
*
1313
* @author Anurag
1414
*/
15-
class StringToBitmapConverter : IConverter<Any?> {
15+
class StringToBitmapConverter : IConverter<Bitmap?> {
1616

1717
/**
1818
* The function will convert the string stored in the cache
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.arcane.coldstoragecache.cache
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
import android.util.Log
6+
import com.arcane.coldstoragecache.callback.OnValueFetchedCallback
7+
import com.arcane.coldstoragecache.model.CachedDataModel
8+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
9+
import io.mockk.every
10+
import io.mockk.mockkStatic
11+
import org.junit.Assert
12+
import org.junit.Before
13+
import org.junit.Test
14+
import org.mockito.Mockito
15+
16+
class CacheTest {
17+
18+
private lateinit var cacheWithString: Cache
19+
20+
private val testKey = "key"
21+
22+
23+
@Before
24+
fun resetValues() {
25+
cacheWithString = CacheWithStringValue()
26+
mockkStatic(Log::class)
27+
every { Log.v(any(), any()) } returns 0
28+
every { Log.d(any(), any()) } returns 0
29+
every { Log.i(any(), any()) } returns 0
30+
every { Log.e(any(), any()) } returns 0
31+
}
32+
33+
@Test
34+
fun testGetFromCacheWithCacheHit() {
35+
cacheWithString.addToCache(testKey, "testValue")
36+
val onValueFetchedCallback = object : OnValueFetchedCallback<String?> {
37+
override fun valueFetched(output: String?) {
38+
Assert.assertNotNull(output)
39+
Assert.assertEquals(output!!, "testValue")
40+
}
41+
}
42+
cacheWithString.get(testKey, onValueFetchedCallback)
43+
}
44+
45+
@Test
46+
fun testGetFromCacheWithCacheMiss() {
47+
val onValueFetchedCallback = object : OnValueFetchedCallback<String?> {
48+
override fun valueFetched(output: String?) {
49+
Assert.assertNotNull(output)
50+
Assert.assertEquals(output!!, "key2")
51+
}
52+
}
53+
cacheWithString.get(testKey, onValueFetchedCallback)
54+
}
55+
56+
@Test
57+
fun testInitializeMethod() {
58+
val allMap = hashMapOf<String, String>()
59+
val applicationContext = Mockito.mock(Context::class.java)
60+
val sharedPreferences = Mockito.mock(SharedPreferences::class.java)
61+
Mockito.`when`(
62+
applicationContext.getSharedPreferences(
63+
Mockito.anyString(),
64+
Mockito.anyInt()
65+
)
66+
).thenReturn(sharedPreferences)
67+
mockDataInSharedPref(sharedPreferences, allMap)
68+
Cache.initialize(applicationContext)
69+
Thread.sleep(1000)
70+
71+
for (i in allMap.keys) {
72+
val value = cacheWithString.getWithoutUpdate(i)
73+
if (i.contains("Present")) {
74+
Assert.assertEquals(i, "TestObject", value)
75+
} else {
76+
Assert.assertNull(value)
77+
}
78+
79+
}
80+
}
81+
82+
83+
private fun mockDataInSharedPref(
84+
sharedPreferences: SharedPreferences,
85+
allMap: HashMap<String, String>
86+
) {
87+
88+
val mapper = jacksonObjectMapper()
89+
for (i in 1..5) {
90+
val cachedDataModel = CachedDataModel(
91+
"TestObject",
92+
System.currentTimeMillis(), 2000
93+
)
94+
val modelAsString = mapper.writeValueAsString(cachedDataModel)
95+
Mockito.`when`(
96+
sharedPreferences.getString(
97+
"Present$i",
98+
""
99+
)
100+
).thenReturn(modelAsString)
101+
allMap["Present$i"] = modelAsString
102+
103+
}
104+
for (i in 1..10) {
105+
val cachedDataModel = CachedDataModel(
106+
"TestObject",
107+
System.currentTimeMillis(), 10
108+
)
109+
val modelAsString = mapper.writeValueAsString(cachedDataModel)
110+
Mockito.`when`(
111+
sharedPreferences.getString(
112+
"Absent$i",
113+
""
114+
)
115+
).thenReturn(modelAsString)
116+
allMap["Absent$i"] = modelAsString
117+
}
118+
Mockito.`when`(sharedPreferences.all).thenReturn(allMap)
119+
}
120+
121+
122+
class CacheWithStringValue : Cache() {
123+
override fun update(key: String): String? {
124+
return "key2"
125+
}
126+
127+
}
128+
129+
130+
}

0 commit comments

Comments
 (0)