Skip to content
Permalink
Browse files

Implemented: Add unit tests for ‘UtilHttp#makeParamListWithSuffix’

(OFBIZ-11138)

Adapt implementation to facilitate mocking.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863440 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 19, 2019
1 parent 1720a2f commit 229fdbb6d51b0798f2ac230be47c4d9d859c1b41
@@ -600,21 +600,19 @@ public static String canonicalizeParameter(String paramValue) {

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();
@@ -176,4 +176,30 @@ public void ampmMakeParamValueFromComposite() {
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");
}
}

0 comments on commit 229fdbb

Please sign in to comment.
You can’t perform that action at this time.