Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented: Add new StringUtil function to convert map to encoded string #852

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -212,6 +213,30 @@ public static Map<String, String> strToMap(String str) {
return strToMap(str, "|", false);
}

/**
* Creates an encoded String from a Map of name/value pairs
* @param mapToConvert The Map of name/value pairs
* @return String The encoded String like key1=value1|key2=value2, null if map is empty
*/
public static String mapToStr(Map<? extends Object, ? extends Object> mapToConvert) {
if (UtilValidate.isEmpty(mapToConvert)) {
return null;
}
return mapToConvert.entrySet().stream().map(entry -> {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());

try {
return new StringBuilder(URLEncoder.encode(key, "UTF-8"))
.append("=")
.append(URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Debug.logError(e, MODULE);
}
return "";
}).collect(Collectors.joining("|"));
}

/**
* Reads a String version of a List (should contain only strings) and creates a new List
* @param s String value of a Map ({n1=v1, n2=v2})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.ofbiz.base.util;

import com.ibm.icu.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -83,6 +86,33 @@ public void testStrToMap() {
StringUtil.strToMap(" 1 = one | 2 = two ", true));
}

@Test
public void testMapToStr() {
// Test Null
assertNull("null-string", StringUtil.mapToStr(null));
assertNull("empty", StringUtil.mapToStr(Map.of()));

// Test simple case
assertEquals("single", "1=one", StringUtil.mapToStr(Map.of("1", "one")));
LinkedHashMap<String, String> doubleMap = new LinkedHashMap<>();
doubleMap.put("1", "one");
doubleMap.put("2", "two");
assertEquals("double", "1=one|2=two", StringUtil.mapToStr(doubleMap));

// Test with object case
LinkedHashMap<Object, Object> doubleObjectMap = new LinkedHashMap<>();
doubleObjectMap.put(Integer.valueOf(1), Long.valueOf(1));
doubleObjectMap.put(Integer.valueOf(2), BigDecimal.ONE);
assertEquals("double with number classe", "1=1|2=1", StringUtil.mapToStr(doubleObjectMap));

// Test with special char
assertEquals("single with =", "1=%3Done", StringUtil.mapToStr(Map.of("1", "=one")));
LinkedHashMap<String, String> doublePipeMap = new LinkedHashMap<>();
doublePipeMap.put("1", "|one");
doublePipeMap.put("2|", "two");
assertEquals("double with pipe", "1=%7Cone|2%7C=two", StringUtil.mapToStr(doublePipeMap));
}

@Test
public void testToList() {
for (String s: new String[] {"", "[", "]", "]["}) {
Expand Down
Loading