|
|
@ -21,30 +21,55 @@ package com.tencent.cloud.common.util;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
|
|
|
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 static org.assertj.core.api.Assertions.fail;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* test for {@link JacksonUtils}
|
|
|
|
* Test for {@link JacksonUtils}.
|
|
|
|
*@author lepdou 2022-05-27
|
|
|
|
*
|
|
|
|
|
|
|
|
* @author lepdou, Haotian Zhang
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
public class JacksonUtilsTest {
|
|
|
|
public class JacksonUtilsTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void test() {
|
|
|
|
public void testSerialize2Json() {
|
|
|
|
Map<String, String> sourceMap = new HashMap<>();
|
|
|
|
Map<String, String> sourceMap = new HashMap<>();
|
|
|
|
sourceMap.put("k1", "v1");
|
|
|
|
sourceMap.put("k1", "v1");
|
|
|
|
sourceMap.put("k2", "v2");
|
|
|
|
sourceMap.put("k2", "v2");
|
|
|
|
sourceMap.put("k3", "v3");
|
|
|
|
sourceMap.put("k3", "v3");
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> map = JacksonUtils.deserialize2Map(JacksonUtils.serialize2Json(sourceMap));
|
|
|
|
String jsonStr = JacksonUtils.serialize2Json(sourceMap);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(jsonStr).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void testDeserialize2Map() {
|
|
|
|
|
|
|
|
String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}";
|
|
|
|
|
|
|
|
Map<String, String> map = JacksonUtils.deserialize2Map(jsonStr);
|
|
|
|
|
|
|
|
assertThat(map.size()).isEqualTo(3);
|
|
|
|
|
|
|
|
assertThat(map.get("k1")).isEqualTo("v1");
|
|
|
|
|
|
|
|
assertThat(map.get("k2")).isEqualTo("v2");
|
|
|
|
|
|
|
|
assertThat(map.get("k3")).isEqualTo("v3");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(JacksonUtils.deserialize2Map("")).isNotNull();
|
|
|
|
|
|
|
|
assertThat(JacksonUtils.deserialize2Map("")).isEmpty();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals(sourceMap.size(), map.size());
|
|
|
|
jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"";
|
|
|
|
Assert.assertEquals(sourceMap.get("k1"), map.get("k1"));
|
|
|
|
try {
|
|
|
|
Assert.assertEquals(sourceMap.get("k2"), map.get("k2"));
|
|
|
|
JacksonUtils.deserialize2Map(jsonStr);
|
|
|
|
Assert.assertEquals(sourceMap.get("k3"), map.get("k3"));
|
|
|
|
fail("RuntimeException should be thrown.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (RuntimeException exception) {
|
|
|
|
|
|
|
|
assertThat(exception.getMessage()).isEqualTo("Json to map failed.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Throwable throwable) {
|
|
|
|
|
|
|
|
fail("RuntimeException should be thrown.");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|