[ Server ] [ Server ] - 角色部分CRUD加测试代码

This commit is contained in:
zeaslity
2023-10-08 16:51:16 +08:00
parent 2594514a87
commit 8b6da2f9e8
10 changed files with 394 additions and 34 deletions

View File

@@ -61,6 +61,19 @@
<version>1.5.5.Final</version>
</dependency>
<!--<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</dependency>-->
<!--制造假数据-->
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>2.0.2</version>
</dependency>
<!-- add in 2023-02-08 needed for some operation -->
<dependency>
<groupId>org.springframework.cloud</groupId>
@@ -81,12 +94,6 @@
<version>3.0.3</version>
</dependency>
<!--<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-influx</artifactId>
<scope>runtime</scope>
</dependency>-->
<!-- 添加于 2023-02-11 用于Oracle Object Storage-->
<!-- https://docs.oracle.com/en-us/iaas/tools/java/3.3.0/ -->
<dependency>
@@ -122,7 +129,6 @@
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@@ -167,15 +173,28 @@
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
<!-- Mapstruct should follow the lombok path(s) -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<version>1.18.20</version>
</path>
<!-- This is needed when using Lombok 1.18.16 and above -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>

View File

@@ -7,7 +7,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.DataAccessException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
@@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.*;
/**
@@ -119,23 +118,28 @@ public class GlobalExceptionHandler {
* @param exception 数据库异常
* @return
*/
@ExceptionHandler(value = {SQLException.class, DuplicateKeyException.class})
@ExceptionHandler(value = {SQLException.class})
public R<String> sqlExceptionHandler(SQLException exception) {
R<String> vo = new R<>();
vo.setCode(ResultStat.PARAM_ERROR.getCode());
vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription()));
if (exception instanceof SQLIntegrityConstraintViolationException) {
vo.setData("Data already exsit ! 操作失败!");
} else {
vo.setData("数据库异常,操作失败!");
}
log.debug(
exception.getMessage(),
exception
);
vo.setData("数据库异常,操作失败!");
vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription()));
return vo;
}
@ExceptionHandler(value = {DataAccessException.class})
public R<String> sqlDataExceptionHandler(DataAccessException exception) {
R<String> vo = new R<>();
vo.setCode(ResultStat.PARAM_ERROR.getCode());
vo.setData("数据库操作错误!");
vo.setMsg(exception.getMessage());
return vo;
}

View File

@@ -0,0 +1,21 @@
package io.wdd.common.utils;
import net.datafaker.Faker;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Locale;
@Component
public class FakeDataUtils {
@Bean(name = "fakerInstance")
public Faker NewFakerInstance() {
Faker faker = new Faker(new Locale(
"zh",
"CN"
));
return faker;
}
}

View File

