1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.protolog.tool
18 
19 import com.android.json.stream.JsonReader
20 import org.junit.Assert.assertEquals
21 import org.junit.Test
22 import java.io.StringReader
23 
24 class ViewerConfigParserTest {
25     private val parser = ViewerConfigParser()
26 
27     private fun getJSONReader(str: String): JsonReader {
28         return JsonReader(StringReader(str))
29     }
30 
31     @Test
32     fun parseMessage() {
33         val json = """
34         {
35             "message": "Test completed successfully: %b",
36             "level": "ERROR",
37             "group": "GENERIC_WM"
38         }
39         """
40         val msg = parser.parseMessage(getJSONReader(json))
41         assertEquals("Test completed successfully: %b", msg.messageString)
42         assertEquals("ERROR", msg.level)
43         assertEquals("GENERIC_WM", msg.groupName)
44     }
45 
46     @Test
47     fun parseMessage_reorder() {
48         val json = """
49         {
50             "group": "GENERIC_WM",
51             "level": "ERROR",
52             "message": "Test completed successfully: %b"
53         }
54         """
55         val msg = parser.parseMessage(getJSONReader(json))
56         assertEquals("Test completed successfully: %b", msg.messageString)
57         assertEquals("ERROR", msg.level)
58         assertEquals("GENERIC_WM", msg.groupName)
59     }
60 
61     @Test
62     fun parseMessage_unknownEntry() {
63         val json = """
64         {
65             "unknown": "unknown entries should not block parsing",
66             "message": "Test completed successfully: %b",
67             "level": "ERROR",
68             "group": "GENERIC_WM"
69         }
70         """
71         val msg = parser.parseMessage(getJSONReader(json))
72         assertEquals("Test completed successfully: %b", msg.messageString)
73         assertEquals("ERROR", msg.level)
74         assertEquals("GENERIC_WM", msg.groupName)
75     }
76 
77     @Test(expected = InvalidViewerConfigException::class)
78     fun parseMessage_noMessage() {
79         val json = """
80         {
81             "level": "ERROR",
82             "group": "GENERIC_WM"
83         }
84         """
85         parser.parseMessage(getJSONReader(json))
86     }
87 
88     @Test(expected = InvalidViewerConfigException::class)
89     fun parseMessage_noLevel() {
90         val json = """
91         {
92             "message": "Test completed successfully: %b",
93             "group": "GENERIC_WM"
94         }
95         """
96         parser.parseMessage(getJSONReader(json))
97     }
98 
99     @Test(expected = InvalidViewerConfigException::class)
100     fun parseMessage_noGroup() {
101         val json = """
102         {
103             "message": "Test completed successfully: %b",
104             "level": "ERROR"
105         }
106         """
107         parser.parseMessage(getJSONReader(json))
108     }
109 
110     @Test
111     fun parseGroup() {
112         val json = """
113         {
114             "tag": "WindowManager"
115         }
116         """
117         val group = parser.parseGroup(getJSONReader(json))
118         assertEquals("WindowManager", group.tag)
119     }
120 
121     @Test
122     fun parseGroup_unknownEntry() {
123         val json = """
124         {
125             "unknown": "unknown entries should not block parsing",
126             "tag": "WindowManager"
127         }
128         """
129         val group = parser.parseGroup(getJSONReader(json))
130         assertEquals("WindowManager", group.tag)
131     }
132 
133     @Test(expected = InvalidViewerConfigException::class)
134     fun parseGroup_noTag() {
135         val json = """
136         {
137         }
138         """
139         parser.parseGroup(getJSONReader(json))
140     }
141 
142     @Test
143     fun parseMessages() {
144         val json = """
145         {
146             "70933285": {
147               "message": "Test completed successfully: %b",
148               "level": "ERROR",
149               "group": "GENERIC_WM"
150             },
151             "1792430067": {
152               "message": "Attempted to add window to a display that does not exist: %d. Aborting.",
153               "level": "WARN",
154               "group": "ERROR_WM"
155             }
156         }
157         """
158         val messages = parser.parseMessages(getJSONReader(json))
159         assertEquals(2, messages.size)
160         val msg1 =
161                 ViewerConfigParser.MessageEntry("Test completed successfully: %b",
162                         "ERROR", "GENERIC_WM")
163         val msg2 =
164                 ViewerConfigParser.MessageEntry("Attempted to add window to a display that " +
165                         "does not exist: %d. Aborting.", "WARN", "ERROR_WM")
166 
167         assertEquals(msg1, messages[70933285])
168         assertEquals(msg2, messages[1792430067])
169     }
170 
171     @Test(expected = InvalidViewerConfigException::class)
172     fun parseMessages_invalidHash() {
173         val json = """
174         {
175             "invalid": {
176               "message": "Test completed successfully: %b",
177               "level": "ERROR",
178               "group": "GENERIC_WM"
179             }
180         }
181         """
182         parser.parseMessages(getJSONReader(json))
183     }
184 
185     @Test
186     fun parseGroups() {
187         val json = """
188         {
189             "GENERIC_WM": {
190               "tag": "WindowManager"
191             },
192             "ERROR_WM": {
193               "tag": "WindowManagerError"
194             }
195         }
196         """
197         val groups = parser.parseGroups(getJSONReader(json))
198         assertEquals(2, groups.size)
199         val grp1 = ViewerConfigParser.GroupEntry("WindowManager")
200         val grp2 = ViewerConfigParser.GroupEntry("WindowManagerError")
201         assertEquals(grp1, groups["GENERIC_WM"])
202         assertEquals(grp2, groups["ERROR_WM"])
203     }
204 
205     @Test
206     fun parseConfig() {
207         val json = """
208         {
209           "version": "${Constants.VERSION}",
210           "messages": {
211             "70933285": {
212               "message": "Test completed successfully: %b",
213               "level": "ERROR",
214               "group": "GENERIC_WM"
215             }
216           },
217           "groups": {
218             "GENERIC_WM": {
219               "tag": "WindowManager"
220             }
221           }
222         }
223         """
224         val config = parser.parseConfig(getJSONReader(json))
225         assertEquals(1, config.size)
226         val cfg1 = ViewerConfigParser.ConfigEntry("Test completed successfully: %b",
227                 "ERROR", "WindowManager")
228         assertEquals(cfg1, config[70933285])
229     }
230 
231     @Test(expected = InvalidViewerConfigException::class)
232     fun parseConfig_invalidVersion() {
233         val json = """
234         {
235           "version": "invalid",
236           "messages": {
237             "70933285": {
238               "message": "Test completed successfully: %b",
239               "level": "ERROR",
240               "group": "GENERIC_WM"
241             }
242           },
243           "groups": {
244             "GENERIC_WM": {
245               "tag": "WindowManager"
246             }
247           }
248         }
249         """
250         parser.parseConfig(getJSONReader(json))
251     }
252 
253     @Test(expected = InvalidViewerConfigException::class)
254     fun parseConfig_noVersion() {
255         val json = """
256         {
257           "messages": {
258             "70933285": {
259               "message": "Test completed successfully: %b",
260               "level": "ERROR",
261               "group": "GENERIC_WM"
262             }
263           },
264           "groups": {
265             "GENERIC_WM": {
266               "tag": "WindowManager"
267             }
268           }
269         }
270         """
271         parser.parseConfig(getJSONReader(json))
272     }
273 
274     @Test(expected = InvalidViewerConfigException::class)
275     fun parseConfig_noMessages() {
276         val json = """
277         {
278           "version": "${Constants.VERSION}",
279           "groups": {
280             "GENERIC_WM": {
281               "tag": "WindowManager"
282             }
283           }
284         }
285         """
286         parser.parseConfig(getJSONReader(json))
287     }
288 
289     @Test(expected = InvalidViewerConfigException::class)
290     fun parseConfig_noGroups() {
291         val json = """
292         {
293           "version": "${Constants.VERSION}",
294           "messages": {
295             "70933285": {
296               "message": "Test completed successfully: %b",
297               "level": "ERROR",
298               "group": "GENERIC_WM"
299             }
300           }
301         }
302         """
303         parser.parseConfig(getJSONReader(json))
304     }
305 
306     @Test(expected = InvalidViewerConfigException::class)
307     fun parseConfig_missingGroup() {
308         val json = """
309         {
310           "version": "${Constants.VERSION}",
311           "messages": {
312             "70933285": {
313               "message": "Test completed successfully: %b",
314               "level": "ERROR",
315               "group": "GENERIC_WM"
316             }
317           },
318           "groups": {
319             "ERROR_WM": {
320               "tag": "WindowManager"
321             }
322           }
323         }
324         """
325         parser.parseConfig(getJSONReader(json))
326     }
327 }
328