Skip to content

Commit

Permalink
issue-29 - Add new annotation to ignore generation (#30)
Browse files Browse the repository at this point in the history
* issue-29 - Add new annotation to ignore generation

* issue-29 - Correction PR for Codacy

* issue-29 - Update solution for List, Map and Set. Plus refactor of previous solution

* issue 29 - Correction of @DslExclude

* issue - 29 - Add new annotation into Readme

* issue - 29 - Correction several style errors

* DslExclude fix & integration with buildExpectedInstace feature

- Fixes template to avoid including excluded fields in dsl with empty values
- Integrates DslExclude with the latest buildExpectedInstace method

* relocate checkstyle.xml

---------

Co-authored-by: Tiago Simoes <[email protected]>
Co-authored-by: Jose E. Garcia Maciñeiras <[email protected]>
Co-authored-by: Alejandro Pena Lorenzo <[email protected]>
Co-authored-by: Alejandro Pena Lorenzo <[email protected]>
  • Loading branch information
5 people authored Nov 22, 2023
1 parent c74db2e commit 90b2c84
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 105 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ That is the only requirement, all other annotations are optional and used for cu
| `@Example` | false | Field | Used to provide an specific value for a field. If not present in a field, the value created will be random. |
| `@Min` | false | Field | Defines the maximum value for numeric fields, or number of elements if applied to collections. Will be ignored if an `@Example` is present. |
| `@Max` | false | Field | Defines the minimum value for numeric fields, or number of elements if applied to collections. Will be ignored if an `@Example` is present. |
| `@DslExclude` | false | Field | Ignore de generation of example values. |

`@Example` values are always provided as String. For Dates and ZonedDateTime the only format supported in this version is the one shown in the example below.
Support for custom date formats will be included in following releases.
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sngular</groupId>
<artifactId>pact-annotation-processor</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>

<name>PactDslBuilder - Annotation Processor</name>
<description>Pact DSL Builder annotation processor.</description>
Expand Down Expand Up @@ -220,7 +220,7 @@
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>

<configLocation>styles/checkstyle.xml</configLocation>
<configLocation>checkstyle.xml</configLocation>
</configuration>
<dependencies>
<dependency>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/sngular/annotation/pact/DslExclude.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sngular.annotation.pact;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface DslExclude {
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import com.sngular.annotation.pact.DslExclude;
import com.sngular.annotation.pact.Example;
import com.sngular.annotation.pact.PactDslBodyBuilder;
import com.sngular.annotation.processor.exception.TemplateFactoryException;
Expand Down Expand Up @@ -211,6 +212,7 @@ private DslComplexField composeDslComplexField(final Element element) {
.needBuilder(checkIfOwn(element))
.complexType(DslComplexTypeEnum.OBJECT)
.fieldValidations(extractValidations(element))
.empty(Objects.nonNull(element.getAnnotation(DslExclude.class)))
.build();
}

Expand All @@ -222,6 +224,7 @@ private DslComplexField composeCollection(final Element element) {
.fields(extractTypes(element))
.fieldValidations(extractValidations(element))
.complexType(DslComplexTypeEnum.COLLECTION)
.empty(Objects.nonNull(element.getAnnotation(DslExclude.class)))
.build();
}

Expand Down Expand Up @@ -299,12 +302,17 @@ private DslSimpleField composeDslSimpleField(final Element fieldElement, final T
.onlyValueFunction(insideCollection)
.suffixValue(mapping.getSuffixValue())
.formatValue(mapping.getFormatValue())
.fieldValidations(validationBuilder.build());
if (Objects.nonNull(fieldElement.getAnnotation(Example.class))) {
.fieldValidations(validationBuilder.build())
.empty(false);

if (Objects.nonNull(fieldElement.getAnnotation(DslExclude.class))) {
simpleFieldBuilder.empty(true);
} else if (Objects.nonNull(fieldElement.getAnnotation(Example.class))) {
simpleFieldBuilder.defaultValue(getDefaultValue(fieldElement, mapping.getFieldType()));
} else {
simpleFieldBuilder.defaultValue(mapping.getRandomDefaultValue(validationBuilder.build()));
}

return simpleFieldBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ public final String getFunctionOnlyValue() {

@Override
public final Integer getRandomDefaultValue(final FieldValidations fieldValidations) {
if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
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);

return uniformRandomProvider.nextInt(minValue, maxValue);
randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
}

return uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
return randomDefaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ public final String getFunctionOnlyValue() {

@Override
public final Number getRandomDefaultValue(final FieldValidations fieldValidations) {
if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
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);

return uniformRandomProvider.nextDouble(minValue, maxValue);
randomDefaultValue = uniformRandomProvider.nextDouble(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextDouble(0, Double.MAX_VALUE);
}

return uniformRandomProvider.nextDouble(0, Double.MAX_VALUE);
return randomDefaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ public final String getFunctionOnlyValue() {

@Override
public final Integer getRandomDefaultValue(final FieldValidations fieldValidations) {
if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
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);

return uniformRandomProvider.nextInt(minValue, maxValue);
randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
}

return uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
return randomDefaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ public final String getFunctionOnlyValue() {

@Override
public final Long getRandomDefaultValue(final FieldValidations fieldValidations) {
if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
final long randomDefaultValue;

if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
final long minValue = Objects.nonNull(fieldValidations.getMin()) ? fieldValidations.getMin() : Long.MIN_VALUE;
final long maxValue = Objects.nonNull(fieldValidations.getMax()) ? fieldValidations.getMax() : Long.MIN_VALUE;

return uniformRandomProvider.nextLong(minValue, maxValue);
randomDefaultValue = uniformRandomProvider.nextLong(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextLong(0, Long.MAX_VALUE);
}

return uniformRandomProvider.nextLong(0, Long.MAX_VALUE);
return randomDefaultValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ public final String getFunctionOnlyValue() {

@Override
public final Integer getRandomDefaultValue(final FieldValidations fieldValidations) {
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);

return uniformRandomProvider.nextInt(minValue, maxValue);
randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
}

return uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
return randomDefaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

public class StringMapping implements TypeMapping<String> {

private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create();

public static final int DEFAULT_MAX = 15;

public static final int DEFAULT_MIN = 1;

private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create();

@Override
public final String getFieldType() {
return "java.lang.String";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public class DslComplexField extends DslField {

boolean needBuilder;

boolean empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class DslSimpleField extends DslField {
FieldValidations fieldValidations;

boolean onlyValueFunction;

boolean empty;
}


Loading

0 comments on commit 90b2c84

Please sign in to comment.