You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/10/23 09:48:50 UTC
[shardingsphere-elasticjob] branch master updated: Add test cases
for JobPropertiesValidator (#1650)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new ee47992 Add test cases for JobPropertiesValidator (#1650)
ee47992 is described below
commit ee47992821b9f836e80176e6411791039181d605
Author: Yanjie Zhou <zh...@aliyun.com>
AuthorDate: Fri Oct 23 17:48:40 2020 +0800
Add test cases for JobPropertiesValidator (#1650)
---
...talkJobErrorHandlerPropertiesValidatorTest.java | 66 ++++++++++++++++++
...mailJobErrorHandlerPropertiesValidatorTest.java | 69 +++++++++++++++++++
...chatJobErrorHandlerPropertiesValidatorTest.java | 66 ++++++++++++++++++
.../validator/JobPropertiesValidateRuleTest.java | 78 ++++++++++++++++++++++
4 files changed, 279 insertions(+)
diff --git a/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerPropertiesValidatorTest.java b/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerPropertiesValidatorTest.java
new file mode 100644
index 0000000..5942484
--- /dev/null
+++ b/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerPropertiesValidatorTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.shardingsphere.elasticjob.error.handler.dingtalk;
+
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerPropertiesValidator;
+import org.apache.shardingsphere.elasticjob.infra.spi.ElasticJobServiceLoader;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class DingtalkJobErrorHandlerPropertiesValidatorTest {
+
+ @Before
+ public void startup() {
+ ElasticJobServiceLoader.registerTypedService(JobErrorHandlerPropertiesValidator.class);
+ }
+
+ @Test
+ public void assertValidateWithNormal() {
+ Properties properties = new Properties();
+ properties.setProperty(DingtalkPropertiesConstants.WEBHOOK, "webhook");
+ properties.setProperty(DingtalkPropertiesConstants.READ_TIMEOUT_MILLISECONDS, "1000");
+ properties.setProperty(DingtalkPropertiesConstants.CONNECT_TIMEOUT_MILLISECONDS, "2000");
+ DingtalkJobErrorHandlerPropertiesValidator actual = getValidator();
+ actual.validate(properties);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void assertValidateWithPropsIsNull() {
+ DingtalkJobErrorHandlerPropertiesValidator actual = getValidator();
+ actual.validate(null);
+ }
+
+ @Test
+ public void assertValidateWithWebhookIsNull() {
+ DingtalkJobErrorHandlerPropertiesValidator actual = getValidator();
+ try {
+ actual.validate(new Properties());
+ } catch (NullPointerException e) {
+ assertThat(e.getMessage(), is(String.format("The property `%s` is required.", DingtalkPropertiesConstants.WEBHOOK)));
+ }
+ }
+
+ private DingtalkJobErrorHandlerPropertiesValidator getValidator() {
+ return (DingtalkJobErrorHandlerPropertiesValidator) ElasticJobServiceLoader.newTypedServiceInstance(JobErrorHandlerPropertiesValidator.class, "DINGTALK", null).get();
+ }
+}
diff --git a/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerPropertiesValidatorTest.java b/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerPropertiesValidatorTest.java
new file mode 100644
index 0000000..e9d98bf
--- /dev/null
+++ b/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerPropertiesValidatorTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.shardingsphere.elasticjob.error.handler.email;
+
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerPropertiesValidator;
+import org.apache.shardingsphere.elasticjob.infra.spi.ElasticJobServiceLoader;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class EmailJobErrorHandlerPropertiesValidatorTest {
+
+ @Before
+ public void startup() {
+ ElasticJobServiceLoader.registerTypedService(JobErrorHandlerPropertiesValidator.class);
+ }
+
+ @Test
+ public void assertValidateWithNormal() {
+ Properties properties = new Properties();
+ properties.setProperty(EmailPropertiesConstants.HOST, "host");
+ properties.setProperty(EmailPropertiesConstants.PORT, "465");
+ properties.setProperty(EmailPropertiesConstants.USERNAME, "username");
+ properties.setProperty(EmailPropertiesConstants.PASSWORD, "password");
+ properties.setProperty(EmailPropertiesConstants.FROM, "from@xxx.xx");
+ properties.setProperty(EmailPropertiesConstants.TO, "to@xxx.xx");
+ EmailJobErrorHandlerPropertiesValidator actual = getValidator();
+ actual.validate(properties);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void assertValidateWithPropsIsNull() {
+ EmailJobErrorHandlerPropertiesValidator actual = getValidator();
+ actual.validate(null);
+ }
+
+ @Test
+ public void assertValidateWithHostIsNull() {
+ EmailJobErrorHandlerPropertiesValidator actual = getValidator();
+ try {
+ actual.validate(new Properties());
+ } catch (NullPointerException e) {
+ assertThat(e.getMessage(), is(String.format("The property `%s` is required.", EmailPropertiesConstants.HOST)));
+ }
+ }
+
+ private EmailJobErrorHandlerPropertiesValidator getValidator() {
+ return (EmailJobErrorHandlerPropertiesValidator) ElasticJobServiceLoader.newTypedServiceInstance(JobErrorHandlerPropertiesValidator.class, "EMAIL", null).get();
+ }
+}
diff --git a/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerPropertiesValidatorTest.java b/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerPropertiesValidatorTest.java
new file mode 100644
index 0000000..3eff575
--- /dev/null
+++ b/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerPropertiesValidatorTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.shardingsphere.elasticjob.error.handler.wechat;
+
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerPropertiesValidator;
+import org.apache.shardingsphere.elasticjob.infra.spi.ElasticJobServiceLoader;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class WechatJobErrorHandlerPropertiesValidatorTest {
+
+ @Before
+ public void startup() {
+ ElasticJobServiceLoader.registerTypedService(JobErrorHandlerPropertiesValidator.class);
+ }
+
+ @Test
+ public void assertValidateWithNormal() {
+ Properties properties = new Properties();
+ properties.setProperty(WechatPropertiesConstants.WEBHOOK, "webhook");
+ properties.setProperty(WechatPropertiesConstants.READ_TIMEOUT_MILLISECONDS, "1000");
+ properties.setProperty(WechatPropertiesConstants.CONNECT_TIMEOUT_MILLISECONDS, "2000");
+ WechatJobErrorHandlerPropertiesValidator actual = getValidator();
+ actual.validate(properties);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void assertValidateWithPropsIsNull() {
+ WechatJobErrorHandlerPropertiesValidator actual = getValidator();
+ actual.validate(null);
+ }
+
+ @Test
+ public void assertValidateWithWebhookIsNull() {
+ WechatJobErrorHandlerPropertiesValidator actual = getValidator();
+ try {
+ actual.validate(new Properties());
+ } catch (NullPointerException e) {
+ assertThat(e.getMessage(), is(String.format("The property `%s` is required.", WechatPropertiesConstants.WEBHOOK)));
+ }
+ }
+
+ private WechatJobErrorHandlerPropertiesValidator getValidator() {
+ return (WechatJobErrorHandlerPropertiesValidator) ElasticJobServiceLoader.newTypedServiceInstance(JobErrorHandlerPropertiesValidator.class, "WECHAT", null).get();
+ }
+}
diff --git a/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/validator/JobPropertiesValidateRuleTest.java b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/validator/JobPropertiesValidateRuleTest.java
new file mode 100644
index 0000000..399e6b3
--- /dev/null
+++ b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/validator/JobPropertiesValidateRuleTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.shardingsphere.elasticjob.infra.validator;
+
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class JobPropertiesValidateRuleTest {
+
+ @Test
+ public void assertValidateIsRequiredWithValidateError() {
+ try {
+ JobPropertiesValidateRule.validateIsRequired(new Properties(), "key");
+ } catch (NullPointerException ex) {
+ assertThat(ex.getMessage(), is(String.format("The property `%s` is required.", "key")));
+ }
+ }
+
+ @Test
+ public void assertValidateIsRequiredWithNormal() {
+ Properties properties = new Properties();
+ properties.setProperty("key", "value");
+ JobPropertiesValidateRule.validateIsRequired(properties, "key");
+ }
+
+ @Test
+ public void assertValidateIsPositiveIntegerWithValueNoExist() {
+ JobPropertiesValidateRule.validateIsPositiveInteger(new Properties(), "key");
+ }
+
+ @Test
+ public void assertValidateIsPositiveIntegerWithNormal() {
+ Properties properties = new Properties();
+ properties.setProperty("key", "1");
+ JobPropertiesValidateRule.validateIsPositiveInteger(new Properties(), "key");
+ }
+
+ @Test
+ public void assertValidateIsPositiveIntegerWithWrongString() {
+ Properties properties = new Properties();
+ properties.setProperty("key", "wrong_value");
+ try {
+ JobPropertiesValidateRule.validateIsPositiveInteger(properties, "key");
+ } catch (IllegalArgumentException ex) {
+ assertThat(ex.getMessage(), is(String.format("The property `%s` should be integer.", "key")));
+ }
+ }
+
+ @Test
+ public void assertValidateIsPositiveIntegerWithNegativeNumber() {
+ Properties properties = new Properties();
+ properties.setProperty("key", "-1");
+ try {
+ JobPropertiesValidateRule.validateIsPositiveInteger(properties, "key");
+ } catch (IllegalArgumentException ex) {
+ assertThat(ex.getMessage(), is(String.format("The property `%s` should be positive.", "key")));
+ }
+ }
+}