svn commit: r1863437 - in /ofbiz/ofbiz-framework/trunk/framework/base/src: main/java/org/apache/ofbiz/base/util/UtilHttp.java test/java/org/apache/ofbiz/base/util/UtilHttpTest.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1863437 - in /ofbiz/ofbiz-framework/trunk/framework/base/src: main/java/org/apache/ofbiz/base/util/UtilHttp.java test/java/org/apache/ofbiz/base/util/UtilHttpTest.java

mthl
Author: mthl
Date: Fri Jul 19 21:50:14 2019
New Revision: 1863437

URL: http://svn.apache.org/viewvc?rev=1863437&view=rev
Log:
Implemented: Add unit tests for ‘UtilHttp#makeParamValueFromComposite’
(OFBIZ-11138)

The implementation has been adapted to facilitate mocking.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java?rev=1863437&r1=1863436&r2=1863437&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java Fri Jul 19 21:50:14 2019
@@ -1401,21 +1401,19 @@ public final class UtilHttp {
 
         // collect the composite fields into a map
         Map<String, String> data = new HashMap<>();
-        for (Enumeration<String> names = UtilGenerics.cast(request.getParameterNames()); names.hasMoreElements();) {
-            String name = names.nextElement();
+        request.getParameterMap().forEach((name, values) -> {
             if (!name.startsWith(prefix + COMPOSITE_DELIMITER)) {
-                continue;
+                return;
             }
-
             // extract the suffix of the composite name
             String suffix = name.substring(name.indexOf(COMPOSITE_DELIMITER) + COMPOSITE_DELIMITER_LENGTH);
 
             // and the value of this parameter
-            String value = request.getParameter(name);
+            String value = values[0];
 
             // key = suffix, value = parameter data
             data.put(suffix, value);
-        }
+        });
         if (Debug.verboseOn()) { Debug.logVerbose("Creating composite type with parameter data: " + data.toString(), module); }
 
         // handle recomposition of data into the compositeType

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java?rev=1863437&r1=1863436&r2=1863437&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java Fri Jul 19 21:50:14 2019
@@ -21,11 +21,16 @@ package org.apache.ofbiz.base.util;
 import static org.apache.ofbiz.base.util.UtilHttp.getPathInfoOnlyParameterMap;
 import static org.hamcrest.Matchers.*;
 import static org.hamcrest.Matchers.hasEntry;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.when;
 
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.Month;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Locale;
 import java.util.Map;
 import java.util.function.Predicate;
 
@@ -122,4 +127,54 @@ public class UtilHttpTest {
                 not(hasEntry("foo", "1")),
                 hasEntry("bar", Arrays.asList("2", "3"))));
     }
+
+    @Test
+    public void basicMakeParamValueFromComposite() {
+        when(req.getParameter("meetingDate_c_compositeType")).thenReturn("Timestamp");
+        when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
+                "meetingDate_c_date", new String[] {"2019-07-14"},
+                "meetingDate_c_hour", new String[] {"13"},
+                "meetingDate_c_minutes", new String[] {"8"}));
+        assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
+                equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 13, 8))));
+    }
+
+    @Test
+    public void emptyTypeMakeParamValueFromComposite() {
+        when(req.getParameter("meetingDate_c_compositeType")).thenReturn(null);
+        when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
+                "meetingDate_c_date", new String[] {"2019-07-14"},
+                "meetingDate_c_hour", new String[] {"13"},
+                "meetingDate_c_minutes", new String[] {"8"}));
+        assertNull(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT));
+    }
+
+    @Test
+    public void ampmMakeParamValueFromComposite() {
+        when(req.getParameter("meetingDate_c_compositeType")).thenReturn("Timestamp");
+
+        when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
+                "meetingDate_c_date", new String[] {"2019-07-14"},
+                "meetingDate_c_hour", new String[] {"12"},
+                "meetingDate_c_minutes", new String[] {"8"},
+                "meetingDate_c_ampm", new String[] {"AM"}));
+        assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
+                equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 0, 8))));
+
+        when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
+                "meetingDate_c_date", new String[] {"2019-07-14"},
+                "meetingDate_c_hour", new String[] {"8"},
+                "meetingDate_c_minutes", new String[] {"8"},
+                "meetingDate_c_ampm", new String[] {"PM"}));
+        assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
+                equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 20, 8))));
+
+        when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
+                "meetingDate_c_date", new String[] {"2019-07-14"},
+                "meetingDate_c_hour", new String[] {"18"},
+                "meetingDate_c_minutes", new String[] {"8"},
+                "meetingDate_c_ampm", new String[] {"PM"}));
+        assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate", Locale.ROOT),
+                equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 18, 8))));
+    }
 }