File tree 2 files changed +26
-2
lines changed
main/java/ai/pams/android/kotlin/models/notification
test/java/ai/pams/android/kotlin
2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package ai.pams.android.kotlin.models.notification
2
2
3
3
import ai.pams.android.kotlin.utils.DateUtils
4
4
import android.content.Context
5
+ import org.json.JSONException
5
6
import org.json.JSONObject
6
7
7
8
class NotificationList (
@@ -19,7 +20,14 @@ class NotificationList(
19
20
val noti = NotificationList ();
20
21
val list = mutableListOf<NotificationItem >()
21
22
22
- val json = JSONObject (jsonString)
23
+ var json: JSONObject ?
24
+ try {
25
+ json = JSONObject (jsonString)
26
+ }catch (e: JSONException ){
27
+ noti.items = listOf ()
28
+ return noti
29
+ }
30
+
23
31
val itemsArray = json.optJSONArray(" items" )
24
32
val count = itemsArray?.length() ? : 0
25
33
for (i in 0 until count){
@@ -34,7 +42,7 @@ class NotificationList(
34
42
val iterator = jsonPayload?.keys()
35
43
while (iterator?.hasNext() == true ){
36
44
val key = iterator.next()
37
- jsonPayload?.get (key)?.let { data ->
45
+ jsonPayload?.opt (key)?.let { data->
38
46
payload[key] = data
39
47
}
40
48
}
Original file line number Diff line number Diff line change 1
1
package ai.pams.android.kotlin
2
2
3
+ import ai.pams.android.kotlin.models.notification.NotificationList
3
4
import ai.pams.android.kotlin.utils.DateUtils
5
+ import android.content.Context
6
+ import androidx.test.platform.app.InstrumentationRegistry
4
7
import org.junit.Assert.*
8
+ import org.junit.Before
5
9
import org.junit.Test
6
10
import org.junit.runner.RunWith
7
11
import org.robolectric.RobolectricTestRunner
@@ -13,10 +17,22 @@ import org.robolectric.RobolectricTestRunner
13
17
*/
14
18
@RunWith(RobolectricTestRunner ::class )
15
19
class ExampleUnitTest {
20
+ lateinit var instrumentationContext: Context
21
+
22
+ @Before
23
+ fun setup () {
24
+ instrumentationContext = InstrumentationRegistry .getInstrumentation().context
25
+ }
26
+
16
27
@Test
17
28
fun testParseDate () {
18
29
val str = " 2022-06-29 03:53:51"
19
30
val date = DateUtils .localDateTimeFromString(str)
20
31
assertEquals(date.hour, 10 )
21
32
}
33
+
34
+ @Test
35
+ fun testNotificationJSONError (){
36
+ NotificationList .parse(" <some html>" , instrumentationContext)
37
+ }
22
38
}
You can’t perform that action at this time.
0 commit comments