Unverified Commit 74bd3330 authored by jin's avatar jin Committed by GitHub
Browse files

feat:optimizing code (#2245)

* feat:optimizing code

* add licence and  format code
parent f4897b56
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -214,12 +214,20 @@ public class AlertGroupController extends BaseController{
    @GetMapping(value = "/verify-group-name")
    @ResponseStatus(HttpStatus.OK)
    public Result verifyGroupName(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                 @RequestParam(value ="groupName") String groupName
    ) {
        logger.info("login user {}, verfiy group name: {}",
                loginUser.getUserName(),groupName);
                                 @RequestParam(value ="groupName") String groupName) {
        logger.info("login user {}, verify group name: {}", loginUser.getUserName(), groupName);

        return alertGroupService.verifyGroupName(loginUser, groupName);
        boolean exist= alertGroupService.existGroupName(groupName);
        Result result = new Result();
        if (exist) {
            logger.error("group {} has exist, can't create again.", groupName);
            result.setCode(Status.ALERT_GROUP_EXIST.getCode());
            result.setMsg(Status.ALERT_GROUP_EXIST.getMsg());
        } else {
            result.setCode(Status.SUCCESS.getCode());
            result.setMsg(Status.SUCCESS.getMsg());
        }
        return result;
    }

    /**
+15 −28
Original line number Diff line number Diff line
@@ -16,17 +16,17 @@
 */
package org.apache.dolphinscheduler.api.service;

import java.util.*;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.entity.AlertGroup;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.entity.UserAlertGroup;
import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper;
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
@@ -35,11 +35,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * alert group service
 */
@@ -52,8 +47,7 @@ public class AlertGroupService extends BaseService{
    private AlertGroupMapper alertGroupMapper;

    @Autowired
    private UserAlertGroupMapper userAlertGroupMapper;

    private UserAlertGroupService userAlertGroupService;
    /**
     * query alert group list
     *
@@ -199,7 +193,7 @@ public class AlertGroupService extends BaseService{
            return result;
        }

        userAlertGroupMapper.deleteByAlertgroupId(id);
        userAlertGroupService.deleteByAlertGroupId(id);
        alertGroupMapper.deleteById(id);
        putMsg(result, Status.SUCCESS);
        return result;
@@ -223,22 +217,26 @@ public class AlertGroupService extends BaseService{
            return result;
        }

        userAlertGroupMapper.deleteByAlertgroupId(alertgroupId);
        userAlertGroupService.deleteByAlertGroupId(alertgroupId);
        if (StringUtils.isEmpty(userIds)) {
            putMsg(result, Status.SUCCESS);
            return result;
        }

        String[] userIdsArr = userIds.split(",");

        for (String userId : userIdsArr) {
        Date now = new Date();
        List<UserAlertGroup> alertGroups = new ArrayList<>(userIds.length());
        for (String userId : userIdsArr) {
            UserAlertGroup userAlertGroup = new UserAlertGroup();
            userAlertGroup.setAlertgroupId(alertgroupId);
            userAlertGroup.setUserId(Integer.parseInt(userId));
            userAlertGroup.setCreateTime(now);
            userAlertGroup.setUpdateTime(now);
            userAlertGroupMapper.insert(userAlertGroup);
            alertGroups.add(userAlertGroup);
        }

        if (CollectionUtils.isNotEmpty(alertGroups)) {
            userAlertGroupService.saveBatch(alertGroups);
        }

        putMsg(result, Status.SUCCESS);
@@ -248,22 +246,11 @@ public class AlertGroupService extends BaseService{
    /**
     * verify group name exists
     *
     * @param loginUser login user
     * @param groupName group name
     * @return check result code
     */
    public Result verifyGroupName(User loginUser, String groupName) {
        Result result = new Result();
    public boolean existGroupName(String groupName) {
        List<AlertGroup> alertGroup = alertGroupMapper.queryByGroupName(groupName);
        if (alertGroup != null && alertGroup.size() > 0) {
            logger.error("group {} has exist, can't create again.", groupName);
            result.setCode(Status.ALERT_GROUP_EXIST.getCode());
            result.setMsg(Status.ALERT_GROUP_EXIST.getMsg());
        } else {
            result.setCode(Status.SUCCESS.getCode());
            result.setMsg(Status.SUCCESS.getMsg());
        }

        return result;
        return CollectionUtils.isNotEmpty(alertGroup);
    }
}
+38 −0
Original line number Diff line number Diff line
/*
 * 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.dolphinscheduler.api.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dolphinscheduler.dao.entity.UserAlertGroup;
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class UserAlertGroupService extends ServiceImpl<UserAlertGroupMapper, UserAlertGroup> {

    @Autowired
    private UserAlertGroupMapper userAlertGroupMapper;

    boolean deleteByAlertGroupId(Integer groupId) {
        return userAlertGroupMapper.deleteByAlertgroupId(groupId) >= 1;
    }

}
+28 −19
Original line number Diff line number Diff line
@@ -18,9 +18,12 @@ package org.apache.dolphinscheduler.api.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.UserType;
@@ -31,9 +34,12 @@ import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper;
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper;
import org.junit.After;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import static org.mockito.ArgumentMatchers.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
@@ -41,14 +47,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;

@RunWith(MockitoJUnitRunner.class)
public class AlertGroupServiceTest {

@@ -60,6 +58,8 @@ public class AlertGroupServiceTest {
    private AlertGroupMapper alertGroupMapper;
    @Mock
    private UserAlertGroupMapper userAlertGroupMapper;
    @Mock
    UserAlertGroupService userAlertGroupService;

    private String groupName = "AlertGroupServiceTest";

@@ -160,25 +160,34 @@ public class AlertGroupServiceTest {


    }

    @Test
    public void testGrantUser() {

        Map<String, Object>  result = alertGroupService.grantUser(getLoginUser(),1,"123,321");
        Integer groupId = 1;

        ArgumentCaptor<Integer> groupArgument = ArgumentCaptor.forClass(Integer.class);

        Mockito.when(userAlertGroupService.deleteByAlertGroupId(anyInt())).thenReturn(true);

        Map<String, Object> result = alertGroupService.grantUser(getLoginUser(), groupId, "123,321");
        Mockito.verify(userAlertGroupService).deleteByAlertGroupId(groupArgument.capture());

        logger.info(result.toString());
        Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
        assertEquals(groupArgument.getValue(), groupId);
        assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
    }

    @Test
    public void testVerifyGroupName() {
        //group name not exist
        Result result = alertGroupService.verifyGroupName(getLoginUser(), groupName);
        logger.info(result.toString());
        Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg());
        boolean result = alertGroupService.existGroupName(groupName);
        Assert.assertFalse(result);
        Mockito.when(alertGroupMapper.queryByGroupName(groupName)).thenReturn(getList());

        //group name exist
        result = alertGroupService.verifyGroupName(getLoginUser(), groupName);
        logger.info(result.toString());
        Assert.assertEquals(Status.ALERT_GROUP_EXIST.getMsg(),result.getMsg());
        result = alertGroupService.existGroupName(groupName);
        Assert.assertTrue(result);
    }


+53 −0
Original line number Diff line number Diff line
/*
 * 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.dolphinscheduler.api.service;

import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

/**
 *
 */
@RunWith(MockitoJUnitRunner.class)
public class UserAlertGroupServiceTest {

    @InjectMocks
    UserAlertGroupService userAlertGroupService;

    @Mock
    UserAlertGroupMapper userAlertGroupMapper;

    @Test
    public void deleteByAlertGroupId() {

        Integer groupId = 1;
        userAlertGroupService.deleteByAlertGroupId(groupId);
        ArgumentCaptor<Integer> argumentCaptor = ArgumentCaptor.forClass(Integer.class);

        Mockito.verify(userAlertGroupMapper).deleteByAlertgroupId(argumentCaptor.capture());
        assertEquals(argumentCaptor.getValue(), groupId);

    }

}
 No newline at end of file
Loading