Refator JacksonUtils and JacksonUtilsTest

pull/365/head
cheese8 3 years ago
parent c91b5622f0
commit 90c31b34a8

11299
log.txt

File diff suppressed because one or more lines are too long

@ -17,16 +17,13 @@
package com.tencent.cloud.common.util; package com.tencent.cloud.common.util;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
/** /**
* Utils for Jackson. * Utils for Jackson.
* *
@ -39,8 +36,6 @@ public final class JacksonUtils {
*/ */
public static final ObjectMapper OM = new ObjectMapper(); public static final ObjectMapper OM = new ObjectMapper();
private static final Logger LOG = LoggerFactory.getLogger(JacksonUtils.class);
private JacksonUtils() { private JacksonUtils() {
} }
@ -56,7 +51,6 @@ public final class JacksonUtils {
return OM.writeValueAsString(object); return OM.writeValueAsString(object);
} }
catch (JsonProcessingException e) { catch (JsonProcessingException e) {
LOG.error("Object to Json failed. {}", object, e);
throw new RuntimeException("Object to Json failed.", e); throw new RuntimeException("Object to Json failed.", e);
} }
} }
@ -79,9 +73,6 @@ public final class JacksonUtils {
return new HashMap<>(); return new HashMap<>();
} }
catch (JsonProcessingException e) { catch (JsonProcessingException e) {
LOG.error(
"Json to map failed. check if the format of the json string[{}] is correct.",
jsonStr, e);
throw new RuntimeException("Json to map failed.", e); throw new RuntimeException("Json to map failed.", e);
} }
} }

@ -18,15 +18,16 @@
package com.tencent.cloud.common.util; package com.tencent.cloud.common.util;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat; import java.util.HashMap;
import static org.assertj.core.api.Assertions.fail; import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test for {@link JacksonUtils}. * Test for {@link JacksonUtils}.
@ -42,34 +43,27 @@ public class JacksonUtilsTest {
sourceMap.put("k1", "v1"); sourceMap.put("k1", "v1");
sourceMap.put("k2", "v2"); sourceMap.put("k2", "v2");
sourceMap.put("k3", "v3"); sourceMap.put("k3", "v3");
assertEquals(JacksonUtils.serialize2Json(sourceMap), "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}");
String jsonStr = JacksonUtils.serialize2Json(sourceMap);
assertThat(jsonStr).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}");
} }
@Test @Test
public void testDeserialize2Map() { public void testDeserialize2Map() {
String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}"; String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}";
Map<String, String> map = JacksonUtils.deserialize2Map(jsonStr); Map<String, String> map = JacksonUtils.deserialize2Map(jsonStr);
assertThat(map.size()).isEqualTo(3); assertEquals(map.size(), 3);
assertThat(map.get("k1")).isEqualTo("v1"); assertEquals(map.get("k1"), "v1");
assertThat(map.get("k2")).isEqualTo("v2"); assertEquals(map.get("k2"), "v2");
assertThat(map.get("k3")).isEqualTo("v3"); assertEquals(map.get("k3"), "v3");
assertThat(JacksonUtils.deserialize2Map("")).isNotNull();
assertThat(JacksonUtils.deserialize2Map("")).isEmpty();
jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"";
try {
JacksonUtils.deserialize2Map(jsonStr);
fail("RuntimeException should be thrown.");
} }
catch (RuntimeException exception) {
assertThat(exception.getMessage()).isEqualTo("Json to map failed."); @Test
} public void testDeserializeBlankIntoEmptyMap() {
catch (Throwable throwable) { assertTrue(JacksonUtils.deserialize2Map("").isEmpty());
fail("RuntimeException should be thrown.");
} }
@Test
public void testDeserializeThrowsRuntimeException() {
String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"";
assertThrows(RuntimeException.class, () -> JacksonUtils.deserialize2Map(jsonStr), "Json to map failed.");
} }
} }

Loading…
Cancel
Save