Skip to content

Commit

Permalink
Create Jackson annotation TCK (#667)
Browse files Browse the repository at this point in the history
* Create Jackson annotation TCK

* Correct
  • Loading branch information
dstepanov authored Nov 13, 2023
1 parent 00e5e9c commit 91e263d
Show file tree
Hide file tree
Showing 75 changed files with 6,722 additions and 372 deletions.
30 changes: 30 additions & 0 deletions serde-jackson-tck/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
plugins {
id("groovy")
id("java-library")
id("io.micronaut.build.internal.serde-tests")
}
dependencies {
annotationProcessor(mn.micronaut.inject.java)
annotationProcessor(projects.micronautSerdeProcessor)

api(projects.micronautSerdeApi)
api(projects.micronautSerdeSupport)
api(projects.micronautSerdeProcessor)
api(mn.micronaut.inject.java.test)
api(mnTest.micronaut.test.spock)
api(libs.jetbrains.annotations)
api(mn.jackson.annotations)

compileOnly(mn.micronaut.inject.groovy)
compileOnly(mn.jackson.databind)
}

tasks.named("spotlessGroovyCheck").configure {
enabled = false
}
tasks.named("spotlessJavaCheck").configure {
enabled = false
}
tasks.named("checkstyleMain").configure {
enabled = false
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.micronaut.serde.jackson.annotation
package io.micronaut.serde.jackson

import io.micronaut.serde.jackson.JsonCompileSpec
abstract class JsonAliasSpec extends JsonCompileSpec {

class JsonAliasSpec extends JsonCompileSpec {
void 'test JsonAlias with simple properties'() {
given:
def context = buildContext('''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.micronaut.serde.jackson


import spock.lang.Unroll

abstract class JsonBasicSerdeSpec extends JsonCompileSpec {

@Unroll
void "test basic collection type #type with include NON_ABSENT"() {
given:
def context = buildContext('test.Test', """
package test;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonInclude;
@com.fasterxml.jackson.annotation.JsonClassDescription
class Test {
@JsonInclude(JsonInclude.Include.NON_ABSENT)
private $type value;
public void setValue($type value) {
this.value = value;
}
public $type getValue() {
return value;
}
}
""", data)
expect:
writeJson(jsonMapper, beanUnderTest) == result
def read = jsonMapper.readValue(result, typeUnderTest)
typeUnderTest.type.isInstance(read)
read.value == data.value

cleanup:
context.close()

where:
type | data | result
"Optional<String>" | [value: Optional.empty()] | '{}'
"OptionalInt" | [value: OptionalInt.empty()] | '{}'
"OptionalDouble" | [value: OptionalDouble.empty()] | '{}'
"OptionalLong" | [value: OptionalLong.empty()] | '{}'
"List<String>" | [value: ["Test"]] | '{"value":["Test"]}'
"Optional<String>" | [value: Optional.of("Test")] | '{"value":"Test"}'
"List<? extends CharSequence>" | [value: ["Test"]] | '{"value":["Test"]}'
"List<Boolean>" | [value: [true]] | '{"value":[true]}'
"Iterable<String>" | [value: ["Test"]] | '{"value":["Test"]}'
"Iterable<Boolean>" | [value: [true]] | '{"value":[true]}'
"Set<String>" | [value: ["Test"] as Set] | '{"value":["Test"]}'
"Set<Boolean>" | [value: [true] as Set] | '{"value":[true]}'
"Collection<String>" | [value: ["Test"]] | '{"value":["Test"]}'
"Collection<Boolean>" | [value: [true]] | '{"value":[true]}'
"Map<String, Boolean>" | [value: [foo: true]] | '{"value":{"foo":true}}'

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.micronaut.serde.jackson

import io.micronaut.annotation.processing.JavaAnnotationMetadataBuilder
import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.micronaut.annotation.processing.test.JavaParser
import io.micronaut.context.ApplicationContext
import io.micronaut.core.type.Argument
import io.micronaut.json.JsonMapper
import org.intellij.lang.annotations.Language

abstract class JsonCompileSpec extends AbstractTypeElementSpec implements JsonSpec {

JsonMapper jsonMapper
Object beanUnderTest
Argument<?> typeUnderTest

@Override
protected JavaParser newJavaParser() {
JavaAnnotationMetadataBuilder.clearCaches()
return super.newJavaParser()
}

ApplicationContext buildContext(String className, @Language("java") String source, Map<String, Object> properties) {
ApplicationContext context =
buildContext(className, source, true)

jsonMapper = context.getBean(JsonMapper)

def t = context.classLoader
.loadClass(className)
typeUnderTest = Argument.of(t)
beanUnderTest = t.newInstance(properties)
return context
}

Object newInstance(ApplicationContext context, String name, Map args) {
return context.classLoader.loadClass(name).newInstance(args)
}

Object newInstance(ApplicationContext context, String name, Object[] args) {
return context.classLoader.loadClass(name).newInstance(args)
}

Argument<Object> argumentOf(ApplicationContext context, String name) {
return Argument.of(context.classLoader.loadClass(name))
}

@Override
ApplicationContext buildContext(@Language("java") String source) {
ApplicationContext context =
buildContext("test.Source" + System.currentTimeMillis(), source, true)


jsonMapper = context.getBean(JsonMapper)
return context
}

@Override
ApplicationContext buildContext(String className, @Language("java") String cls, boolean includeAllBeans) {
def context = super.buildContext(className, cls, true)
jsonMapper = context.getBean(JsonMapper)
return context
}

@Override
ApplicationContext buildContext(String className, @Language("java") String cls) {
def context = super.buildContext(className, cls, true)
def t = context.classLoader
.loadClass(className)
typeUnderTest = Argument.of(t)
jsonMapper = context.getBean(JsonMapper)
return context
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.micronaut.serde.jackson


import spock.lang.Unroll

import java.util.function.Predicate

abstract class JsonFilterSpec extends JsonCompileSpec {

@Unroll
void "test @JsonFilter ignore a value for #value, (ignore = #ignoredValue)"() {
given:
def context = buildContext("""
package jsonfilter;
import io.micronaut.serde.annotation.Serdeable;
import com.fasterxml.jackson.annotation.JsonFilter;
import java.util.List;
@Serdeable
@JsonFilter("ignore-value")
record Test (
${type} value
) {}
""")
context.getBean(PredicateFilter).predicate = { it -> !Objects.equals(it, ignoredValue) }
def bean = newInstance(context, 'jsonfilter.Test', value)
String json = writeJson(jsonMapper, bean)

expect:
json == result

cleanup:
context.close()

where:
type | ignoredValue | value | result
'String' | null | null | '{}'
'String' | 'ignored' | null | '{"value":null}'
'String' | 'ignored' | 'ignored' | '{}'
'List<String>' | ['ignored'] | ['ignored'] | '{}'
'List<String>' | [] | ['a', 'b'] | '{"value":["a","b"]}'
'List<String>' | null | [] | '{"value":[]}'
}

static interface PredicateFilter {

void setPredicate(Predicate<Object> predicate);
}
}
Loading

0 comments on commit 91e263d

Please sign in to comment.