Skip to content

Commit

Permalink
Implemented: Add unit tests for ‘UtilHttp#makeParamValueFromComposite’
Browse files Browse the repository at this point in the history
(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 479c16d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.