-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improve]: fix host vailed util & add vaild util test.
Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
- Loading branch information
Showing
4 changed files
with
247 additions
and
2 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
84 changes: 84 additions & 0 deletions
84
common/src/test/java/org/apache/hertzbeat/common/support/vaild/EmailParamValidatorTest.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,84 @@ | ||
/* | ||
* 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 org.apache.hertzbeat.common.support.vaild; | ||
|
||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.apache.hertzbeat.common.support.valid.EmailParamValidator; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Test case for {@link EmailParamValidator} | ||
*/ | ||
|
||
class EmailParamValidatorTest { | ||
|
||
@InjectMocks | ||
private EmailParamValidator emailParamValidator; | ||
|
||
@Mock | ||
private ConstraintValidatorContext context; | ||
|
||
@Mock | ||
private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
when(context.buildConstraintViolationWithTemplate(any())).thenReturn(constraintViolationBuilder); | ||
} | ||
|
||
@Test | ||
public void testIsValid() { | ||
boolean result = emailParamValidator.isValid(null, context); | ||
assertFalse(result); | ||
|
||
result = emailParamValidator.isValid("123456345@qq.com", context); | ||
assertTrue(result); | ||
|
||
result = emailParamValidator.isValid("", context); | ||
assertFalse(result); | ||
|
||
result = emailParamValidator.isValid(" ", context); | ||
assertFalse(result); | ||
|
||
result = emailParamValidator.isValid("test@example.com", context); | ||
assertTrue(result); | ||
|
||
result = emailParamValidator.isValid("test@example", context); | ||
assertFalse(result); | ||
|
||
result = emailParamValidator.isValid("test@sub.example.com", context); | ||
assertTrue(result); | ||
|
||
|
||
String longEmail = "verylongemailaddress@subdomain.domain.example.com"; | ||
result = emailParamValidator.isValid(longEmail, context); | ||
assertTrue(result); | ||
} | ||
|
||
} | ||
|
84 changes: 84 additions & 0 deletions
84
common/src/test/java/org/apache/hertzbeat/common/support/vaild/HostParamValidatorTest.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,84 @@ | ||
/* | ||
* 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 org.apache.hertzbeat.common.support.vaild; | ||
|
||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.apache.hertzbeat.common.support.valid.HostParamValidator; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Test case for {@link HostParamValidator} | ||
*/ | ||
|
||
class HostParamValidatorTest { | ||
|
||
@InjectMocks | ||
private HostParamValidator hostParamValidator; | ||
|
||
@Mock | ||
private ConstraintValidatorContext context; | ||
|
||
@Mock | ||
private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
when(context.buildConstraintViolationWithTemplate(any())).thenReturn(constraintViolationBuilder); | ||
} | ||
|
||
@Test | ||
public void testIsValid() { | ||
boolean result = hostParamValidator.isValid(null, context); | ||
assertFalse(result); | ||
|
||
result = hostParamValidator.isValid("", context); | ||
assertFalse(result); | ||
|
||
result = hostParamValidator.isValid(" ", context); | ||
assertFalse(result); | ||
|
||
result = hostParamValidator.isValid("192.168.1.1", context); | ||
assertTrue(result); | ||
|
||
result = hostParamValidator.isValid("2001:0db8:85a3:0000:0000:8a2e:0370:7334", context); | ||
assertTrue(result); | ||
|
||
result = hostParamValidator.isValid("www.example.com", context); | ||
assertTrue(result); | ||
|
||
result = hostParamValidator.isValid("http://www.example.com", context); | ||
assertTrue(result); | ||
|
||
result = hostParamValidator.isValid("https://www.baidu.com", context); | ||
assertTrue(result); | ||
|
||
result = hostParamValidator.isValid("ht!tp://www.example.com", context); | ||
assertFalse(result); | ||
} | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
...n/src/test/java/org/apache/hertzbeat/common/support/vaild/PhoneNumParamValidatorTest.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,75 @@ | ||
/* | ||
* 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 org.apache.hertzbeat.common.support.vaild; | ||
|
||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.apache.hertzbeat.common.support.valid.PhoneNumParamValidator; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Test case for {@link PhoneNumParamValidator} | ||
*/ | ||
|
||
class PhoneNumParamValidatorTest { | ||
|
||
@InjectMocks | ||
private PhoneNumParamValidator phoneNumParamValidator; | ||
|
||
@Mock | ||
private ConstraintValidatorContext context; | ||
|
||
@Mock | ||
private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
when(context.buildConstraintViolationWithTemplate(any())).thenReturn(constraintViolationBuilder); | ||
} | ||
|
||
@Test | ||
public void testIsValid() { | ||
boolean result = phoneNumParamValidator.isValid(null, context); | ||
assertFalse(result); | ||
|
||
result = phoneNumParamValidator.isValid("", context); | ||
assertFalse(result); | ||
|
||
result = phoneNumParamValidator.isValid("123456", context); | ||
assertFalse(result); | ||
|
||
result = phoneNumParamValidator.isValid("abc123", context); | ||
assertFalse(result); | ||
|
||
result = phoneNumParamValidator.isValid("13900001234", context); | ||
assertTrue(result); | ||
|
||
result = phoneNumParamValidator.isValid("1234567890123456789", context); | ||
assertFalse(result); | ||
} | ||
} | ||
|