✨ Add metrics page
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / MetricsPageTest.kt
1 /**
2  * Sone - MetricsPageTest.kt - Copyright © 2019 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.web.pages
19
20 import com.codahale.metrics.*
21 import net.pterodactylus.sone.test.*
22 import net.pterodactylus.sone.web.*
23 import net.pterodactylus.sone.web.page.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
26 import kotlin.test.*
27
28 class MetricsPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> MetricsPage(webInterface, loaders, templateRenderer, metricRegistry) }) {
29
30         companion object {
31                 val metricRegistry = MetricRegistry()
32         }
33
34         @Test
35         fun `page returns correct path`() {
36                 assertThat(page.path, equalTo("metrics.html"))
37         }
38
39         @Test
40         fun `page does not require login`() {
41                 assertThat(page.requiresLogin(), equalTo(false))
42         }
43
44         @Test
45         fun `page returns correct title`() {
46                 addTranslation("Page.Metrics.Title", "metrics page title")
47                 assertThat(page.getPageTitle(soneRequest), equalTo("metrics page title"))
48         }
49
50         @Test
51         fun `page can be created by dependency injection`() {
52                 assertThat(baseInjector.getInstance<MetricsPage>(), notNullValue())
53         }
54
55         @Test
56         fun `page is annotated with the correct menuname`() {
57                 assertThat(page.menuName, equalTo("Metrics"))
58         }
59
60         @Test
61         fun `page is annotated with correct template path`() {
62                 assertThat(page.templatePath, equalTo("/templates/metrics.html"))
63         }
64
65         @Test
66         fun `metrics page lists stats about sone parsing durations`() {
67                 createHistogram("sone.parsing.duration")
68                 page.handleRequest(soneRequest, templateContext)
69                 verifyHistogram("soneParsingDuration")
70         }
71
72         private fun verifyHistogram(name: String) {
73                 assertThat(templateContext["${name}Count"] as Int, equalTo(5))
74                 assertThat(templateContext["${name}Min"] as Long, equalTo(1L))
75                 assertThat(templateContext["${name}Max"] as Long, equalTo(10L))
76                 assertThat(templateContext["${name}Median"] as Double, equalTo(8.0))
77                 assertThat(templateContext["${name}Percentile75"] as Double, equalTo(9.0))
78                 assertThat(templateContext["${name}Percentile95"] as Double, equalTo(10.0))
79                 assertThat(templateContext["${name}Percentile98"] as Double, equalTo(10.0))
80                 assertThat(templateContext["${name}Percentile99"] as Double, equalTo(10.0))
81                 assertThat(templateContext["${name}Percentile999"] as Double, equalTo(10.0))
82         }
83
84         private fun createHistogram(name: String) = metricRegistry.histogram(name).run {
85                 update(10)
86                 update(9)
87                 update(1)
88                 update(1)
89                 update(8)
90         }
91
92 }