This is an automated email from the ASF dual-hosted git repository.
benjobs pushed a commit to branch dev-2.1.3
in repository https://gitbox.apache.org/repos/asf/incubator-streampark.git
The following commit(s) were added to refs/heads/dev-2.1.3 by this push:
new 2e4306e4b [Improve] Refactor update setting interface (#3585)
2e4306e4b is described below
commit 2e4306e4b54f07becbffc7e38c7be2bf5fdc372f
Author: zhengke zhou <[email protected]>
AuthorDate: Tue Feb 27 23:27:45 2024 +0800
[Improve] Refactor update setting interface (#3585)
* Refactor update setting interface
* [Improve] fix code format
* [Improve] Refactor update setting interface
---------
Co-authored-by: benjobs <[email protected]>
---
.../streampark/console/core/bean/DockerConfig.java | 69 ++++++++++
.../streampark/console/core/bean/MavenConfig.java | 100 +++++++++++++++
.../core/bean/SettingAlertEmailConfigParams.java | 99 ++++++++++++++
.../core/bean/SettingDockerConfigParams.java | 86 +++++++++++++
.../console/core/controller/SettingController.java | 41 ++++++
.../console/core/service/SettingService.java | 9 ++
.../core/service/impl/SettingServiceImpl.java | 10 ++
.../bean/SettingAlertEmailConfigParamsTest.java | 140 ++++++++++++++++++++
.../core/bean/SettingDockerConfigParamsTest.java | 142 +++++++++++++++++++++
.../console/core/service/SettingServiceTest.java | 130 +++++++++++++++++++
10 files changed, 826 insertions(+)
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/DockerConfig.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/DockerConfig.java
new file mode 100644
index 000000000..9be549fe2
--- /dev/null
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/DockerConfig.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.streampark.console.core.bean;
+
+import org.apache.streampark.console.core.service.SettingService;
+
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * The DockerConfig class represents the configuration for an email system. It
holds the SMTP host,
+ * port, from address, username, password, and whether SSL is enabled.
+ *
+ * <p>This class also provides a static factory method to create an
DockerConfig object from a map
+ * of settings.
+ */
+@Data
+@Slf4j
+public class DockerConfig {
+
+ private String address;
+ private String user;
+ private String password;
+ private String namespace;
+
+ public static DockerConfig fromSetting() {
+ try {
+ DockerConfig dockerConfig = new DockerConfig();
+
+ dockerConfig.setAddress(
+ SettingService.SETTINGS
+ .get(SettingService.KEY_DOCKER_REGISTER_ADDRESS)
+ .getSettingValue());
+
+ dockerConfig.setUser(
+
SettingService.SETTINGS.get(SettingService.KEY_DOCKER_REGISTER_USER).getSettingValue());
+
+ dockerConfig.setPassword(
+ SettingService.SETTINGS
+ .get(SettingService.KEY_DOCKER_REGISTER_PASSWORD)
+ .getSettingValue());
+
+ dockerConfig.setNamespace(
+ SettingService.SETTINGS
+ .get(SettingService.KEY_DOCKER_REGISTER_NAMESPACE)
+ .getSettingValue());
+
+ return dockerConfig;
+ } catch (Exception e) {
+ log.warn("Failed to create DockerConfig from settings", e);
+ }
+ return null;
+ }
+}
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/MavenConfig.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/MavenConfig.java
new file mode 100644
index 000000000..d073ea7e9
--- /dev/null
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/MavenConfig.java
@@ -0,0 +1,100 @@
+/*
+ * 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.streampark.console.core.bean;
+
+import org.apache.streampark.common.conf.CommonConfig;
+import org.apache.streampark.common.conf.InternalConfigHolder;
+import org.apache.streampark.console.core.entity.Setting;
+import org.apache.streampark.console.core.service.SettingService;
+
+import org.apache.commons.lang3.StringUtils;
+
+import lombok.Data;
+
+import java.util.Map;
+
+/**
+ * This class represents the Maven configuration for the application. It
provides methods to
+ * retrieve the various Maven configuration options.
+ */
+@Data
+public class MavenConfig {
+
+ /** File path for Maven settings. */
+ private String mvnSettings;
+
+ /** Repository URL for Maven. */
+ private String mvnRepository;
+
+ /** User for Maven authentication. */
+ private String mvnAuthUser;
+
+ /** Password for Maven authentication. */
+ private String mvnAuthPassword;
+
+ /** */
+ public static MavenConfig fromSetting() {
+ MavenConfig mavenConfig = new MavenConfig();
+ Map<String, Setting> settings = SettingService.SETTINGS;
+ if (settings.containsKey(CommonConfig.MAVEN_SETTINGS_PATH().key())) {
+ mavenConfig.setMvnSettings(
+
settings.get(CommonConfig.MAVEN_SETTINGS_PATH().key()).getSettingValue());
+ }
+
+ if (settings.containsKey(CommonConfig.MAVEN_REMOTE_URL().key())) {
+ mavenConfig.setMvnRepository(
+
settings.get(CommonConfig.MAVEN_REMOTE_URL().key()).getSettingValue());
+ }
+
+ if (settings.containsKey(CommonConfig.MAVEN_AUTH_USER().key())) {
+ mavenConfig.setMvnAuthUser(
+
settings.get(CommonConfig.MAVEN_AUTH_USER().key()).getSettingValue());
+ }
+
+ if (settings.containsKey(CommonConfig.MAVEN_AUTH_PASSWORD().key())) {
+ mavenConfig.setMvnAuthPassword(
+
settings.get(CommonConfig.MAVEN_AUTH_PASSWORD().key()).getSettingValue());
+ }
+
+ return mavenConfig;
+ }
+
+ /**
+ * Updates the internal configuration of Maven based on the assigned
instance variables. If the
+ * instance variables mvnSettings, mvnRepository, mvnAuthUser, or
mvnAuthPassword have non-empty
+ * values, they will be updated in the internal configuration.
+ */
+ public void updateConfig() {
+
+ if (StringUtils.isNotBlank(mvnSettings)) {
+ InternalConfigHolder.set(CommonConfig.MAVEN_SETTINGS_PATH(),
mvnSettings);
+ }
+
+ if (StringUtils.isNotBlank(mvnRepository)) {
+ InternalConfigHolder.set(CommonConfig.MAVEN_REMOTE_URL(), mvnRepository);
+ }
+
+ if (StringUtils.isNotBlank(mvnAuthUser)) {
+ InternalConfigHolder.set(CommonConfig.MAVEN_AUTH_USER(), mvnAuthUser);
+ }
+
+ if (StringUtils.isNotBlank(mvnAuthPassword)) {
+ InternalConfigHolder.set(CommonConfig.MAVEN_AUTH_PASSWORD(),
mvnAuthPassword);
+ }
+ }
+}
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/SettingAlertEmailConfigParams.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/SettingAlertEmailConfigParams.java
new file mode 100644
index 000000000..f6120d9e3
--- /dev/null
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/SettingAlertEmailConfigParams.java
@@ -0,0 +1,99 @@
+/*
+ * 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.streampark.console.core.bean;
+
+import org.apache.streampark.console.core.entity.Setting;
+
+import lombok.Data;
+
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+@Data
+public class SettingAlertEmailConfigParams {
+ private Setting host;
+ private Setting port;
+ private Setting from;
+ private Setting username;
+ private Setting password;
+ private Setting ssl;
+ /* Only letters, digits, underscores, periods, and hyphens are allowed */
+ private static final String EMAIL_ADDRESS_REGEXP =
+ "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
+ /* The port value must be between 0-65535 */
+ private static final String PORT_REGEXP =
+
"^([1-9]\\d{0,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$";
+ private static final String FROM_REGEXP = "";
+ /* Letters start with 5-16 bytes, alphanumeric underscores are allowed */
+ private static final String USER_NAME_REGEXP =
"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
+ /* Letters start with 5-16 bytes, alphanumeric underscores are allowed */
+ private static final String PASS_WORD_REGEXP =
"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
+ /* Whether SSL is enabled or not can only be true false */
+ private static final String SSL_REGEXP = "^(true|false)$";
+
+ public static boolean verifyParams(SettingAlertEmailConfigParams params) {
+ return params.verifyHost()
+ && params.verifyPort()
+ && params.verifyFrom()
+ && params.verifyUserName()
+ && params.verifyPassWord()
+ && params.verifySSL();
+ }
+
+ private boolean verifyHost() {
+ if (Objects.isNull(getHost()) ||
Objects.isNull(getHost().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(EMAIL_ADDRESS_REGEXP, getHost().getSettingValue());
+ }
+
+ private boolean verifyPort() {
+ if (Objects.isNull(getPort()) ||
Objects.isNull(getPort().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(PORT_REGEXP, getPort().getSettingValue());
+ }
+
+ private boolean verifyFrom() {
+ if (Objects.isNull(getFrom()) ||
Objects.isNull(getFrom().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(FROM_REGEXP, getFrom().getSettingValue());
+ }
+
+ private boolean verifyUserName() {
+ if (Objects.isNull(getUsername()) ||
Objects.isNull(getUsername().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(USER_NAME_REGEXP, getUsername().getSettingValue());
+ }
+
+ private boolean verifyPassWord() {
+ if (Objects.isNull(getPassword()) ||
Objects.isNull(getPassword().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(PASS_WORD_REGEXP, getPassword().getSettingValue());
+ }
+
+ private boolean verifySSL() {
+ if (Objects.isNull(getSsl()) ||
Objects.isNull(getSsl().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(SSL_REGEXP, getSsl().getSettingValue());
+ }
+}
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/SettingDockerConfigParams.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/SettingDockerConfigParams.java
new file mode 100644
index 000000000..af81ef115
--- /dev/null
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/SettingDockerConfigParams.java
@@ -0,0 +1,86 @@
+/*
+ * 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.streampark.console.core.bean;
+
+import org.apache.streampark.console.core.entity.Setting;
+
+import lombok.Data;
+
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+@Data
+public class SettingDockerConfigParams {
+ private Setting username;
+ private Setting password;
+ private Setting address;
+ private Setting namespace;
+
+ /* Letters start with 5-16 bytes, alphanumeric underscores are allowed */
+ private static final String USER_NAME_REGEXP =
"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
+ /* Letters start with 5-16 bytes, alphanumeric underscores are allowed */
+ private static final String PASSWORD_REGEXP = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
+ /*ipv4 match rule */
+ private static final String IPV4_ADDRESS_REGEX =
+
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
+ /*ipv6 match rule */
+ private static final String IPV6_ADDRESS_REGEX =
"^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)$";
+ /* domain match rule */
+ private static final String DOMAIN_NAME_REGEX =
+
"^((http:\\/\\/)|(https:\\/\\/))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(\\/)";
+ /* Docker namespace rules based on the format of the domain name */
+ private static final String NAMESPACE_REGEX =
"^(?!-)[a-zA-Z0-9-]{1,253}(?<!-)$";
+
+ public static boolean verifyParams(SettingDockerConfigParams params) {
+ return params.verifyUserName()
+ && params.verifyPassWord()
+ && params.verifyAddress()
+ && params.verifyNameSpace();
+ }
+
+ private boolean verifyUserName() {
+ if (Objects.isNull(getUsername()) ||
Objects.isNull(getUsername().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(USER_NAME_REGEXP, getUsername().getSettingValue());
+ }
+
+ private boolean verifyPassWord() {
+ if (Objects.isNull(getPassword()) ||
Objects.isNull(getPassword().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(PASSWORD_REGEXP, getPassword().getSettingValue());
+ }
+
+ private boolean verifyAddress() {
+ if (Objects.isNull(getAddress()) ||
Objects.isNull(getAddress().getSettingValue())) {
+ return false;
+ }
+
+ return Pattern.matches(IPV4_ADDRESS_REGEX, getAddress().getSettingValue())
+ || Pattern.matches(IPV6_ADDRESS_REGEX, getAddress().getSettingValue())
+ || Pattern.matches(DOMAIN_NAME_REGEX, getAddress().getSettingValue());
+ }
+
+ private boolean verifyNameSpace() {
+ if (Objects.isNull(getNamespace()) ||
Objects.isNull(getNamespace().getSettingValue())) {
+ return false;
+ }
+ return Pattern.matches(NAMESPACE_REGEX, getNamespace().getSettingValue());
+ }
+}
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/SettingController.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/SettingController.java
index 82f463696..50f39d33a 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/SettingController.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/SettingController.java
@@ -18,7 +18,10 @@
package org.apache.streampark.console.core.controller;
import org.apache.streampark.common.util.HadoopUtils;
+import org.apache.streampark.console.base.domain.ResponseCode;
import org.apache.streampark.console.base.domain.RestResponse;
+import org.apache.streampark.console.core.bean.SettingAlertEmailConfigParams;
+import org.apache.streampark.console.core.bean.SettingDockerConfigParams;
import org.apache.streampark.console.core.entity.Setting;
import org.apache.streampark.console.core.service.SettingService;
@@ -31,9 +34,11 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import java.util.Arrays;
import java.util.List;
@Tag(name = "SETTING_TAG")
@@ -70,6 +75,42 @@ public class SettingController {
return RestResponse.success(updated);
}
+ @Operation(summary = "Update docker setting")
+ @PostMapping("update/docker")
+ @RequiresPermissions("setting:update")
+ public RestResponse updateDocker(@RequestBody SettingDockerConfigParams
params) {
+ if (!SettingDockerConfigParams.verifyParams(params)) {
+ return RestResponse.fail("The parameter is incorrect, please check!",
ResponseCode.CODE_FAIL);
+ }
+
+ List<Setting> settings =
+ Arrays.asList(
+ params.getAddress(), params.getNamespace(),
+ params.getUsername(), params.getPassword());
+ boolean updated = settingService.updateSettings(settings);
+ return RestResponse.success(updated);
+ }
+
+ @Operation(summary = "Update alert email")
+ @PostMapping("update/alert/email")
+ @RequiresPermissions("setting:update")
+ public RestResponse updateAlertEmail(@RequestBody
SettingAlertEmailConfigParams params) {
+ if (!SettingAlertEmailConfigParams.verifyParams(params)) {
+ return RestResponse.fail("The parameter is incorrect, please check!",
ResponseCode.CODE_FAIL);
+ }
+
+ List<Setting> settings =
+ Arrays.asList(
+ params.getHost(),
+ params.getPort(),
+ params.getFrom(),
+ params.getUsername(),
+ params.getPassword(),
+ params.getSsl());
+ boolean updated = settingService.updateSettings(settings);
+ return RestResponse.success(updated);
+ }
+
@Operation(summary = "Check hadoop status")
@PostMapping("checkHadoop")
public RestResponse checkHadoop() {
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/SettingService.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/SettingService.java
index 7f4eba939..bab221278 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/SettingService.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/SettingService.java
@@ -22,6 +22,7 @@ import org.apache.streampark.console.core.entity.Setting;
import com.baomidou.mybatisplus.extension.service.IService;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -53,6 +54,14 @@ public interface SettingService extends IService<Setting> {
boolean update(Setting setting);
+ /**
+ * * Updates the specified Settings.
+ *
+ * @param settings list of setting object to update
+ * @return true if the update is successful, false otherwise
+ */
+ boolean updateSettings(List<Setting> settings);
+
String getMavenSettings();
String getMavenRepository();
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/SettingServiceImpl.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/SettingServiceImpl.java
index 2a1fd57b3..6494059b8 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/SettingServiceImpl.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/SettingServiceImpl.java
@@ -95,6 +95,16 @@ public class SettingServiceImpl extends
ServiceImpl<SettingMapper, Setting>
}
}
+ public boolean updateSettings(List<Setting> settings) {
+ for (Setting each : settings) {
+ boolean result = update(each);
+ if (!result) {
+ return false;
+ }
+ }
+ return true;
+ }
+
@Override
public SenderEmail getSenderEmail() {
try {
diff --git
a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/bean/SettingAlertEmailConfigParamsTest.java
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/bean/SettingAlertEmailConfigParamsTest.java
new file mode 100644
index 000000000..6ae7feeb2
--- /dev/null
+++
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/bean/SettingAlertEmailConfigParamsTest.java
@@ -0,0 +1,140 @@
+/*
+ * 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.streampark.console.core.bean;
+
+import org.apache.streampark.console.core.entity.Setting;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class SettingAlertEmailConfigParamsTest {
+ private SettingAlertEmailConfigParams alertEmailConfigParams;
+ private Setting setting;
+ private Method method;
+
+ @BeforeEach
+ void setUp() {
+ alertEmailConfigParams = new SettingAlertEmailConfigParams();
+ setting = new Setting();
+ }
+
+ @Test
+ void verifyEmailHostTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ alertEmailConfigParams.setHost(setting);
+ initVerifyMethod("verifyHost");
+
+ setting.setSettingValue("test");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("[email protected]");
+ Assertions.assertTrue((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("testEmail/test.com");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+ }
+
+ @Test
+ void verifyEmailPortTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ alertEmailConfigParams.setPort(setting);
+ initVerifyMethod("verifyPort");
+
+ setting.setSettingValue("3306");
+ Assertions.assertTrue((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("65535");
+ Assertions.assertTrue((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("-1");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("0");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("65536");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+ }
+
+ /* TODO .. */
+ @Test
+ void verifyFromTest() throws NoSuchMethodException {
+ alertEmailConfigParams.setHost(setting);
+ initVerifyMethod("verifyFrom");
+ }
+
+ @Test
+ void verifyUserNameTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ alertEmailConfigParams.setUsername(setting);
+ initVerifyMethod("verifyUserName");
+
+ setting.setSettingValue("Aa111111");
+ Assertions.assertTrue((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("Aa@aa_");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("test");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+ }
+
+ @Test
+ void verifyPassWordTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ alertEmailConfigParams.setPassword(setting);
+ initVerifyMethod("verifyPassWord");
+
+ setting.setSettingValue("AaaAaa");
+ Assertions.assertTrue((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("AaAa");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue("123456789");
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(alertEmailConfigParams));
+ }
+
+ void initVerifyMethod(final String methodName) throws NoSuchMethodException {
+ Class<SettingAlertEmailConfigParams> clazz =
SettingAlertEmailConfigParams.class;
+ method = clazz.getDeclaredMethod(methodName);
+ method.setAccessible(true);
+ }
+}
diff --git
a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/bean/SettingDockerConfigParamsTest.java
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/bean/SettingDockerConfigParamsTest.java
new file mode 100644
index 000000000..abb2b0570
--- /dev/null
+++
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/bean/SettingDockerConfigParamsTest.java
@@ -0,0 +1,142 @@
+/*
+ * 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.streampark.console.core.bean;
+
+import org.apache.streampark.console.core.entity.Setting;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class SettingDockerConfigParamsTest {
+ private SettingDockerConfigParams dockerConfigParams;
+ private Setting setting;
+ private Method method;
+
+ @BeforeEach
+ void setUp() {
+ dockerConfigParams = new SettingDockerConfigParams();
+ setting = new Setting();
+ }
+
+ @Test
+ void verifyUserNameTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ dockerConfigParams.setUsername(setting);
+ initVerifyMethod("verifyUserName");
+
+ setting.setSettingValue("Aa111111");
+ Assertions.assertTrue((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("Aa111111@");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("Aa@aa_");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("test");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+ }
+
+ @Test
+ void verifyPassWordTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ dockerConfigParams.setPassword(setting);
+ initVerifyMethod("verifyPassWord");
+
+ setting.setSettingValue("AaaAaa");
+ Assertions.assertTrue((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("AaAa");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("123456789");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+ }
+
+ @Test
+ void verifyAddressTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ dockerConfigParams.setAddress(setting);
+ initVerifyMethod("verifyAddress");
+
+ setting.setSettingValue("https://www.google.com/");
+ Assertions.assertTrue((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("http://www.google.com/");
+ Assertions.assertTrue((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("www.google.com");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
+ Assertions.assertTrue((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("127.0.0.1");
+ Assertions.assertTrue((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("htp://www.google.com");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("ww.google.com");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("localhost");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue("0.0.0");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+ }
+
+ @Test
+ void verifyNameSpaceTest()
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
+ dockerConfigParams.setAddress(setting);
+ initVerifyMethod("verifyNameSpace");
+
+ setting.setSettingValue("dom.mod");
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+
+ setting.setSettingValue(null);
+ Assertions.assertFalse((boolean) method.invoke(dockerConfigParams));
+ }
+
+ void initVerifyMethod(final String methodName) throws NoSuchMethodException {
+ Class<SettingDockerConfigParams> clazz = SettingDockerConfigParams.class;
+ method = clazz.getDeclaredMethod(methodName);
+ method.setAccessible(true);
+ }
+}
diff --git
a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/SettingServiceTest.java
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/SettingServiceTest.java
new file mode 100644
index 000000000..17f839ff6
--- /dev/null
+++
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/SettingServiceTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.streampark.console.core.service;
+
+import org.apache.streampark.console.SpringTestBase;
+import org.apache.streampark.console.core.bean.SettingAlertEmailConfigParams;
+import org.apache.streampark.console.core.bean.SettingDockerConfigParams;
+import org.apache.streampark.console.core.entity.Setting;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Arrays;
+import java.util.List;
+
+class SettingServiceTest extends SpringTestBase {
+ private final SettingDockerConfigParams dockerConfigParams = new
SettingDockerConfigParams();
+ private final SettingAlertEmailConfigParams alertEmailConfigParams =
+ new SettingAlertEmailConfigParams();
+
+ @Autowired SettingService settingService;
+
+ /*docker config*/
+ void initDockerConfigParams(SettingDockerConfigParams params) {
+ Setting address = settingService.get("docker.register.address");
+ address.setSettingValue("test-address-setting-value");
+ Setting username = settingService.get("docker.register.user");
+ username.setSettingValue("test-username-setting-value");
+ Setting password = settingService.get("docker.register.password");
+ password.setSettingValue("test-password-setting-value");
+ Setting namespace = settingService.get("docker.register.namespace");
+ namespace.setSettingValue("test-namespace-setting-value");
+ params.setAddress(address);
+ params.setUsername(username);
+ params.setPassword(password);
+ params.setNamespace(namespace);
+ }
+
+ @Test
+ void testUpdateDockerConfigTest() {
+ initDockerConfigParams(dockerConfigParams);
+ List<Setting> settings =
+ Arrays.asList(
+ dockerConfigParams.getAddress(),
+ dockerConfigParams.getNamespace(),
+ dockerConfigParams.getUsername(),
+ dockerConfigParams.getPassword());
+ settingService.updateSettings(settings);
+
+ Assertions.assertEquals(
+ "test-address-setting-value",
+ settingService.get("docker.register.address").getSettingValue());
+ Assertions.assertEquals(
+ "test-username-setting-value",
+ settingService.get("docker.register.user").getSettingValue());
+ Assertions.assertEquals(
+ "test-password-setting-value",
+ settingService.get("docker.register.password").getSettingValue());
+ Assertions.assertEquals(
+ "test-namespace-setting-value",
+ settingService.get("docker.register.namespace").getSettingValue());
+ }
+
+ /*alert email config*/
+ void initAlertEmailConfigParams(SettingAlertEmailConfigParams params) {
+ Setting host = settingService.get("alert.email.host");
+ host.setSettingValue("test-host-setting-value");
+ Setting port = settingService.get("alert.email.port");
+ port.setSettingValue("test-port-setting-value");
+ Setting from = settingService.get("alert.email.from");
+ from.setSettingValue("test-from-setting-value");
+ Setting username = settingService.get("alert.email.userName");
+ username.setSettingValue("test-username-setting-value");
+ Setting password = settingService.get("alert.email.password");
+ password.setSettingValue("test-password-setting-value");
+ Setting ssl = settingService.get("alert.email.ssl");
+ ssl.setSettingValue("test-ssl-setting-value");
+ params.setHost(host);
+ params.setPort(port);
+ params.setFrom(from);
+ params.setUsername(username);
+ params.setPassword(password);
+ params.setSsl(ssl);
+ }
+
+ @Test
+ void testUpdateAlertEmailConfigTest() {
+ initAlertEmailConfigParams(alertEmailConfigParams);
+ List<Setting> settings =
+ Arrays.asList(
+ alertEmailConfigParams.getHost(),
+ alertEmailConfigParams.getPort(),
+ alertEmailConfigParams.getFrom(),
+ alertEmailConfigParams.getUsername(),
+ alertEmailConfigParams.getPassword(),
+ alertEmailConfigParams.getSsl());
+ settingService.updateSettings(settings);
+
+ Assertions.assertEquals(
+ "test-host-setting-value",
settingService.get("alert.email.host").getSettingValue());
+ Assertions.assertEquals(
+ "test-port-setting-value",
settingService.get("alert.email.port").getSettingValue());
+ Assertions.assertEquals(
+ "test-from-setting-value",
settingService.get("alert.email.from").getSettingValue());
+ Assertions.assertEquals(
+ "test-username-setting-value",
+ settingService.get("alert.email.userName").getSettingValue());
+ Assertions.assertEquals(
+ "test-password-setting-value",
+ settingService.get("alert.email.password").getSettingValue());
+ Assertions.assertEquals(
+ "test-ssl-setting-value",
settingService.get("alert.email.ssl").getSettingValue());
+ }
+}