@@ -6,6 +6,7 @@ import io.wdd.server.beans.vo.ServerRoleVO;
import io.wdd.server.coreService.CoreRoleService;
import io.wdd.server.service.ServerRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -18,9 +19,6 @@ public class CoreRoleServiceImpl implements CoreRoleService {
@Resource
ServerRoleService serverRoleService;
@Resource
ServerRoleDTOMapper serverRoleDTOMapper;
@Override
public List<ServerRolePO> roleGetAll() {
return serverRoleService.list();
@@ -29,12 +27,29 @@ public class CoreRoleServiceImpl implements CoreRoleService {
@Override
public boolean roleCreate(ServerRoleVO serverRoleVO) {
ServerRolePO serverRolePO = serverRoleDTOMapper.voToPo(serverRoleVO);
if (!checkDataValid(serverRoleVO)) {
return false;
}
ServerRolePO serverRolePO = ServerRoleDTOMapper.INSTANCE.voToPo(serverRoleVO);
return serverRoleService.save(serverRolePO);
}
private boolean checkDataValid(ServerRoleVO serverRoleVO) {
if (StringUtils.isBlank(serverRoleVO.getRoleName())) {
return false;
}
return serverRoleVO.getRoleNum() < 128 && serverRoleVO.getRoleNum() >= 0;
}
@Override
public boolean roleUpdate(ServerRolePO serverRolePO) {
if (!checkDataValid(ServerRoleDTOMapper.INSTANCE.poToVo(serverRolePO))) {
return false;
}
return serverRoleService.saveOrUpdate(serverRolePO);
}

View File

@@ -0,0 +1,168 @@
server:
port: 9999
spring:
main:
allow-circular-references: true
allow-bean-definition-overriding: true
rabbitmq:
host: 42.192.52.227
port: 20672
username: boge
password: boge8tingH
virtual-host: /
listener:
simple:
retry:
# ack failed will reentrant the Rabbit Listener
max-attempts: 2
enabled: true
# retry interval unit ms
max-interval: 65000
initial-interval: 65000
redis:
host: 42.192.52.227
port: 21370
database: 0
password: boge8tingH
# cluster:
# nodes:
# - 43.154.83.213:21370
# - 43.154.83.213:21371
# - 43.154.83.213:21372
# - 43.154.83.213:21373
# - 43.154.83.213:21374
# - 43.154.83.213:21375
# # 获取失败 最大重定向次数
# max-redirects: 3
# timeout: 50000
#如果用以前的jedis可以把下面的lettuce换成jedis即可
lettuce:
pool:
# 连接池最大连接数默认值为8
max-active: 16
# 连接池最大阻塞时间(使用负值表示没有限制)默认值为-1
max-wait: -1
# 连接池中最大空闲连接数默认值为8
max-idle: 10
# 连接池中的最小空闲连接数默认值为0
min-idle: 10
time-between-eviction-runs: 50000
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://42.192.52.227:21306/octopus_server?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: boge
password: boge8tingH
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 3
# 空闲连接存活最大时间默认60000010分钟
idle-timeout: 180000
# 连接池最大连接数默认是10
maximum-pool-size: 5
# 此属性控制从池返回的连接的默认自动提交行为,默认值true
auto-commit: true
connection-test-query: SELECT 1
# 最大文件上传
servlet:
multipart:
max-file-size: 500MB
max-request-size: 500MB
mybatis-plus:
type-aliases-package: io.wdd.server.beans.po
global-config:
db-column-underline: true
db-config:
# modify ethe id strategy
id-type: assign_id
# logic delete field globally
logicDeleteField: isDelete
logic-not-delete-value: 0
logic-delete-value: 1
banner: false
configuration:
# 希望知道所有的sql是怎么执行的, 配置输出日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
# 数据库下划线--实体类也是下划线 需要为false
map-underscore-to-camel-case: true
# 一级缓存的 缓存级别默认为 session如果要关闭一级缓存可以设置为 statement
local-cache-scope: session
# 是否开启二级缓存
cache-enabled: false
# 分页插件配置
interceptor: com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor
# 默认地址为 classpath*:/mapper/**/*.xml
# mapper-locations: classpath*:/real-mappers/**/*.xml
octopus:
message:
# agent boot up default common exchange
init_exchange: InitExchange
# server will send message to agent using this common queue
init_to_server: InitToServer
# agent boot up default common exchange routing key
init_to_server_key: InitToServerKey
# server will receive message from agent using this common queue
init_from_server: InitFromServer
# agent boot up default common exchange routing key
init_from_server_key: InitFromServerKey
# initialization register time out (unit ms) default is 5 min
init_ttl: "3000000"
# Octopus Exchange Name == server comunicate with agent
octopus_exchange: OctopusExchange
# Octopus Message To Server == all agent send info to server queue and topic
octopus_to_server: OctopusToServer
executor:
name: executor-functions
status:
name: octopus-agent
healthy:
type: cron
cron: 10 * * * * ? *
start-delay: 30
metric:
type: cron
cron: 30 * * * * ? *
start-delay: 40
oss:
# 这里只是因为需要一个层级不一定下面的都是oracle
oracle:
seoul1:
namespace: cnk8d6fazu16
region: ap-seoul-1
key: aed62d24d85e2da809ce02bf272420ba4ed74820
secret: rQdEcn69K049+JkA1IGoQmC1k8zma8zfWvZvVS0h144=
capacity: 10737418240
seoul2:
namespace: cncvl8ro2rbf
region: ap-seoul-1
key: 9e413c6e66269bc65d7ec951d93ba9c6a9781f6e
secret: dkXD7PysjrhsTKfNIbKupUmtxdfOvYCyLXf0MXa4hnU=
capacity: 10737418240
tokyo1:
namespace: nrjcs6lwr9vy
region: ap-tokyo-1
key: 0584c323d6c8d24cc2fc8c2d716a4ea35bb99ae6
secret: +xicO9obeqzC5a/WY1rXvl5pMWSWbVIpMt3Qv691NtU=
capacity: 10737418240
phoenix1:
namespace: axqr6x6t48wm
region: us-phoenix-1
key: e87a121f1548b244c7bd649a1f0ca35195d46cf2
secret: uT+NIgJiKPjSaPT8EVUw3xbLSCv/CFMFuebVauznafk=
capacity: 10737418240
london1:
namespace: lrmzslyt8jzs
region: uk-london-1
key: 57671886f9f1bcc5ac7235b5a0e6123f5ca271b3
secret: ukWae6TXjID2Wqxh+7mAPAf4busZPGzwAh/WDKZ5MOQ=
capacity: 10737418240
# 开启debug模式
logging:
level:
io.wdd.rpc: debug

View File

@@ -6,7 +6,7 @@ spring:
allow-circular-references: true
allow-bean-definition-overriding: true
rabbitmq:
host: 42.192.52.227
host: 192.168.35.71
port: 20672
username: boge
password: boge8tingH
@@ -21,7 +21,7 @@ spring:
max-interval: 65000
initial-interval: 65000
redis:
host: 42.192.52.227
host: 192.168.35.71
port: 21370
database: 0
password: boge8tingH
@@ -50,7 +50,7 @@ spring:
time-between-eviction-runs: 50000
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://42.192.52.227:21306/octopus_server?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
url: jdbc:mysql://192.168.35.71:21306/octopus_server?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: boge
password: boge8tingH
type: com.zaxxer.hikari.HikariDataSource

View File

@@ -0,0 +1,131 @@
package io.wdd.server.controller;
import io.wdd.common.response.R;
import io.wdd.server.beans.po.ServerRolePO;
import io.wdd.server.beans.vo.ServerRoleVO;
import net.datafaker.Faker;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class ServerRoleTest {
@Resource
RoleController roleController;
@Resource
Faker fakerInstance;
// Test that roleGetAll() returns a non-empty list of ServerRolePO objects
@Test
public void test_roleGetAll_returnsNonEmptyList() {
List<ServerRolePO> result = roleController
.roleGetAll()
.getData();
assertNotNull(result);
assertFalse(result.isEmpty());
}
// Test that roleCreate() creates a new ServerRolePO object successfully
@Test
public void test_roleCreate_createsNewServerRolePO() {
ServerRoleVO serverRoleVO = new ServerRoleVO();
// Set properties of serverRoleVO
serverRoleVO.setRoleName(fakerInstance
.dungeonsAndDragons()
.cities());
serverRoleVO.setRoleNum(fakerInstance
.number()
.numberBetween(
10,
99
));
R<String> result = roleController.roleCreate(serverRoleVO);
assertEquals(
"创建角色成功!",
result.getData()
);
}
// Test that roleUpdate() updates an existing ServerRolePO object successfully
@Test
public void test_roleUpdate_updatesExistingServerRolePO() {
List<ServerRolePO> serverRolePOList = roleController
.roleGetAll()
.getData();
int size = serverRolePOList.size();
if (!(size > 0)) {
System.out.println("没有查询到内容! 跳过");
}
// Set properties of serverRolePO
ServerRolePO serverRolePO = serverRolePOList
.stream()
.skip(new Random().nextInt(size))
.findFirst()
.get();
System.out.println("serverRolePO old = " + serverRolePO);
serverRolePO.setRoleName(fakerInstance
.dungeonsAndDragons()
.cities());
R<String> result = roleController.roleUpdate(serverRolePO);
assertEquals(
"更新角色成功!",
result.getData()
);
}
// Test that roleCreate() fails when given invalid input
@Test
public void test_roleCreate_failsWithInvalidInput() {
ServerRoleVO serverRoleVO = new ServerRoleVO();
// Set invalid properties of serverRoleVO
R<String> result = roleController.roleCreate(serverRoleVO);
assertEquals(
"创建角色失败!",
result.getData()
);
}
// Test that roleUpdate() fails when given invalid input
@Test
public void test_roleUpdate_failsWithInvalidInput() {
ServerRolePO serverRolePO = new ServerRolePO();
// Set invalid properties of serverRolePO
R<String> result = roleController.roleUpdate(serverRolePO);
assertEquals(
"更新角色失败!",
result.getData()
);
}
// Test that roleDelete() fails when given invalid input
@Test
public void test_roleDelete_failsWithInvalidInput() {
Long roleId = null; // Set invalid roleId
R<String> result = roleController.roleDelete(roleId);
assertEquals(
"删除角色失败!",
result.getData()
);
}
}

View File

@@ -1,4 +1,4 @@
package io.wdd.server;
package io.wdd.server.func;
import io.wdd.func.xray.service.XrayCoreService;
import org.junit.jupiter.api.Test;