✅ Fix tests failing with non-English locales
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / HistogramRendererTest.kt
1 /**
2  * Sone - HistogramRendererTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template
19
20 import com.codahale.metrics.*
21 import net.pterodactylus.sone.freenet.*
22 import net.pterodactylus.sone.test.OverrideLocale
23 import net.pterodactylus.util.template.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
26 import org.jsoup.*
27 import org.jsoup.nodes.*
28 import org.junit.*
29 import java.util.*
30 import java.util.Locale.ENGLISH
31
32 /**
33  * Unit test for [HistogramRenderer].
34  */
35 class HistogramRendererTest {
36
37         @get:Rule
38         val overrideLocale = OverrideLocale(ENGLISH)
39
40         private val translation = object : Translation {
41                 override val currentLocale = Locale.ENGLISH
42                 override fun translate(key: String) = "Metric Name".takeIf { key == "Page.Metrics.TestHistogram.Title" } ?: ""
43         }
44         private val metricRenderer = HistogramRenderer()
45         private val templateContext = TemplateContext().apply {
46                 addFilter("html", HtmlFilter())
47                 addFilter("duration", DurationFormatFilter())
48                 addFilter("l10n", L10nFilter(translation))
49         }
50
51         @Test
52         fun `histogram is rendered as table row`() {
53                 createAndVerifyTableRow {
54                         assertThat(it.nodeName(), equalTo("tr"))
55                 }
56         }
57
58         @Test
59         fun `histogram has eleven columns`() {
60                 createAndVerifyTableRow {
61                         assertThat(it.getElementsByTag("td"), hasSize(11))
62                 }
63         }
64
65         @Test
66         fun `first column contains translated metric name`() {
67                 createAndVerifyTableRow(mapOf("name" to "test.histogram")) {
68                         assertThat(it.getElementsByTag("td")[0].text(), equalTo("Metric Name"))
69                 }
70         }
71
72         @Test
73         fun `second column is numeric`() {
74                 verifyColumnIsNumeric(1)
75         }
76
77         @Test
78         fun `second column contains count`() {
79                 createAndVerifyTableRow {
80                         assertThat(it.getElementsByTag("td")[1].text(), equalTo("2001"))
81                 }
82         }
83
84         @Test
85         fun `third column is numeric`() {
86                 verifyColumnIsNumeric(2)
87         }
88
89         @Test
90         fun `third column contains min value`() {
91                 createAndVerifyTableRow {
92                         assertThat(it.getElementsByTag("td")[2].text(), equalTo("2.0ms"))
93                 }
94         }
95
96         @Test
97         fun `fourth column is numeric`() {
98                 verifyColumnIsNumeric(3)
99         }
100
101         @Test
102         fun `fourth column contains max value`() {
103                 createAndVerifyTableRow {
104                         assertThat(it.getElementsByTag("td")[3].text(), equalTo("998.0ms"))
105                 }
106         }
107
108         @Test
109         fun `fifth column is numeric`() {
110                 verifyColumnIsNumeric(4)
111         }
112
113         @Test
114         fun `fifth column contains mean value`() {
115                 createAndVerifyTableRow {
116                         assertThat(it.getElementsByTag("td")[4].text(), equalTo("492.7ms"))
117                 }
118         }
119
120         @Test
121         fun `sixth column is numeric`() {
122                 verifyColumnIsNumeric(5)
123         }
124
125         @Test
126         fun `sixth column contains median value`() {
127                 createAndVerifyTableRow {
128                         assertThat(it.getElementsByTag("td")[5].text(), equalTo("483.6ms"))
129                 }
130         }
131
132         @Test
133         fun `seventh column is numeric`() {
134                 verifyColumnIsNumeric(6)
135         }
136
137         @Test
138         fun `seventh column contains 75th percentile`() {
139                 createAndVerifyTableRow {
140                         assertThat(it.getElementsByTag("td")[6].text(), equalTo("740.9ms"))
141                 }
142         }
143
144         @Test
145         fun `eighth column is numeric`() {
146                 verifyColumnIsNumeric(7)
147         }
148
149         @Test
150         fun `eighth column contains 95th percentile`() {
151                 createAndVerifyTableRow {
152                         assertThat(it.getElementsByTag("td")[7].text(), equalTo("940.9ms"))
153                 }
154         }
155
156         @Test
157         fun `ninth column is numeric`() {
158                 verifyColumnIsNumeric(8)
159         }
160
161         @Test
162         fun `ninth column contains 98th percentile`() {
163                 createAndVerifyTableRow {
164                         assertThat(it.getElementsByTag("td")[8].text(), equalTo("975.6ms"))
165                 }
166         }
167
168         @Test
169         fun `tenth column is numeric`() {
170                 verifyColumnIsNumeric(9)
171         }
172
173         @Test
174         fun `tenth column contains 99th percentile`() {
175                 createAndVerifyTableRow {
176                         assertThat(it.getElementsByTag("td")[9].text(), equalTo("991.6ms"))
177                 }
178         }
179
180         @Test
181         fun `eleventh column is numeric`() {
182                 verifyColumnIsNumeric(10)
183         }
184
185         @Test
186         fun `eleventh column contains 99,9th percentile`() {
187                 createAndVerifyTableRow {
188                         assertThat(it.getElementsByTag("td")[10].text(), equalTo("998.0ms"))
189                 }
190         }
191
192         private fun createAndVerifyTableRow(parameters: Map<String, Any?>? = null, verify: (Element) -> Unit) =
193                         metricRenderer.format(templateContext, histogram, parameters)
194                                         .let { "<table id='t'>$it</table>" }
195                                         .let(Jsoup::parseBodyFragment)
196                                         .getElementById("t").child(0).child(0)
197                                         .let(verify)
198
199         private fun verifyColumnIsNumeric(column: Int) =
200                         createAndVerifyTableRow {
201                                 assertThat(it.getElementsByTag("td")[column].classNames(), hasItem("numeric"))
202                         }
203
204 }
205
206 private val random = Random(1)
207 private val histogram = MetricRegistry().histogram("test.histogram") { Histogram(SlidingWindowReservoir(1028)) }.apply {
208         (0..2000).map { random.nextInt(1_000_000) }.forEach(this::update)
209 }