Skip to content

Commit 84703c3

Browse files
完善trpc spring support模块单测
1 parent e72139d commit 84703c3

31 files changed

+2136
-13
lines changed

jacoco.exec

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.spring.cloud.gateway;
13+
14+
import com.tencent.trpc.spring.cloud.gateway.service.MyService;
15+
import org.junit.Assert;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.boot.test.context.SpringBootTest;
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.test.context.junit4.SpringRunner;
22+
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest(classes = TrpcGatewayApplication.class)
25+
public class TrpcGatewayApplicationTest {
26+
27+
@Autowired
28+
private MyService myService;
29+
30+
@Autowired
31+
private ApplicationContext applicationContext;
32+
33+
@Test
34+
public void testContextLoads() {
35+
// 测试上下文加载
36+
Assert.assertNotNull(applicationContext);
37+
}
38+
39+
@Test
40+
public void testComponentBehavior() {
41+
Assert.assertNotNull(myService);
42+
Assert.assertTrue(myService.invoke());
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.spring.cloud.gateway.client;
13+
14+
import com.tencent.trpc.core.rpc.CloseFuture;
15+
import com.tencent.trpc.core.rpc.GenericClient;
16+
import com.tencent.trpc.core.rpc.RpcClientContext;
17+
import com.tencent.trpc.core.rpc.TRpcProxy;
18+
import java.net.URI;
19+
import java.net.URISyntaxException;
20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.concurrent.CompletionStage;
25+
import java.util.function.Predicate;
26+
import org.jetbrains.annotations.NotNull;
27+
import org.junit.Assert;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.mockito.Mockito;
32+
import org.powermock.api.mockito.PowerMockito;
33+
import org.powermock.core.classloader.annotations.PowerMockIgnore;
34+
import org.powermock.core.classloader.annotations.PrepareForTest;
35+
import org.powermock.modules.junit4.PowerMockRunner;
36+
import org.springframework.cloud.gateway.filter.FilterDefinition;
37+
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
38+
import org.springframework.cloud.gateway.route.Route;
39+
import org.springframework.cloud.gateway.route.Route.Builder;
40+
import org.springframework.cloud.gateway.route.RouteDefinition;
41+
import org.springframework.test.util.ReflectionTestUtils;
42+
import org.springframework.web.server.ServerWebExchange;
43+
44+
@RunWith(PowerMockRunner.class)
45+
@PrepareForTest(TRpcProxy.class)
46+
@PowerMockIgnore({"javax.management.*", "javax.security.*", "javax.ws.*"})
47+
public class TrpcGatewayClientTest {
48+
49+
private TrpcGatewayClient client;
50+
51+
private Route route;
52+
53+
private Builder builder;
54+
55+
@Before
56+
public void setUp() throws URISyntaxException {
57+
PowerMockito.mockStatic(TRpcProxy.class);
58+
RouteDefinition definition = initData();
59+
builder = Route.builder(definition);
60+
Predicate<ServerWebExchange> predicate = serverWebExchange -> {
61+
serverWebExchange.checkNotModified("test");
62+
return true;
63+
};
64+
ReflectionTestUtils.setField(builder, "predicate", predicate);
65+
Predicate<ServerWebExchange> predicate2 = serverWebExchange -> {
66+
serverWebExchange.checkNotModified("test2");
67+
return true;
68+
};
69+
builder.and(predicate2);
70+
route = builder.build();
71+
Mockito.when(TRpcProxy.getProxy(Mockito.anyString())).thenReturn(new GenericClient() {
72+
73+
@Override
74+
public CompletionStage<byte[]> asyncInvoke(RpcClientContext context, byte[] body) {
75+
return new CloseFuture<>();
76+
}
77+
78+
@Override
79+
public byte[] invoke(RpcClientContext context, byte[] body) {
80+
return new byte[0];
81+
}
82+
});
83+
84+
client = new TrpcGatewayClient(route);
85+
}
86+
87+
private static @NotNull RouteDefinition initData() throws URISyntaxException {
88+
RouteDefinition definition = new RouteDefinition();
89+
definition.setId("1");
90+
List<FilterDefinition> list = new ArrayList<>();
91+
list.add(new FilterDefinition());
92+
definition.setFilters(list);
93+
URI uri = new URI("https://www.tencent.com/sayHello/nameParam");
94+
definition.setUri(uri);
95+
Map<String, Object> metaData = new HashMap<>();
96+
metaData.put("k1", new Object());
97+
definition.setMetadata(metaData);
98+
definition.setOrder(1024);
99+
List<PredicateDefinition> predicates = new ArrayList<>();
100+
PredicateDefinition p = new PredicateDefinition();
101+
p.setName("trpc-Name");
102+
p.setArgs(new HashMap<>());
103+
predicates.add(p);
104+
definition.setPredicates(predicates);
105+
return definition;
106+
}
107+
108+
@Test
109+
public void testOpen() throws URISyntaxException {
110+
Object callInfo = ReflectionTestUtils.getField(client, "callInfo");
111+
Assert.assertNotNull(callInfo);
112+
}
113+
114+
@Test(expected = NullPointerException.class)
115+
public void testParseCallInfoWithNull() {
116+
TrpcGatewayClient.parseCallInfo("");
117+
}
118+
119+
@Test
120+
public void testParseCallInfoWithIllegalArgument() {
121+
try {
122+
TrpcGatewayClient.parseCallInfo("http://www.abc.com");
123+
} catch (Exception e) {
124+
Assert.assertTrue(
125+
e.getMessage().contains("Create RpcInvocation fail: URI does not meet path specification"));
126+
}
127+
}
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.spring.cloud.gateway.filter;
13+
14+
import static org.mockito.Mockito.mock;
15+
import static org.mockito.Mockito.when;
16+
17+
import com.tencent.trpc.spring.cloud.gateway.filter.TrpcGatewayFilterFactory.Config;
18+
import com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriter;
19+
import com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriterTest;
20+
import com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriter;
21+
import com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriterTest;
22+
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcRequestRewriter;
23+
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcResponseRewriter;
24+
import org.junit.Assert;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.mockito.Mockito;
28+
import org.springframework.cloud.gateway.filter.GatewayFilter;
29+
import org.springframework.test.util.ReflectionTestUtils;
30+
31+
public class TrpcGatewayFilterFactoryTest {
32+
33+
private TrpcGatewayFilterFactory factory;
34+
35+
private TrpcGatewayFilterFactory.Config config;
36+
37+
@Before
38+
public void setUp() {
39+
config = mock(Config.class);
40+
factory = new TrpcGatewayFilterFactory();
41+
ReflectionTestUtils.setField(factory, "requestRewriter", mock(TrpcRequestRewriter.class));
42+
ReflectionTestUtils.setField(factory, "responseRewriter", mock(TrpcResponseRewriter.class));
43+
}
44+
45+
@Test
46+
public void testApply() {
47+
when(config.isEnabled()).thenReturn(Boolean.TRUE);
48+
GatewayFilter filter = factory.apply(config);
49+
Assert.assertNotNull(filter);
50+
Assert.assertTrue(filter instanceof TrpcRoutingFilter);
51+
}
52+
53+
@Test
54+
public void testLoadRequestRewriter() {
55+
setRequest();
56+
}
57+
58+
@Test(expected = IllegalArgumentException.class)
59+
public void testLoadRequestRewriterWithException() {
60+
String className = "com.tencent.MyRequestRewriter";
61+
Mockito.when(config.getRequestRewriter()).thenReturn(className);
62+
factory.apply(config);
63+
}
64+
65+
@Test
66+
public void testLoadRequestRewriterWithNull() {
67+
String className = "com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriterTest";
68+
Mockito.when(config.getRequestRewriter()).thenReturn(className);
69+
factory.apply(config);
70+
Object requestRewriter = ReflectionTestUtils.getField(factory, "requestRewriter");
71+
Assert.assertFalse(requestRewriter instanceof MyRequestRewriterTest);
72+
}
73+
74+
@Test
75+
public void testLoadResponseRewriter() {
76+
setRequest();
77+
String respClassName = "com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriter";
78+
Mockito.when(config.getResponseRewriter()).thenReturn(respClassName);
79+
factory.apply(config);
80+
Object requestRewriter = ReflectionTestUtils.getField(factory, "responseRewriter");
81+
Assert.assertTrue(requestRewriter instanceof MyResponseRewriter);
82+
}
83+
84+
85+
@Test(expected = IllegalArgumentException.class)
86+
public void testLoadResponseRewriterWithException() {
87+
setRequest();
88+
String className = "com.tencent.MyRequestRewriter";
89+
Mockito.when(config.getRequestRewriter()).thenReturn(className);
90+
factory.apply(config);
91+
}
92+
93+
@Test
94+
public void testLoadResponseRewriterWithNull() {
95+
setRequest();
96+
String className = "com.tencent.trpc.spring.cloud.gateway.filter.response.MyResponseRewriterTest";
97+
Mockito.when(config.getResponseRewriter()).thenReturn(className);
98+
factory.apply(config);
99+
Object requestRewriter = ReflectionTestUtils.getField(factory, "responseRewriter");
100+
System.out.println(requestRewriter);
101+
Assert.assertFalse(requestRewriter instanceof MyResponseRewriterTest);
102+
}
103+
104+
private void setRequest() {
105+
String className = "com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriter";
106+
Mockito.when(config.getRequestRewriter()).thenReturn(className);
107+
factory.apply(config);
108+
Object requestRewriter = ReflectionTestUtils.getField(factory, "requestRewriter");
109+
Assert.assertTrue(requestRewriter instanceof MyRequestRewriter);
110+
}
111+
112+
@Test
113+
public void testConfig() {
114+
TrpcGatewayFilterFactory.Config newConfig = new Config();
115+
newConfig.setEnabled(true);
116+
Assert.assertTrue(newConfig.isEnabled());
117+
118+
String requestWriter = "com.tencent.trpc.spring.cloud.gateway.filter.request.MyRequestRewriter";
119+
newConfig.setRequestRewriter(requestWriter);
120+
Assert.assertEquals(requestWriter, newConfig.getRequestRewriter());
121+
122+
String metaData = "k1:v1;k2:v2";
123+
newConfig.setMetadata(metaData);
124+
Assert.assertEquals(metaData, newConfig.getMetadata());
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.spring.cloud.gateway.filter.request;
13+
14+
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcRequestRewriter;
15+
import org.springframework.cloud.gateway.route.Route;
16+
import org.springframework.core.io.buffer.DataBuffer;
17+
import org.springframework.web.server.ServerWebExchange;
18+
19+
public class MyRequestRewriter implements TrpcRequestRewriter {
20+
21+
@Override
22+
public DataBuffer resolver(ServerWebExchange exchange, Route route, DataBuffer body) {
23+
return null;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.spring.cloud.gateway.filter.request;
13+
14+
public class MyRequestRewriterTest extends Thread {
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.spring.cloud.gateway.filter.response;
13+
14+
import com.tencent.trpc.spring.cloud.gateway.rewriter.TrpcResponseRewriter;
15+
import java.util.Map;
16+
import org.springframework.web.server.ServerWebExchange;
17+
import reactor.core.publisher.Mono;
18+
19+
public class MyResponseRewriter implements TrpcResponseRewriter {
20+
21+
@Override
22+
public Mono<Void> write(ServerWebExchange exchange, Map<String, Object> metaData, Mono<byte[]> result) {
23+
return null;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.spring.cloud.gateway.filter.response;
13+
14+
public class MyResponseRewriterTest extends Thread{
15+
16+
}

0 commit comments

Comments
 (0)