Skip to content

Commit 2f0107c

Browse files
qinnnyulbeiwei30
authored andcommitted
[Dubbo-1687]Add unit tests for dubbo-filter-validation module (#1736)
* Add unit tests for dubbo-filter-validation module * add more jaxb api libs for testing with jdk9, because it does't contain them by default any more
1 parent 322add3 commit 2f0107c

File tree

7 files changed

+324
-0
lines changed

7 files changed

+324
-0
lines changed

dubbo-filter/dubbo-filter-validation/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,35 @@
3939
<groupId>javax.validation</groupId>
4040
<artifactId>validation-api</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.hibernate</groupId>
44+
<artifactId>hibernate-validator</artifactId>
45+
<scope>test</scope>
46+
<version>${hibernate_validator_version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>javax.el</groupId>
50+
<artifactId>javax.el-api</artifactId>
51+
<scope>test</scope>
52+
<version>${el_api_version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>javax.xml.bind</groupId>
56+
<artifactId>jaxb-api</artifactId>
57+
<scope>test</scope>
58+
<version>${jaxb_api_version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.sun.xml.bind</groupId>
62+
<artifactId>jaxb-impl</artifactId>
63+
<scope>test</scope>
64+
<version>${jaxb_api_version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.sun.xml.bind</groupId>
68+
<artifactId>jaxb-core</artifactId>
69+
<scope>test</scope>
70+
<version>${jaxb_api_version}</version>
71+
</dependency>
4272
</dependencies>
4373
</project>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.support.jvalidation;
18+
19+
import com.alibaba.dubbo.common.URL;
20+
import com.alibaba.dubbo.validation.Validation;
21+
import com.alibaba.dubbo.validation.Validator;
22+
import org.junit.Test;
23+
24+
import javax.validation.ValidationException;
25+
26+
import static org.hamcrest.core.Is.is;
27+
import static org.junit.Assert.assertThat;
28+
29+
public class JValidationTest {
30+
@Test(expected = ValidationException.class)
31+
public void testReturnTypeWithInvalidValidationProvider() throws Exception {
32+
Validation jValidation = new JValidation();
33+
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.JValidation?" +
34+
"jvalidation=com.alibaba.dubbo.validation.Validation");
35+
jValidation.getValidator(url);
36+
}
37+
38+
@Test
39+
public void testReturnTypeWithDefaultValidatorProvider() throws Exception {
40+
Validation jValidation = new JValidation();
41+
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.JValidation");
42+
Validator validator = jValidation.getValidator(url);
43+
assertThat(validator instanceof JValidator, is(true));
44+
}
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.support.jvalidation;
18+
19+
import com.alibaba.dubbo.common.URL;
20+
import com.alibaba.dubbo.validation.support.jvalidation.mock.ValidationParameter;
21+
import org.junit.Test;
22+
23+
import javax.validation.ConstraintViolationException;
24+
25+
public class JValidatorTest {
26+
@Test(expected = NoSuchMethodException.class)
27+
public void testItWithNonExistMethod() throws Exception {
28+
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
29+
JValidator jValidator = new JValidator(url);
30+
jValidator.validate("nonExistingMethod", new Class<?>[]{String.class}, new Object[]{"arg1"});
31+
}
32+
33+
@Test
34+
public void testItWithExistMethod() throws Exception {
35+
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
36+
JValidator jValidator = new JValidator(url);
37+
jValidator.validate("someMethod1", new Class<?>[]{String.class}, new Object[]{"anything"});
38+
}
39+
40+
@Test(expected = ConstraintViolationException.class)
41+
public void testItWhenItViolatedConstraint() throws Exception {
42+
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
43+
JValidator jValidator = new JValidator(url);
44+
jValidator.validate("someMethod2", new Class<?>[]{ValidationParameter.class}, new Object[]{new ValidationParameter()});
45+
}
46+
47+
@Test
48+
public void testItWhenItMeetsConstraint() throws Exception {
49+
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
50+
JValidator jValidator = new JValidator(url);
51+
jValidator.validate("someMethod2", new Class<?>[]{ValidationParameter.class}, new Object[]{new ValidationParameter("NotBeNull")});
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.support.jvalidation.mock;
18+
19+
import com.alibaba.dubbo.validation.MethodValidated;
20+
21+
import javax.validation.constraints.NotNull;
22+
23+
public interface JValidatorTestTarget {
24+
@MethodValidated
25+
public void someMethod1(String anything);
26+
27+
@MethodValidated(Test2.class)
28+
public void someMethod2(@NotNull ValidationParameter validationParameter);
29+
30+
@interface Test2 {
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.support.jvalidation.mock;
18+
19+
import javax.validation.constraints.NotNull;
20+
21+
public class ValidationParameter {
22+
@NotNull
23+
private String parameter;
24+
25+
public ValidationParameter() {
26+
}
27+
28+
public ValidationParameter(String parameter) {
29+
this.parameter = parameter;
30+
}
31+
}

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
<junit_version>4.12</junit_version>
9292
<hazelcast_version>3.9-EA</hazelcast_version>
9393
<hamcrest_version>1.3</hamcrest_version>
94+
<hibernate_validator_version>5.2.4.Final</hibernate_validator_version>
95+
<el_api_version>2.2.4</el_api_version>
96+
<jaxb_api_version>2.2.7</jaxb_api_version>
9497
<cglib_version>2.2</cglib_version>
9598
<mockito_version>2.18.3</mockito_version>
9699
<!-- Build args -->

0 commit comments

Comments
 (0)