Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlukas authored and sbuettner committed May 29, 2024
1 parent c39c67c commit f878bf5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.RECORD_COMPONENT})
public @interface TemplateProperty {
public static boolean OPTIONAL_DEFAULT = false;

/** Custom property ID that can be referenced in conditions */
String id() default "";
Expand All @@ -52,7 +53,7 @@
String description() default "";

/** Whether the property should be marked as optional in the element template */
boolean optional() default false;
boolean optional() default OPTIONAL_DEFAULT;

/**
* Overrides the property type. By default, the generator will use the field type to determine the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@
package io.camunda.connector.generator.java.processor;

import io.camunda.connector.generator.dsl.PropertyBuilder;
import io.camunda.connector.generator.java.annotation.TemplateProperty;
import io.camunda.connector.generator.java.util.TemplateGenerationContext;
import java.lang.reflect.Field;

public interface FieldProcessor {

void process(
Field field, PropertyBuilder propertyBuilder, final TemplateGenerationContext context);

static boolean isOptional(Field field) {
var annotation = field.getAnnotation(TemplateProperty.class);
if (annotation == null) {
return TemplateProperty.OPTIONAL_DEFAULT;
}
return annotation.optional();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ public void process(
constraintsBuilder.pattern(
new PropertyConstraints.Pattern(pattern.getLeft(), pattern.getRight()));
}

if (pattern != null && !hasNotEmptyConstraint(field) && FieldProcessor.isOptional(field)) {
constraintsBuilder.notEmpty(false);
}
var constraints = constraintsBuilder.build();
if (!isConstraintEmpty(constraints)) {
if (pattern != null && !hasNotEmptyConstraint(field)) {
constraintsBuilder.notEmpty(false);
}
propertyBuilder.constraints(constraints);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void process(
if (annotation == null) {
return;
}
builder.optional(annotation.optional());
builder.optional(FieldProcessor.isOptional(field));

if (!(builder instanceof DropdownPropertyBuilder)) {
if (annotation.feel() == Property.FeelMode.system_default) {
Expand Down Expand Up @@ -182,6 +182,9 @@ private PropertyConstraints buildConstraints(TemplateProperty propertyAnnotation
builder.minLength(constraintsAnnotation.minLength());
}
if (!constraintsAnnotation.pattern().value().isBlank()) {
if (!constraintsAnnotation.notEmpty() && propertyAnnotation.optional()) {
builder.notEmpty(false);
}
builder.pattern(
new PropertyConstraints.Pattern(
constraintsAnnotation.pattern().value(), constraintsAnnotation.pattern().message()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,27 @@ void validationPresent_notEmpty_objectProperty() {
assertThat(notEmptyProperty.getConstraints().minLength()).isNull();
assertThat(notEmptyProperty.getConstraints().maxLength()).isNull();
}

@Test
void validationPresent_Pattern_optional() {
var template = generator.generate(MyConnectorFunction.MinimallyAnnotated.class).getFirst();
var mayBeEmptyOrRegexValidated = getPropertyById("mayBeEmptyOrRegexValidated", template);
assertThat(mayBeEmptyOrRegexValidated.getConstraints()).isNotNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().notEmpty()).isFalse();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().minLength()).isNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().maxLength()).isNull();
}

@Test
void validationPresent_Pattern_optional_jakarta() {
var template = generator.generate(MyConnectorFunction.MinimallyAnnotated.class).getFirst();
var mayBeEmptyOrRegexValidated =
getPropertyById("mayBeEmptyOrRegexValidatedJakartaStyle", template);
assertThat(mayBeEmptyOrRegexValidated.getConstraints()).isNotNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().notEmpty()).isFalse();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().minLength()).isNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().maxLength()).isNull();
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.camunda.connector.generator.java.annotation.TemplateProperty;
import io.camunda.connector.generator.java.annotation.TemplateProperty.DefaultValueType;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyCondition;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyConstraints;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyType;
import io.camunda.connector.generator.java.annotation.TemplateSubType;
import io.camunda.connector.generator.java.example.outbound.MyConnectorInput.AnnotatedSealedType.FirstAnnotatedSubType;
Expand Down Expand Up @@ -99,7 +100,17 @@ public record MyConnectorInput(
@TemplateProperty(
id = "dependsOnBooleanPropertyTrue",
condition = @PropertyCondition(property = "booleanProperty", equals = "true"))
String dependsOnBooleanPropertyTrue) {
String dependsOnBooleanPropertyTrue,
@TemplateProperty(
id = "mayBeEmptyOrRegexValidated",
optional = true,
constraints =
@PropertyConstraints(
pattern = @TemplateProperty.Pattern(value = "xxx", message = "Oh no!")))
String mayBeEmptyOrRegexValidated,
@TemplateProperty(id = "mayBeEmptyOrRegexValidatedJakartaStyle", optional = true)
@Pattern(regexp = "xxx", message = "Oh no!")
String mayBeEmptyOrRegexValidatedJakartaStyle) {

sealed interface NonAnnotatedSealedType permits FirstSubType, NestedSealedType, SecondSubType {

Expand Down

0 comments on commit f878bf5

Please sign in to comment.