This is an automated email from the ASF dual-hosted git repository.

gitgabrio pushed a commit to branch main
in repository 
https://gitbox.apache.org/repos/asf/incubator-kie-kogito-runtimes.git


The following commit(s) were added to refs/heads/main by this push:
     new dd65b9ec2a [incubator-kie-issues#2163] Implement property retrieval 
from YAML files (#4254)
dd65b9ec2a is described below

commit dd65b9ec2aaaa1559666aa5410fd4355a83b87bc
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Thu Apr 16 10:32:20 2026 +0000

    [incubator-kie-issues#2163] Implement property retrieval from YAML files 
(#4254)
    
    * [incubator-kie-issues#2163] Implement property retrieval from YAML files
    
    * [incubator-kie-issues#2163] Fix FileReader usage
    
    ---------
    
    Co-authored-by: Gabriele-Cardosi <[email protected]>
---
 kogito-codegen-modules/kogito-codegen-api/pom.xml  | 11 +++
 .../context/impl/AbstractKogitoBuildContext.java   | 56 ++++++++++++++
 .../impl/AbstractKogitoBuildContextTest.java       | 86 ++++++++++++++++++++++
 .../src/test/resources/application.yaml            | 34 +++++++++
 .../src/test/resources/application.yml             | 34 +++++++++
 5 files changed, 221 insertions(+)

diff --git a/kogito-codegen-modules/kogito-codegen-api/pom.xml 
b/kogito-codegen-modules/kogito-codegen-api/pom.xml
index ee2837e05d..37eb7c0cfb 100644
--- a/kogito-codegen-modules/kogito-codegen-api/pom.xml
+++ b/kogito-codegen-modules/kogito-codegen-api/pom.xml
@@ -33,6 +33,7 @@
 
   <properties>
     <java.module.name>org.kie.kogito.codegen.api</java.module.name>
+    <version.org.yaml.snakeyaml>2.6</version.org.yaml.snakeyaml>
   </properties>
 
     <dependencyManagement>
@@ -44,6 +45,11 @@
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
+            <dependency>
+                <groupId>org.yaml</groupId>
+                <artifactId>snakeyaml</artifactId>
+                <version>${version.org.yaml.snakeyaml}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
@@ -68,6 +74,11 @@
             <artifactId>kogito-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.yaml</groupId>
+            <artifactId>snakeyaml</artifactId>
+        </dependency>
+
         <!-- test -->
         <dependency>
             <groupId>org.junit.jupiter</groupId>
diff --git 
a/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContext.java
 
b/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContext.java
index f8cb06a9a0..907b305f72 100644
--- 
a/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContext.java
+++ 
b/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContext.java
@@ -21,6 +21,7 @@ package org.kie.kogito.codegen.api.context.impl;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.text.MessageFormat;
 import java.util.Collection;
 import java.util.Collections;
@@ -31,6 +32,7 @@ import java.util.Objects;
 import java.util.Optional;
 import java.util.Properties;
 import java.util.Set;
+import java.util.TreeMap;
 import java.util.function.Predicate;
 
 import javax.lang.model.SourceVersion;
@@ -47,6 +49,7 @@ import org.kie.kogito.codegen.api.context.KogitoBuildContext;
 import org.kie.kogito.codegen.api.utils.AddonsConfigDiscovery;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
 
 public abstract class AbstractKogitoBuildContext implements KogitoBuildContext 
{
 
@@ -100,11 +103,64 @@ public abstract class AbstractKogitoBuildContext 
implements KogitoBuildContext {
             } catch (IOException ioe) {
                 LOGGER.debug("Unable to load '" + 
APPLICATION_PROPERTIES_FILE_NAME + "'.");
             }
+            File ymlFile = new File(resourcePath, 
APPLICATION_PROPERTIES_YML_FILE_NAME);
+            loadYmlProperties(ymlFile, applicationProperties);
+            ymlFile = new File(resourcePath, 
APPLICATION_PROPERTIES_YAML_FILE_NAME);
+            loadYmlProperties(ymlFile, applicationProperties);
         }
 
         return applicationProperties;
     }
 
+    protected static void loadYmlProperties(File ymlFile, Properties 
applicationProperties) {
+        Map<String, String> ymlMap = loadYmlStringMap(ymlFile);
+        if (ymlMap != null) {
+            applicationProperties.putAll(ymlMap);
+        }
+    }
+
+    protected static Map<String, String> loadYmlStringMap(File ymlFile) {
+        TreeMap<String, Object> ymlMap = loadYmlMap(ymlFile);
+        if (ymlMap != null) {
+            return convertYamlObjectToMap(ymlMap);
+        } else {
+            return null;
+        }
+    }
+
+    protected static TreeMap<String, Object> loadYmlMap(File ymlFile) {
+        if (ymlFile.exists() && ymlFile.isFile() && ymlFile.canRead()) {
+            Yaml yaml = new Yaml();
+            try (FileReader yamlFileReader = new FileReader(ymlFile, 
StandardCharsets.UTF_8)) {
+                return yaml.loadAs(yamlFileReader, TreeMap.class);
+            } catch (IOException e) {
+                LOGGER.debug("Unable to load '{}'.", ymlFile.getName(), e);
+            }
+        } else {
+            LOGGER.debug("Unable to load '{}'.", ymlFile.getName());
+        }
+        return null;
+    }
+
+    protected static Map<String, String> 
convertYamlObjectToMap(TreeMap<String, Object> toConvert) {
+        Map<String, String> toReturn = new HashMap<>();
+        convertYamlObjectToMap(toConvert, new StringBuilder(), toReturn);
+        return toReturn;
+    }
+
+    protected static void convertYamlObjectToMap(Map<String, Object> toRead, 
StringBuilder builder, Map<String, String> toPopulate) {
+        toRead.forEach((key, value) -> {
+            if (value instanceof Map) {
+                StringBuilder newBuilder = new StringBuilder(builder);
+                convertYamlObjectToMap((Map<String, Object>) value, 
newBuilder.append(key).append("."), toPopulate);
+            } else {
+                String property = builder.toString() + key;
+                String propertyValue = value != null ? value.toString() : "";
+                toPopulate.put(property, propertyValue);
+            }
+        });
+    }
+
     @Override
     public boolean hasClassAvailable(String fqcn) {
         return classAvailabilityResolver.test(fqcn);
diff --git 
a/kogito-codegen-modules/kogito-codegen-api/src/test/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContextTest.java
 
b/kogito-codegen-modules/kogito-codegen-api/src/test/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContextTest.java
index 61b4e50dfa..d47119621b 100644
--- 
a/kogito-codegen-modules/kogito-codegen-api/src/test/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContextTest.java
+++ 
b/kogito-codegen-modules/kogito-codegen-api/src/test/java/org/kie/kogito/codegen/api/context/impl/AbstractKogitoBuildContextTest.java
@@ -18,16 +18,28 @@
  */
 package org.kie.kogito.codegen.api.context.impl;
 
+import java.io.File;
+import java.util.Collection;
+import java.util.Map;
 import java.util.Properties;
+import java.util.TreeMap;
 
+import org.drools.util.FileUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
 import org.kie.kogito.KogitoGAV;
 import org.kie.kogito.codegen.api.AddonsConfig;
 import org.kie.kogito.codegen.api.context.KogitoBuildContext;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static 
org.drools.codegen.common.DroolsModelBuildContext.APPLICATION_PROPERTIES_YAML_FILE_NAME;
+import static 
org.drools.codegen.common.DroolsModelBuildContext.APPLICATION_PROPERTIES_YML_FILE_NAME;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class AbstractKogitoBuildContextTest {
 
@@ -87,6 +99,80 @@ class AbstractKogitoBuildContextTest {
         
assertThat(builder.withGAV(KogitoGAV.EMPTY_GAV).build().getGAV()).hasValue(KogitoGAV.EMPTY_GAV);
     }
 
+    @MethodSource("testData")
+    @ParameterizedTest
+    void loadYmlProperties(String fileName) {
+        File ymlFile = FileUtils.getFile(fileName);
+        assertTrue(ymlFile.exists());
+        Properties properties = new Properties();
+        AbstractKogitoBuildContext.loadYmlProperties(ymlFile, properties);
+        assertEquals("test", properties.getProperty("this.is.a"));
+        assertEquals("notNull", properties.getProperty("this.is.b"));
+        assertEquals("test", properties.getProperty("this.was.a"));
+        assertEquals("notNull", properties.getProperty("this.was.b"));
+        assertEquals("test", properties.getProperty("that.is.a"));
+        assertEquals("notNull", properties.getProperty("that.is.b"));
+        assertEquals("test", properties.getProperty("that.was.a"));
+        assertEquals("notNull", properties.getProperty("that.was.b"));
+    }
+
+    @MethodSource("testData")
+    @ParameterizedTest
+    void loadYmlStringMap(String fileName) {
+        File ymlFile = FileUtils.getFile(fileName);
+        assertTrue(ymlFile.exists());
+        Map<String, String> retrieved = 
AbstractKogitoBuildContext.loadYmlStringMap(ymlFile);
+        assertNotNull(retrieved);
+        commonCheck(retrieved);
+    }
+
+    @MethodSource("testData")
+    @ParameterizedTest
+    void loadYmlMap(String fileName) {
+        File ymlFile = FileUtils.getFile(fileName);
+        assertTrue(ymlFile.exists());
+        TreeMap<String, Object> retrieved = 
AbstractKogitoBuildContext.loadYmlMap(ymlFile);
+        assertNotNull(retrieved);
+        assertTrue(retrieved.containsKey("this"));
+    }
+
+    @MethodSource("testData")
+    @ParameterizedTest
+    void convertYamlObjectToMap(String fileName) {
+        File ymlFile = FileUtils.getFile(fileName);
+        assertTrue(ymlFile.exists());
+        TreeMap<String, Object> ymlMap = 
AbstractKogitoBuildContext.loadYmlMap(ymlFile);
+        assertNotNull(ymlMap);
+
+        Map<String, String> retrieved = 
AbstractKogitoBuildContext.convertYamlObjectToMap(ymlMap);
+        commonCheck(retrieved);
+    }
+
+    private static Collection<String> testData() {
+        return java.util.List.of(APPLICATION_PROPERTIES_YML_FILE_NAME, 
APPLICATION_PROPERTIES_YAML_FILE_NAME);
+    }
+
+    private void commonCheck(Map<String, String> retrieved) {
+        assertNotNull(retrieved);
+        assertTrue(retrieved.containsKey("this.is.a"));
+        assertEquals("test", retrieved.get("this.is.a"));
+        assertTrue(retrieved.containsKey("this.is.b"));
+        assertEquals("notNull", retrieved.get("this.is.b"));
+        assertTrue(retrieved.containsKey("this.was.a"));
+        assertEquals("test", retrieved.get("this.was.a"));
+        assertTrue(retrieved.containsKey("this.was.b"));
+        assertEquals("notNull", retrieved.get("this.was.b"));
+
+        assertTrue(retrieved.containsKey("that.is.a"));
+        assertEquals("test", retrieved.get("that.is.a"));
+        assertTrue(retrieved.containsKey("that.is.b"));
+        assertEquals("notNull", retrieved.get("that.is.b"));
+        assertTrue(retrieved.containsKey("that.was.a"));
+        assertEquals("test", retrieved.get("that.was.a"));
+        assertTrue(retrieved.containsKey("that.was.b"));
+        assertEquals("notNull", retrieved.get("that.was.b"));
+    }
+
     static class MockKogitoBuildContext extends AbstractKogitoBuildContext {
 
         public static Builder builder() {
diff --git 
a/kogito-codegen-modules/kogito-codegen-api/src/test/resources/application.yaml 
b/kogito-codegen-modules/kogito-codegen-api/src/test/resources/application.yaml
new file mode 100644
index 0000000000..ed6278c5a1
--- /dev/null
+++ 
b/kogito-codegen-modules/kogito-codegen-api/src/test/resources/application.yaml
@@ -0,0 +1,34 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+this:
+  is:
+    a: test
+    b: notNull
+  was:
+    a: test
+    b: notNull
+
+that:
+  is:
+    a: test
+    b: notNull
+  was:
+    a: test
+    b: notNull
\ No newline at end of file
diff --git 
a/kogito-codegen-modules/kogito-codegen-api/src/test/resources/application.yml 
b/kogito-codegen-modules/kogito-codegen-api/src/test/resources/application.yml
new file mode 100644
index 0000000000..ed6278c5a1
--- /dev/null
+++ 
b/kogito-codegen-modules/kogito-codegen-api/src/test/resources/application.yml
@@ -0,0 +1,34 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+this:
+  is:
+    a: test
+    b: notNull
+  was:
+    a: test
+    b: notNull
+
+that:
+  is:
+    a: test
+    b: notNull
+  was:
+    a: test
+    b: notNull
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to