Refator JacksonUtils and JacksonUtilsTest in 2020.0

pull/366/head
cheese8 3 years ago
parent 676aaa41dc
commit 65ce8b022c

@ -8,3 +8,4 @@
- [feat:support reading configuration from application.yml or application.properties.](https://github.com/Tencent/spring-cloud-tencent/pull/261) - [feat:support reading configuration from application.yml or application.properties.](https://github.com/Tencent/spring-cloud-tencent/pull/261)
- [change the way from escape to encode in 2020.0](https://github.com/Tencent/spring-cloud-tencent/pull/257) - [change the way from escape to encode in 2020.0](https://github.com/Tencent/spring-cloud-tencent/pull/257)
- [fix:fix ClassNotFoundException while not importing openfeign when using circuit-breaker module.](https://github.com/Tencent/spring-cloud-tencent/pull/270) - [fix:fix ClassNotFoundException while not importing openfeign when using circuit-breaker module.](https://github.com/Tencent/spring-cloud-tencent/pull/270)
- [Refator JacksonUtils and JacksonUtilsTest](https://github.com/Tencent/spring-cloud-tencent/pull/365)

@ -30,7 +30,7 @@ import org.springframework.util.StringUtils;
/** /**
* Utils for Jackson. * Utils for Jackson.
* *
* @author Haotian Zhang * @author Haotian Zhang, cheese8
*/ */
public final class JacksonUtils { public final class JacksonUtils {
@ -80,8 +80,7 @@ public final class JacksonUtils {
} }
catch (JsonProcessingException e) { catch (JsonProcessingException e) {
LOG.error( LOG.error(
"Json to map failed. check if the format of the json string[{}] is correct.", "Json to map failed. check if the format of the json string[{}] is correct.", jsonStr, e);
jsonStr, e);
throw new RuntimeException("Json to map failed.", e); throw new RuntimeException("Json to map failed.", e);
} }
} }

@ -26,28 +26,25 @@ 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.assertThat;
import static org.assertj.core.api.Assertions.fail; import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Test for {@link JacksonUtils}. * Test for {@link JacksonUtils}.
* *
* @author lepdou, Haotian Zhang * @author lepdou, Haotian Zhang, cheese8
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class JacksonUtilsTest { public class JacksonUtilsTest {
@Test @Test
public void testSerialize2Json() { 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");
assertThat(JacksonUtils.serialize2Json(sourceMap)).isEqualTo("{\"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\"}";
@ -56,20 +53,19 @@ public class JacksonUtilsTest {
assertThat(map.get("k1")).isEqualTo("v1"); assertThat(map.get("k1")).isEqualTo("v1");
assertThat(map.get("k2")).isEqualTo("v2"); assertThat(map.get("k2")).isEqualTo("v2");
assertThat(map.get("k3")).isEqualTo("v3"); assertThat(map.get("k3")).isEqualTo("v3");
}
assertThat(JacksonUtils.deserialize2Map("")).isNotNull();
assertThat(JacksonUtils.deserialize2Map("")).isEmpty(); @Test
public void testDeserializeBlankIntoEmptyMap() {
jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\""; Map<String, String> map = JacksonUtils.deserialize2Map("");
try { assertThat(map).isNotNull();
JacksonUtils.deserialize2Map(jsonStr); assertThat(map).isEmpty();
fail("RuntimeException should be thrown."); }
}
catch (RuntimeException exception) { @Test
assertThat(exception.getMessage()).isEqualTo("Json to map failed."); public void testDeserializeThrowsRuntimeException() {
} String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"";
catch (Throwable throwable) { assertThatThrownBy(() -> JacksonUtils.deserialize2Map(jsonStr))
fail("RuntimeException should be thrown."); .isExactlyInstanceOf(RuntimeException.class).hasMessage("Json to map failed.");
}
} }
} }

Loading…
Cancel
Save