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

rzo1 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/storm.git


The following commit(s) were added to refs/heads/master by this push:
     new ce21f7990 BugFix Utils.java (#8972)
ce21f7990 is described below

commit ce21f7990363bad83861f2e0775533d101c3c061
Author: Valerio Baldazzi <[email protected]>
AuthorDate: Sat Aug 22 19:24:32 2026 +0200

    BugFix Utils.java (#8972)
    
    * BugFix Utils.java
    
    - fixed bug in findOne overloaded method
    
    * added unit tests
---
 .../src/jvm/org/apache/storm/utils/Utils.java      |  2 +-
 .../test/jvm/org/apache/storm/utils/UtilsTest.java | 57 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/storm-client/src/jvm/org/apache/storm/utils/Utils.java 
b/storm-client/src/jvm/org/apache/storm/utils/Utils.java
index e57e7f3f0..feebf0fab 100644
--- a/storm-client/src/jvm/org/apache/storm/utils/Utils.java
+++ b/storm-client/src/jvm/org/apache/storm/utils/Utils.java
@@ -1687,7 +1687,7 @@ public class Utils {
         if (map == null) {
             return null;
         }
-        return findOne(pred, (Set<T>) map.entrySet());
+        return findOne(pred, map.values());
     }
 
     public static Map<String, Object> parseJson(String json) {
diff --git a/storm-client/test/jvm/org/apache/storm/utils/UtilsTest.java 
b/storm-client/test/jvm/org/apache/storm/utils/UtilsTest.java
index 6086f5095..ed0c82cfe 100644
--- a/storm-client/test/jvm/org/apache/storm/utils/UtilsTest.java
+++ b/storm-client/test/jvm/org/apache/storm/utils/UtilsTest.java
@@ -44,6 +44,7 @@ import org.apache.storm.thrift.transport.TTransportException;
 import org.apache.storm.topology.BoltDeclarer;
 import org.apache.storm.topology.TopologyBuilder;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -768,4 +769,60 @@ public class UtilsTest {
         }
         return data;
     }
+
+    // FIND ONE TESTS
+
+    /** Test findOne method with null IPredicate, empty Map. Expected = null 
value */
+    @Test
+    public void findOneNullIPredicateEmptyCollectionShouldPass() {
+
+        assertNull(Utils.findOne(null, Map.of()));
+    }
+
+    /** Test findOne method with not valid IPredicate (throws 
RuntimeException), null Map. Expected = null value */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void findOneNotCorrectIPredicateNullCollectionShouldPass() {
+
+        IPredicate<Integer> mockPredicate = Mockito.mock(IPredicate.class);
+        
Mockito.lenient().when(mockPredicate.test(Mockito.any())).thenThrow(new 
RuntimeException());
+        assertNull(Utils.findOne(mockPredicate, (Map<Integer, Integer>) null));
+    }
+
+    /** Test findOne method with correct IPredicate (first element in the map 
is coherent with check) , one value Map. Expected = first element */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void findOneValidIPredicateValidCollectionShouldPass() {
+
+        IPredicate<Integer> mockPredicate = Mockito.mock(IPredicate.class);
+        Mockito.when(mockPredicate.test(Mockito.any())).thenReturn(true);
+        Map<Integer, Integer> map = Map.of(1,1);
+        Integer integer = 1;
+        assertEquals(integer, Utils.findOne(mockPredicate, map));
+    }
+
+    /** Test findOne method with correct IPredicate (second element in the map 
is coherent with check) , two values Map. Expected = second element */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void findOneValidIPredicateValidCollection2ShouldPass() {
+
+        IPredicate<Integer> mockPredicate = Mockito.mock(IPredicate.class);
+        Integer integer = 1;
+        Integer integer2 = 2;
+        Mockito.lenient().when(mockPredicate.test(integer)).thenReturn(false);
+        Mockito.lenient().when(mockPredicate.test(integer2)).thenReturn(true);
+        Map<Integer, Integer> map = Map.of(1,1,2,2);
+        assertEquals(integer2, Utils.findOne(mockPredicate, map));
+    }
+
+    /** Test findOne method with not correct IPredicate (no element in the map 
is coherent with check) , one value Map. Expected = null */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void findOneNotCorrectIPredicateValidCollectionShouldPass() {
+
+        IPredicate<Integer> mockPredicate = Mockito.mock(IPredicate.class);
+        Mockito.when(mockPredicate.test(Mockito.any())).thenReturn(false);
+        Map<Integer, Integer> map = Map.of(1,1);
+        assertNull(Utils.findOne(mockPredicate, map));
+    }
 }

Reply via email to