增加sonar测试并修复sonar测试的问题

This commit is contained in:
zlt
2019-02-04 14:18:47 +08:00
parent e16d8f5d02
commit 110bdc5a84
58 changed files with 181 additions and 233 deletions
@@ -9,10 +9,7 @@ import com.central.common.model.PageResult;
import com.central.generator.service.SysGeneratorService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
@@ -30,16 +27,16 @@ public class SysGeneratorController {
* 列表
*/
@ResponseBody
@RequestMapping("/list")
public PageResult list(@RequestParam Map<String, Object> params) {
@GetMapping("/list")
public PageResult getTableList(@RequestParam Map<String, Object> params) {
return sysGeneratorService.queryList(params);
}
/**
* 生成代码
* 生成代码FileUtil
*/
@RequestMapping("/code")
public void code(String tables, HttpServletResponse response) throws IOException {
@GetMapping("/code")
public void makeCode(String tables, HttpServletResponse response) throws IOException {
byte[] data = sysGeneratorService.generatorCode(tables.split(","));
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"generator.zip\"");
@@ -3,6 +3,7 @@ package com.central.generator.utils;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@@ -33,6 +34,10 @@ import org.apache.velocity.app.Velocity;
*/
@Slf4j
public class GenUtils {
private GenUtils() {
throw new IllegalStateException("Utility class");
}
private final static String FILE_NAME_MODEL = "Model.java.vm";
private final static String FILE_NAME_MAPPER = "Mapper.java.vm";
private final static String FILE_NAME_MAPPERXML = "Mapper.xml.vm";
@@ -40,17 +45,20 @@ public class GenUtils {
private final static String FILE_NAME_SERVICEIMPL = "ServiceImpl.java.vm";
private final static String FILE_NAME_CONTROLLER = "Controller.java.vm";
private final static String FILE_NAME_PAGE = "index.html.vm";
private final static String TEMPLATE_PATH = "template/";
private final static String PACKAGE = "package";
private final static String MODULE_NAME = "moduleName";
public static List<String> getTemplates() {
List<String> templates = new ArrayList<String>();
templates.add("template/"+FILE_NAME_MODEL);
templates.add("template/"+FILE_NAME_MAPPER);
templates.add("template/"+FILE_NAME_MAPPERXML);
templates.add("template/"+FILE_NAME_SERVICE);
templates.add("template/"+FILE_NAME_SERVICEIMPL);
templates.add("template/"+FILE_NAME_CONTROLLER);
List<String> templates = new ArrayList<>();
templates.add(TEMPLATE_PATH+FILE_NAME_MODEL);
templates.add(TEMPLATE_PATH+FILE_NAME_MAPPER);
templates.add(TEMPLATE_PATH+FILE_NAME_MAPPERXML);
templates.add(TEMPLATE_PATH+FILE_NAME_SERVICE);
templates.add(TEMPLATE_PATH+FILE_NAME_SERVICEIMPL);
templates.add(TEMPLATE_PATH+FILE_NAME_CONTROLLER);
templates.add("template/"+FILE_NAME_PAGE);
templates.add(TEMPLATE_PATH+FILE_NAME_PAGE);
return templates;
}
@@ -123,8 +131,8 @@ public class GenUtils {
map.put("columns", tableEntity.getColumns());
map.put("hasBigDecimal", hasBigDecimal);
map.put("mainPath", mainPath);
map.put("package", config.getString("package"));
map.put("moduleName", config.getString("moduleName"));
map.put(PACKAGE, config.getString(PACKAGE));
map.put(MODULE_NAME, config.getString(MODULE_NAME));
map.put("author", config.getString("author"));
map.put("email", config.getString("email"));
map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
@@ -141,8 +149,8 @@ public class GenUtils {
tpl.merge(context, sw);
//添加到zip
zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package"), config.getString("moduleName"))));
IOUtils.write(sw.toString(), zip, "UTF-8");
zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString(PACKAGE), config.getString(MODULE_NAME))));
IOUtils.write(sw.toString(), zip, StandardCharsets.UTF_8);
zip.closeEntry();
} catch (IOException e) {
log.error("generatorCode-error", e);
@@ -1,7 +1,6 @@
package com.central.file.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -1,5 +1,6 @@
package com.central.file.config;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
@@ -19,7 +20,7 @@ import com.central.file.model.FileType;
@Configuration
public class OssServiceFactory {
private Map<FileType, IFileService> map = new HashMap<>();
private Map<FileType, IFileService> map = new EnumMap<>(FileType.class);
@Autowired
private IFileService aliyunOssServiceImpl;
@@ -5,7 +5,6 @@ import java.util.Map;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.model.SysUser;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.multipart.MultipartFile;
@@ -6,7 +6,6 @@ import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.OSSClient;
import com.central.file.mapper.FileMapper;
import com.central.file.model.FileInfo;
import com.central.file.model.FileType;
@@ -40,15 +40,6 @@ public class QiniuOssServiceImplI extends AbstractIFileService implements Initia
return FileType.QINIU;
}
/**
* 获取上传凭证
*
* @return
*/
private String getUploadToken() {
return this.auth.uploadToken(bucket, null, 3600, putPolicy);
}
@Override
protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
// 调用put方法上传
@@ -6,6 +6,7 @@ import java.io.InputStream;
import java.util.Date;
import com.central.file.model.FileInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
@@ -13,7 +14,11 @@ import org.springframework.web.multipart.MultipartFile;
* @author zlt
* 文件工具类
*/
@Slf4j
public class FileUtil {
private FileUtil() {
throw new IllegalStateException("Utility class");
}
public static FileInfo getFileInfo(MultipartFile file) throws Exception {
String md5 = fileMd5(file.getInputStream());
@@ -37,7 +42,7 @@ public class FileUtil {
try {
return DigestUtils.md5Hex(inputStream);
} catch (IOException e) {
e.printStackTrace();
log.error("fileMd5-error", e);
}
return null;
}
@@ -54,7 +59,7 @@ public class FileUtil {
file.transferTo(targetFile);
return path;
} catch (Exception e) {
e.printStackTrace();
log.error("saveFile-error", e);
}
return null;
}
@@ -75,9 +75,8 @@ public class SysMenuController {
@ApiOperation(value = "根据roleId获取对应的菜单")
@GetMapping("/{roleId}/menus")
public List<Map<String, Object>> findMenusByRoleId(@PathVariable Long roleId) {
Set<Long> roleIds = new HashSet<Long>() {{
add(roleId);
}};
Set<Long> roleIds = new HashSet<>();
roleIds.add(roleId);
//获取该角色对应的菜单
List<SysMenu> roleMenus = menuService.findByRoles(roleIds);
//全部的菜单列表
@@ -169,7 +168,6 @@ public class SysMenuController {
return Collections.emptyList();
}
List<SysMenu> menus = menuService.findByRoleCodes(roles.parallelStream().map(SysRole::getCode).collect(Collectors.toSet()), CommonConstant.MENU);
List<SysMenu> sysMenus = treeBuilder(menus);
return sysMenus;
return treeBuilder(menus);
}
}
@@ -39,6 +39,8 @@ import javax.servlet.http.HttpServletResponse;
@RestController
@Api(tags = "用户模块api")
public class SysUserController {
private static final String ADMIN_CHANGE_MSG = "超级管理员不给予修改";
@Autowired
private ISysUserService appUserService;
@@ -163,7 +165,7 @@ public class SysUserController {
public Result updateEnabled(@RequestParam Map<String, Object> params) {
Long id = MapUtils.getLong(params, "id");
if (checkAdmin(id)) {
return Result.failed("超级管理员不给予修改");
return Result.failed(ADMIN_CHANGE_MSG);
}
return appUserService.updateEnabled(params);
}
@@ -176,7 +178,7 @@ public class SysUserController {
@PutMapping(value = "/users/{id}/password")
public Result resetPassword(@PathVariable Long id) {
if (checkAdmin(id)) {
return Result.failed("超级管理员不给予修改");
return Result.failed(ADMIN_CHANGE_MSG);
}
appUserService.updatePassword(id, null, null);
return Result.succeed("重置成功");
@@ -188,7 +190,7 @@ public class SysUserController {
@PutMapping(value = "/users/password")
public Result resetPassword(@RequestBody SysUser sysUser) {
if (checkAdmin(sysUser.getId())) {
return Result.failed("超级管理员不给予修改");
return Result.failed(ADMIN_CHANGE_MSG);
}
appUserService.updatePassword(sysUser.getId(), sysUser.getOldPassword(), sysUser.getNewPassword());
return Result.succeed("重置成功");
@@ -202,7 +204,7 @@ public class SysUserController {
@DeleteMapping(value = "/users/{id}")
public Result delete(@PathVariable Long id) {
if (checkAdmin(id)) {
return Result.failed("超级管理员不给予修改");
return Result.failed(ADMIN_CHANGE_MSG);
}
appUserService.delUser(id);
return Result.succeed("删除成功");
@@ -33,9 +33,7 @@ public class SysMenuServiceImpl extends SuperServiceImpl<SysMenuMapper, SysMenu>
sysRoleMenuMapper.delete(roleId, null);
if (!CollectionUtils.isEmpty(menuIds)) {
menuIds.forEach(menuId -> {
sysRoleMenuMapper.save(roleId, menuId);
});
menuIds.forEach(menuId -> sysRoleMenuMapper.save(roleId, menuId));
}
}
@@ -66,6 +66,7 @@ public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole>
}
@Override
@Transactional
public Result saveOrUpdateRole(SysRole sysRole) {
if (sysRole.getId() == null) {
this.saveRole(sysRole);
@@ -80,7 +80,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
loginAppUser.setRoles(sysRoles);
if (!CollectionUtils.isEmpty(sysRoles)) {
Set<Long> roleIds = sysRoles.parallelStream().map(r -> r.getId()).collect(Collectors.toSet());
Set<Long> roleIds = sysRoles.parallelStream().map(SuperEntity::getId).collect(Collectors.toSet());
List<SysMenu> menus = roleMenuMapper.findMenusByRoleIds(roleIds, CommonConstant.PERMISSION);
if (!CollectionUtils.isEmpty(menus)) {
Set<String> permissions = menus.parallelStream().map(p -> p.getPathMethod()+":"+p.getPath())
@@ -154,9 +154,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
userRoleMapper.deleteUserRole(id, null);
if (!CollectionUtils.isEmpty(roleIds)) {
roleIds.forEach(roleId -> {
userRoleMapper.saveUserRoles(id, roleId);
});
roleIds.forEach(roleId -> userRoleMapper.saveUserRoles(id, roleId));
}
}
@@ -188,10 +186,8 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
List<Long> userIds = list.stream().map(SysUser::getId).collect(Collectors.toList());
List<SysRole> sysRoles = userRoleMapper.findRolesByUserIds(userIds);
list.forEach(u -> {
u.setRoles(sysRoles.stream().filter(r -> !ObjectUtils.notEqual(u.getId(), r.getUserId()))
.collect(Collectors.toList()));
});
list.forEach(u -> u.setRoles(sysRoles.stream().filter(r -> !ObjectUtils.notEqual(u.getId(), r.getUserId()))
.collect(Collectors.toList())));
}
return PageResult.<SysUser>builder().data(list).code(0).count(total).build();
}
@@ -238,9 +234,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
userRoleMapper.deleteUserRole(sysUser.getId(), null);
List roleIds = Arrays.asList(sysUser.getRoleId().split(","));
if (!CollectionUtils.isEmpty(roleIds)) {
roleIds.forEach(roleId -> {
userRoleMapper.saveUserRoles(sysUser.getId(), Long.parseLong(roleId.toString()));
});
roleIds.forEach(roleId -> userRoleMapper.saveUserRoles(sysUser.getId(), Long.parseLong(roleId.toString())));
}
}
return result ? Result.succeed(sysUser, "操作成功") : Result.failed("操作失败");
@@ -250,7 +244,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
@Override
public boolean delUser(Long id) {
userRoleMapper.deleteUserRole(id, null);
return baseMapper.deleteById(id) > 0 ? true : false;
return baseMapper.deleteById(id) > 0;
}
@Override