svn commit: r1863440 - 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: r1863440 - 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 22:07:55 2019
New Revision: 1863440

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

Adapt implementation 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=1863440&r1=1863439&r2=1863440&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 22:07:55 2019
@@ -600,21 +600,19 @@ public final class UtilHttp {
 
     public static List<Object> makeParamListWithSuffix(HttpServletRequest request, Map<String, ? extends Object> additionalFields, String suffix, String prefix) {
         List<Object> paramList = new ArrayList<>();
-        Enumeration<String> parameterNames = UtilGenerics.cast(request.getParameterNames());
-        while (parameterNames.hasMoreElements()) {
-            String parameterName = parameterNames.nextElement();
+        request.getParameterMap().forEach((parameterName, values) -> {
             if (parameterName.endsWith(suffix)) {
                 if (UtilValidate.isNotEmpty(prefix)) {
                     if (parameterName.startsWith(prefix)) {
-                        String value = request.getParameter(parameterName);
+                        String value = values[0];
                         paramList.add(value);
                     }
                 } else {
-                    String value = request.getParameter(parameterName);
+                    String value = values[0];
                     paramList.add(value);
                 }
             }
-        }
+        });
         if (additionalFields != null) {
             for (Map.Entry<String, ? extends Object> entry: additionalFields.entrySet()) {
                 String fieldName = entry.getKey();

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=1863440&r1=1863439&r2=1863440&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 22:07:55 2019
@@ -176,4 +176,30 @@ public class UtilHttpTest {
         assertThat(UtilHttp.makeParamValueFromComposite(req, "meetingDate"),
                 equalTo(Timestamp.valueOf(LocalDateTime.of(2019, Month.JULY, 14, 18, 8))));
     }
+
+    @Test
+    public void basicMakeParamListWithSuffix() {
+        when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
+                "foo_suf", new String[] {"0"},
+                "bar_suf", new String[] {"1"},
+                "baz", new String[] {"2"}));
+        assertThat(UtilHttp.makeParamListWithSuffix(req, "_suf", null), containsInAnyOrder("0", "1"));
+        assertThat(UtilHttp.makeParamListWithSuffix(req, "_suf", "b"), contains("1"));
+    }
+
+    @Test
+    public void additionalParamsMakeParamListWithSuffix() {
+        when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
+                "foo_suf", new String[] {"0"},
+                "bar_suf", new String[] {"1"},
+                "baz", new String[] {"2"}));
+        Map<String, Object> extra = UtilMisc.toMap("baz_suf", "3");
+        assertThat(UtilHttp.makeParamListWithSuffix(req, extra, "_suf", null), containsInAnyOrder("0", "1", "3"));
+        assertThat(UtilHttp.makeParamListWithSuffix(req, extra, "_suf", "b"), containsInAnyOrder("1", "3"));
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void missingRequestMakeParamListWithSuffix() {
+        UtilHttp.makeParamListWithSuffix(null, "suffix", "prefix");
+    }
 }