|
|
|
@ -25,14 +25,13 @@ import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test for {@link JacksonUtils}.
|
|
|
|
|
*
|
|
|
|
|
* @author lepdou, Haotian Zhang
|
|
|
|
|
* @author lepdou, Haotian Zhang, cheese8
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
|
public class JacksonUtilsTest {
|
|
|
|
@ -43,27 +42,30 @@ public class JacksonUtilsTest {
|
|
|
|
|
sourceMap.put("k1", "v1");
|
|
|
|
|
sourceMap.put("k2", "v2");
|
|
|
|
|
sourceMap.put("k3", "v3");
|
|
|
|
|
assertEquals(JacksonUtils.serialize2Json(sourceMap), "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}");
|
|
|
|
|
assertThat(JacksonUtils.serialize2Json(sourceMap)).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);
|
|
|
|
|
assertEquals(map.size(), 3);
|
|
|
|
|
assertEquals(map.get("k1"), "v1");
|
|
|
|
|
assertEquals(map.get("k2"), "v2");
|
|
|
|
|
assertEquals(map.get("k3"), "v3");
|
|
|
|
|
assertThat(map.size()).isEqualTo(3);
|
|
|
|
|
assertThat(map.get("k1")).isEqualTo("v1");
|
|
|
|
|
assertThat(map.get("k2")).isEqualTo("v2");
|
|
|
|
|
assertThat(map.get("k3")).isEqualTo("v3");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDeserializeBlankIntoEmptyMap() {
|
|
|
|
|
assertTrue(JacksonUtils.deserialize2Map("").isEmpty());
|
|
|
|
|
Map<String, String> map = JacksonUtils.deserialize2Map("");
|
|
|
|
|
assertThat(map).isNotNull();
|
|
|
|
|
assertThat(map).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDeserializeThrowsRuntimeException() {
|
|
|
|
|
String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"";
|
|
|
|
|
assertThrows(RuntimeException.class, () -> JacksonUtils.deserialize2Map(jsonStr), "Json to map failed.");
|
|
|
|
|
assertThatThrownBy(() -> JacksonUtils.deserialize2Map(jsonStr))
|
|
|
|
|
.isExactlyInstanceOf(RuntimeException.class).hasMessage("Json to map failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|