From 8b6da2f9e863e497deb492de26f4f4fa6f4e4e19 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Sun, 8 Oct 2023 16:51:16 +0800 Subject: [PATCH] =?UTF-8?q?[=20Server=20]=20[=20Server=20]=20-=20=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E9=83=A8=E5=88=86CRUD=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/pom.xml | 45 +++-- .../handler/GlobalExceptionHandler.java | 30 ++-- .../io/wdd/common/utils/FakeDataUtils.java | 21 +++ .../coreService/impl/CoreRoleServiceImpl.java | 23 ++- .../main/resources/application-octopus.yml | 168 ++++++++++++++++++ server/src/main/resources/application.yml | 6 +- .../wdd/server/controller/ServerRoleTest.java | 131 ++++++++++++++ .../{ => func}/ExecutionEntityJson.json | 0 .../server/{ => func}/XrayGenerateTest.java | 2 +- .../wdd/source/octopus/simple-middleware.yaml | 2 + 10 files changed, 394 insertions(+), 34 deletions(-) create mode 100644 server/src/main/java/io/wdd/common/utils/FakeDataUtils.java create mode 100644 server/src/main/resources/application-octopus.yml create mode 100644 server/src/test/java/io/wdd/server/controller/ServerRoleTest.java rename server/src/test/java/io/wdd/server/{ => func}/ExecutionEntityJson.json (100%) rename server/src/test/java/io/wdd/server/{ => func}/XrayGenerateTest.java (93%) diff --git a/server/pom.xml b/server/pom.xml index a45fbbc..5b4f59b 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -61,6 +61,19 @@ 1.5.5.Final + + + + + net.datafaker + datafaker + 2.0.2 + + org.springframework.cloud @@ -81,12 +94,6 @@ 3.0.3 - - @@ -122,7 +129,6 @@ 31.1-jre - org.projectlombok lombok @@ -167,15 +173,28 @@ - org.springframework.boot - spring-boot-maven-plugin + org.apache.maven.plugins + maven-compiler-plugin - - + + + org.mapstruct + mapstruct-processor + 1.5.5.Final + + + org.projectlombok lombok - - + 1.18.20 + + + + org.projectlombok + lombok-mapstruct-binding + 0.2.0 + + diff --git a/server/src/main/java/io/wdd/common/handler/GlobalExceptionHandler.java b/server/src/main/java/io/wdd/common/handler/GlobalExceptionHandler.java index ee0e58c..ed1a492 100644 --- a/server/src/main/java/io/wdd/common/handler/GlobalExceptionHandler.java +++ b/server/src/main/java/io/wdd/common/handler/GlobalExceptionHandler.java @@ -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 sqlExceptionHandler(SQLException exception) { R 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 sqlDataExceptionHandler(DataAccessException exception) { + + R vo = new R<>(); + + vo.setCode(ResultStat.PARAM_ERROR.getCode()); + + vo.setData("数据库操作错误!"); + vo.setMsg(exception.getMessage()); return vo; } diff --git a/server/src/main/java/io/wdd/common/utils/FakeDataUtils.java b/server/src/main/java/io/wdd/common/utils/FakeDataUtils.java new file mode 100644 index 0000000..948086a --- /dev/null +++ b/server/src/main/java/io/wdd/common/utils/FakeDataUtils.java @@ -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; + } +} diff --git a/server/src/main/java/io/wdd/server/coreService/impl/CoreRoleServiceImpl.java b/server/src/main/java/io/wdd/server/coreService/impl/CoreRoleServiceImpl.java index d23ad26..9dd76b9 100644 --- a/server/src/main/java/io/wdd/server/coreService/impl/CoreRoleServiceImpl.java +++ b/server/src/main/java/io/wdd/server/coreService/impl/CoreRoleServiceImpl.java @@ -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 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); } diff --git a/server/src/main/resources/application-octopus.yml b/server/src/main/resources/application-octopus.yml new file mode 100644 index 0000000..de88b42 --- /dev/null +++ b/server/src/main/resources/application-octopus.yml @@ -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 + # 空闲连接存活最大时间,默认600000(10分钟) + 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 + diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index fbfa6d0..dce6514 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -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 diff --git a/server/src/test/java/io/wdd/server/controller/ServerRoleTest.java b/server/src/test/java/io/wdd/server/controller/ServerRoleTest.java new file mode 100644 index 0000000..976c8c7 --- /dev/null +++ b/server/src/test/java/io/wdd/server/controller/ServerRoleTest.java @@ -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 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 result = roleController.roleCreate(serverRoleVO); + assertEquals( + "创建角色成功!", + result.getData() + ); + } + + // Test that roleUpdate() updates an existing ServerRolePO object successfully + @Test + public void test_roleUpdate_updatesExistingServerRolePO() { + List 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 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 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 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 result = roleController.roleDelete(roleId); + assertEquals( + "删除角色失败!", + result.getData() + ); + } + + +} diff --git a/server/src/test/java/io/wdd/server/ExecutionEntityJson.json b/server/src/test/java/io/wdd/server/func/ExecutionEntityJson.json similarity index 100% rename from server/src/test/java/io/wdd/server/ExecutionEntityJson.json rename to server/src/test/java/io/wdd/server/func/ExecutionEntityJson.json diff --git a/server/src/test/java/io/wdd/server/XrayGenerateTest.java b/server/src/test/java/io/wdd/server/func/XrayGenerateTest.java similarity index 93% rename from server/src/test/java/io/wdd/server/XrayGenerateTest.java rename to server/src/test/java/io/wdd/server/func/XrayGenerateTest.java index 179c8b5..8c27fdc 100644 --- a/server/src/test/java/io/wdd/server/XrayGenerateTest.java +++ b/server/src/test/java/io/wdd/server/func/XrayGenerateTest.java @@ -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; diff --git a/source/src/main/java/io/wdd/source/octopus/simple-middleware.yaml b/source/src/main/java/io/wdd/source/octopus/simple-middleware.yaml index 8b422b4..415e951 100644 --- a/source/src/main/java/io/wdd/source/octopus/simple-middleware.yaml +++ b/source/src/main/java/io/wdd/source/octopus/simple-middleware.yaml @@ -69,6 +69,8 @@ volumes: driver: local rabbitmq_data: driver: local + redis_data: + driver: local networks: app-tier: