|
| 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 | +} |
0 commit comments