From e0f7059655281dda56d330a843db10cad6f9d527 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Sun, 8 Oct 2023 17:47:52 +0800 Subject: [PATCH] =?UTF-8?q?[=20Server=20]=20[=20Server=20]=20-=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9EServerInfo=E7=9A=84=E6=B5=8B=E8=AF=95=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/wdd/common/utils/FakeDataUtils.java | 105 +++++++++++++++- .../io/wdd/rpc/init/AcceptAgentInitInfo.java | 1 - .../server/config/OctopusObjectMapper.java | 14 --- .../server/controller/ServerController.java | 25 ++-- .../coreService/impl/CoreAppServiceImpl.java | 22 +++- .../impl/CoreDomainServiceImpl.java | 19 ++- .../impl/CoreServerServiceImpl.java | 16 +-- ...tityUtils.java => EntityConvertUtils.java} | 11 +- .../MyBatisAutoInsertInterceptor.java | 2 +- .../server/utils/MyBatisPlusGenerator.java | 6 - .../wdd/server/controller/ServerInfoTest.java | 118 ++++++++++++++++++ 11 files changed, 278 insertions(+), 61 deletions(-) delete mode 100644 server/src/main/java/io/wdd/server/config/OctopusObjectMapper.java rename server/src/main/java/io/wdd/server/utils/{EntityUtils.java => EntityConvertUtils.java} (84%) rename server/src/main/java/io/wdd/server/{handler => utils}/MyBatisAutoInsertInterceptor.java (97%) delete mode 100644 server/src/main/java/io/wdd/server/utils/MyBatisPlusGenerator.java create mode 100644 server/src/test/java/io/wdd/server/controller/ServerInfoTest.java diff --git a/server/src/main/java/io/wdd/common/utils/FakeDataUtils.java b/server/src/main/java/io/wdd/common/utils/FakeDataUtils.java index 948086a..445ce8e 100644 --- a/server/src/main/java/io/wdd/common/utils/FakeDataUtils.java +++ b/server/src/main/java/io/wdd/common/utils/FakeDataUtils.java @@ -1,5 +1,6 @@ package io.wdd.common.utils; +import io.wdd.server.beans.vo.ServerInfoVO; import net.datafaker.Faker; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @@ -9,11 +10,111 @@ import java.util.Locale; @Component public class FakeDataUtils { + private static final String[] cityList = {"Tokyo Tokyo JP", + "Phoenix Arizona US", + "London England GB", + "Seoul Seoul KR", + "Shanghai Shanghai CN", + "Tokyo Chiba JP" + }; + private static final String[] archList = { + "amd64", + "arm64", + "arm32", + "xia32", + "miples" + }; + private static final String[] ipAddr = { + "123.45.67.89", + "98.76.54.32", + "203.0.113.45", + "87.65.43.21", + "192.168.0.1", + "203.0.113.99", + "54.32.10.98", + "123.123.123.123", + "66.77.88.99", + "203.0.113.200" + }; + private static final String[] ipAddrV6 = { + "2001:0db8:85a3:0000:0000:8a2e:0370:7334", + "2001:0db8:abcd:0123:4567:89ab:cdef:0123", + "2001:0db8:0010:0000:0000:ff00:0042:8329", + "2001:0db8:1234:5678:90ab:cdef:feeb:daed", + "2001:0db8:dead:beef:cafe:babe:face:c0de", + "2001:0db8:0012:3456:7890:abcd:ef01:2345", + "2001:0db8:8765:4321:fedc:ba98:7654:3210", + "2001:0db8:aaaa:bbbb:cccc:dddd:eeee:ffff", + "2001:0db8:0000:1111:2222:3333:4444:5555", + "2001:0db8:9999:8888:7777:6666:5555:4444" + }; + + public static final ServerInfoVO FakeServerInfo() { + + Faker faker = new Faker(new Locale( + Locale.SIMPLIFIED_CHINESE.getLanguage(), + Locale.SIMPLIFIED_CHINESE.getCountry() + )); + + String machineId = faker.examplify("256035a5c78c434e8a33a5708ca46a64"); + + String location = cityList[faker + .random() + .nextInt(cityList.length)]; + String arch = archList[faker + .random() + .nextInt(archList.length)]; + + String cityName = location.split(" ")[0]; + String number = faker.numerify("08"); + + String serverName = String.join( + "-", + cityName, + arch, + number + ); + + String agentTopicName = serverName + "-" + machineId.substring( + 0, + 6 + ); + + + String publicIpV4 = ipAddr[faker + .random() + .nextInt(ipAddr.length)]; + + String publicIpV6 = ipAddrV6[faker + .random() + .nextInt(ipAddrV6.length)]; + + ServerInfoVO serverInfoVO = ServerInfoVO + .builder() + .topicName(agentTopicName) + .serverName(serverName) + .serverIpPbV4(publicIpV4) + .serverIpPbV6(publicIpV6) + .location(location) + .cpuBrand("AMD EPYC 7551 32-Core Processor") + .cpuCore("1 @ 2400.010 MHz") + .osInfo("Ubuntu 22.04.1 LTS") + .osKernelInfo("5.15.0-1027-oracle") + .comment(faker + .artist() + .name()) + .machineId(machineId) + .build(); + + + return serverInfoVO; + } + @Bean(name = "fakerInstance") public Faker NewFakerInstance() { Faker faker = new Faker(new Locale( - "zh", - "CN" + Locale.SIMPLIFIED_CHINESE.getLanguage(), + Locale.SIMPLIFIED_CHINESE.getCountry() )); return faker; diff --git a/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java b/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java index d503baf..f14c57f 100644 --- a/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java +++ b/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java @@ -77,7 +77,6 @@ public class AcceptAgentInitInfo { @Resource DaemonDatabaseOperator databaseOperator; - @Resource ObjectMapper objectMapper; /** diff --git a/server/src/main/java/io/wdd/server/config/OctopusObjectMapper.java b/server/src/main/java/io/wdd/server/config/OctopusObjectMapper.java deleted file mode 100644 index d22b3a7..0000000 --- a/server/src/main/java/io/wdd/server/config/OctopusObjectMapper.java +++ /dev/null @@ -1,14 +0,0 @@ -package io.wdd.server.config; - - -//@Configuration -//public class OctopusObjectMapper { -// -// //注意:该段代码并未覆盖SpringBoot自动装配的ObjectMapper对象,而是加强其配置。 -// // use the common config of object mapper -// @Bean -// public Jackson2ObjectMapperBuilderCustomizer customJackson() { -// return OctopusObjectMapperConfig.common(); -// } -// -//} diff --git a/server/src/main/java/io/wdd/server/controller/ServerController.java b/server/src/main/java/io/wdd/server/controller/ServerController.java index 6c0c31a..babb3ba 100644 --- a/server/src/main/java/io/wdd/server/controller/ServerController.java +++ b/server/src/main/java/io/wdd/server/controller/ServerController.java @@ -4,23 +4,16 @@ package io.wdd.server.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; import io.wdd.common.response.R; -import io.wdd.server.beans.po.DomainInfoPO; import io.wdd.server.beans.po.ServerInfoPO; import io.wdd.server.beans.request.ServerQueryEntity; -import io.wdd.server.beans.vo.AppInfoVO; -import io.wdd.server.beans.vo.DomainInfoVO; import io.wdd.server.beans.vo.ServerInfoVO; import io.wdd.server.coreService.CoreServerService; -import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.Nullable; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.List; - @Api("Octopus Server - Agent服务器") @RestController @RequestMapping("/server") @@ -29,12 +22,12 @@ public class ServerController { @Autowired CoreServerService coreServerService; - @GetMapping("/all") + /*@GetMapping("/all") @ApiOperation("获取所有的服务器信息") - public R serverGetAll() { + public R> serverGetAll() { return R.ok(coreServerService.serverGetAll()); - } + }*/ @PostMapping("/all") @ApiOperation("获取服务器信息-web") @@ -45,7 +38,7 @@ public class ServerController { return R.ok(coreServerService.serverGetByPage(serverQueryEntity)); } - @GetMapping("/allIncludeDelete") + /*@GetMapping("/allIncludeDelete") public R serverGetAllIncludeDelete() { return R.ok(coreServerService.serverGetAllIncludeDelete()); @@ -63,7 +56,7 @@ public class ServerController { ipv4, serverLocation )); - } + }*/ @PostMapping("/serverCreate") public R serverCreate(@RequestBody @Validated ServerInfoVO serverInfoVO) { @@ -104,7 +97,7 @@ public class ServerController { * server 1______n app * */ - // get + /*// get @GetMapping("/appGetAll") public R> appGetAll(@RequestParam(value = "serverId", required = true) Long serverId) { @@ -142,7 +135,7 @@ public class ServerController { return R.failed("delete app unsuccessful"); - } + }*/ // modify -- just modify the appInfo is ok @@ -155,7 +148,7 @@ public class ServerController { * */ // read - @GetMapping("domainGetAll") + /*@GetMapping("domainGetAll") public R> getAll(@RequestParam(value = "serverId") Long serverId) { return R.ok(coreServerService.domainGetAll(serverId)); @@ -221,6 +214,6 @@ public class ServerController { } return R.failed("delete domain failed !"); - } + }*/ } diff --git a/server/src/main/java/io/wdd/server/coreService/impl/CoreAppServiceImpl.java b/server/src/main/java/io/wdd/server/coreService/impl/CoreAppServiceImpl.java index d7fc289..8e80e05 100644 --- a/server/src/main/java/io/wdd/server/coreService/impl/CoreAppServiceImpl.java +++ b/server/src/main/java/io/wdd/server/coreService/impl/CoreAppServiceImpl.java @@ -5,7 +5,7 @@ import io.wdd.server.beans.po.AppInfoPO; import io.wdd.server.beans.vo.AppInfoVO; import io.wdd.server.coreService.CoreAppService; import io.wdd.server.service.AppInfoService; -import io.wdd.server.utils.EntityUtils; +import io.wdd.server.utils.EntityConvertUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -27,7 +27,10 @@ public class CoreAppServiceImpl implements CoreAppService { .eq(StringUtils.isNoneEmpty(appName), AppInfoPO::getAppName, appName) .list(); - List appInfoPVOList = EntityUtils.cvToTarget(appInfoPOList, AppInfoVO.class); + List appInfoPVOList = EntityConvertUtils.cvToTarget( + appInfoPOList, + AppInfoVO.class + ); return appInfoPVOList.get(0); } @@ -35,7 +38,10 @@ public class CoreAppServiceImpl implements CoreAppService { @Override public List appGetAll() { - return EntityUtils.cvToTarget(appInfoService.list(), AppInfoVO.class); + return EntityConvertUtils.cvToTarget( + appInfoService.list(), + AppInfoVO.class + ); } @Override @@ -43,7 +49,10 @@ public class CoreAppServiceImpl implements CoreAppService { return appInfoService.save( - EntityUtils.cvToTarget(appInfoVO,AppInfoPO.class) + EntityConvertUtils.cvToTarget( + appInfoVO, + AppInfoPO.class + ) ); } @@ -51,7 +60,10 @@ public class CoreAppServiceImpl implements CoreAppService { public boolean updateAppInfo(AppInfoVO appInfoVO) { return appInfoService.updateById( - EntityUtils.cvToTarget(appInfoVO,AppInfoPO.class) + EntityConvertUtils.cvToTarget( + appInfoVO, + AppInfoPO.class + ) ); } diff --git a/server/src/main/java/io/wdd/server/coreService/impl/CoreDomainServiceImpl.java b/server/src/main/java/io/wdd/server/coreService/impl/CoreDomainServiceImpl.java index c2b5b1c..e05a8b0 100644 --- a/server/src/main/java/io/wdd/server/coreService/impl/CoreDomainServiceImpl.java +++ b/server/src/main/java/io/wdd/server/coreService/impl/CoreDomainServiceImpl.java @@ -1,12 +1,12 @@ package io.wdd.server.coreService.impl; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; +import io.wdd.common.handler.MyRuntimeException; import io.wdd.server.beans.po.DomainInfoPO; import io.wdd.server.beans.vo.DomainInfoVO; import io.wdd.server.coreService.CoreDomainService; -import io.wdd.common.handler.MyRuntimeException; import io.wdd.server.service.DomainInfoService; -import io.wdd.server.utils.EntityUtils; +import io.wdd.server.utils.EntityConvertUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.util.Assert; @@ -23,7 +23,10 @@ public class CoreDomainServiceImpl implements CoreDomainService { @Override public List getAll() { - return EntityUtils.cvToTarget(domainInfoService.list(), DomainInfoVO.class); + return EntityConvertUtils.cvToTarget( + domainInfoService.list(), + DomainInfoVO.class + ); } @Override @@ -41,13 +44,19 @@ public class CoreDomainServiceImpl implements CoreDomainService { Assert.notEmpty(domainInfoPOList,"no such domain !"); - return EntityUtils.cvToTarget(domainInfoPOList, DomainInfoVO.class); + return EntityConvertUtils.cvToTarget( + domainInfoPOList, + DomainInfoVO.class + ); } @Override public boolean create(DomainInfoVO domainInfoVO) { - return domainInfoService.save(EntityUtils.cvToTarget(domainInfoVO, DomainInfoPO.class)); + return domainInfoService.save(EntityConvertUtils.cvToTarget( + domainInfoVO, + DomainInfoPO.class + )); } @Override diff --git a/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java b/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java index b89d6da..6a7985f 100644 --- a/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java +++ b/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java @@ -11,7 +11,7 @@ import io.wdd.server.beans.vo.DomainInfoVO; import io.wdd.server.beans.vo.ServerInfoVO; import io.wdd.server.coreService.CoreServerService; import io.wdd.server.service.*; -import io.wdd.server.utils.EntityUtils; +import io.wdd.server.utils.EntityConvertUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; @@ -149,7 +149,7 @@ public class CoreServerServiceImpl implements CoreServerService { public boolean serverCreate(ServerInfoVO serverInfoVO) { // BeanUtils.copyProperties(serverInfoVO, serverInfoPO); - ServerInfoPO serverInfoPO = EntityUtils.cvToTarget( + ServerInfoPO serverInfoPO = EntityConvertUtils.cvToTarget( serverInfoVO, ServerInfoPO.class ); @@ -179,7 +179,7 @@ public class CoreServerServiceImpl implements CoreServerService { throw new RuntimeException(e); } } else { - po = EntityUtils.cvToTarget( + po = EntityConvertUtils.cvToTarget( serverInfoVO, ServerInfoPO.class ); @@ -256,7 +256,7 @@ public class CoreServerServiceImpl implements CoreServerService { .collect(Collectors.toList())); - return EntityUtils.cvToTarget( + return EntityConvertUtils.cvToTarget( appInfoPOList, AppInfoVO.class ); @@ -272,7 +272,7 @@ public class CoreServerServiceImpl implements CoreServerService { ); // 1- save appInfo itself - AppInfoPO appInfoPO = EntityUtils.cvToTarget( + AppInfoPO appInfoPO = EntityConvertUtils.cvToTarget( appInfoVO, AppInfoPO.class ); @@ -285,7 +285,7 @@ public class CoreServerServiceImpl implements CoreServerService { serverAppRelationService.save(relationPO); - return EntityUtils.cvToTarget( + return EntityConvertUtils.cvToTarget( appInfoPO, AppInfoVO.class ); @@ -368,7 +368,7 @@ public class CoreServerServiceImpl implements CoreServerService { .collect(Collectors.toList())); - return EntityUtils.cvToTarget( + return EntityConvertUtils.cvToTarget( domainInfoPOList, DomainInfoVO.class ); @@ -416,7 +416,7 @@ public class CoreServerServiceImpl implements CoreServerService { .collect(Collectors.toList()); - return EntityUtils.cvToTarget( + return EntityConvertUtils.cvToTarget( domainInfoPOList, DomainInfoVO.class ); diff --git a/server/src/main/java/io/wdd/server/utils/EntityUtils.java b/server/src/main/java/io/wdd/server/utils/EntityConvertUtils.java similarity index 84% rename from server/src/main/java/io/wdd/server/utils/EntityUtils.java rename to server/src/main/java/io/wdd/server/utils/EntityConvertUtils.java index 1e22699..b55e9ba 100644 --- a/server/src/main/java/io/wdd/server/utils/EntityUtils.java +++ b/server/src/main/java/io/wdd/server/utils/EntityConvertUtils.java @@ -11,16 +11,21 @@ import java.util.stream.Collectors; @Component @Slf4j(topic = "Bean Type Convert") -public class EntityUtils { +public class EntityConvertUtils { public static T cvToTarget(S source, Class clazz) { T t = null; try { - t = clazz.getDeclaredConstructor().newInstance(); + t = clazz + .getDeclaredConstructor() + .newInstance(); - BeanUtils.copyProperties(source, t); + BeanUtils.copyProperties( + source, + t + ); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | diff --git a/server/src/main/java/io/wdd/server/handler/MyBatisAutoInsertInterceptor.java b/server/src/main/java/io/wdd/server/utils/MyBatisAutoInsertInterceptor.java similarity index 97% rename from server/src/main/java/io/wdd/server/handler/MyBatisAutoInsertInterceptor.java rename to server/src/main/java/io/wdd/server/utils/MyBatisAutoInsertInterceptor.java index 4fc69ac..2c0ed83 100644 --- a/server/src/main/java/io/wdd/server/handler/MyBatisAutoInsertInterceptor.java +++ b/server/src/main/java/io/wdd/server/utils/MyBatisAutoInsertInterceptor.java @@ -1,4 +1,4 @@ -package io.wdd.server.handler; +package io.wdd.server.utils; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import lombok.extern.slf4j.Slf4j; diff --git a/server/src/main/java/io/wdd/server/utils/MyBatisPlusGenerator.java b/server/src/main/java/io/wdd/server/utils/MyBatisPlusGenerator.java deleted file mode 100644 index 23b8e84..0000000 --- a/server/src/main/java/io/wdd/server/utils/MyBatisPlusGenerator.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.wdd.server.utils; - -public class MyBatisPlusGenerator { - - -} diff --git a/server/src/test/java/io/wdd/server/controller/ServerInfoTest.java b/server/src/test/java/io/wdd/server/controller/ServerInfoTest.java new file mode 100644 index 0000000..01cd394 --- /dev/null +++ b/server/src/test/java/io/wdd/server/controller/ServerInfoTest.java @@ -0,0 +1,118 @@ +package io.wdd.server.controller; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import io.wdd.common.response.R; +import io.wdd.common.utils.FakeDataUtils; +import io.wdd.server.beans.po.ServerInfoPO; +import io.wdd.server.beans.request.ServerQueryEntity; +import io.wdd.server.beans.vo.ServerInfoVO; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; + +import javax.annotation.Resource; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +public class ServerInfoTest { + + @Resource + ServerController serverController; + + // Test serverGetAllByPage returns expected result + @Test + public void test_serverGetAllByPage_returns_expected_result() { + // Arrange + ServerQueryEntity serverQueryEntity = new ServerQueryEntity(); + + // Act + R> result = serverController.serverGetAllByPage(serverQueryEntity); + + // Assert + assertNotNull(result); + assertEquals( + HttpStatus.OK, + result.getCode() + ); + assertNotNull(result.getData()); + } + + // Test serverCreate creates a new server successfully + @Test + public void test_serverCreate_creates_new_server_successfully() { + + ServerInfoVO serverInfoVO = FakeDataUtils.FakeServerInfo(); + + // Act + R result = serverController.serverCreate(serverInfoVO); + + // Assert + assertNotNull(result); + assertEquals( + "Create Server Success !", + result.getData() + ); + } + + // Test serverGetAllByPage with invalid input returns error + @Test + public void test_serverGetAllByPage_with_invalid_input_returns_error() { + // Arrange + ServerQueryEntity serverQueryEntity = null; + + // Act + R> result = serverController.serverGetAllByPage(serverQueryEntity); + + // Assert + assertNotNull(result); + assertEquals( + HttpStatus.BAD_REQUEST, + result.getCode() + ); + assertNull(result.getData()); + } + + // Test serverCreate with invalid input returns error + @Test + public void test_serverCreate_with_invalid_input_returns_error() { + // Arrange + ServerInfoVO serverInfoVO = null; + + // Act + R result = serverController.serverCreate(serverInfoVO); + + // Assert + assertNotNull(result); + assertEquals( + HttpStatus.BAD_REQUEST, + result.getCode() + ); + assertEquals( + "Create Server Failed !", + result.getData() + ); + } + + // Test serverUpdate with invalid input returns error + @Test + public void test_serverUpdate_with_invalid_input_returns_error() { + // Arrange + ServerInfoPO serverInfoPO = null; + + // Act + R result = serverController.serverUpdate(serverInfoPO); + + // Assert + assertNotNull(result); + assertEquals( + HttpStatus.BAD_REQUEST, + result.getCode() + ); + assertEquals( + "Server info update failed !", + result.getData() + ); + } + +}