generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Jackson annotation TCK (#667)
* Create Jackson annotation TCK * Correct
- Loading branch information
Showing
75 changed files
with
6,722 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
5 changes: 2 additions & 3 deletions
5
...e/jackson/annotation/JsonAliasSpec.groovy → ...ronaut/serde/jackson/JsonAliasSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
serde-jackson-tck/src/main/groovy/io/micronaut/serde/jackson/JsonBasicSerdeSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}' | ||
|
||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
serde-jackson-tck/src/main/groovy/io/micronaut/serde/jackson/JsonCompileSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
serde-jackson-tck/src/main/groovy/io/micronaut/serde/jackson/JsonFilterSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.