+/**
+ * Sone - MetricsPageTest.kt - Copyright © 2019 David ‘Bombe’ Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.pages
+
+import com.codahale.metrics.*
+import net.pterodactylus.sone.test.*
+import net.pterodactylus.sone.web.*
+import net.pterodactylus.sone.web.page.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import kotlin.test.*
+
+class MetricsPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> MetricsPage(webInterface, loaders, templateRenderer, metricRegistry) }) {
+
+ companion object {
+ val metricRegistry = MetricRegistry()
+ }
+
+ @Test
+ fun `page returns correct path`() {
+ assertThat(page.path, equalTo("metrics.html"))
+ }
+
+ @Test
+ fun `page does not require login`() {
+ assertThat(page.requiresLogin(), equalTo(false))
+ }
+
+ @Test
+ fun `page returns correct title`() {
+ addTranslation("Page.Metrics.Title", "metrics page title")
+ assertThat(page.getPageTitle(soneRequest), equalTo("metrics page title"))
+ }
+
+ @Test
+ fun `page can be created by dependency injection`() {
+ assertThat(baseInjector.getInstance<MetricsPage>(), notNullValue())
+ }
+
+ @Test
+ fun `page is annotated with the correct menuname`() {
+ assertThat(page.menuName, equalTo("Metrics"))
+ }
+
+ @Test
+ fun `page is annotated with correct template path`() {
+ assertThat(page.templatePath, equalTo("/templates/metrics.html"))
+ }
+
+ @Test
+ fun `metrics page lists stats about sone parsing durations`() {
+ createHistogram("sone.parsing.duration")
+ page.handleRequest(soneRequest, templateContext)
+ verifyHistogram("soneParsingDuration")
+ }
+
+ private fun verifyHistogram(name: String) {
+ assertThat(templateContext["${name}Count"] as Int, equalTo(5))
+ assertThat(templateContext["${name}Min"] as Long, equalTo(1L))
+ assertThat(templateContext["${name}Max"] as Long, equalTo(10L))
+ assertThat(templateContext["${name}Median"] as Double, equalTo(8.0))
+ assertThat(templateContext["${name}Percentile75"] as Double, equalTo(9.0))
+ assertThat(templateContext["${name}Percentile95"] as Double, equalTo(10.0))
+ assertThat(templateContext["${name}Percentile98"] as Double, equalTo(10.0))
+ assertThat(templateContext["${name}Percentile99"] as Double, equalTo(10.0))
+ assertThat(templateContext["${name}Percentile999"] as Double, equalTo(10.0))
+ }
+
+ private fun createHistogram(name: String) = metricRegistry.histogram(name).run {
+ update(10)
+ update(9)
+ update(1)
+ update(1)
+ update(8)
+ }
+
+}