11package ge.tbcbank.retrocache
22
3+ import okhttp3.Protocol
34import org.junit.Assert.assertEquals
45import org.junit.Assert.assertNotNull
56import org.junit.Assert.assertNull
@@ -12,7 +13,7 @@ class RetroCacheManagerTest {
1213 @Test
1314 fun `get returns cached object when present` () {
1415 val cacheManager = RetroCacheManager .Builder ().build()
15- val model = RetroCacheValue ( " test " , " 123 " )
16+ val model = getRetroCacheModel( )
1617 cacheManager[" key" ] = model
1718
1819 val cachedModel = cacheManager[" key" ]
@@ -23,12 +24,14 @@ class RetroCacheManagerTest {
2324 @Test
2425 fun `get returns null when object expired` () {
2526 val cacheManager = RetroCacheManager .Builder ().build()
26- val model =
27- RetroCacheValue (
28- " test" ,
29- " 123" ,
30- expirationTime = System .currentTimeMillis() - 1000
31- )
27+ val model = RetroCacheValue (
28+ responseJson = " test" ,
29+ responseCode = 200 ,
30+ responseProtocol = Protocol .HTTP_1_1 ,
31+ responseHeaders = arrayOf(),
32+ tag = " 123" ,
33+ expirationTime = System .currentTimeMillis() - 1000
34+ )
3235 cacheManager[" key" ] = model
3336
3437 val cachedModel = cacheManager[" key" ]
@@ -39,8 +42,8 @@ class RetroCacheManagerTest {
3942 @Test
4043 fun `set adds object to cache` () {
4144 val cacheManager = RetroCacheManager .Builder ().build()
42- val model = RetroCacheValue (" test" , " 123" )
4345
46+ val model = getRetroCacheModel()
4447 cacheManager[" key" ] = model
4548
4649 val cachedModel = cacheManager[" key" ]
@@ -50,7 +53,7 @@ class RetroCacheManagerTest {
5053 @Test
5154 fun `remove removes object from cache` () {
5255 val cacheManager = RetroCacheManager .Builder ().build()
53- val model = RetroCacheValue ( " test " , " 123 " )
56+ val model = getRetroCacheModel( )
5457 cacheManager[" key" ] = model
5558
5659 cacheManager.remove(" key" )
@@ -59,11 +62,19 @@ class RetroCacheManagerTest {
5962 assertNull(cachedModel)
6063 }
6164
65+ private fun getRetroCacheModel (responseJson : String = "test", tag : String = "123") = RetroCacheValue (
66+ responseJson = responseJson,
67+ responseCode = 200 ,
68+ responseProtocol = Protocol .HTTP_1_1 ,
69+ responseHeaders = arrayOf(),
70+ tag = tag
71+ )
72+
6273 @Test
6374 fun `clearAll removes all objects from cache` () {
6475 val cacheManager = RetroCacheManager .Builder ().build()
65- cacheManager[" key1" ] = RetroCacheValue (" test1" , " 123" )
66- cacheManager[" key2" ] = RetroCacheValue (" test2" , " 456" )
76+ cacheManager[" key1" ] = getRetroCacheModel (" test1" , " 123" )
77+ cacheManager[" key2" ] = getRetroCacheModel (" test2" , " 456" )
6778
6879 cacheManager.clearAll()
6980
@@ -74,9 +85,9 @@ class RetroCacheManagerTest {
7485 @Test
7586 fun `clearAllByTag removes objects with specified tag` () {
7687 val cacheManager = RetroCacheManager .Builder ().build()
77- cacheManager[" key1" ] = RetroCacheValue (" test1" , tag = " tag1" )
78- cacheManager[" key2" ] = RetroCacheValue (" test2" , tag = " tag1" )
79- cacheManager[" key3" ] = RetroCacheValue (" test3" , tag = " tag2" )
88+ cacheManager[" key1" ] = getRetroCacheModel (" test1" , tag = " tag1" )
89+ cacheManager[" key2" ] = getRetroCacheModel (" test2" , tag = " tag1" )
90+ cacheManager[" key3" ] = getRetroCacheModel (" test3" , tag = " tag2" )
8091
8192 cacheManager.clearAllByTag(" tag1" )
8293
@@ -108,9 +119,9 @@ class RetroCacheManagerTest {
108119 @Test
109120 fun `last accessed object is moved to top of cache` () {
110121 val cacheManager = RetroCacheManager .Builder ().build()
111- cacheManager[" key1" ] = RetroCacheValue (" test1" )
112- cacheManager[" key2" ] = RetroCacheValue (" test2" )
113- cacheManager[" key3" ] = RetroCacheValue (" test3" )
122+ cacheManager[" key1" ] = getRetroCacheModel (" test1" )
123+ cacheManager[" key2" ] = getRetroCacheModel (" test2" )
124+ cacheManager[" key3" ] = getRetroCacheModel (" test3" )
114125
115126 var first: RetroCacheValue ? = null
116127 var last: RetroCacheValue ? = null
@@ -139,12 +150,12 @@ class RetroCacheManagerTest {
139150 @Test
140151 fun `old cached responses are removed if maxMemory limit has reached` () {
141152 val cacheManager = RetroCacheManager .Builder ()
142- .maxMemorySize(100 * 1024 ) // 100 kb max memory size
143- .maxObjectSize(1 * 1024 ) // 1 kb max memory size
153+ .maxMemorySize(100 * sizeOfObject(getRetroCacheModel(get1KbString()))) // fits 100 object
154+ .maxObjectSize(1 * sizeOfObject(getRetroCacheModel(get1KbString())))
144155 .build()
145156
146157 (0 .. 110 ).forEach {// add 10 items more than limit
147- cacheManager[it.toString()] = RetroCacheValue (oneKbString )
158+ cacheManager[it.toString()] = getRetroCacheModel(get1KbString() )
148159 }
149160
150161 (0 .. 110 ).forEach {
@@ -163,9 +174,8 @@ class RetroCacheManagerTest {
163174 .maxObjectSize(1 * 1024 ) // 1 kb max memory size
164175 .build()
165176
166- val invalidSizedString = oneKbString + " 12345"
167- cacheManager[" key" ] = RetroCacheValue (invalidSizedString)
168- println (sizeOfObject(RetroCacheValue (invalidSizedString)))
177+ val invalidSizedString = get1KbString() + " 12345"
178+ cacheManager[" key" ] = getRetroCacheModel(invalidSizedString)
169179 assertNull(cacheManager[" key" ])
170180 }
171181
@@ -180,62 +190,11 @@ class RetroCacheManagerTest {
180190 }
181191
182192
183- companion object {
184- private val oneKbString = """
185- {
186- "id": 123,
187- "name": "John Doe",
188- "email": "johndoe@example.com",
189- "age": 30,
190- "address": {
191- "street": "123 Main St",
192- "city": "Anytown",
193- "country": "USA"
194- }
195- }
196- {
197- "id": 123,
198- "name": "John Doe",
199- "email": "johndoe@example.com",
200- "age": 30,
201- "address": {
202- "street": "123 Main St",
203- "city": "Anytown",
204- "country": "USA"
205- }
206- }
207- {
208- "id": 123,
209- "name": "John Doe",
210- "email": "johndoe@example.com",
211- "age": 30,
212- "address": {
213- "street": "123 Main St",
214- "city": "Anytown",
215- "country": "USA"
216- }
217- }
218- {
219- "id": 123,
220- "name": "John Doe",
221- "email": "johndoe@example.com",
222- "age": 30,
223- "address": {
224- "street": "123 Main St",
225- "city": "Anytown",
226- "country": "USA"
227- }
228- }
229- {
230- "id": 123,
231- "name": "John Doe",
232- "email": "johndoe@example.com",
233- "age": 30,
234- "address": {
235- "street": "123 Main St",
236- "city": "Anytown",
237- }1111111
238- }
239- """ .trimIndent()
193+ private fun get1KbString (): String {
194+ val stringBuilder = StringBuilder ()
195+ repeat(1024 ) {
196+ stringBuilder.append(' a' )
197+ }
198+ return stringBuilder.toString()
240199 }
241200}
0 commit comments