<commit_before>= {project-name} - Configuring Your Application

Hardcoded values in your code is a _no go_ (even if we all did it at some point ;-)).
In this guide, we learn how to configure your application.

== Prerequisites

To complete this guide, you need:

* between 5 and 10 minutes
* an IDE
* JDK 1.8+ installed with `JAVA_HOME` configured appropriately
* Apache Maven 3.5.3+

Remember, you need to configure Maven as indicated in the link:maven-config.html[Maven configuration page].

== Solution

We recommend you to follow the instructions in the next sections and create the application step by step.
However, you can go right to the completed example.

Clone the Git repository: `git clone https://github.com/jbossas/protean-quickstarts.git`, or download an https://github.com/jbossas/protean-quickstarts/archive/master.zip[archive].

The solution is located in the `application-configuration` directory.

== Creating the Maven project

First, we need a new project. Create a new project with the following command:

[source, subs=attributes+]
----
mvn org.jboss.shamrock:shamrock-maven-plugin:${shamrock-version}:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=application-configuration \
    -DclassName="org.acme.config.GreetingResource" \
    -Dpath="/greeting"
----

It generates:

* the Maven structure
* a landing page accessible on `http://localhost:8080`
* an example of `Dockerfile`
* the application configuration file
* an `org.acme.config.GreetingResource` resource
* an associated test

== Injecting configuration value

Shamrock uses https://microprofile.io/project/eclipse/microprofile-config[MicroProfile Config] to inject the configuration in the application.
The injection uses 2 annotations: `@Inject` and `@ConfigProperty`:

[source,java]
----
@Inject
@ConfigProperty(name = "greeting.message")
private String message;
----

Edit the `org.acme.config.GreetingResource`, and introduce the 3 following configuration properties:

[source,java]
----
@Inject
@ConfigProperty(name = "greeting.message")
private String message;

@Inject
@ConfigProperty(name = "greeting.suffix", defaultValue="!")
private String suffix;

@Inject
@ConfigProperty(name = "greeting.name")
private Optional<String> name;
----

The first property is _mandatory_. If you do not provide a value, the build fails.
The second property injects the given default value if the configuration file does not provide a value.
The third property is optional. The injected `Optional` is empty as the configuration file does not provide a value.

Now, modify the `hello` method to use the injected properties:

[source,java]
----
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
    return message + " " + name.orElse("world") + suffix;
}
----


== Create the configuration

By default, Shamrock reads `META-INF/microprofile-config.properties`.
Edit the `src/main/resources/META-INF/microprofile-config.properties` with the following content:

[source]
----
# Your configuration properties
greeting.message = hello
greeting.name = shamrock
----

Once set, check the application with:

[source]
----
$ curl  http://localhost:8080/greeting
hello shamrock!
----

TIP: If the application requires configuration values and these values are not set, an error is thrown.
So you can quickly know when your configuration is complete.

== Update the test

We also need to update the functional test to reflect the changes made to endpoint.
Edit the `src/test/java/org/acme/config/GreetingResourceTest.java` file and change the content of the `testHelloEndpoint` method to:


[source, java]
----
package org.acme.config;

import org.jboss.shamrock.test.junit.ShamrockTest;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@ShamrockTest
public class GreetingResourceTest {

    @Test
    public void testHelloEndpoint() {
        given()
          .when().get("/greeting")
          .then()
             .statusCode(200)
             .body(is("hello shamrock!")); // Modified line
    }

}
----

== Package and run the application

Run the application with: `mvn compile shamrock:dev`.
Open your browser to http://localhost:8080/greeting.

Changing the configuration file is immediately reflected.
You can add the `greeting.suffix`, remove the other properties, change the values, etc.

As usual, the application can be packaged using `mvn clean package` and executed using the `-runner.jar` file.
You can also generate the native executable with `mvn clean package -Pnative`<commit_msg>Add a missing final dot<commit_after>= {project-name} - Configuring Your Application

