Skip to content

Commit

Permalink
style: fix ktlint issue
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospereira committed Jul 2, 2024
1 parent 8e29b93 commit c217090
Show file tree
Hide file tree
Showing 18 changed files with 658 additions and 652 deletions.
94 changes: 46 additions & 48 deletions src/accessibilityTest/kotlin/br/ufpe/liber/AccessibilityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,58 @@ import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions

@MicronautTest
class AccessibilityTest(
private val server: EmbeddedServer,
private val context: ApplicationContext,
) : BehaviorSpec({
val axeBuilder = AxeBuilder().withTags(listOf("wcag21a", "wcag21aa", "wcag22aa", "experimental"))
val driver = ChromeDriver(
ChromeOptions()
.addArguments("--no-sandbox")
.addArguments("--disable-dev-shm-usage")
.addArguments("--headless"),
)
class AccessibilityTest(private val server: EmbeddedServer, private val context: ApplicationContext) :
BehaviorSpec({
val axeBuilder = AxeBuilder().withTags(listOf("wcag21a", "wcag21aa", "wcag22aa", "experimental"))
val driver = ChromeDriver(
ChromeOptions()
.addArguments("--no-sandbox")
.addArguments("--disable-dev-shm-usage")
.addArguments("--headless"),
)

val parameterlessRoutes = context.getBean<Router>()
.uriRoutes()
// filter in only routes that DO NOT require a parameter
.filter { it.httpMethod == HttpMethod.GET && it.uriMatchTemplate.variables.isEmpty() }
// filter in only UI routes
.filter { it.produces.contains(MediaType.TEXT_HTML_TYPE) }
// get only the paths
.map { it.uriMatchTemplate.toPathString() }
// More readable?
.sorted()
// Avoid possible duplications
.distinct()
// since `uriRoutes` returns a `Stream`.
.toList()
val parameterlessRoutes = context.getBean<Router>()
.uriRoutes()
// filter in only routes that DO NOT require a parameter
.filter { it.httpMethod == HttpMethod.GET && it.uriMatchTemplate.variables.isEmpty() }
// filter in only UI routes
.filter { it.produces.contains(MediaType.TEXT_HTML_TYPE) }
// get only the paths
.map { it.uriMatchTemplate.toPathString() }
// More readable?
.sorted()
// Avoid possible duplications
.distinct()
// since `uriRoutes` returns a `Stream`.
.toList()

beforeSpec {
context.getBean<AssetsResolver>()
}
beforeSpec {
context.getBean<AssetsResolver>()
}

// Called once per Spec, after all tests have completed for that spec.
finalizeSpec {
driver.close()
}
// Called once per Spec, after all tests have completed for that spec.
finalizeSpec {
driver.close()
}

fun url(path: String): String = "http://${server.host}:${server.port}$path"
fun url(path: String): String = "http://${server.host}:${server.port}$path"

fun checkAccessibility(path: String) {
driver.get(url(path))
driver.title shouldNotContain "404"
driver.title shouldNotContain "500"
val results = axeBuilder.analyze(driver)
results.violations shouldBe emptyList()
}
fun checkAccessibility(path: String) {
driver.get(url(path))
driver.title shouldNotContain "404"
driver.title shouldNotContain "500"
val results = axeBuilder.analyze(driver)
results.violations shouldBe emptyList()
}

given("The server is running") {
`when`("accessing the main pages") {
@Suppress("detekt:SpreadOperator")
forAll(*parameterlessRoutes.map(::row).toTypedArray()) { path ->
then("$path passes accessibility tests") {
checkAccessibility(path)
given("The server is running") {
`when`("accessing the main pages") {
@Suppress("detekt:SpreadOperator")
forAll(*parameterlessRoutes.map(::row).toTypedArray()) { path ->
then("$path passes accessibility tests") {
checkAccessibility(path)
}
}
}
}
}
})
})
5 changes: 4 additions & 1 deletion src/main/kotlin/br/ufpe/liber/assets/Assets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ data class Encoding(val http: String, val extension: String, val priority: Int)
}

@Suppress("IDENTIFIER_LENGTH") // it is okay to use `q` because it is part of the spec.
data class AcceptEncoding(val name: String, val q: Optional<Float> = Optional.empty()) : Comparable<AcceptEncoding> {
data class AcceptEncoding(
val name: String,
val q: Optional<Float> = Optional.empty(),
) : Comparable<AcceptEncoding> {
@Suppress("MAGIC_NUMBER")
val qualityValue = q.getOrDefault(1f)

Expand Down
35 changes: 15 additions & 20 deletions src/main/kotlin/br/ufpe/liber/controllers/AssetsController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import java.time.LocalDateTime
import java.util.Optional

@Controller("/static/{+path}")
class AssetsController(
private val assetsResolver: AssetsResolver,
private val resourceResolver: ResourceResolver,
) {
class AssetsController(private val assetsResolver: AssetsResolver, private val resourceResolver: ResourceResolver) {
@Get
fun asset(
path: String,
Expand All @@ -45,22 +42,20 @@ class AssetsController(
.map { HttpResponse.notModified() }
}

private fun httpResponseForAsset(asset: Asset, encoding: String): HttpResponse<StreamedFile> {
return asset
.preferredEncodedResource(encoding)
.flatMap { availableEncoding ->
resourceResolver
.getResourceAsStream(asset.classpath(availableEncoding.extension))
.map { inputStream ->
HttpResponse
.ok(StreamedFile(inputStream, asset.mediaType()))
.contentEncoding(availableEncoding.http)
}
}
.or { tryPlainAsset(asset) }
.map { response -> setCacheHeaders(asset, response) }
.orElse(HttpResponse.notFound())
}
private fun httpResponseForAsset(asset: Asset, encoding: String): HttpResponse<StreamedFile> = asset
.preferredEncodedResource(encoding)
.flatMap { availableEncoding ->
resourceResolver
.getResourceAsStream(asset.classpath(availableEncoding.extension))
.map { inputStream ->
HttpResponse
.ok(StreamedFile(inputStream, asset.mediaType()))
.contentEncoding(availableEncoding.http)
}
}
.or { tryPlainAsset(asset) }
.map { response -> setCacheHeaders(asset, response) }
.orElse(HttpResponse.notFound())

private fun tryPlainAsset(asset: Asset): Optional<MutableHttpResponse<StreamedFile>> = resourceResolver
.getResourceAsStream(asset.classpath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import jakarta.inject.Singleton
@Produces(MediaType.TEXT_HTML)
@Requires(classes = [Exception::class, ExceptionHandler::class])
class DefaultExceptionHandler(private val templates: Templates) :
ExceptionHandler<Exception, HttpResponse<*>>, KteController {
ExceptionHandler<Exception, HttpResponse<*>>,
KteController {
override fun handle(request: HttpRequest<*>, exception: Exception): HttpResponse<KteWriteable> =
serverError(templates.internalServerError(request.path, exception.message!!))
}
4 changes: 1 addition & 3 deletions src/main/kotlin/br/ufpe/liber/pagination/Pagination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ data class Pagination(
return min(count, possibleFrom)
}

private fun calculateTo(): Int {
return min(count, from + perPage - 1)
}
private fun calculateTo(): Int = min(count, from + perPage - 1)

@Suppress("SAY_NO_TO_VAR")
fun listPages(): List<PaginationItem> {
Expand Down
26 changes: 12 additions & 14 deletions src/test/kotlin/br/ufpe/liber/ApplicationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.test.extensions.kotest5.annotation.MicronautTest

@MicronautTest
class ApplicationTest(
private val server: EmbeddedServer,
private val context: ApplicationContext,
) : BehaviorSpec({
given("Application") {
`when`("application starts") {
then("server is running") { server.isRunning shouldBe true }
}
`when`("has a health check") {
then("it should return UP") {
val client = context.createBean(HttpClient::class.java, server.url).toBlocking()
client.exchange<Any>("/health").status.code shouldBe 200
class ApplicationTest(private val server: EmbeddedServer, private val context: ApplicationContext) :
BehaviorSpec({
given("Application") {
`when`("application starts") {
then("server is running") { server.isRunning shouldBe true }
}
`when`("has a health check") {
then("it should return UP") {
val client = context.createBean(HttpClient::class.java, server.url).toBlocking()
client.exchange<Any>("/health").status.code shouldBe 200
}
}
}
}
})
})
49 changes: 25 additions & 24 deletions src/test/kotlin/br/ufpe/liber/TemplatesFactoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,35 @@ import io.micronaut.context.env.Environment
import io.mockk.every
import io.mockk.mockk

class TemplatesFactoryTest : BehaviorSpec({
val factory = TemplatesFactory()
given("createTemplate") {
`when`("it is dev environment") {
val environment = mockk<Environment>()
every { environment.activeNames } answers { setOf("something", Environment.DEVELOPMENT) }

then("it should use dynamic templates") {
val templates = factory.createTemplate(environment)
(templates is DynamicTemplates) shouldBe true
}
}

forAll(
row(Environment.TEST),
row(Environment.CLI),
row(Environment.CLOUD),
row(Environment.BARE_METAL),
) { envName ->
`when`("it is $envName environment") {
class TemplatesFactoryTest :
BehaviorSpec({
val factory = TemplatesFactory()
given("createTemplate") {
`when`("it is dev environment") {
val environment = mockk<Environment>()
every { environment.activeNames } answers { setOf(envName) }
every { environment.activeNames } answers { setOf("something", Environment.DEVELOPMENT) }

then("it should use dynamic templates") {
val templates = factory.createTemplate(environment)
(templates is StaticTemplates) shouldBe true
(templates is DynamicTemplates) shouldBe true
}
}

forAll(
row(Environment.TEST),
row(Environment.CLI),
row(Environment.CLOUD),
row(Environment.BARE_METAL),
) { envName ->
`when`("it is $envName environment") {
val environment = mockk<Environment>()
every { environment.activeNames } answers { setOf(envName) }

then("it should use dynamic templates") {
val templates = factory.createTemplate(environment)
(templates is StaticTemplates) shouldBe true
}
}
}
}
}
})
})
Loading

0 comments on commit c217090

Please sign in to comment.