2 * Sone - MetricsPageTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.pages
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.*
28 class MetricsPageTest : WebPageTest() {
30 private val metricRegistry = MetricRegistry()
31 override val page by lazy { MetricsPage(webInterface, loaders, templateRenderer, metricRegistry) }
34 fun `page returns correct path`() {
35 assertThat(page.path, equalTo("metrics.html"))
39 fun `page does not require login`() {
40 assertThat(page.requiresLogin(), equalTo(false))
44 fun `page returns correct title`() {
45 addTranslation("Page.Metrics.Title", "metrics page title")
46 assertThat(page.getPageTitle(soneRequest), equalTo("metrics page title"))
50 fun `page can be created by dependency injection`() {
51 assertThat(baseInjector.getInstance<MetricsPage>(), notNullValue())
55 fun `page is annotated with the correct menuname`() {
56 assertThat(page.menuName, equalTo("Metrics"))
60 fun `page is annotated with correct template path`() {
61 assertThat(page.templatePath, equalTo("/templates/metrics.html"))
65 @Suppress("UNCHECKED_CAST")
66 fun `metrics page stores histograms in template context`() {
67 createHistogram("sone.random.duration2")
68 createHistogram("sone.random.duration1")
69 page.handleRequest(soneRequest, templateContext)
70 val histograms = templateContext["histograms"] as Map<String, Histogram>
71 assertThat(histograms.entries.map { it.key to it.value }, containsInAnyOrder(
72 "sone.random.duration1" to metricRegistry.histogram("sone.random.duration1"),
73 "sone.random.duration2" to metricRegistry.histogram("sone.random.duration2")
77 private fun createHistogram(name: String) = metricRegistry.histogram(name).run {