This is an automated email from the ASF dual-hosted git repository.
likeguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 6a7f0ad07 [ISSUE #3275] improve unit test coverage of
'org.apache.shenyu.common.utils' (#4223)
6a7f0ad07 is described below
commit 6a7f0ad07e3041f11d102fbc73dbbcd5913c9f6f
Author: xcsnx <[email protected]>
AuthorDate: Tue Nov 29 20:17:09 2022 +0800
[ISSUE #3275] improve unit test coverage of
'org.apache.shenyu.common.utils' (#4223)
* test common: Improve the test coverage of 'JsonUtilsTest','GsonUtilsTest'
* test common: Improve the test coverage of 'JsonUtilsTest','GsonUtilsTest'
* test common: Improve the test coverage of 'PathUtils'
* test common: Improve the test coverage of 'SignUtils'
* test common: Improve the test coverage of 'UriUtils'
* test common: Improve the test coverage of 'org.apache.shenyu.common.utils'
Co-authored-by: ‘xcsnx’ <‘[email protected]’>
---
.../apache/shenyu/common/utils/GsonUtilsTest.java | 25 ++++++++++++++++++++++
.../apache/shenyu/common/utils/JsonUtilsTest.java | 21 ++++++++++++++++++
.../apache/shenyu/common/utils/PathUtilsTest.java | 11 ++++++++++
.../apache/shenyu/common/utils/SignUtilsTest.java | 11 ++++++++++
.../apache/shenyu/common/utils/UriUtilsTest.java | 18 ++++++++++++++++
5 files changed, 86 insertions(+)
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/GsonUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/GsonUtilsTest.java
index 069f147e0..562a7aa37 100644
---
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/GsonUtilsTest.java
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/GsonUtilsTest.java
@@ -26,9 +26,12 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.tuple.Pair;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -315,6 +318,28 @@ public class GsonUtilsTest {
.build();
}
+ @Test
+ public void testDurationGson() {
+ LocalDateTime start = LocalDateTime.of(2022, 1, 1, 1, 1, 1);
+ LocalDateTime end = LocalDateTime.of(2022, 1, 2, 2, 2, 2);
+ Duration expectDuration = Duration.between(start, end);
+ String testStringDuration = "PT25H1M1S";
+ Duration testDuration =
GsonUtils.getInstance().fromJson(testStringDuration, Duration.class);
+ assertEquals(expectDuration, testDuration);
+ }
+
+ @Test
+ public void testFromCurrentList() {
+ Map<String, Object> map = ImmutableMap.of("id", "123", "name", "test",
"data", "测试");
+ List<Map<String, Object>> list =
ImmutableList.of(ImmutableMap.copyOf(map), ImmutableMap.copyOf(map),
+ ImmutableMap.copyOf(map));
+ String json = "[{\"name\":\"test\",\"id\":\"123\",\"data\":\"测试\"},"
+ + "{\"name\":\"test\",\"id\":\"123\",\"data\":\"测试\"},"
+ + "{\"name\":\"test\",\"id\":\"123\",\"data\":\"测试\"}]";
+ List<? extends Map> testList =
GsonUtils.getInstance().fromCurrentList(json, map.getClass());
+ Assertions.assertEquals(list, testList);
+ }
+
private static class TestObject {
private Boolean bool;
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/JsonUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/JsonUtilsTest.java
index 8e2113e70..cbbb1266f 100644
---
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/JsonUtilsTest.java
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/JsonUtilsTest.java
@@ -109,6 +109,27 @@ public final class JsonUtilsTest {
assertEquals(testMap.get("result").get("not_class"),
"ClassNotFoundException.class");
}
+ @Test
+ public void testJsonToObject() {
+ TestObject testObject = JsonUtils.jsonToObject(EXPECTED_JSON,
TestObject.class);
+ assertNotNull(testObject);
+ assertEquals(testObject.getName(), "test object");
+ }
+
+ @Test
+ public void testJsonToMapByValueTypeRef() {
+ Map<String, Object> stringObjectMap =
JsonUtils.jsonToMap(EXPECTED_JSON, Object.class);
+ assertEquals(stringObjectMap.get("name"), "test object");
+ }
+
+ @Test
+ public void testToMap() {
+ TestObject testObject = JsonUtils.jsonToObject(EXPECTED_JSON,
TestObject.class);
+ Map<String, Object> testObjectMap = JsonUtils.toMap(testObject);
+ assertNotNull(testObjectMap);
+ assertEquals(testObjectMap.get("name"), "test object");
+ }
+
static class TestObject {
private int id;
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathUtilsTest.java
index fd88be5d1..cad939bf4 100644
---
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathUtilsTest.java
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathUtilsTest.java
@@ -28,6 +28,8 @@ public class PathUtilsTest {
private static final String URI = "springCloud/test";
+ private static final String URI_SLASH = "springCloud/test/";
+
@Test
public void testDecoratorPath() {
String uri = PathUtils.decoratorPath(URI);
@@ -45,4 +47,13 @@ public class PathUtilsTest {
uri = PathUtils.decoratorContextPath(URI_WRAPPER);
assertThat(uri, is(URI));
}
+
+ @Test
+ public void testDecoratorPathWithSlash() {
+ String uri = PathUtils.decoratorPathWithSlash(URI);
+ assertThat(uri, is(URI + AdminConstants.URI_SLASH_SUFFIX));
+
+ uri = PathUtils.decoratorContextPath(URI_SLASH);
+ assertThat(uri, is(URI + AdminConstants.URI_SLASH_SUFFIX));
+ }
}
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/SignUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/SignUtilsTest.java
index df516f44d..e3dcee744 100644
---
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/SignUtilsTest.java
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/SignUtilsTest.java
@@ -24,6 +24,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Test cases for SignUtils.
@@ -57,4 +58,14 @@ public final class SignUtilsTest {
public void testGenerateKey() {
assertNotNull(SignUtils.getInstance().generateKey());
}
+
+ @Test
+ public void testTransStringMap() {
+ Map<String, Object> jsonParams = new HashMap<>();
+ jsonParams.put("a", "1");
+ jsonParams.put("b", "2");
+ Map<String, String> stringStringMap =
SignUtils.transStringMap(jsonParams);
+ assertEquals(stringStringMap.get("a").getClass(), String.class);
+ assertEquals(stringStringMap.get("a"), "1");
+ }
}
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UriUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UriUtilsTest.java
index 941897b9a..3047e6835 100644
---
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UriUtilsTest.java
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UriUtilsTest.java
@@ -24,6 +24,7 @@ import java.net.URI;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
/**
* Test cases for UriUtils.
@@ -82,4 +83,21 @@ public final class UriUtilsTest {
ret = UriUtils.getPathWithParams(uri);
assertEquals("/path?key=val", ret);
}
+
+ @Test
+ void appendScheme() {
+ String uri = UriUtils.appendScheme("example.com", "http");
+ assertEquals("http://example.com", uri);
+
+ uri = UriUtils.appendScheme("example.com", "https");
+ assertEquals("https://example.com", uri);
+
+ uri = UriUtils.appendScheme("https://example.com", "http");
+ assertEquals("https://example.com", uri);
+ assertNotEquals("http://example.com", uri);
+
+ uri = UriUtils.appendScheme("http://example.com", "https");
+ assertEquals("http://example.com", uri);
+ assertNotEquals("https://example.com", uri);
+ }
}