Skip to content
Permalink
Browse files

Implemented: Add unit tests for ‘UtilHttp#makeParamValueFromComposite’

(OFBIZ-11138)

The implementation has been adapted to facilitate mocking.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863437 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 19, 2019
1 parent e1d132a commit 479c16d96b369c799675f547e40b33ebf48d0a76
@@ -1401,21 +1401,19 @@ public static Object makeParamValueFromComposite(HttpServletRequest request, Str

// 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
@@ -21,11 +21,16 @@
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 void filteredGetParameterMap() {
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))));
}
}

0 comments on commit 479c16d

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