Skip to content

Commit

Permalink
Update SmallRye Config to 3.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Dec 4, 2024
1 parent 57f0e20 commit 002ae3c
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 300 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<microprofile-lra.version>2.0</microprofile-lra.version>
<microprofile-openapi.version>4.0.2</microprofile-openapi.version>
<smallrye-common.version>2.8.0</smallrye-common.version>
<smallrye-config.version>3.10.2</smallrye-config.version>
<smallrye-config.version>3.10.3-SNAPSHOT</smallrye-config.version>
<smallrye-health.version>4.1.0</smallrye-health.version>
<smallrye-metrics.version>4.0.0</smallrye-metrics.version>
<smallrye-open-api.version>4.0.3</smallrye-open-api.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,76 @@
package io.quarkus.hibernate.validator.test.config;

import static org.junit.jupiter.api.Assertions.assertThrows;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.Duration;
import java.time.Period;
import java.util.List;

import jakarta.inject.Inject;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.function.Consumer;

import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Unremovable;
import io.quarkus.runtime.configuration.DurationConverter;
import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.ConfigValidationException;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.WithConverter;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;
import io.smallrye.config.WithParentName;

public class ConfigMappingInvalidTest {
@RegisterExtension
static final QuarkusUnitTest UNIT_TEST = new QuarkusUnitTest().setArchiveProducer(
static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset("validator.server.host=localhost\n" +
"validator.server.services=redis,postgresql\n" +
"validator.hierarchy.number=1\n" +
"validator.repeatable.name=a"), "application.properties"));

@Inject
SmallRyeConfig config;
.addAsResource("application-mappings-validation.properties", "application.properties"))
.assertException(new Consumer<Throwable>() {
@Override
public void accept(final Throwable throwable) {
// can't use instanceOf because of different ClassLoaders
assertEquals(ConfigValidationException.class.getName(), throwable.getClass().getName());
String message = throwable.getMessage();
assertTrue(message.contains("validator.server.host must be less than or equal to 3"));
assertTrue(message.contains("validator.hierarchy.number must be greater than or equal to 10"));
assertTrue(message.contains("validator.repeatable.name size must be between 2"));

assertTrue(message.contains("cloud.port must be greater than or equal to 8000"));
assertTrue(message.contains("cloud.log.days must be less than or equal to 15"));
assertTrue(message.contains("cloud.cors.origins[1].port must be greater than or equal to 8000"));
assertTrue(message.contains("cloud.info.name size must be between 0 and 3"));
assertTrue(message.contains("cloud.info.code must be less than or equal to 3"));
assertTrue(message.contains("cloud.info.alias[0] size must be between 0 and 3"));
assertTrue(message.contains("cloud.info.admins.root[1].username size must be between 0 and 4"));
assertTrue(message.contains("cloud.info.firewall.accepted[1] size must be between 8 and 15"));
assertTrue(message.contains("cloud.proxy.timeout must be less than or equal to 10"));
assertTrue(message.contains("cloud server is not prod"));
}
});

@Test
void invalid() {
assertThrows(ConfigValidationException.class, () -> config.getConfigMapping(Server.class),
"validator.server.host must be less than or equal to 3");
}

@Test
@Disabled("Requires https://github.com/smallrye/smallrye-config/pull/923")
void invalidHierarchy() {
assertThrows(ConfigValidationException.class, () -> config.getConfigMapping(Child.class),
"validator.hierarchy.number must be greater than or equal to 10");
}

@Test
void repeatable() {
assertThrows(ConfigValidationException.class, () -> config.getConfigMapping(Repeatable.class));
fail();
}

@Unremovable
Expand Down Expand Up @@ -80,4 +100,138 @@ public interface Repeatable {
@Size(min = 2)
String name();
}

@Unremovable
@ConfigMapping(prefix = "cloud")
@Prod
public interface Cloud {
String host();

@Min(8000)
int port();

@WithConverter(DurationConverter.class)
Duration timeout();

@WithName("io-threads")
int threads();

@WithParentName
Map<String, Form> form();

Optional<Ssl> ssl();

Optional<Proxy> proxy();

Optional<Cors> cors();

Log log();

Info info();

interface Form {
String loginPage();

String errorPage();

String landingPage();

Optional<String> cookie();

@WithDefault("1")
List<Integer> positions();
}

interface Ssl {
int port();

String certificate();

@WithDefault("TLSv1.3,TLSv1.2")
List<String> protocols();
}

interface Proxy {
boolean enable();

@Max(10)
int timeout();
}

interface Log {
@WithDefault("false")
boolean enabled();

@WithDefault(".log")
String suffix();

@WithDefault("true")
boolean rotate();

@WithDefault("COMMON")
Pattern pattern();

Period period();

@Max(15)
int days();

enum Pattern {
COMMON,
SHORT,
COMBINED,
LONG;
}
}

interface Cors {
List<Origin> origins();

List<@Size(min = 2) String> methods();

interface Origin {
@Size(min = 5)
String host();

@Min(8000)
int port();
}
}

interface Info {
Optional<@Size(max = 3) String> name();

@Max(3)
OptionalInt code();

Optional<List<@Size(max = 3) String>> alias();

Map<String, List<Admin>> admins();

Map<String, List<@Size(min = 8, max = 15) String>> firewall();

interface Admin {
@Size(max = 4)
String username();
}
}
}

@Target({ ElementType.TYPE_USE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = Prod.Validator.class)
public @interface Prod {
String message() default "server is not prod";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

class Validator implements ConstraintValidator<Prod, Cloud> {
@Override
public boolean isValid(final Cloud value, final ConstraintValidatorContext context) {
return value.host().equals("prod");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
validator.server.host=localhost
validator.server.services=redis,postgresql
validator.hierarchy.number=1
validator.repeatable.name=a

cloud.host=localhost
cloud.port=5000
cloud.io-threads=200
cloud.timeout=60s
cloud.form.error-page=error.html
cloud.form.landing-page=index.html
cloud.form.login-page=login.html
cloud.info.admins.root[0].username=root
cloud.info.admins.root[1].username=super
cloud.info.alias=James
cloud.info.alias[0]=James
cloud.info.code=007
cloud.info.firewall.accepted=127.0.0.1,8.8.8
cloud.info.firewall.accepted[0]=127.0.0.1
cloud.info.firewall.accepted[1]=8.8.8
cloud.info.name=Bond
cloud.log.days=20
cloud.log.period=P1D
cloud.proxy.enable=true
cloud.proxy.timeout=20
cloud.ssl.certificate=certificate
cloud.ssl.port=8443
cloud.cors.methods=GET,POST
cloud.cors.methods[0]=GET
cloud.cors.methods[1]=POST
cloud.cors.origins[0].host=some-server
cloud.cors.origins[0].port=9000
cloud.cors.origins[1].host=localhost
cloud.cors.origins[1].port=1
Loading

0 comments on commit 002ae3c

Please sign in to comment.