This is an automated email from the ASF dual-hosted git repository.

healchow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new 1b7a88517 [INLONG-4841][Manager] Optimize the service validation 
method (#4861)
1b7a88517 is described below

commit 1b7a88517b41bab1d67dc5fdd18ad792fbe89128
Author: leosanqing <[email protected]>
AuthorDate: Thu Jul 7 10:56:17 2022 +0800

    [INLONG-4841][Manager] Optimize the service validation method (#4861)
---
 .../common/pojo/cluster/ClusterTagRequest.java     |  2 +-
 .../pojo/group/InlongGroupApproveRequest.java      |  4 ++
 .../common/pojo/group/InlongGroupRequest.java      |  4 +-
 .../manager/common/util/ValidationUtils.java       | 46 +++++++++++++++++
 .../manager/service/group/InlongGroupService.java  |  9 +++-
 .../service/group/InlongGroupServiceImpl.java      |  7 +--
 .../manager/common/utils/ValidationUtilsTest.java  | 57 ++++++++++++++++++++++
 .../manager/service/group/GroupServiceTest.java    | 56 +++++++++++++++++++++
 8 files changed, 175 insertions(+), 10 deletions(-)

diff --git 
a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/cluster/ClusterTagRequest.java
 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/cluster/ClusterTagRequest.java
index 79f993f65..885482cb2 100644
--- 
a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/cluster/ClusterTagRequest.java
+++ 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/cluster/ClusterTagRequest.java
@@ -32,7 +32,7 @@ import javax.validation.constraints.NotNull;
 @ApiModel("Cluster tag request")
 public class ClusterTagRequest {
 
-    @NotNull(groups = UpdateValidation.class)
+    @NotNull(groups = UpdateValidation.class, message = "id must not be null")
     @ApiModelProperty(value = "Primary key")
     private Integer id;
 
diff --git 
a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupApproveRequest.java
 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupApproveRequest.java
index 3d66691e0..0e0ce1399 100644
--- 
a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupApproveRequest.java
+++ 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupApproveRequest.java
@@ -21,6 +21,8 @@ import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
+import javax.validation.constraints.NotBlank;
+
 /**
  * Inlong group approval info
  */
@@ -31,9 +33,11 @@ public class InlongGroupApproveRequest {
     @ApiModelProperty(value = "Primary key")
     private Integer id;
 
+    @NotBlank(message = "inlongGroupId must not be blank")
     @ApiModelProperty(value = "Inlong group id", required = true)
     private String inlongGroupId;
 
+    @NotBlank(message = "mqType must not be blank")
     @ApiModelProperty(value = "MQ Type")
     private String mqType;
 
diff --git 
a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupRequest.java
 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupRequest.java
index d809ed32f..df1082bb1 100644
--- 
a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupRequest.java
+++ 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/pojo/group/InlongGroupRequest.java
@@ -59,7 +59,7 @@ public class InlongGroupRequest {
     @ApiModelProperty(value = "MQ type, replaced by mqType")
     private String middlewareType;
 
-    @NotBlank
+    @NotBlank(message = "mqType must not be blank")
     @ApiModelProperty(value = "MQ type, high throughput: TUBE, high 
consistency: PULSAR")
     private String mqType;
 
@@ -97,8 +97,8 @@ public class InlongGroupRequest {
     @ApiModelProperty(value = "The maximum length of a single piece of data, 
unit: Byte")
     private Integer maxLength;
 
+    @NotBlank(message = "inCharges must not be blank")
     @ApiModelProperty(value = "Name of responsible person, separated by 
commas")
-    @NotBlank
     private String inCharges;
 
     @ApiModelProperty(value = "Name of followers, separated by commas")
diff --git 
a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/util/ValidationUtils.java
 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/util/ValidationUtils.java
new file mode 100644
index 000000000..7d11d277d
--- /dev/null
+++ 
b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/util/ValidationUtils.java
@@ -0,0 +1,46 @@
+
+/*
+ * 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.inlong.manager.common.util;
+
+import lombok.experimental.UtilityClass;
+import org.apache.commons.collections.CollectionUtils;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validator;
+import java.util.Set;
+
+/**
+ * With various annotations of javax, it is used to verify object properties, 
mainly used in the service layer
+ */
+@UtilityClass
+public class ValidationUtils {
+
+    /**
+     * Validate objects using Javax annotations such as @NotBlank, @NotNull, 
etc., mainly used in the Service layer
+     * The function is the same as the parameter validation of the controller 
layer
+     */
+    public static void validate(Validator validator, Object object, 
Class<?>... groups) {
+        Set<ConstraintViolation<Object>> constraintViolations = 
validator.validate(object, groups);
+        if (CollectionUtils.isNotEmpty(constraintViolations)) {
+            throw new ConstraintViolationException(constraintViolations);
+        }
+    }
+
+}
\ No newline at end of file
diff --git 
a/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupService.java
 
b/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupService.java
index 3160de544..09bbaf1f6 100644
--- 
a/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupService.java
+++ 
b/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupService.java
@@ -27,6 +27,8 @@ import 
org.apache.inlong.manager.common.pojo.group.InlongGroupPageRequest;
 import org.apache.inlong.manager.common.pojo.group.InlongGroupRequest;
 import org.apache.inlong.manager.common.pojo.group.InlongGroupTopicInfo;
 
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
 import java.util.List;
 
 /**
@@ -66,7 +68,8 @@ public interface InlongGroupService {
      * @param operator Operator name
      * @return Inlong group id
      */
-    String update(InlongGroupRequest groupInfo, String operator);
+    String update(@Valid @NotNull(message = "inlongGroupRequest must not be 
null") InlongGroupRequest groupInfo,
+            String operator);
 
     /**
      * Modify the status of the specified group
@@ -119,7 +122,9 @@ public interface InlongGroupService {
      * @param operator Edit person's name
      * @return whether succeed
      */
-    boolean updateAfterApprove(InlongGroupApproveRequest approveInfo, String 
operator);
+    boolean updateAfterApprove(
+            @Valid @NotNull(message = "approveInfo must not be null") 
InlongGroupApproveRequest approveInfo,
+            String operator);
 
     /**
      * Save or update extended information
diff --git 
a/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupServiceImpl.java
 
b/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupServiceImpl.java
index c785834ab..c17912c23 100644
--- 
a/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupServiceImpl.java
+++ 
b/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/group/InlongGroupServiceImpl.java
@@ -55,6 +55,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.validation.annotation.Validated;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -67,6 +68,7 @@ import java.util.stream.Collectors;
  * Inlong group service layer implementation
  */
 @Service
+@Validated
 public class InlongGroupServiceImpl implements InlongGroupService {
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(InlongGroupServiceImpl.class);
@@ -206,7 +208,6 @@ public class InlongGroupServiceImpl implements 
InlongGroupService {
             propagation = Propagation.REQUIRES_NEW)
     public String update(InlongGroupRequest request, String operator) {
         LOGGER.debug("begin to update inlong group={} by user={}", request, 
operator);
-        Preconditions.checkNotNull(request, "inlong group request cannot be 
empty");
 
         String groupId = request.getInlongGroupId();
         InlongGroupEntity entity = groupMapper.selectByGroupId(groupId);
@@ -350,11 +351,7 @@ public class InlongGroupServiceImpl implements 
InlongGroupService {
     @Transactional(rollbackFor = Throwable.class, propagation = 
Propagation.REQUIRES_NEW)
     public boolean updateAfterApprove(InlongGroupApproveRequest approveInfo, 
String operator) {
         LOGGER.debug("begin to update inlong group after approve={}", 
approveInfo);
-        Preconditions.checkNotNull(approveInfo, "inlong approve request cannot 
be empty");
         String groupId = approveInfo.getInlongGroupId();
-        Preconditions.checkNotNull(groupId, 
ErrorCodeEnum.GROUP_ID_IS_EMPTY.getMessage());
-        String mqType = approveInfo.getMqType();
-        Preconditions.checkNotNull(mqType, "MQ type cannot be empty");
 
         // update status to [GROUP_APPROVE_PASSED]
         this.updateStatus(groupId, GroupStatus.APPROVE_PASSED.getCode(), 
operator);
diff --git 
a/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/common/utils/ValidationUtilsTest.java
 
b/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/common/utils/ValidationUtilsTest.java
new file mode 100644
index 000000000..e46c07081
--- /dev/null
+++ 
b/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/common/utils/ValidationUtilsTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.inlong.manager.common.utils;
+
+import org.apache.inlong.manager.common.pojo.cluster.ClusterTagRequest;
+import org.apache.inlong.manager.common.pojo.common.UpdateValidation;
+import org.apache.inlong.manager.common.util.ValidationUtils;
+import org.apache.inlong.manager.service.ServiceBaseTest;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import javax.annotation.Resource;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validator;
+
+/**
+ * Test {@link ValidationUtils}
+ */
+class ValidationUtilsTest extends ServiceBaseTest {
+
+    @Resource
+    Validator validator;
+
+    @Test
+    void testValidate4Group() {
+        ConstraintViolationException violationException = 
Assertions.assertThrows(
+                ConstraintViolationException.class,
+                () -> ValidationUtils.validate(validator, new 
ClusterTagRequest(), UpdateValidation.class));
+
+        Assertions.assertTrue(violationException.getMessage().contains("id: id 
must not be null"));
+    }
+
+    @Test
+    void testValidate() {
+        ConstraintViolationException violationException = 
Assertions.assertThrows(
+                ConstraintViolationException.class,
+                () -> ValidationUtils.validate(validator, new 
ClusterTagRequest()));
+
+        Assertions.assertEquals("clusterTag: clusterTag cannot be blank", 
violationException.getMessage());
+    }
+
+}
diff --git 
a/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/service/group/GroupServiceTest.java
 
b/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/service/group/GroupServiceTest.java
new file mode 100644
index 000000000..7560dacb5
--- /dev/null
+++ 
b/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/service/group/GroupServiceTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.inlong.manager.service.group;
+
+import org.apache.inlong.manager.common.pojo.group.InlongGroupApproveRequest;
+import org.apache.inlong.manager.common.pojo.group.tube.InlongTubeRequest;
+import org.apache.inlong.manager.service.ServiceBaseTest;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import javax.annotation.Resource;
+import javax.validation.ConstraintViolationException;
+
+/**
+ * Test for {@link InlongGroupService}
+ */
+class GroupServiceTest extends ServiceBaseTest {
+
+    @Resource
+    InlongGroupService groupService;
+
+    @Test
+    void testGroupUpdateFailByValid() {
+        ConstraintViolationException exception = Assertions.assertThrows(
+                ConstraintViolationException.class,
+                () -> groupService.update(new InlongTubeRequest(), ""));
+
+        Assertions.assertTrue(exception.getMessage().contains("inCharges: 
inCharges must not be blank"));
+        Assertions.assertTrue(exception.getMessage().contains("inlongGroupId: 
inlongGroupId cannot be blank"));
+    }
+
+    @Test
+    void testUpdateAfterApproveFailByValid() {
+        ConstraintViolationException exception = 
Assertions.assertThrows(ConstraintViolationException.class,
+                () -> groupService.updateAfterApprove(new 
InlongGroupApproveRequest(), ""));
+
+        Assertions.assertTrue(exception.getMessage().contains("mqType: mqType 
must not be blank"));
+        Assertions.assertTrue(exception.getMessage().contains("inlongGroupId: 
inlongGroupId must not be blank"));
+
+    }
+}

Reply via email to