Skip to content

Commit

Permalink
GH-458 Initialize StatisticsFacade test
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Jul 9, 2021
1 parent cddae5d commit c4396b1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ internal class FrontendHandler(private val frontendFacade: FrontendFacade) : Rou
FrontendFacade::class.java.getResourceAsStream("/static/$uri")
?.let {
ctx.result(it)
.encoding(Charsets.UTF_8)
.contentType(MimeTypes.getDefaultMimeByExtension(uri))
.encoding("UTF-8")
}
?: ctx.status(HttpStatus.NOT_FOUND_404)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StatisticsFacade internal constructor(
fun increaseRecord(type: RecordType, uri: String) =
recordsBulk.merge(RecordIdentifier(type, uri), 1) { cached, value -> cached + value }

internal fun saveRecordsBulk() =
fun saveRecordsBulk() =
recordsBulk.toMap().also {
recordsBulk.clear() // read doesn't lock, so there is a possibility of dropping a few records between toMap and clear. Might be improved in the future
statisticsRepository.incrementRecords(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.panda_lang.reposilite.maven.api.DocumentInfo
import org.panda_lang.utilities.commons.function.Result
import java.io.InputStream
import java.io.OutputStream
import java.nio.charset.Charset
import java.nio.file.Path
import java.nio.file.Paths

Expand All @@ -36,6 +37,9 @@ fun Context.error(error: ErrorResponse): Context =
fun Context.contentLength(length: Long): Context =
also { res.setContentLengthLong(length) }

fun Context.encoding(encoding: Charset): Context =
encoding(encoding.name())

fun Context.encoding(encoding: String): Context =
also { res.characterEncoding = encoding }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.panda_lang.reposilite.statistics

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.panda_lang.reposilite.statistics.api.RecordType.REQUEST

internal class StatisticsFacadeTest : StatisticsSpec() {

@Test
fun `should increase records after saving the bulk`() {
// given: an uri to request
val uri = "/x/y/z"

// when: the given uri is requested twice
statisticsFacade.increaseRecord(REQUEST, uri)
statisticsFacade.increaseRecord(REQUEST, uri)
statisticsFacade.saveRecordsBulk()

// then: it should be properly stored in repository as a single record
val records = statisticsFacade.findRecordsByPhrase(REQUEST, uri)
assertEquals(1, records.size)
assertEquals(2, records.get(0).count)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.panda_lang.reposilite.statistics

import net.dzikoysk.dynamiclogger.backend.InMemoryLogger
import org.panda_lang.reposilite.statistics.infrastructure.InMemoryStatisticsRepository

internal open class StatisticsSpec {

protected val logger = InMemoryLogger()
protected val statisticsFacade = StatisticsFacade(logger, InMemoryStatisticsRepository())

}

0 comments on commit c4396b1

Please sign in to comment.