Hardcoded values in your code is a _no go_ (even if we all did it at some point ;-)).
In this guide, we learn how to configure your application.

== Prerequisites

To complete this guide, you need:

* between 5 and 10 minutes
* an IDE
* JDK 1.8+ installed with `JAVA_HOME` configured appropriately
* Apache Maven 3.5.3+

Remember, you need to configure Maven as indicated in the link:maven-config.html[Maven configuration page].

== Solution

We recommend you to follow the instructions in the next sections and create the application step by step.
However, you can go right to the completed example.

Clone the Git repository: `git clone https://github.com/jbossas/protean-quickstarts.git`, or download an https://github.com/jbossas/protean-quickstarts/archive/master.zip[archive].

The solution is located in the `application-configuration` directory.

== Creating the Maven project

First, we need a new project. Create a new project with the following command:

[source, subs=attributes+]
----
mvn org.jboss.shamrock:shamrock-maven-plugin:${shamrock-version}:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=application-configuration \
    -DclassName="org.acme.config.GreetingResource" \
    -Dpath="/greeting"
----

It generates:

* the Maven structure
* a landing page accessible on `http://localhost:8080`
* an example of `Dockerfile`
* the application configuration file
* an `org.acme.config.GreetingResource` resource
* an associated test

== Injecting configuration value

Shamrock uses https://microprofile.io/project/eclipse/microprofile-config[MicroProfile Config] to inject the configuration in the application.
The injection uses 2 annotations: `@Inject` and `@ConfigProperty`:

[source,java]
----
@Inject
@ConfigProperty(name = "greeting.message")
private String message;
----

Edit the `org.acme.config.GreetingResource`, and introduce the 3 following configuration properties:

[source,java]
----
@Inject
@ConfigProperty(name = "greeting.message")
private String message;

@Inject
@ConfigProperty(name = "greeting.suffix", defaultValue="!")
private String suffix;

@Inject
@ConfigProperty(name = "greeting.name")
private Optional<String> name;
----

The first property is _mandatory_. If you do not provide a value, the build fails.
The second property injects the given default value if the configuration file does not provide a value.
The third property is optional. The injected `Optional` is empty as the configuration file does not provide a value.

Now, modify the `hello` method to use the injected properties:

[source,java]
----
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
    return message + " " + name.orElse("world") + suffix;
}
----


== Create the configuration

By default, Shamrock reads `META-INF/microprofile-config.properties`.
Edit the `src/main/resources/META-INF/microprofile-config.properties` with the following content:

[source]
----
# Your configuration properties
greeting.message = hello
greeting.name = shamrock
----

Once set, check the application with:

[source]
----
$ curl  http://localhost:8080/greeting
hello shamrock!
----

TIP: If the application requires configuration values and these values are not set, an error is thrown.
So you can quickly know when your configuration is complete.

== Update the test

We also need to update the functional test to reflect the changes made to endpoint.
Edit the `src/test/java/org/acme/config/GreetingResourceTest.java` file and change the content of the `testHelloEndpoint` method to:


[source, java]
----
package org.acme.config;

import org.jboss.shamrock.test.junit.ShamrockTest;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@ShamrockTest
public class GreetingResourceTest {

    @Test
    public void testHelloEndpoint() {
        given()
          .when().get("/greeting")
          .then()
             .statusCode(200)
             .body(is("hello shamrock!")); // Modified line
    }

}
----

== Package and run the application

Run the application with: `mvn compile shamrock:dev`.
Open your browser to http://localhost:8080/greeting.

Changing the configuration file is immediately reflected.
You can add the `greeting.suffix`, remove the other properties, change the values, etc.

As usual, the application can be packaged using `mvn clean package` and executed using the `-runner.jar` file.
You can also generate the native executable with `mvn clean package -Pnative`.
{'fraction_non_alphanumeric': 0.09900665866171815, 'fraction_numerical': 0.005457919441109049, 'mean_word_length': 4.235428571428572, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '934827', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2584, 'n_words': 975}