[ Server ] [ Server ] - 新增ServerInfo的测试内容
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -77,7 +77,6 @@ public class AcceptAgentInitInfo {
|
||||
@Resource
|
||||
DaemonDatabaseOperator databaseOperator;
|
||||
|
||||
|
||||
@Resource
|
||||
ObjectMapper objectMapper;
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
// }
|
||||
//
|
||||
//}
|
||||
@@ -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<List> serverGetAll() {
|
||||
public R<List<ServerInfoVO>> 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<List> 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<List<AppInfoVO>> 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<List<DomainInfoVO>> 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 !");
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
@@ -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<AppInfoVO> appInfoPVOList = EntityUtils.cvToTarget(appInfoPOList, AppInfoVO.class);
|
||||
List<AppInfoVO> appInfoPVOList = EntityConvertUtils.cvToTarget(
|
||||
appInfoPOList,
|
||||
AppInfoVO.class
|
||||
);
|
||||
|
||||
return appInfoPVOList.get(0);
|
||||
}
|
||||
@@ -35,7 +38,10 @@ public class CoreAppServiceImpl implements CoreAppService {
|
||||
@Override
|
||||
public List<AppInfoVO> 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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<DomainInfoVO> 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
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
@@ -11,16 +11,21 @@ import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@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) {
|
||||
|
||||
T t = null;
|
||||
|
||||
try {
|
||||
t = clazz.getDeclaredConstructor().newInstance();
|
||||
t = clazz
|
||||
.getDeclaredConstructor()
|
||||
.newInstance();
|
||||
|
||||
BeanUtils.copyProperties(source, t);
|
||||
BeanUtils.copyProperties(
|
||||
source,
|
||||
t
|
||||
);
|
||||
|
||||
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||
@@ -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;
|
||||
@@ -1,6 +0,0 @@
|
||||
package io.wdd.server.utils;
|
||||
|
||||
public class MyBatisPlusGenerator {
|
||||
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user