Skip to content

Commit

Permalink
Add extension for testing with BLNS
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Aug 13, 2024
1 parent d3e60c7 commit afb70d0
Show file tree
Hide file tree
Showing 12 changed files with 3,026 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ out/
build/
logs/
!gradle/wrapper/gradle-wrapper.jar
# kotlin-language-server
kls_database.db
10 changes: 10 additions & 0 deletions extension/blns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `kotlin-faker-blns`

[![Maven Central](https://img.shields.io/maven-central/v/io.github.serpro69/kotlin-faker-blns?style=for-the-badge)](https://search.maven.org/artifact/io.github.serpro69/kotlin-faker-blns)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/io.github.serpro69/kotlin-faker-blns?label=snapshot-version&server=https%3A%2F%2Foss.sonatype.org&style=for-the-badge&color=yellow)](#downloading)

`kotlin-faker-blns` module provides extensions for the [big-list-of-naughty-strings](https://github.com/minimaxir/big-list-of-naughty-strings), a list of strings which have a high probability of causing issues when used as user-input data, and can therefore be quite useful in testing.

## Usage

Documentation for this extension is available at [serpro69.github.io/kotlin-faker/](https://serpro69.github.io/kotlin-faker/extensions/blns-extension).
15 changes: 15 additions & 0 deletions extension/blns/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
`faker-ext-conventions`
}

dependencies {
compileOnly(projects.core)
implementation(libs.bundles.jackson)
testImplementation(projects.core) // needed for tests since we have compileOnly dependency
testImplementation(libs.bundles.test.kotest)
}

tasks.test {
useJUnitPlatform()
dependsOn(":core:shadowJar")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.github.serpro69.kfaker.blns

import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import io.github.serpro69.kfaker.AbstractFaker
import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.FakerConfig
import io.github.serpro69.kfaker.FakerDsl
import io.github.serpro69.kfaker.fakerConfig
import io.github.serpro69.kfaker.RandomService

/**
* A kotlin-faker extension that helps to use
* [The Big List of Naughty Strings](https://github.com/minimaxir/big-list-of-naughty-strings)
* for various test-related inputs.
*/
@Suppress("unused")
class Blns @JvmOverloads constructor(config: FakerConfig = fakerConfig { }) : AbstractFaker(config) {
private val get: (filename: String) -> List<String> = {
val inStr = requireNotNull(javaClass.classLoader.getResourceAsStream(it))
Mapper.readValue(inStr, jacksonTypeRef())
}

/**
* @property all a list of all strings.
*/
val all: List<String> by lazy { get("blns.json") }

/**
* @property allBase64 a list of all base64-encoded strings.
*/
val allBase64: List<String> by lazy { get("blns.base64.json") }

/**
* Returns a random string of [all] strings (or [allBase64] strings if [base64] is `true`)
*/
fun random(base64: Boolean = false): String =
if (base64) randomService.randomValue(allBase64) else randomService.randomValue(all)

/**
* Returns a portion of [all] strings (or [allBase64] strings if [base64] is `true`)
* with pseudo-randomly generated `fromIndex` and (possibly) `toIndex` values.
*
* @param size the desired size of the resulting list.
* If `size <= 0` then `toIndex` will also be randomly-generated.
*/
fun sublist(size: Int, base64: Boolean = false): List<String> =
if (base64) randomService.randomSublist(allBase64, size) else randomService.randomSublist(all, size)

/**
* Returns a portion of [all] strings (or [allBase64] strings if [base64] is `true`)
* with pseudo-randomly generated `fromIndex` and (possibly) `toIndex` values.
*
* @param sizeRange the desired size range of the resulting list.
* The `size` of the returned list is the result of calling [RandomService.nextInt] with the given [sizeRange].
* IF `size <= 0` then `toIndex` will also be randomly-generated.
*/
fun sublist(sizeRange: IntRange, base64: Boolean = false): List<String> =
if (base64) randomService.randomSublist(allBase64, sizeRange) else randomService.randomSublist(all, sizeRange)

@FakerDsl
/**
* DSL builder for creating instances of [Blns]
*/
class Builder internal constructor() : AbstractFaker.Builder<Blns>() {

/**
* Builds an instance of [Faker] with this [config].
*/
override fun build(): Blns = Blns(config)
}
}

/**
* Applies the [block] function to [Blns.Builder]
* and returns as an instance of [Blns] from that builder.
*/
fun blns(block: Blns.Builder.() -> Unit): Blns = Blns.Builder().apply(block).build()
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.serpro69.kfaker.blns

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import java.io.InputStream

internal object Mapper {
private val mapper = ObjectMapper()

init {
mapper.registerModule(KotlinModule.Builder().build())
}

fun <T> readValue(inputStream: InputStream, typeRef: TypeReference<T>): T = mapper.readValue(inputStream, typeRef)
}
Loading

0 comments on commit afb70d0

Please sign in to comment.