Search in sources :

Example 1 with ConfigFunction

use of com.github.victools.jsonschema.generator.ConfigFunction in project jsonschema-generator by victools.

the class JacksonModuleTest method testPropertyNameOverride.

@Test
@Parameters
public void testPropertyNameOverride(String fieldName, String expectedOverrideValue, String kebabCaseName) throws Exception {
    new JacksonModule().applyToConfigBuilder(this.configBuilder);
    ArgumentCaptor<ConfigFunction<FieldScope, String>> captor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.fieldConfigPart, Mockito.times(2)).withPropertyNameOverrideResolver(captor.capture());
    FieldScope field = new TestType(TestClassForPropertyNameOverride.class).getMemberField(fieldName);
    List<String> overrideValues = captor.getAllValues().stream().map(nameOverride -> nameOverride.apply(field)).collect(Collectors.toList());
    Assert.assertEquals(expectedOverrideValue, overrideValues.get(0));
    Assert.assertEquals(kebabCaseName, overrideValues.get(1));
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) FieldScope(com.github.victools.jsonschema.generator.FieldScope) MethodScope(com.github.victools.jsonschema.generator.MethodScope) JsonPropertyDescription(com.fasterxml.jackson.annotation.JsonPropertyDescription) SchemaGeneratorGeneralConfigPart(com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart) SchemaGeneratorConfigPart(com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) RunWith(org.junit.runner.RunWith) TypeScope(com.github.victools.jsonschema.generator.TypeScope) Test(org.junit.Test) Collectors(java.util.stream.Collectors) PropertyNamingStrategies(com.fasterxml.jackson.databind.PropertyNamingStrategies) Mockito(org.mockito.Mockito) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) JsonNaming(com.fasterxml.jackson.databind.annotation.JsonNaming) JsonClassDescription(com.fasterxml.jackson.annotation.JsonClassDescription) JUnitParamsRunner(junitparams.JUnitParamsRunner) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Assert(org.junit.Assert) Parameters(junitparams.Parameters) Before(org.junit.Before) FieldScope(com.github.victools.jsonschema.generator.FieldScope) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 2 with ConfigFunction

use of com.github.victools.jsonschema.generator.ConfigFunction in project jsonschema-generator by victools.

the class JacksonModuleTest method testDescriptionForTypeResolver.

@Test
@Parameters
public void testDescriptionForTypeResolver(String fieldName, String expectedDescription) throws Exception {
    new JacksonModule().applyToConfigBuilder(this.configBuilder);
    FieldScope field = new TestType(TestClassForDescription.class).getMemberField(fieldName);
    ArgumentCaptor<ConfigFunction<TypeScope, String>> captor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.typesInGeneralConfigPart).withDescriptionResolver(captor.capture());
    String description = captor.getValue().apply(field);
    Assert.assertEquals(expectedDescription, description);
}
Also used : FieldScope(com.github.victools.jsonschema.generator.FieldScope) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 3 with ConfigFunction

use of com.github.victools.jsonschema.generator.ConfigFunction in project jsonschema-generator by victools.

the class AdditionalPropertiesModuleTest method testResolveAdditionalProperties.

@Test
@Parameters
@SuppressWarnings("unchecked")
public void testResolveAdditionalProperties(Module moduleInstance, Type expectedAdditionalProperties, Type type, Type[] typeParameters) {
    moduleInstance.applyToConfigBuilder(this.builder);
    ArgumentCaptor<ConfigFunction<TypeScope, Type>> resolverCaptor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.generalConfigPart).withAdditionalPropertiesResolver(resolverCaptor.capture());
    ConfigFunction<TypeScope, Type> additionalPropertiesResolver = resolverCaptor.getValue();
    TypeContext typeContext = this.getContext().getTypeContext();
    TypeScope scope = typeContext.createTypeScope(typeContext.resolve(type, typeParameters));
    Type result = additionalPropertiesResolver.apply(scope);
    if (expectedAdditionalProperties == null || expectedAdditionalProperties == Void.class) {
        Assert.assertEquals(expectedAdditionalProperties, result);
    } else {
        Assert.assertEquals(typeContext.resolve(expectedAdditionalProperties), result);
    }
}
Also used : Type(java.lang.reflect.Type) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) TypeScope(com.github.victools.jsonschema.generator.TypeScope) TypeContext(com.github.victools.jsonschema.generator.TypeContext) Parameters(junitparams.Parameters) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest)

