Skip to content

Commit

Permalink
[Improve] add unit test & fix some bugs (#2326)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
  • Loading branch information
yuluo-yx authored Jul 21, 2024
1 parent a8c1921 commit f84e1fc
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public class HostParamValidator implements ConstraintValidator<HostValid, String
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value != null && value.toLowerCase().contains(HTTP)){
value = value.replaceAll(PATTERN_HTTP, BLANK);
value = value.replaceFirst(PATTERN_HTTP, BLANK);
}
if (value != null && value.toLowerCase().contains(HTTPS)){
value = value.replace(PATTERN_HTTPS, BLANK);
value = value.replaceFirst(PATTERN_HTTPS, BLANK);
}

return IpDomainUtil.validateIpDomain(value);
}

}
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);
}

}

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);
}
}

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);
}
}

0 comments on commit f84e1fc

Please sign in to comment.