This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new ef00da1ce [type: test]:fixes #3270 Increase the test coverage of
package: plugin-websocket to 80% (#3282)
ef00da1ce is described below
commit ef00da1ce05aa738ce6fa6fa6e8686ade54eb736
Author: qinghai777 <[email protected]>
AuthorDate: Tue Apr 19 17:14:50 2022 +0800
[type: test]:fixes #3270 Increase the test coverage of package:
plugin-websocket to 80% (#3282)
* rocketmq-unittest01
* rocketmq-unittest02
* rocketmq-unittest03
* rocketmq-unittest04
* fix the bug of test-rocketmq01
* test_websocket_plugin
---
.../plugin/websocket/WebSocketPluginTest.java | 21 +++++
.../WebSocketShenyuContextDecoratorTest.java | 52 +++++++++++++
.../handler/WebSocketPluginDataHandlerTest.java | 90 ++++++++++++++++++++++
3 files changed, 163 insertions(+)
diff --git
a/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java
b/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java
index 6429c873e..5707c4420 100644
---
a/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java
+++
b/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java
@@ -33,6 +33,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
+import org.springframework.http.HttpHeaders;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.reactive.socket.client.WebSocketClient;
@@ -41,7 +42,11 @@ import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.net.InetSocketAddress;
+import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -116,6 +121,9 @@ public class WebSocketPluginTest {
initMockInfo();
when(webSocketService.handleRequest(any(),
any())).thenReturn(Mono.empty());
StepVerifier.create(Mono.defer(() ->
webSocketPlugin.doExecute(exchange, chain, selectorData,
ruleData))).expectSubscription().verifyComplete();
+ SelectorData selectorData1 = new SelectorData();
+ selectorData1.setId("1");
+ assertEquals(webSocketPlugin.doExecute(exchange, chain, selectorData1,
new RuleData()), chain.execute(exchange));
}
/**
@@ -143,6 +151,19 @@ public class WebSocketPluginTest {
assertEquals(PluginEnum.WEB_SOCKET.getCode(),
webSocketPlugin.getOrder());
}
+ @Test
+ public void getSubProtocolsTest() throws NoSuchMethodException,
InvocationTargetException, IllegalAccessException, InstantiationException {
+ Class<?>[] clazz = webSocketPlugin.getClass().getDeclaredClasses();
+ Class<?> cla = clazz[0];
+ Method method = cla.getDeclaredMethod("getSubProtocols");
+ method.setAccessible(true);
+ Constructor declaredConstructor =
cla.getDeclaredConstructor(URI.class, WebSocketClient.class, HttpHeaders.class,
List.class);
+ declaredConstructor.setAccessible(true);
+ Object obj = declaredConstructor.newInstance(null, null, null, null);
+ List<String> list = (List<String>) method.invoke(obj);
+ assertEquals(list.isEmpty(), true);
+ }
+
/**
* Init mock info.
*/
diff --git
a/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/context/WebSocketShenyuContextDecoratorTest.java
b/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/context/WebSocketShenyuContextDecoratorTest.java
new file mode 100644
index 000000000..e8e3b150a
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/context/WebSocketShenyuContextDecoratorTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.plugin.websocket.context;
+
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The Test Case For WebSocketShenyuContextDecorator.
+ */
+public final class WebSocketShenyuContextDecoratorTest {
+
+ private WebSocketShenyuContextDecorator webSocketShenyuContextDecorator;
+
+ @BeforeEach
+ public void setUp() {
+ this.webSocketShenyuContextDecorator = new
WebSocketShenyuContextDecorator();
+ }
+
+ @Test
+ public void testDecorator() {
+ MetaData metaData = null;
+ ShenyuContext shenyuContext = new ShenyuContext();
+ webSocketShenyuContextDecorator.decorator(shenyuContext, metaData);
+ Assertions.assertEquals(shenyuContext.getMethod(), null);
+ Assertions.assertEquals(shenyuContext.getRpcType(), "websocket");
+ Assertions.assertEquals(shenyuContext.getModule(),
"websocket-websocket");
+ }
+
+ @Test
+ public void testRpcType() {
+ Assertions.assertEquals(webSocketShenyuContextDecorator.rpcType(),
"websocket");
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/handler/WebSocketPluginDataHandlerTest.java
b/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/handler/WebSocketPluginDataHandlerTest.java
new file mode 100644
index 000000000..8e587fd62
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/handler/WebSocketPluginDataHandlerTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.plugin.websocket.handler;
+
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.dto.convert.rule.impl.WebSocketRuleHandle;
+import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager;
+import org.apache.shenyu.loadbalancer.entity.Upstream;
+import org.apache.shenyu.plugin.base.cache.CommonHandleCache;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * The Test Case For WebSocketPluginDataHandler.
+ */
+public class WebSocketPluginDataHandlerTest {
+
+ private WebSocketPluginDataHandler webSocketPluginDataHandler;
+
+ private SelectorData selectorData = new SelectorData();
+
+ private RuleData ruleData = new RuleData();
+
+ private List<Upstream> upstreamList;
+
+ @BeforeEach
+ public void setUp() {
+ this.webSocketPluginDataHandler = new WebSocketPluginDataHandler();
+ this.upstreamList = Stream.of(3)
+ .map(weight -> Upstream.builder()
+ .url("mock-" + weight)
+ .build())
+ .collect(Collectors.toList());
+ selectorData.setId("1");
+ ruleData.setSelectorId("1");
+ ruleData.setHandle("{\"urlPath\":\"test\"}");
+ ruleData.setName("test");
+ }
+
+ @Test
+ public void testRemoveSelector() throws NoSuchFieldException,
IllegalAccessException {
+ UpstreamCacheManager instance = UpstreamCacheManager.getInstance();
+ instance.submit("1", upstreamList);
+ Field field = instance.getClass().getDeclaredField("UPSTREAM_MAP");
+ field.setAccessible(true);
+ Map<String, List<Upstream>> map = (Map<String, List<Upstream>>)
field.get(instance);
+ Assertions.assertNotEquals(map.get("1"), null);
+ webSocketPluginDataHandler.removeSelector(selectorData);
+ Assertions.assertEquals(map.get("1"), null);
+ }
+
+ @Test
+ public void testRemoveRule() {
+ Supplier<CommonHandleCache<String, WebSocketRuleHandle>> cache =
WebSocketPluginDataHandler.CACHED_HANDLE;
+ cache.get().cachedHandle("1_test", new WebSocketRuleHandle());
+ Assertions.assertNotEquals(cache.get().obtainHandle("1_test"), null);
+ webSocketPluginDataHandler.removeRule(ruleData);
+ Assertions.assertEquals(cache.get().obtainHandle("1_test"), null);
+ }
+
+ @Test
+ public void testPluginNamed() {
+ Assertions.assertEquals(webSocketPluginDataHandler.pluginNamed(),
"websocket");
+ }
+}
+