1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to You under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+ package com .alibaba .dubbo .validation .filter ;
18+
19+ import com .alibaba .dubbo .common .URL ;
20+ import com .alibaba .dubbo .rpc .*;
21+ import com .alibaba .dubbo .validation .Validation ;
22+ import com .alibaba .dubbo .validation .Validator ;
23+ import org .junit .Before ;
24+ import org .junit .Test ;
25+
26+ import static org .hamcrest .MatcherAssert .assertThat ;
27+ import static org .hamcrest .core .Is .is ;
28+ import static org .mockito .BDDMockito .given ;
29+ import static org .mockito .Mockito .mock ;
30+
31+ public class ValidationFilterTest {
32+ private Invoker <?> invoker = mock (Invoker .class );
33+ private Validation validation = mock (Validation .class );
34+ private Validator validator = mock (Validator .class );
35+ private RpcInvocation invocation = mock (RpcInvocation .class );
36+
37+ private ValidationFilter validationFilter ;
38+
39+ @ Before
40+ public void setUp () throws Exception {
41+ this .validationFilter = new ValidationFilter ();
42+ }
43+
44+ @ Test
45+ public void testItWithNotExistClass () throws Exception {
46+ URL url = URL .valueOf ("test://test:11/test?default.validation=true" );
47+
48+ given (validation .getValidator (url )).willThrow (new IllegalStateException ("Not found class test, cause: test" ));
49+ given (invoker .invoke (invocation )).willReturn (new RpcResult ("success" ));
50+ given (invoker .getUrl ()).willReturn (url );
51+ given (invocation .getMethodName ()).willReturn ("echo1" );
52+ given (invocation .getParameterTypes ()).willReturn (new Class <?>[]{String .class });
53+ given (invocation .getArguments ()).willReturn (new Object []{"arg1" });
54+
55+ validationFilter .setValidation (validation );
56+ Result result = validationFilter .invoke (invoker , invocation );
57+
58+ assertThat (result .getException ().getMessage (), is ("Not found class test, cause: test" ));
59+
60+ }
61+
62+ @ Test
63+ public void testItWithExistClass () throws Exception {
64+ URL url = URL .valueOf ("test://test:11/test?default.validation=true" );
65+
66+ given (validation .getValidator (url )).willReturn (validator );
67+ given (invoker .invoke (invocation )).willReturn (new RpcResult ("success" ));
68+ given (invoker .getUrl ()).willReturn (url );
69+ given (invocation .getMethodName ()).willReturn ("echo1" );
70+ given (invocation .getParameterTypes ()).willReturn (new Class <?>[]{String .class });
71+ given (invocation .getArguments ()).willReturn (new Object []{"arg1" });
72+
73+ validationFilter .setValidation (validation );
74+ Result result = validationFilter .invoke (invoker , invocation );
75+
76+ assertThat (String .valueOf (result .getValue ()), is ("success" ));
77+ }
78+
79+ @ Test
80+ public void testItWithoutUrlParameters () throws Exception {
81+ URL url = URL .valueOf ("test://test:11/test" );
82+
83+ given (validation .getValidator (url )).willReturn (validator );
84+ given (invoker .invoke (invocation )).willReturn (new RpcResult ("success" ));
85+ given (invoker .getUrl ()).willReturn (url );
86+ given (invocation .getMethodName ()).willReturn ("echo1" );
87+ given (invocation .getParameterTypes ()).willReturn (new Class <?>[]{String .class });
88+ given (invocation .getArguments ()).willReturn (new Object []{"arg1" });
89+
90+ validationFilter .setValidation (validation );
91+ Result result = validationFilter .invoke (invoker , invocation );
92+
93+ assertThat (String .valueOf (result .getValue ()), is ("success" ));
94+ }
95+
96+ @ Test
97+ public void testItWhileMethodNameStartWithDollar () throws Exception {
98+ URL url = URL .valueOf ("test://test:11/test" );
99+
100+ given (validation .getValidator (url )).willReturn (validator );
101+ given (invoker .invoke (invocation )).willReturn (new RpcResult ("success" ));
102+ given (invoker .getUrl ()).willReturn (url );
103+ given (invocation .getMethodName ()).willReturn ("$echo1" );
104+ given (invocation .getParameterTypes ()).willReturn (new Class <?>[]{String .class });
105+ given (invocation .getArguments ()).willReturn (new Object []{"arg1" });
106+
107+ validationFilter .setValidation (validation );
108+ Result result = validationFilter .invoke (invoker , invocation );
109+
110+ assertThat (String .valueOf (result .getValue ()), is ("success" ));
111+
112+ }
113+
114+
115+ @ Test (expected = RpcException .class )
116+ public void testItWhileThrowoutRpcException () throws Exception {
117+ URL url = URL .valueOf ("test://test:11/test?default.validation=true" );
118+
119+ given (validation .getValidator (url )).willThrow (new RpcException ("rpc exception" ));
120+ given (invoker .invoke (invocation )).willReturn (new RpcResult ("success" ));
121+ given (invoker .getUrl ()).willReturn (url );
122+ given (invocation .getMethodName ()).willReturn ("echo1" );
123+ given (invocation .getParameterTypes ()).willReturn (new Class <?>[]{String .class });
124+ given (invocation .getArguments ()).willReturn (new Object []{"arg1" });
125+
126+ validationFilter .setValidation (validation );
127+ validationFilter .invoke (invoker , invocation );
128+ }
129+ }
0 commit comments