-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
7 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...er-validation/src/test/java/com/alibaba/dubbo/validation/filter/ValidationFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.dubbo.validation.filter; | ||
|
||
import com.alibaba.dubbo.common.URL; | ||
import com.alibaba.dubbo.rpc.*; | ||
import com.alibaba.dubbo.validation.Validation; | ||
import com.alibaba.dubbo.validation.Validator; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ValidationFilterTest { | ||
private Invoker<?> invoker = mock(Invoker.class); | ||
private Validation validation = mock(Validation.class); | ||
private Validator validator = mock(Validator.class); | ||
private RpcInvocation invocation = mock(RpcInvocation.class); | ||
|
||
private ValidationFilter validationFilter; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
this.validationFilter = new ValidationFilter(); | ||
} | ||
|
||
@Test | ||
public void testItWithNotExistClass() throws Exception { | ||
URL url = URL.valueOf("test://test:11/test?default.validation=true"); | ||
|
||
given(validation.getValidator(url)).willThrow(new IllegalStateException("Not found class test, cause: test")); | ||
given(invoker.invoke(invocation)).willReturn(new RpcResult("success")); | ||
given(invoker.getUrl()).willReturn(url); | ||
given(invocation.getMethodName()).willReturn("echo1"); | ||
given(invocation.getParameterTypes()).willReturn(new Class<?>[]{String.class}); | ||
given(invocation.getArguments()).willReturn(new Object[]{"arg1"}); | ||
|
||
validationFilter.setValidation(validation); | ||
Result result = validationFilter.invoke(invoker, invocation); | ||
|
||
assertThat(result.getException().getMessage(), is("Not found class test, cause: test")); | ||
|
||
} | ||
|
||
@Test | ||
public void testItWithExistClass() throws Exception { | ||
URL url = URL.valueOf("test://test:11/test?default.validation=true"); | ||
|
||
given(validation.getValidator(url)).willReturn(validator); | ||
given(invoker.invoke(invocation)).willReturn(new RpcResult("success")); | ||
given(invoker.getUrl()).willReturn(url); | ||
given(invocation.getMethodName()).willReturn("echo1"); | ||
given(invocation.getParameterTypes()).willReturn(new Class<?>[]{String.class}); | ||
given(invocation.getArguments()).willReturn(new Object[]{"arg1"}); | ||
|
||
validationFilter.setValidation(validation); | ||
Result result = validationFilter.invoke(invoker, invocation); | ||
|
||
assertThat(String.valueOf(result.getValue()), is("success")); | ||
} | ||
|
||
@Test | ||
public void testItWithoutUrlParameters() throws Exception { | ||
URL url = URL.valueOf("test://test:11/test"); | ||
|
||
given(validation.getValidator(url)).willReturn(validator); | ||
given(invoker.invoke(invocation)).willReturn(new RpcResult("success")); | ||
given(invoker.getUrl()).willReturn(url); | ||
given(invocation.getMethodName()).willReturn("echo1"); | ||
given(invocation.getParameterTypes()).willReturn(new Class<?>[]{String.class}); | ||
given(invocation.getArguments()).willReturn(new Object[]{"arg1"}); | ||
|
||
validationFilter.setValidation(validation); | ||
Result result = validationFilter.invoke(invoker, invocation); | ||
|
||
assertThat(String.valueOf(result.getValue()), is("success")); | ||
} | ||
|
||
@Test | ||
public void testItWhileMethodNameStartWithDollar() throws Exception { | ||
URL url = URL.valueOf("test://test:11/test"); | ||
|
||
given(validation.getValidator(url)).willReturn(validator); | ||
given(invoker.invoke(invocation)).willReturn(new RpcResult("success")); | ||
given(invoker.getUrl()).willReturn(url); | ||
given(invocation.getMethodName()).willReturn("$echo1"); | ||
given(invocation.getParameterTypes()).willReturn(new Class<?>[]{String.class}); | ||
given(invocation.getArguments()).willReturn(new Object[]{"arg1"}); | ||
|
||
validationFilter.setValidation(validation); | ||
Result result = validationFilter.invoke(invoker, invocation); | ||
|
||
assertThat(String.valueOf(result.getValue()), is("success")); | ||
|
||
} | ||
|
||
|
||
@Test(expected = RpcException.class) | ||
public void testItWhileThrowoutRpcException() throws Exception { | ||
URL url = URL.valueOf("test://test:11/test?default.validation=true"); | ||
|
||
given(validation.getValidator(url)).willThrow(new RpcException("rpc exception")); | ||
given(invoker.invoke(invocation)).willReturn(new RpcResult("success")); | ||
given(invoker.getUrl()).willReturn(url); | ||
given(invocation.getMethodName()).willReturn("echo1"); | ||
given(invocation.getParameterTypes()).willReturn(new Class<?>[]{String.class}); | ||
given(invocation.getArguments()).willReturn(new Object[]{"arg1"}); | ||
|
||
validationFilter.setValidation(validation); | ||
validationFilter.invoke(invoker, invocation); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.dubbo.validation.support.jvalidation; | ||
|
||
import com.alibaba.dubbo.common.URL; | ||
import com.alibaba.dubbo.validation.Validation; | ||
import com.alibaba.dubbo.validation.Validator; | ||
import org.junit.Test; | ||
|
||
import javax.validation.ValidationException; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class JValidationTest { | ||
@Test(expected = ValidationException.class) | ||
public void testReturnTypeWithInvalidValidationProvider() throws Exception { | ||
Validation jValidation = new JValidation(); | ||
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.JValidation?" + | ||
"jvalidation=com.alibaba.dubbo.validation.Validation"); | ||
jValidation.getValidator(url); | ||
} | ||
|
||
@Test | ||
public void testReturnTypeWithDefaultValidatorProvider() throws Exception { | ||
Validation jValidation = new JValidation(); | ||
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.JValidation"); | ||
Validator validator = jValidation.getValidator(url); | ||
assertThat(validator instanceof JValidator, is(true)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...dation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.dubbo.validation.support.jvalidation; | ||
|
||
import com.alibaba.dubbo.common.URL; | ||
import com.alibaba.dubbo.validation.support.jvalidation.mock.ValidationParameter; | ||
import org.junit.Test; | ||
|
||
import javax.validation.ConstraintViolationException; | ||
|
||
public class JValidatorTest { | ||
@Test(expected = NoSuchMethodException.class) | ||
public void testItWithNonExistMethod() throws Exception { | ||
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); | ||
JValidator jValidator = new JValidator(url); | ||
jValidator.validate("nonExistingMethod", new Class<?>[]{String.class}, new Object[]{"arg1"}); | ||
} | ||
|
||
@Test | ||
public void testItWithExistMethod() throws Exception { | ||
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); | ||
JValidator jValidator = new JValidator(url); | ||
jValidator.validate("someMethod1", new Class<?>[]{String.class}, new Object[]{"anything"}); | ||
} | ||
|
||
@Test(expected = ConstraintViolationException.class) | ||
public void testItWhenItViolatedConstraint() throws Exception { | ||
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); | ||
JValidator jValidator = new JValidator(url); | ||
jValidator.validate("someMethod2", new Class<?>[]{ValidationParameter.class}, new Object[]{new ValidationParameter()}); | ||
} | ||
|
||
@Test | ||
public void testItWhenItMeetsConstraint() throws Exception { | ||
URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); | ||
JValidator jValidator = new JValidator(url); | ||
jValidator.validate("someMethod2", new Class<?>[]{ValidationParameter.class}, new Object[]{new ValidationParameter("NotBeNull")}); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/JValidatorTestTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.dubbo.validation.support.jvalidation.mock; | ||
|
||
import com.alibaba.dubbo.validation.MethodValidated; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
public interface JValidatorTestTarget { | ||
@MethodValidated | ||
public void someMethod1(String anything); | ||
|
||
@MethodValidated(Test2.class) | ||
public void someMethod2(@NotNull ValidationParameter validationParameter); | ||
|
||
@interface Test2 { | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
.../test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/ValidationParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.dubbo.validation.support.jvalidation.mock; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
public class ValidationParameter { | ||
@NotNull | ||
private String parameter; | ||
|
||
public ValidationParameter() { | ||
} | ||
|
||
public ValidationParameter(String parameter) { | ||
this.parameter = parameter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters