Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from ralf-ueberfuhr-ars/feature/config
Browse files Browse the repository at this point in the history
Make initializer configurable.
  • Loading branch information
ralf-ueberfuhr-ars authored Jun 20, 2024
2 parents 0e30a8f + a7e97df commit 76ddc4d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,69 @@
package de.schulung.sample.quarkus.domain;

import io.quarkus.arc.properties.IfBuildProperty;
import io.quarkus.runtime.Startup;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.eclipse.microprofile.config.inject.ConfigProperties;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.time.LocalDate;
import java.time.Month;

@Dependent // destroy object immediately
@RequiredArgsConstructor
//@IfBuildProfile("dev") - nur für lokale Entwicklung
//@UnlessBuildProfile("prod") - NICHT für prod
@IfBuildProperty(
name = "customers.initialization.enabled",
stringValue = "true"
)
public class CustomersServiceInitializer {

@Getter
@Setter
@ConfigProperties(prefix = "customers.initialization.sample")
@Dependent
public static class InitialCustomerConfig {

@ConfigProperty(defaultValue = "John Doe")
String name;
@ConfigProperty(defaultValue = "2010-04-05")
String birthdate;

}

/*
* @ConfigProperties ist ein CDI @Qualifier
* -> beim Nutzer: @Inject @ConfigProperties
* -> Probleme mit @RequiredArgsConstructor von Lombox
* -> hier ein CDI Producer, der das Object auch als @Default bereitstellt
*/
@Dependent
public static class InitialCustomerConfigProvider {

@Inject
@ConfigProperties
@Getter(onMethod_ = {
@Produces,
@Default
})
InitialCustomerConfig cfg;

}

private final CustomersService service;
private final InitialCustomerConfig customerConfig;

@Startup
public void initializeData() {
var customer = Customer.builder()
.name("Tom")
.birthdate(LocalDate.of(2000, Month.DECEMBER, 6))
.name(customerConfig.getName())
.birthdate(LocalDate.parse(customerConfig.getBirthdate()))
.build();
service.createCustomer(customer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.schulung.sample.quarkus.further.mpconfig;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Qualifier;
import lombok.Getter;
import org.eclipse.microprofile.config.inject.ConfigProperty;

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

@ApplicationScoped
public class ConfigurationPropertiesProvider {

@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.TYPE,
ElementType.FIELD,
ElementType.METHOD,
ElementType.PARAMETER
})
@Qualifier
public @interface InitialCustomerName {
}

@ConfigProperty(
name = "customers.initialization.sample.name",
defaultValue = "John Doe"
)
@Getter(onMethod_ = {
@Produces,
@InitialCustomerName
})
String initialCustomerName;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.schulung.sample.quarkus.further.mpconfig;

import de.schulung.sample.quarkus.further.mpconfig.ConfigurationPropertiesProvider.InitialCustomerName;
import io.quarkus.arc.profile.UnlessBuildProfile;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@UnlessBuildProfile("prod")
@Path("/config-properties")
public class ConfigurationTestResource {

@Inject
@InitialCustomerName
String customerName;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String answer() {
return "Initial customer name: " + customerName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ quarkus.log.category."performance".level=DEBUG
quarkus.log.min-level=DEBUG
quarkus.log.console.enable=true
quarkus.log.console.format=%d{HH:mm:ss} %-5p [%c{2.}]] (%t) %s%e%n
quarkus.log.console.level=DEBUG
quarkus.log.console.level=DEBUG

customers.initialization.enabled=false
%dev.customers.initialization.enabled=true

customers.initialization.sample.name=Max
%dev.customers.initialization.sample.name=Julia
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package de.schulung.sample.quarkus.domain;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
@TestProfile(CustomersServiceInitializerTests.InitializationEnabledProfile.class)
public class CustomersServiceInitializerTests {

@Inject
Expand All @@ -18,5 +23,15 @@ void shouldHaveAtLeastOneCustomer() {
.hasAtLeastOneElementOfType(Customer.class);
}

public static class InitializationEnabledProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"customers.initialization.enabled",
"true"
);
}
}


}

0 comments on commit 76ddc4d

Please sign in to comment.