This is an automated email from the ASF dual-hosted git repository.
hefengen 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 d44d240e30 [type:test][ISSUE #4775] Add rest of the case for ShenYu
sdk core (#5270)
d44d240e30 is described below
commit d44d240e30604a60d85526fc5174037eca9022de
Author: YxYL <[email protected]>
AuthorDate: Mon Nov 6 09:30:40 2023 +0800
[type:test][ISSUE #4775] Add rest of the case for ShenYu sdk core (#5270)
* [type:test][ISSUE apache#4537] Add test case for
org.apache.shenyu.sdk.core.ShenyuRequest
* [type:test][ISSUE apache#4537] Add test case for
org.apache.shenyu.sdk.core.ShenyuResponse
* [type:test][ISSUE apache#4537] Add test case for
org.apache.shenyu.sdk.core.ShenyuResponse
* [type:test][ISSUE apache#4537] Add test case for
org.apache.shenyu.sdk.core.ShenyuRequest
* [type:test][ISSUE apache#4537] Add test case for
org.apache.shenyu.sdk.core.util.Types
* [type:test][ISSUE apache#4537] Add test case for
org.apache.shenyu.sdk.core.retry.RetryableException
* [type:test][ISSUE apache#4537] Add test case for
org.apache.shenyu.sdk.core.common.RequestTemplate
---------
Co-authored-by: moremind <[email protected]>
---
.../sdk/core/common/RequestTemplateTest.java | 136 +++++++++++++++++++++
.../sdk/core/retry/RetryableExceptionTest.java | 82 +++++++++++++
.../org/apache/shenyu/sdk/core/util/TypesTest.java | 61 +++++++++
3 files changed, 279 insertions(+)
diff --git
a/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/common/RequestTemplateTest.java
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/common/RequestTemplateTest.java
new file mode 100644
index 0000000000..c7cfcd432a
--- /dev/null
+++
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/common/RequestTemplateTest.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.sdk.core.common;
+
+import org.apache.shenyu.common.utils.UUIDUtils;
+import org.apache.shenyu.sdk.core.ShenyuRequest;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Test for {@link RequestTemplate}.
+ */
+public class RequestTemplateTest {
+
+ private RequestTemplate requestTemplate;
+
+ @Before
+ public void setUp() throws NoSuchMethodException {
+ requestTemplate = new RequestTemplate();
+
+ final Method method = createMethod();
+ requestTemplate.setReturnType(method.getReturnType());
+ requestTemplate.setMethod(method);
+
+ requestTemplate.setName("testClientName");
+ requestTemplate.setUrl("/url/a/b");
+ requestTemplate.setPath("/id/{id}");
+ // if there is shenyuClientFactoryBean path not null, template should
append clientFactoryBean path
+ // if (StringUtils.hasText(shenyuClientFactoryBean.getPath())) {
+ // requestTemplate.setPath(shenyuClientFactoryBean.getPath() +
requestTemplate.getPath());
+ // }
+
+
requestTemplate.setContextId(UUIDUtils.getInstance().generateShortUuid());
+ requestTemplate.setHttpMethod(ShenyuRequest.HttpMethod.GET);
+
+ Map<String, Collection<String>> headerMap = new HashMap<>();
+ headerMap.put("header", Arrays.asList("header1", "header2"));
+ String body = "{key1:\"value1\"}";
+ requestTemplate.setHeaders(headerMap);
+ requestTemplate.setBody(body);
+
+ requestTemplate.setParamMetadataList(analysisParamMetadata(method));
+
+ }
+
+ @Test
+ public void testFrom() {
+ assertNotNull(requestTemplate);
+ RequestTemplate newRequestTemplate =
RequestTemplate.from(requestTemplate);
+ assertNotNull(newRequestTemplate);
+ }
+
+ @Test
+ public void testRequest() {
+ assertNotNull(requestTemplate);
+ ShenyuRequest shenyuRequest = requestTemplate.request();
+ Assert.assertNotNull(shenyuRequest);
+ }
+
+ public void assertNotNull(final RequestTemplate requestTemplate) {
+ Assert.assertNotNull(requestTemplate);
+ Assert.assertNotNull(requestTemplate.getReturnType());
+ Assert.assertNotNull(requestTemplate.getMethod());
+ Assert.assertNotNull(requestTemplate.getName());
+ Assert.assertNotNull(requestTemplate.getUrl());
+ Assert.assertNotNull(requestTemplate.getPath());
+ Assert.assertNotNull(requestTemplate.getContextId());
+ Assert.assertNotNull(requestTemplate.getHttpMethod());
+ Assert.assertNotNull(requestTemplate.getHeaders());
+ Assert.assertNotNull(requestTemplate.getBody());
+ Assert.assertNotNull(requestTemplate.getParamMetadataList());
+ }
+
+ /**
+ * analysisParamMetadata.
+ *
+ * @param method method
+ * @return {@link List}
+ */
+ public static List<RequestTemplate.ParamMetadata>
analysisParamMetadata(final Method method) {
+ Parameter[] parameters = method.getParameters();
+ if (parameters == null || parameters.length == 0) {
+ return Collections.emptyList();
+ }
+ List<RequestTemplate.ParamMetadata> params = new
ArrayList<>(parameters.length);
+ for (int index = 0; index < parameters.length; index++) {
+ Annotation[] annotations = parameters[index].getAnnotations();
+ if (annotations == null || annotations.length == 0) {
+ continue;
+ }
+ params.add(new RequestTemplate.ParamMetadata(annotations,
parameters[index].getType(), index));
+ }
+ return params;
+ }
+
+ private static Method createMethod() throws NoSuchMethodException {
+ return Controller.class.getDeclaredMethod("getControllerMethod");
+ }
+
+ static class Controller {
+ private Object controllerMethod;
+
+ public Object getControllerMethod() {
+ return controllerMethod;
+ }
+
+ }
+
+}
diff --git
a/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/retry/RetryableExceptionTest.java
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/retry/RetryableExceptionTest.java
new file mode 100644
index 0000000000..17fe2f06d8
--- /dev/null
+++
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/retry/RetryableExceptionTest.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.sdk.core.retry;
+
+import org.apache.shenyu.sdk.core.ShenyuRequest;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import static java.lang.String.format;
+
+/**
+ * Test for {@link RetryableException}.
+ */
+public class RetryableExceptionTest {
+
+ private ShenyuRequest request;
+
+ private RetryableException retryableException;
+
+ @Before
+ public void setUp() {
+ Map<String, Collection<String>> headerMap = new HashMap<>();
+ headerMap.put("header", Arrays.asList("header1", "header2"));
+ request = ShenyuRequest.create(ShenyuRequest.HttpMethod.GET,
"https://shenyu.apache.org",
+ headerMap, null, null, null);
+
+ try {
+ //do request logic...
+ //when occurred IOException
+ throw new IOException();
+ } catch (IOException cause) {
+ //then throw RetryableException
+ retryableException = new RetryableException(
+ format("%s executing %s %s", cause.getMessage(),
request.getHttpMethod(), request.getUrl()),
+ cause,
+ new Date(),
+ request);
+ Assert.assertNotNull(retryableException);
+ //throw retryableException;
+ }
+ }
+
+ @Test
+ public void retryableExceptionTest() {
+ Assert.assertNotNull(retryableException);
+ }
+
+ @Test
+ public void retryAfterTest() {
+ Date date = retryableException.retryAfter();
+ Assert.assertNotNull(date);
+ }
+
+ @Test
+ public void methodTest() {
+ ShenyuRequest.HttpMethod httpMethod = retryableException.method();
+ Assert.assertNotNull(httpMethod);
+ }
+}
diff --git
a/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/util/TypesTest.java
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/util/TypesTest.java
new file mode 100644
index 0000000000..7ce8ea0f92
--- /dev/null
+++
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/util/TypesTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.sdk.core.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+
+import static org.mockito.Mockito.mock;
+
+/**
+ * Test for {@link Types}.
+ */
+public class TypesTest {
+
+ @Test
+ public void resolveReturnTypeTest() {
+ //case1: baseType and overridingType are booth Class instances
+ Class<?> baseType = Number.class;
+ Class<?> overridingType = Integer.class;
+ Type result = Types.resolveReturnType(baseType, overridingType);
+ Assert.assertEquals(result, overridingType);
+
+ //case2: baseType is Class instance, overridingType is
ParameterizedType instance
+ baseType = Comparable.class;
+ ParameterizedType parameterizedType = mock(ParameterizedType.class);
+ result = Types.resolveReturnType(baseType, parameterizedType);
+ Assert.assertEquals(result, parameterizedType);
+
+ //case3: baseType is Class instance, overridingType is TypeVariable
instance
+ baseType = Comparable.class;
+ TypeVariable typeVariable = mock(TypeVariable.class);
+ result = Types.resolveReturnType(baseType, typeVariable);
+ Assert.assertEquals(result, typeVariable);
+
+ //case4 :vaseType and overridingType are not matching any conditions
+ baseType = String.class;
+ overridingType = Integer.class;
+ result = Types.resolveReturnType(baseType, overridingType);
+ Assert.assertEquals(result, baseType);
+ }
+
+}