diff --git a/pom.xml b/pom.xml index 3eaac0f..7af0640 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.sngular pact-annotation-processor - 1.1.8 + 1.1.9 PactDslBuilder - Annotation Processor Pact DSL Builder annotation processor. diff --git a/src/main/java/com/sngular/annotation/processor/PactDslProcessor.java b/src/main/java/com/sngular/annotation/processor/PactDslProcessor.java index 9747f24..742ffb5 100644 --- a/src/main/java/com/sngular/annotation/processor/PactDslProcessor.java +++ b/src/main/java/com/sngular/annotation/processor/PactDslProcessor.java @@ -37,12 +37,14 @@ import com.sngular.annotation.pact.PactDslBodyBuilder; import com.sngular.annotation.processor.exception.TemplateFactoryException; import com.sngular.annotation.processor.exception.TemplateGenerationException; +import com.sngular.annotation.processor.mapping.BigDecimalMapping; import com.sngular.annotation.processor.mapping.BigIntegerMapping; import com.sngular.annotation.processor.mapping.BooleanMapping; import com.sngular.annotation.processor.mapping.ByteMapping; import com.sngular.annotation.processor.mapping.CharMapping; import com.sngular.annotation.processor.mapping.DateMapping; -import com.sngular.annotation.processor.mapping.DecimalMapping; +import com.sngular.annotation.processor.mapping.DoubleMapping; +import com.sngular.annotation.processor.mapping.FloatMapping; import com.sngular.annotation.processor.mapping.IntegerMapping; import com.sngular.annotation.processor.mapping.LongMapping; import com.sngular.annotation.processor.mapping.ShortMapping; @@ -72,33 +74,24 @@ public class PactDslProcessor extends AbstractProcessor { static final Map TYPE_MAPPING = ImmutableMap.builder() .put("int", new IntegerMapping()) - .put("java.lang.Integer", new IntegerMapping()) .put("Integer", new IntegerMapping()) - .put("java.math.BigInteger", new BigIntegerMapping()) .put("BigInteger", new BigIntegerMapping()) - .put("biginteger", new BigIntegerMapping()) .put("short", new ShortMapping()) - .put("java.lang.Short", new ShortMapping()) .put("Short", new ShortMapping()) .put("byte", new ByteMapping()) + .put("Byte", new ByteMapping()) .put("long", new LongMapping()) - .put("java.lang.Long", new LongMapping()) .put("Long", new LongMapping()) .put("char", new CharMapping()) .put("Character", new CharMapping()) - .put("java.lang.Character", new CharMapping()) - .put("java.lang.String", new StringMapping()) .put("String", new StringMapping()) - .put("float", new DecimalMapping()) - .put("Float", new DecimalMapping()) - .put("double", new DecimalMapping()) - .put("java.lang.Double", new DecimalMapping()) - .put("Double", new DecimalMapping()) - .put("java.math.BigDecimal", new DecimalMapping()) - .put("BigDecimal", new DecimalMapping()) + .put("float", new FloatMapping()) + .put("Float", new FloatMapping()) + .put("double", new DoubleMapping()) + .put("Double", new DoubleMapping()) + .put("BigDecimal", new BigDecimalMapping()) .put("boolean", new BooleanMapping()) .put("Boolean", new BooleanMapping()) - .put("java.lang.Boolean", new BooleanMapping()) .put("date", new DateMapping()) .put("java.time.ZonedDateTime", new ZonedDateTimeMapping()) .put("ZonedDateTime", new ZonedDateTimeMapping()) @@ -330,15 +323,15 @@ private static Object getDefaultValue(final Element fieldElement, final String t final Object realValue; final String value = fieldElement.getAnnotation(Example.class).value(); if (NumberUtils.isCreatable(value)) { - realValue = switch (type.toLowerCase()) { - case "integer", "int" -> NumberUtils.toInt(value); - case "biginteger" -> NumberUtils.createBigInteger(value); - case "long" -> NumberUtils.toLong(value); - case "short" -> NumberUtils.toShort(value); - case "byte" -> NumberUtils.toByte(value); - case "float" -> NumberUtils.toFloat(value); - case "double" -> NumberUtils.toDouble(value); - case "bigdecimal", "java.math.bigdecimal" -> NumberUtils.createNumber(value); + realValue = switch (type) { + case "int", "Integer" -> NumberUtils.toInt(value); + case "BigInteger" -> NumberUtils.createBigInteger(value); + case "long", "Long" -> NumberUtils.toLong(value); + case "short", "Short" -> NumberUtils.toShort(value); + case "byte", "Byte" -> NumberUtils.toByte(value); + case "float", "Float" -> NumberUtils.toFloat(value); + case "double", "Double" -> NumberUtils.toDouble(value); + case "BigDecimal" -> NumberUtils.createBigDecimal(value); default -> throw new IllegalStateException("Unexpected value: " + type); }; } else { diff --git a/src/main/java/com/sngular/annotation/processor/mapping/BigDecimalMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/BigDecimalMapping.java new file mode 100644 index 0000000..d3df9e6 --- /dev/null +++ b/src/main/java/com/sngular/annotation/processor/mapping/BigDecimalMapping.java @@ -0,0 +1,50 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * * License, v. 2.0. If a copy of the MPL was not distributed with this + * * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.sngular.annotation.processor.mapping; + +import java.util.Objects; + +import com.sngular.annotation.processor.model.FieldValidations; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.simple.RandomSource; + +public class BigDecimalMapping implements TypeMapping { + + private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create(); + + @Override + public final String getFieldType() { + return "BigDecimal"; + } + + @Override + public final String getFunctionType() { + return "decimalType"; + } + + @Override + public final String getFunctionOnlyValue() { + return "decimalValue"; + } + + @Override + public final Number getRandomDefaultValue(final FieldValidations fieldValidations) { + final Number randomDefaultValue; + + if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) { + final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), 0); + final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Double.MAX_VALUE); + + randomDefaultValue = uniformRandomProvider.nextDouble(minValue, maxValue); + } else { + randomDefaultValue = uniformRandomProvider.nextDouble(Double.MIN_VALUE, Double.MAX_VALUE); + } + + return randomDefaultValue; + } +} diff --git a/src/main/java/com/sngular/annotation/processor/mapping/BigIntegerMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/BigIntegerMapping.java index 58a0a1b..101acd7 100644 --- a/src/main/java/com/sngular/annotation/processor/mapping/BigIntegerMapping.java +++ b/src/main/java/com/sngular/annotation/processor/mapping/BigIntegerMapping.java @@ -37,12 +37,12 @@ public final Integer getRandomDefaultValue(final FieldValidations fieldValidatio final int randomDefaultValue; if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) { - final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Byte.MIN_VALUE); - final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Byte.MAX_VALUE); + final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), Integer.MIN_VALUE); + final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), Integer.MAX_VALUE); randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue); } else { - randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE); + randomDefaultValue = uniformRandomProvider.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); } return randomDefaultValue; diff --git a/src/main/java/com/sngular/annotation/processor/mapping/ByteMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/ByteMapping.java index a5470f6..20f87d4 100644 --- a/src/main/java/com/sngular/annotation/processor/mapping/ByteMapping.java +++ b/src/main/java/com/sngular/annotation/processor/mapping/ByteMapping.java @@ -24,12 +24,12 @@ public final String getFieldType() { @Override public final String getFunctionType() { - return "byteType"; + return "integerType"; } @Override public final String getFunctionOnlyValue() { - return "byteValue"; + return "integerType"; } @Override @@ -42,7 +42,7 @@ public final Integer getRandomDefaultValue(final FieldValidations fieldValidatio randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue); } else { - randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE); + randomDefaultValue = uniformRandomProvider.nextInt(Byte.MIN_VALUE, Byte.MAX_VALUE); } return randomDefaultValue; diff --git a/src/main/java/com/sngular/annotation/processor/mapping/DecimalMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/DoubleMapping.java similarity index 87% rename from src/main/java/com/sngular/annotation/processor/mapping/DecimalMapping.java rename to src/main/java/com/sngular/annotation/processor/mapping/DoubleMapping.java index 18a3050..b77a1ba 100644 --- a/src/main/java/com/sngular/annotation/processor/mapping/DecimalMapping.java +++ b/src/main/java/com/sngular/annotation/processor/mapping/DoubleMapping.java @@ -13,13 +13,13 @@ import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.simple.RandomSource; -public class DecimalMapping implements TypeMapping { +public class DoubleMapping implements TypeMapping { private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create(); @Override public final String getFieldType() { - return "java.math.BigDecimal"; + return "double"; } @Override @@ -37,8 +37,8 @@ public final Number getRandomDefaultValue(final FieldValidations fieldValidation final Number randomDefaultValue; if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) { - final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Byte.MIN_VALUE); - final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Byte.MAX_VALUE); + final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), 0); + final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Double.MAX_VALUE); randomDefaultValue = uniformRandomProvider.nextDouble(minValue, maxValue); } else { @@ -47,4 +47,9 @@ public final Number getRandomDefaultValue(final FieldValidations fieldValidation return randomDefaultValue; } + + @Override + public final String getSuffixValue() { + return "D"; + } } diff --git a/src/main/java/com/sngular/annotation/processor/mapping/FloatMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/FloatMapping.java new file mode 100644 index 0000000..0881b56 --- /dev/null +++ b/src/main/java/com/sngular/annotation/processor/mapping/FloatMapping.java @@ -0,0 +1,55 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * * License, v. 2.0. If a copy of the MPL was not distributed with this + * * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.sngular.annotation.processor.mapping; + +import java.util.Objects; + +import com.sngular.annotation.processor.model.FieldValidations; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.simple.RandomSource; + +public class FloatMapping implements TypeMapping { + + private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create(); + + @Override + public final String getFieldType() { + return "float"; + } + + @Override + public final String getFunctionType() { + return "decimalType"; + } + + @Override + public final String getFunctionOnlyValue() { + return "decimalValue"; + } + + @Override + public final Number getRandomDefaultValue(final FieldValidations fieldValidations) { + final Number randomDefaultValue; + + if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) { + final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), 0); + final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Float.MAX_VALUE); + + randomDefaultValue = uniformRandomProvider.nextDouble(minValue, maxValue); + } else { + randomDefaultValue = uniformRandomProvider.nextDouble(0, Float.MAX_VALUE); + } + + return randomDefaultValue; + } + + @Override + public final String getSuffixValue() { + return "F"; + } +} diff --git a/src/main/java/com/sngular/annotation/processor/mapping/IntegerMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/IntegerMapping.java index d9436a9..ed7d3c0 100644 --- a/src/main/java/com/sngular/annotation/processor/mapping/IntegerMapping.java +++ b/src/main/java/com/sngular/annotation/processor/mapping/IntegerMapping.java @@ -37,12 +37,12 @@ public final Integer getRandomDefaultValue(final FieldValidations fieldValidatio final int randomDefaultValue; if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) { - final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Byte.MIN_VALUE); - final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Byte.MAX_VALUE); + final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), Integer.MIN_VALUE); + final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), Integer.MAX_VALUE); randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue); } else { - randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE); + randomDefaultValue = uniformRandomProvider.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); } return randomDefaultValue; diff --git a/src/main/java/com/sngular/annotation/processor/mapping/LongMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/LongMapping.java index 2272e24..0565a5a 100644 --- a/src/main/java/com/sngular/annotation/processor/mapping/LongMapping.java +++ b/src/main/java/com/sngular/annotation/processor/mapping/LongMapping.java @@ -42,7 +42,7 @@ public final Long getRandomDefaultValue(final FieldValidations fieldValidations) randomDefaultValue = uniformRandomProvider.nextLong(minValue, maxValue); } else { - randomDefaultValue = uniformRandomProvider.nextLong(0, Long.MAX_VALUE); + randomDefaultValue = uniformRandomProvider.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); } return randomDefaultValue; diff --git a/src/main/java/com/sngular/annotation/processor/mapping/ShortMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/ShortMapping.java index aa33e8a..f29984e 100644 --- a/src/main/java/com/sngular/annotation/processor/mapping/ShortMapping.java +++ b/src/main/java/com/sngular/annotation/processor/mapping/ShortMapping.java @@ -29,18 +29,15 @@ public final String getFunctionType() { @Override public final String getFunctionOnlyValue() { - return "shortValue"; + return "integerValue"; } @Override public final Integer getRandomDefaultValue(final FieldValidations fieldValidations) { final int randomDefaultValue; if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) { - int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Short.MIN_VALUE); - minValue = minValue < Short.MIN_VALUE ? minValue : Short.MIN_VALUE; - - int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Short.MAX_VALUE); - maxValue = maxValue > Short.MAX_VALUE ? maxValue : Short.MAX_VALUE; + final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Short.MIN_VALUE); + final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Short.MAX_VALUE); randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue); } else { diff --git a/src/main/java/com/sngular/annotation/processor/mapping/StringMapping.java b/src/main/java/com/sngular/annotation/processor/mapping/StringMapping.java index bcb40dc..60b5c9c 100644 --- a/src/main/java/com/sngular/annotation/processor/mapping/StringMapping.java +++ b/src/main/java/com/sngular/annotation/processor/mapping/StringMapping.java @@ -24,7 +24,7 @@ public class StringMapping implements TypeMapping { @Override public final String getFieldType() { - return "java.lang.String"; + return "String"; } @Override diff --git a/src/main/resources/templates/templateDslBuilder.ftlh b/src/main/resources/templates/templateDslBuilder.ftlh index 7532c23..88d49ee 100644 --- a/src/main/resources/templates/templateDslBuilder.ftlh +++ b/src/main/resources/templates/templateDslBuilder.ftlh @@ -1,15 +1,17 @@ <#macro writeAttrField field> <#if field.functionByType??> - <#if field.fieldType == "char"> + <#if field.fieldType == "boolean"> + ${field.fieldType} ${field.name} = ${field.defaultValue?string}; + <#elseif field.fieldType == "char"> String ${field.name} = "${field.defaultValue}"; <#elseif field.defaultValue?is_number> - <#if field.functionByType == "decimalType"> - ${field.fieldType} ${field.name} = new BigDecimal("${field.defaultValue?c}"); - <#elseif field.fieldType == "BigInteger"> - ${field.fieldType} ${field.name} = new BigInteger("${field.defaultValue?c}"); + <#if field.fieldType == "BigInteger"> + BigInteger ${field.name} = new BigInteger("${field.defaultValue?c}"); + <#elseif field.fieldType == "BigDecimal"> + BigDecimal ${field.name} = new BigDecimal("${field.defaultValue?c}"); <#else> - ${field.fieldType} ${field.name} = ${field.defaultValue?c}<#if field.suffixValue?has_content && field.suffixValue == "L">L; + ${field.fieldType} ${field.name} = ${field.defaultValue?c}<#if field.suffixValue?has_content>${field.suffixValue}; <#elseif field.defaultValue?is_boolean> ${field.fieldType} ${field.name} = ${field.defaultValue?then('true', 'false')}; @@ -21,7 +23,7 @@ ${field.fieldType} ${field.name} = "${field.defaultValue}"; <#else> - <@writeComplexAttrField complexField=field/> + <@writeComplexAttrField complexField=field/> @@ -105,6 +107,10 @@ pactDslJsonBody.${field.functionByType}("${field.name}", (int) ${field.name}); <#elseif field.fieldType == "BigInteger"> pactDslJsonBody.${field.functionByType}("${field.name}", ${field.name}.intValue()); + <#elseif field.fieldType == "float"> + pactDslJsonBody.${field.functionByType}("${field.name}", (double) ${field.name}); + <#elseif field.fieldType == "byte"> + pactDslJsonBody.${field.functionByType}("${field.name}", (int) ${field.name}); <#else> pactDslJsonBody.${field.functionByType}("${field.name}", ${field.name});