-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add spring support test #45
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.cloud.gateway; | ||
|
||
import com.tencent.trpc.spring.cloud.gateway.service.MyService; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = TrpcGatewayApplication.class) | ||
public class TrpcGatewayApplicationTest { | ||
|
||
@Autowired | ||
private MyService myService; | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Test | ||
public void testContextLoads() { | ||
// 测试上下文加载 | ||
Assert.assertNotNull(applicationContext); | ||
} | ||
|
||
@Test | ||
public void testComponentBehavior() { | ||
Assert.assertNotNull(myService); | ||
Assert.assertTrue(myService.invoke()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.cloud.gateway.client; | ||
|
||
import com.tencent.trpc.core.rpc.CloseFuture; | ||
import com.tencent.trpc.core.rpc.GenericClient; | ||
import com.tencent.trpc.core.rpc.RpcClientContext; | ||
import com.tencent.trpc.core.rpc.TRpcProxy; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.function.Predicate; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PowerMockIgnore; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.springframework.cloud.gateway.filter.FilterDefinition; | ||
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; | ||
import org.springframework.cloud.gateway.route.Route; | ||
import org.springframework.cloud.gateway.route.Route.Builder; | ||
import org.springframework.cloud.gateway.route.RouteDefinition; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(TRpcProxy.class) | ||
@PowerMockIgnore({"javax.management.*", "javax.security.*", "javax.ws.*"}) | ||
public class TrpcGatewayClientTest { | ||
|
||
private TrpcGatewayClient client; | ||
|
||
private Route route; | ||
|
||
private Builder builder; | ||
|
||
/** | ||
* 初始化build. | ||
* | ||
* @throws URISyntaxException | ||
*/ | ||
@Before | ||
public void setUp() throws URISyntaxException { | ||
PowerMockito.mockStatic(TRpcProxy.class); | ||
RouteDefinition definition = initData(); | ||
builder = Route.builder(definition); | ||
Predicate<ServerWebExchange> predicate = serverWebExchange -> { | ||
serverWebExchange.checkNotModified("test"); | ||
return true; | ||
}; | ||
ReflectionTestUtils.setField(builder, "predicate", predicate); | ||
Predicate<ServerWebExchange> predicate2 = serverWebExchange -> { | ||
serverWebExchange.checkNotModified("test2"); | ||
return true; | ||
}; | ||
builder.and(predicate2); | ||
route = builder.build(); | ||
Mockito.when(TRpcProxy.getProxy(Mockito.anyString())).thenReturn(new GenericClient() { | ||
|
||
@Override | ||
public CompletionStage<byte[]> asyncInvoke(RpcClientContext context, byte[] body) { | ||
return new CloseFuture<>(); | ||
} | ||
|
||
@Override | ||
public byte[] invoke(RpcClientContext context, byte[] body) { | ||
return new byte[0]; | ||
} | ||
}); | ||
|
||
client = new TrpcGatewayClient(route); | ||
} | ||
|
||
private static @NotNull RouteDefinition initData() throws URISyntaxException { | ||
RouteDefinition definition = new RouteDefinition(); | ||
definition.setId("1"); | ||
List<FilterDefinition> list = new ArrayList<>(); | ||
list.add(new FilterDefinition()); | ||
definition.setFilters(list); | ||
URI uri = new URI("https://www.tencent.com/sayHello/nameParam"); | ||
definition.setUri(uri); | ||
Map<String, Object> metaData = new HashMap<>(); | ||
metaData.put("k1", new Object()); | ||
definition.setMetadata(metaData); | ||
definition.setOrder(1024); | ||
List<PredicateDefinition> predicates = new ArrayList<>(); | ||
PredicateDefinition p = new PredicateDefinition(); | ||
p.setName("trpc-Name"); | ||
p.setArgs(new HashMap<>()); | ||
predicates.add(p); | ||
definition.setPredicates(predicates); | ||
return definition; | ||
} | ||
|
||
@Test | ||
public void testOpen() throws URISyntaxException { | ||
Object callInfo = ReflectionTestUtils.getField(client, "callInfo"); | ||
Assert.assertNotNull(callInfo); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testParseCallInfoWithNull() { | ||
TrpcGatewayClient.parseCallInfo(""); | ||
} | ||
|
||
@Test | ||
public void testParseCallInfoWithIllegalArgument() { | ||
try { | ||
TrpcGatewayClient.parseCallInfo("http://www.abc.com"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以对解析后的内容进行断言 |
||
} catch (Exception e) { | ||
Assert.assertTrue( | ||
e.getMessage().contains("Create RpcInvocation fail: URI does not meet path specification")); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.cloud.gateway.filter; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.tencent.trpc.spring.cloud.gateway.filter.TrpcGatewayFilterFactory.Config; | ||
import com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriter; | ||
import com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriterTest; | ||
import com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriter; | ||
import com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriterTest; | ||
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcRequestRewriter; | ||
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcResponseRewriter; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
public class TrpcGatewayFilterFactoryTest { | ||
|
||
private TrpcGatewayFilterFactory factory; | ||
|
||
private TrpcGatewayFilterFactory.Config config; | ||
|
||
@Before | ||
public void setUp() { | ||
config = mock(Config.class); | ||
factory = new TrpcGatewayFilterFactory(); | ||
ReflectionTestUtils.setField(factory, "requestRewriter", mock(TrpcRequestRewriter.class)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议减少使用反射,可以搭建客户端服务端进行单测 |
||
ReflectionTestUtils.setField(factory, "responseRewriter", mock(TrpcResponseRewriter.class)); | ||
} | ||
|
||
@Test | ||
public void testApply() { | ||
when(config.isEnabled()).thenReturn(Boolean.TRUE); | ||
GatewayFilter filter = factory.apply(config); | ||
Assert.assertNotNull(filter); | ||
Assert.assertTrue(filter instanceof TrpcRoutingFilter); | ||
} | ||
|
||
@Test | ||
public void testLoadRequestRewriter() { | ||
setRequest(); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testLoadRequestRewriterWithException() { | ||
String className = "com.tencent.MyRequestRewriter"; | ||
Mockito.when(config.getRequestRewriter()).thenReturn(className); | ||
factory.apply(config); | ||
} | ||
|
||
@Test | ||
public void testLoadRequestRewriterWithNull() { | ||
String className = "com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriterTest"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 代码中的字符串建议提取为常量 |
||
Mockito.when(config.getRequestRewriter()).thenReturn(className); | ||
factory.apply(config); | ||
Object requestRewriter = ReflectionTestUtils.getField(factory, "requestRewriter"); | ||
Assert.assertFalse(requestRewriter instanceof MyRequestRewriterTest); | ||
} | ||
|
||
@Test | ||
public void testLoadResponseRewriter() { | ||
setRequest(); | ||
String respClassName = "com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriter"; | ||
Mockito.when(config.getResponseRewriter()).thenReturn(respClassName); | ||
factory.apply(config); | ||
Object requestRewriter = ReflectionTestUtils.getField(factory, "responseRewriter"); | ||
Assert.assertTrue(requestRewriter instanceof MyResponseRewriter); | ||
} | ||
|
||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testLoadResponseRewriterWithException() { | ||
setRequest(); | ||
String className = "com.tencent.MyRequestRewriter"; | ||
Mockito.when(config.getRequestRewriter()).thenReturn(className); | ||
factory.apply(config); | ||
} | ||
|
||
@Test | ||
public void testLoadResponseRewriterWithNull() { | ||
setRequest(); | ||
String className = "com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriterTest"; | ||
Mockito.when(config.getResponseRewriter()).thenReturn(className); | ||
factory.apply(config); | ||
Object requestRewriter = ReflectionTestUtils.getField(factory, "responseRewriter"); | ||
System.out.println(requestRewriter); | ||
Assert.assertFalse(requestRewriter instanceof MyResponseRewriterTest); | ||
} | ||
|
||
private void setRequest() { | ||
String className = "com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriter"; | ||
Mockito.when(config.getRequestRewriter()).thenReturn(className); | ||
factory.apply(config); | ||
Object requestRewriter = ReflectionTestUtils.getField(factory, "requestRewriter"); | ||
Assert.assertTrue(requestRewriter instanceof MyRequestRewriter); | ||
} | ||
|
||
@Test | ||
public void testConfig() { | ||
TrpcGatewayFilterFactory.Config newConfig = new Config(); | ||
newConfig.setEnabled(true); | ||
Assert.assertTrue(newConfig.isEnabled()); | ||
|
||
String requestWriter = "com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriter"; | ||
newConfig.setRequestRewriter(requestWriter); | ||
Assert.assertEquals(requestWriter, newConfig.getRequestRewriter()); | ||
|
||
String metaData = "k1:v1;k2:v2"; | ||
newConfig.setMetadata(metaData); | ||
Assert.assertEquals(metaData, newConfig.getMetadata()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.cloud.gateway.filter.request; | ||
|
||
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcRequestRewriter; | ||
import org.springframework.cloud.gateway.route.Route; | ||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
public class MyRequestRewriter implements TrpcRequestRewriter { | ||
|
||
@Override | ||
public DataBuffer resolver(ServerWebExchange exchange, Route route, DataBuffer body) { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.cloud.gateway.filter.request; | ||
|
||
public class MyRequestRewriterTest extends Thread { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.cloud.gateway.filter.response; | ||
|
||
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcResponseRewriter; | ||
import java.util.Map; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class MyResponseRewriter implements TrpcResponseRewriter { | ||
|
||
@Override | ||
public Mono<Void> write(ServerWebExchange exchange, Map<String, Object> metaData, Mono<byte[]> result) { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.cloud.gateway.filter.response; | ||
|
||
public class MyResponseRewriterTest extends Thread{ | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
统一使用英文注释,下面也是