[ Server ] [ Server ] - 新增ServerInfo的测试内容

This commit is contained in:
zeaslity
2023-10-08 17:47:52 +08:00
parent 8b6da2f9e8
commit e0f7059655
11 changed files with 278 additions and 61 deletions

View File

@@ -1,5 +1,6 @@
package io.wdd.common.utils; package io.wdd.common.utils;
import io.wdd.server.beans.vo.ServerInfoVO;
import net.datafaker.Faker; import net.datafaker.Faker;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -9,11 +10,111 @@ import java.util.Locale;
@Component @Component
public class FakeDataUtils { 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") @Bean(name = "fakerInstance")
public Faker NewFakerInstance() { public Faker NewFakerInstance() {
Faker faker = new Faker(new Locale( Faker faker = new Faker(new Locale(
"zh", Locale.SIMPLIFIED_CHINESE.getLanguage(),
"CN" Locale.SIMPLIFIED_CHINESE.getCountry()
)); ));
return faker; return faker;

View File

@@ -77,7 +77,6 @@ public class AcceptAgentInitInfo {
@Resource @Resource
DaemonDatabaseOperator databaseOperator; DaemonDatabaseOperator databaseOperator;
@Resource @Resource
ObjectMapper objectMapper; ObjectMapper objectMapper;
/** /**

View File

@@ -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();
// }
//
//}

View File

@@ -4,23 +4,16 @@ package io.wdd.server.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.wdd.common.response.R; 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.po.ServerInfoPO;
import io.wdd.server.beans.request.ServerQueryEntity; 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.beans.vo.ServerInfoVO;
import io.wdd.server.coreService.CoreServerService; import io.wdd.server.coreService.CoreServerService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api("Octopus Server - Agent服务器") @Api("Octopus Server - Agent服务器")
@RestController @RestController
@RequestMapping("/server") @RequestMapping("/server")
@@ -29,12 +22,12 @@ public class ServerController {
@Autowired @Autowired
CoreServerService coreServerService; CoreServerService coreServerService;
@GetMapping("/all") /*@GetMapping("/all")
@ApiOperation("获取所有的服务器信息") @ApiOperation("获取所有的服务器信息")
public R<List> serverGetAll() { public R<List<ServerInfoVO>> serverGetAll() {
return R.ok(coreServerService.serverGetAll()); return R.ok(coreServerService.serverGetAll());
} }*/
@PostMapping("/all") @PostMapping("/all")
@ApiOperation("获取服务器信息-web") @ApiOperation("获取服务器信息-web")
@@ -45,7 +38,7 @@ public class ServerController {
return R.ok(coreServerService.serverGetByPage(serverQueryEntity)); return R.ok(coreServerService.serverGetByPage(serverQueryEntity));
} }
@GetMapping("/allIncludeDelete") /*@GetMapping("/allIncludeDelete")
public R<List> serverGetAllIncludeDelete() { public R<List> serverGetAllIncludeDelete() {
return R.ok(coreServerService.serverGetAllIncludeDelete()); return R.ok(coreServerService.serverGetAllIncludeDelete());
@@ -63,7 +56,7 @@ public class ServerController {
ipv4, ipv4,
serverLocation serverLocation
)); ));
} }*/
@PostMapping("/serverCreate") @PostMapping("/serverCreate")
public R serverCreate(@RequestBody @Validated ServerInfoVO serverInfoVO) { public R serverCreate(@RequestBody @Validated ServerInfoVO serverInfoVO) {
@@ -104,7 +97,7 @@ public class ServerController {
* server 1______n app * server 1______n app
* */ * */
// get /*// get
@GetMapping("/appGetAll") @GetMapping("/appGetAll")
public R<List<AppInfoVO>> appGetAll(@RequestParam(value = "serverId", required = true) Long serverId) { public R<List<AppInfoVO>> appGetAll(@RequestParam(value = "serverId", required = true) Long serverId) {
@@ -142,7 +135,7 @@ public class ServerController {
return R.failed("delete app unsuccessful"); return R.failed("delete app unsuccessful");
} }*/
// modify -- just modify the appInfo is ok // modify -- just modify the appInfo is ok
@@ -155,7 +148,7 @@ public class ServerController {
* */ * */
// read // read
@GetMapping("domainGetAll") /*@GetMapping("domainGetAll")
public R<List<DomainInfoVO>> getAll(@RequestParam(value = "serverId") Long serverId) { public R<List<DomainInfoVO>> getAll(@RequestParam(value = "serverId") Long serverId) {
return R.ok(coreServerService.domainGetAll(serverId)); return R.ok(coreServerService.domainGetAll(serverId));
@@ -221,6 +214,6 @@ public class ServerController {
} }
return R.failed("delete domain failed !"); return R.failed("delete domain failed !");
} }*/
} }

View File

@@ -5,7 +5,7 @@ import io.wdd.server.beans.po.AppInfoPO;
import io.wdd.server.beans.vo.AppInfoVO; import io.wdd.server.beans.vo.AppInfoVO;
import io.wdd.server.coreService.CoreAppService; import io.wdd.server.coreService.CoreAppService;
import io.wdd.server.service.AppInfoService; 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.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -27,7 +27,10 @@ public class CoreAppServiceImpl implements CoreAppService {
.eq(StringUtils.isNoneEmpty(appName), AppInfoPO::getAppName, appName) .eq(StringUtils.isNoneEmpty(appName), AppInfoPO::getAppName, appName)
.list(); .list();
List<AppInfoVO> appInfoPVOList = EntityUtils.cvToTarget(appInfoPOList, AppInfoVO.class); List<AppInfoVO> appInfoPVOList = EntityConvertUtils.cvToTarget(
appInfoPOList,
AppInfoVO.class
);
return appInfoPVOList.get(0); return appInfoPVOList.get(0);
} }
@@ -35,7 +38,10 @@ public class CoreAppServiceImpl implements CoreAppService {
@Override @Override
public List<AppInfoVO> appGetAll() { public List<AppInfoVO> appGetAll() {
return EntityUtils.cvToTarget(appInfoService.list(), AppInfoVO.class); return EntityConvertUtils.cvToTarget(
appInfoService.list(),
AppInfoVO.class
);
} }
@Override @Override
@@ -43,7 +49,10 @@ public class CoreAppServiceImpl implements CoreAppService {
return appInfoService.save( 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) { public boolean updateAppInfo(AppInfoVO appInfoVO) {
return appInfoService.updateById( return appInfoService.updateById(
EntityUtils.cvToTarget(appInfoVO,AppInfoPO.class) EntityConvertUtils.cvToTarget(
appInfoVO,
AppInfoPO.class
)
); );
} }

View File

@@ -1,12 +1,12 @@
package io.wdd.server.coreService.impl; package io.wdd.server.coreService.impl;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; 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.po.DomainInfoPO;
import io.wdd.server.beans.vo.DomainInfoVO; import io.wdd.server.beans.vo.DomainInfoVO;
import io.wdd.server.coreService.CoreDomainService; import io.wdd.server.coreService.CoreDomainService;
import io.wdd.common.handler.MyRuntimeException;
import io.wdd.server.service.DomainInfoService; 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.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -23,7 +23,10 @@ public class CoreDomainServiceImpl implements CoreDomainService {
@Override @Override
public List<DomainInfoVO> getAll() { public List<DomainInfoVO> getAll() {
return EntityUtils.cvToTarget(domainInfoService.list(), DomainInfoVO.class); return EntityConvertUtils.cvToTarget(
domainInfoService.list(),
DomainInfoVO.class
);
} }
@Override @Override
@@ -41,13 +44,19 @@ public class CoreDomainServiceImpl implements CoreDomainService {
Assert.notEmpty(domainInfoPOList,"no such domain !"); Assert.notEmpty(domainInfoPOList,"no such domain !");
return EntityUtils.cvToTarget(domainInfoPOList, DomainInfoVO.class); return EntityConvertUtils.cvToTarget(
domainInfoPOList,
DomainInfoVO.class
);
} }
@Override @Override
public boolean create(DomainInfoVO domainInfoVO) { public boolean create(DomainInfoVO domainInfoVO) {
return domainInfoService.save(EntityUtils.cvToTarget(domainInfoVO, DomainInfoPO.class)); return domainInfoService.save(EntityConvertUtils.cvToTarget(
domainInfoVO,
DomainInfoPO.class
));
} }
@Override @Override

View File

@@ -11,7 +11,7 @@ import io.wdd.server.beans.vo.DomainInfoVO;
import io.wdd.server.beans.vo.ServerInfoVO; import io.wdd.server.beans.vo.ServerInfoVO;
import io.wdd.server.coreService.CoreServerService; import io.wdd.server.coreService.CoreServerService;
import io.wdd.server.service.*; import io.wdd.server.service.*;
import io.wdd.server.utils.EntityUtils; import io.wdd.server.utils.EntityConvertUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -149,7 +149,7 @@ public class CoreServerServiceImpl implements CoreServerService {
public boolean serverCreate(ServerInfoVO serverInfoVO) { public boolean serverCreate(ServerInfoVO serverInfoVO) {
// BeanUtils.copyProperties(serverInfoVO, serverInfoPO); // BeanUtils.copyProperties(serverInfoVO, serverInfoPO);
ServerInfoPO serverInfoPO = EntityUtils.cvToTarget( ServerInfoPO serverInfoPO = EntityConvertUtils.cvToTarget(
serverInfoVO, serverInfoVO,
ServerInfoPO.class ServerInfoPO.class
); );
@@ -179,7 +179,7 @@ public class CoreServerServiceImpl implements CoreServerService {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} else { } else {
po = EntityUtils.cvToTarget( po = EntityConvertUtils.cvToTarget(
serverInfoVO, serverInfoVO,
ServerInfoPO.class ServerInfoPO.class
); );
@@ -256,7 +256,7 @@ public class CoreServerServiceImpl implements CoreServerService {
.collect(Collectors.toList())); .collect(Collectors.toList()));
return EntityUtils.cvToTarget( return EntityConvertUtils.cvToTarget(
appInfoPOList, appInfoPOList,
AppInfoVO.class AppInfoVO.class
); );
@@ -272,7 +272,7 @@ public class CoreServerServiceImpl implements CoreServerService {
); );
// 1- save appInfo itself // 1- save appInfo itself
AppInfoPO appInfoPO = EntityUtils.cvToTarget( AppInfoPO appInfoPO = EntityConvertUtils.cvToTarget(
appInfoVO, appInfoVO,
AppInfoPO.class AppInfoPO.class
); );
@@ -285,7 +285,7 @@ public class CoreServerServiceImpl implements CoreServerService {
serverAppRelationService.save(relationPO); serverAppRelationService.save(relationPO);
return EntityUtils.cvToTarget( return EntityConvertUtils.cvToTarget(
appInfoPO, appInfoPO,
AppInfoVO.class AppInfoVO.class
); );
@@ -368,7 +368,7 @@ public class CoreServerServiceImpl implements CoreServerService {
.collect(Collectors.toList())); .collect(Collectors.toList()));
return EntityUtils.cvToTarget( return EntityConvertUtils.cvToTarget(
domainInfoPOList, domainInfoPOList,
DomainInfoVO.class DomainInfoVO.class
); );
@@ -416,7 +416,7 @@ public class CoreServerServiceImpl implements CoreServerService {
.collect(Collectors.toList()); .collect(Collectors.toList());
return EntityUtils.cvToTarget( return EntityConvertUtils.cvToTarget(
domainInfoPOList, domainInfoPOList,
DomainInfoVO.class DomainInfoVO.class
); );

View File

@@ -11,16 +11,21 @@ import java.util.stream.Collectors;
@Component @Component
@Slf4j(topic = "Bean Type Convert") @Slf4j(topic = "Bean Type Convert")
public class EntityUtils<T, S> { public class EntityConvertUtils<T, S> {
public static <T, S> T cvToTarget(S source, Class<T> clazz) { public static <T, S> T cvToTarget(S source, Class<T> clazz) {
T t = null; T t = null;
try { try {
t = clazz.getDeclaredConstructor().newInstance(); t = clazz
.getDeclaredConstructor()
.newInstance();
BeanUtils.copyProperties(source, t); BeanUtils.copyProperties(
source,
t
);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | } catch (InstantiationException | IllegalAccessException | InvocationTargetException |

View File

@@ -1,4 +1,4 @@
package io.wdd.server.handler; package io.wdd.server.utils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

View File

@@ -1,6 +0,0 @@
package io.wdd.server.utils;
public class MyBatisPlusGenerator {
}

View File

@@ -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<Page<ServerInfoVO>> 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<Page<ServerInfoVO>> 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()
);
}
}