Git Product home page Git Product logo

kfixture's Introduction

Build Status

What is about?

Library give a parameter resolver, which can be used to generate valid data for your class to pass validation of your api.

  • Generate data for all javax validation annotations.
  • Also generate data ignore javax annotations
  • Provide an interface to define custom rules for validation.
  • Valid param go through chain of validation.
  • Suite of test utils for generation parameters and so on.

Use in your project

maven
<dependency>
    <groupId>ru.kontur.kinfra.kfixture</groupId>
    <artifactId>kfixture</artifactId>
    <version>0.6.0</version>
</dependency>
gradle
implementation "ru.kontur.kinfra.kfixture:kfixture:0.5.0"

Fixture generates random data

data class DataGenerate(
    val param: String
)

fun `should just generate random data`(@Fixture fixture: DataGenerate) {}

JavaxFixture generates valid random data

data class JavaxData(
    @field:Pattern("\\d{2}")
    val param: String
)

fun `should just generate valid data`(@JavaxFixture fixture: JavaxData) {}

If kfixture can't generate your class, you can use ParamConstructor

class DurationConstructor: ParamConstructor<Duration> {
    override fun call(context: FixtureContext): Duration {
        return Duration.ofSeconds(10)
    }
}

For custom annotation you have to provide your generation router

class EmailRouter<T> : ValidRouter<T, Email> where T : Any {
    private val emailGenerator = EmailGenerator()

    override fun process(param: T, annotation: Email, clazz: KClass<*>, type: KType): Any? {
        return when (param) {
            is String -> emailGenerator.process(param, annotation, clazz, type)
            else -> throw AnnotationCantBeAppliedException(annotation, clazz)
        }
    }
}

If you need just construct object in validation and do not process annotation generation, you can use ValidationConstructor

class DurationConstructor: ValidationConstructor<Duration> {
    override fun call(context: FixtureContext): Duration {
        return Duration.ofSeconds(10)
    }
}

Create business fixtures just use Customized and Cusomizer

data class TestDto(
    val param: String,
    val param1: String
)

class TestCustomizer: Customizer<TestDto>{
    override fun customize(value: TestDto, context: FixtureContext): TestDto {
          return value.copy(param="AAAA")    
    }  
}

@Fixture
@Customized(sequence=[TestCustomizer::class])
annotation class BusinessFixture

@Test
fun `should generate fixture`(@BusinessFixture fixture: TestDto) {}

kfixture's People

Contributors

kostya05983 avatar kubitre avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.