Example 4 with ConfigFunction

use of com.github.victools.jsonschema.generator.ConfigFunction in project jsonschema-generator by victools.

the class JakartaValidationModuleTest method testStringFormatAndPatternResolvers.

private void testStringFormatAndPatternResolvers(String fieldName, String expectedFormat, String expectedPattern) throws Exception {
    TestType testType = new TestType(TestClassForStringProperties.class);
    FieldScope field = testType.getMemberField(fieldName);
    ArgumentCaptor<ConfigFunction<FieldScope, String>> formatCaptor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.fieldConfigPart).withStringFormatResolver(formatCaptor.capture());
    String formatValue = formatCaptor.getValue().apply(field);
    Assert.assertEquals(expectedFormat, formatValue);
    ArgumentCaptor<ConfigFunction<FieldScope, String>> patternCaptor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.fieldConfigPart).withStringPatternResolver(patternCaptor.capture());
    String patternValue = patternCaptor.getValue().apply(field);
    Assert.assertEquals(expectedPattern, patternValue);
}
Also used : FieldScope(com.github.victools.jsonschema.generator.FieldScope) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction)

Example 5 with ConfigFunction

use of com.github.victools.jsonschema.generator.ConfigFunction in project jsonschema-generator by victools.

the class JakartaValidationModuleTest method testValidationGroupSetting.

@Test
@Parameters
@TestCaseName("{method}({0}, {1}, {2}) [{index}]")
public void testValidationGroupSetting(String testCase, String fieldName, Boolean expectedResult, Class<?>[] validationGroups) {
    JakartaValidationModule module = new JakartaValidationModule();
    if (validationGroups != null) {
        module.forValidationGroups(validationGroups);
    }
    module.applyToConfigBuilder(this.configBuilder);
    ArgumentCaptor<ConfigFunction<FieldScope, Boolean>> captor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.fieldConfigPart).withNullableCheck(captor.capture());
    TestType testType = new TestType(TestClassForValidationGroups.class);
    FieldScope field = testType.getMemberField(fieldName);
    Boolean result = captor.getValue().apply(field);
    Assert.assertEquals(expectedResult, result);
}
Also used : FieldScope(com.github.victools.jsonschema.generator.FieldScope) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Parameters(junitparams.Parameters) Test(org.junit.Test) TestCaseName(junitparams.naming.TestCaseName)

Aggregations

ConfigFunction (com.github.victools.jsonschema.generator.ConfigFunction)23 FieldScope (com.github.victools.jsonschema.generator.FieldScope)20 Parameters (junitparams.Parameters)11 Test (org.junit.Test)11 BigInteger (java.math.BigInteger)4 MethodScope (com.github.victools.jsonschema.generator.MethodScope)3 TypeScope (com.github.victools.jsonschema.generator.TypeScope)3 BigDecimal (java.math.BigDecimal)3 TestCaseName (junitparams.naming.TestCaseName)2 JsonClassDescription (com.fasterxml.jackson.annotation.JsonClassDescription)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonPropertyDescription (com.fasterxml.jackson.annotation.JsonPropertyDescription)1 PropertyNamingStrategies (com.fasterxml.jackson.databind.PropertyNamingStrategies)1 JsonNaming (com.fasterxml.jackson.databind.annotation.JsonNaming)1 AbstractTypeAwareTest (com.github.victools.jsonschema.generator.AbstractTypeAwareTest)1 SchemaGeneratorConfigBuilder (com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder)1 SchemaGeneratorConfigPart (com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart)1 SchemaGeneratorGeneralConfigPart (com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart)1 TypeContext (com.github.victools.jsonschema.generator.TypeContext)1 Type (java.lang.reflect.Type)1