|
| 1 | +/* |
| 2 | + * Copyright 1999-2020 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.alibaba.csp.sentinel.adapter.dubbo.origin; |
| 17 | + |
| 18 | +import com.alibaba.csp.sentinel.adapter.dubbo.DubboUtils; |
| 19 | +import com.alibaba.dubbo.rpc.Invocation; |
| 20 | +import com.alibaba.dubbo.rpc.Invoker; |
| 21 | +import com.alibaba.dubbo.rpc.RpcInvocation; |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author tiecheng |
| 28 | + */ |
| 29 | +public class DubboOriginRegistryTest { |
| 30 | + |
| 31 | + @After |
| 32 | + public void cleanUp() { |
| 33 | + DubboOriginParserRegistry.setDubboOriginParser(new DefaultDubboOriginParser()); |
| 34 | + } |
| 35 | + |
| 36 | + @Test(expected = IllegalArgumentException.class) |
| 37 | + public void testDefaultOriginParserFail() { |
| 38 | + DubboOriginParserRegistry.getDubboOriginParser().parse(null, null); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testDefaultOriginParserSuccess() { |
| 43 | + RpcInvocation invocation = new RpcInvocation(); |
| 44 | + String dubboName = "sentinel"; |
| 45 | + invocation.setAttachment(DubboUtils.DUBBO_APPLICATION_KEY, dubboName); |
| 46 | + String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation); |
| 47 | + Assert.assertEquals(dubboName, origin); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testCustomOriginParser() { |
| 52 | + DubboOriginParserRegistry.setDubboOriginParser(new DubboOriginParser() { |
| 53 | + @Override |
| 54 | + public String parse(Invoker<?> invoker, Invocation invocation) { |
| 55 | + return invocation.getAttachment(DubboUtils.DUBBO_APPLICATION_KEY, "default") + "_" + invocation |
| 56 | + .getMethodName(); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + RpcInvocation invocation = new RpcInvocation(); |
| 61 | + String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation); |
| 62 | + Assert.assertEquals("default_null", origin); |
| 63 | + |
| 64 | + String dubboName = "sentinel"; |
| 65 | + invocation.setAttachment(DubboUtils.DUBBO_APPLICATION_KEY, dubboName); |
| 66 | + origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation); |
| 67 | + Assert.assertEquals(dubboName + "_null", origin); |
| 68 | + |
| 69 | + invocation.setMethodName("hello"); |
| 70 | + origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation); |
| 71 | + Assert.assertEquals(dubboName + "_hello", origin); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments