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-drools.git


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

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

    [incubator-kie-issues#2163] Implement property retrieval from YAML files 
(#6662)
    
    * [incubator-kie-issues#2163] Implement property retrieval from YAML files
    
    * [incubator-kie-issues#2163] Missing license
    
    * [incubator-kie-issues#2163] Fix FileReader usage
    
    ---------
    
    Co-authored-by: Gabriele-Cardosi <[email protected]>
---
 build-parent/pom.xml                               |   7 ++
 drools-model/drools-codegen-common/pom.xml         |   4 +
 .../codegen/common/DroolsModelBuildContext.java    |   2 +
 .../context/AbstractDroolsModelBuildContext.java   |  56 +++++++++-
 .../AbstractDroolsModelBuildContextTest.java       | 113 +++++++++++++++++++++
 .../src/test/resources/application.yaml            |  34 +++++++
 .../src/test/resources/application.yml             |  34 +++++++
 7 files changed, 249 insertions(+), 1 deletion(-)

diff --git a/build-parent/pom.xml b/build-parent/pom.xml
index 54762a6787..73e4118a35 100644
--- a/build-parent/pom.xml
+++ b/build-parent/pom.xml
@@ -193,6 +193,7 @@
     <version.ch.obermuhlner>2.0.1</version.ch.obermuhlner>
     <version.io.smallrye.jandex>3.4.0</version.io.smallrye.jandex>
     <version.org.eclipse.yasson>3.0.4</version.org.eclipse.yasson>
+    <version.org.yaml.snakeyaml>2.6</version.org.yaml.snakeyaml>
 
     <version.com.github.javaparser>3.27.0</version.com.github.javaparser>
 
@@ -1237,6 +1238,12 @@
         <version>${version.com.google.guava}</version>
       </dependency>
 
+      <dependency>
+        <groupId>org.yaml</groupId>
+        <artifactId>snakeyaml</artifactId>
+        <version>${version.org.yaml.snakeyaml}</version>
+      </dependency>
+
       <!-- Used by kie-pmml-trusty projects -->
       <dependency>
         <groupId>org.kie</groupId>
diff --git a/drools-model/drools-codegen-common/pom.xml 
b/drools-model/drools-codegen-common/pom.xml
index a707cc32ae..fcc21018fd 100644
--- a/drools-model/drools-codegen-common/pom.xml
+++ b/drools-model/drools-codegen-common/pom.xml
@@ -47,6 +47,10 @@
             <groupId>com.github.javaparser</groupId>
             <artifactId>javaparser-core</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.yaml</groupId>
+            <artifactId>snakeyaml</artifactId>
+        </dependency>
         <dependency>
           <groupId>org.junit.jupiter</groupId>
           <artifactId>junit-jupiter-api</artifactId>
diff --git 
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/DroolsModelBuildContext.java
 
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/DroolsModelBuildContext.java
index a1515764b5..1378a39b6d 100644
--- 
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/DroolsModelBuildContext.java
+++ 
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/DroolsModelBuildContext.java
@@ -30,6 +30,8 @@ import org.drools.codegen.common.rest.RestAnnotator;
 public interface DroolsModelBuildContext {
 
     String APPLICATION_PROPERTIES_FILE_NAME = "application.properties";
+    String APPLICATION_PROPERTIES_YML_FILE_NAME = "application.yml";
+    String APPLICATION_PROPERTIES_YAML_FILE_NAME = "application.yaml";
     String DEFAULT_PACKAGE_NAME = "org.kie.kogito.app";
     /**
      * (boolean) enable/disable global rest endpoint generation (default true)
diff --git 
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
 
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
index e97a2c9bc1..e633beb81c 100644
--- 
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
+++ 
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
@@ -29,6 +29,7 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Properties;
+import java.util.TreeMap;
 import java.util.function.Predicate;
 
 import javax.lang.model.SourceVersion;
@@ -40,6 +41,7 @@ import 
org.drools.codegen.common.di.DependencyInjectionAnnotator;
 import org.drools.codegen.common.rest.RestAnnotator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
 
 public abstract class AbstractDroolsModelBuildContext implements 
DroolsModelBuildContext {
 
@@ -81,11 +83,63 @@ public abstract class AbstractDroolsModelBuildContext 
implements DroolsModelBuil
             } 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);
+            }
+        });
+    }
+
     public boolean hasClassAvailable(String fqcn) {
         return classAvailabilityResolver.test(fqcn);
     }
diff --git 
a/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContextTest.java
 
b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContextTest.java
new file mode 100644
index 0000000000..4c770c6b6b
--- /dev/null
+++ 
b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContextTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.
+ */
+package org.drools.codegen.common.context;
+
+import org.drools.util.FileUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Properties;
+import java.util.TreeMap;
+
+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.*;
+
+class AbstractDroolsModelBuildContextTest {
+
+    @MethodSource("testData")
+    @ParameterizedTest
+    void loadYmlProperties(String fileName) {
+        File ymlFile = FileUtils.getFile(fileName);
+        assertTrue(ymlFile.exists());
+        Properties properties = new Properties();
+        AbstractDroolsModelBuildContext.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 = 
AbstractDroolsModelBuildContext.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 = 
AbstractDroolsModelBuildContext.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 = 
AbstractDroolsModelBuildContext.loadYmlMap(ymlFile);
+        assertNotNull(ymlMap);
+
+        Map<String, String> retrieved = 
AbstractDroolsModelBuildContext.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"));
+    }
+
+}
\ No newline at end of file
diff --git 
a/drools-model/drools-codegen-common/src/test/resources/application.yaml 
b/drools-model/drools-codegen-common/src/test/resources/application.yaml
new file mode 100644
index 0000000000..ed6278c5a1
--- /dev/null
+++ b/drools-model/drools-codegen-common/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/drools-model/drools-codegen-common/src/test/resources/application.yml 
b/drools-model/drools-codegen-common/src/test/resources/application.yml
new file mode 100644
index 0000000000..ed6278c5a1
--- /dev/null
+++ b/drools-model/drools-codegen-common/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