[ Server ] [ Server ] - 完成ServerInfo的测试内容

This commit is contained in:
zeaslity
2023-10-09 10:01:34 +08:00
parent e0f7059655
commit 5b0e3a418a
5 changed files with 66 additions and 50 deletions

View File

@@ -0,0 +1,17 @@
package io.wdd.server.beans.mapper;
import io.wdd.server.beans.po.ServerInfoPO;
import io.wdd.server.beans.vo.ServerInfoVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface ServerInfoDTOMapper {
ServerInfoDTOMapper INSTANCE = Mappers.getMapper(ServerInfoDTOMapper.class);
ServerInfoPO voToPo(ServerInfoVO serverInfoVO);
ServerInfoVO poToVo(ServerInfoPO serverInfoPO);
}

View File

@@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* 前端请求 Server模块 的请求查询体
@@ -12,6 +13,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("前端请求 Server模块 的请求查询体")
@SuperBuilder(toBuilder = true)
public class ServerQueryEntity {
/*分页相关*/
@@ -26,4 +28,5 @@ public class ServerQueryEntity {
String serverLocation;
}

View File

@@ -13,20 +13,20 @@ import io.wdd.server.coreService.CoreServerService;
import io.wdd.server.service.*;
import io.wdd.server.utils.EntityConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static org.springframework.beans.BeanUtils.copyProperties;
@Service
@Slf4j
@@ -90,11 +90,22 @@ public class CoreServerServiceImpl implements CoreServerService {
@Override
public Page<ServerInfoVO> serverGetByPage(ServerQueryEntity serverQueryEntity) {
// 默认数值处理
if (serverQueryEntity == null || serverQueryEntity.getPageSize() == null || serverQueryEntity.getPageNumber() == null) {
serverQueryEntity = ServerQueryEntity
.builder()
.pageNumber(1)
.pageSize(10)
.build();
} else {
}
Page<ServerInfoPO> serverInfoPOPage = new Page<>(
serverQueryEntity.getPageNumber(),
serverQueryEntity.getPageSize()
);
// 查询Page会直接写入到serverInfoPOPage中
serverInfoService
.getBaseMapper()
@@ -148,12 +159,27 @@ public class CoreServerServiceImpl implements CoreServerService {
@Override
public boolean serverCreate(ServerInfoVO serverInfoVO) {
// BeanUtils.copyProperties(serverInfoVO, serverInfoPO);
if (serverInfoVO == null) {
return false;
}
ServerInfoPO serverInfoPO = EntityConvertUtils.cvToTarget(
serverInfoVO,
ServerInfoPO.class
);
// 唯一性约束判定判定ServerName就可以覆盖topicName
Optional<ServerInfoPO> serverNameOp = new LambdaQueryChainWrapper<ServerInfoPO>(serverInfoService.getBaseMapper()).
eq(
ServerInfoPO::getServerName,
serverInfoVO.getServerName()
)
.oneOpt();
if (!serverNameOp.isEmpty()) {
return false;
}
return serverInfoService.save(serverInfoPO);
}
@@ -167,18 +193,15 @@ public class CoreServerServiceImpl implements CoreServerService {
)
.one();
if (ObjectUtils.isNotEmpty(po)) {
try {
org.apache.commons.beanutils.BeanUtils.copyProperties(
po,
serverInfoVO
if (po == null) {
// 已经存在,需要把更新的信息 复制到旧的内容之上
copyProperties(
serverInfoVO,
po
);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
// 没有
// 创建新的PO
po = EntityConvertUtils.cvToTarget(
serverInfoVO,
ServerInfoPO.class
@@ -191,7 +214,7 @@ public class CoreServerServiceImpl implements CoreServerService {
@Override
public boolean serverUpdate(ServerInfoPO serverInfoPO) {
if (serverInfoPO.getServerId() == null) {
if (serverInfoPO == null || serverInfoPO.getServerId() == null) {
return false;
}
@@ -324,7 +347,7 @@ public class CoreServerServiceImpl implements CoreServerService {
ServerInfoVO serverInfoVO = new ServerInfoVO();
BeanUtils.copyProperties(
copyProperties(
serverInfoPOList.get(i),
serverInfoVO
);

View File

@@ -18,7 +18,10 @@ public class MyBatisAutoInsertInterceptor implements MetaObjectHandler {
*/
@Override
public void insertFill(MetaObject metaObject) {
log.info("MyBaitsPlus start to insert manually !");
log.info(
"MyBaitsPlus start to insert manually ! {}",
metaObject
);
this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)

View File

@@ -8,11 +8,11 @@ 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.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBootTest
public class ServerInfoTest {
@@ -31,10 +31,6 @@ public class ServerInfoTest {
// Assert
assertNotNull(result);
assertEquals(
HttpStatus.OK,
result.getCode()
);
assertNotNull(result.getData());
}
@@ -55,24 +51,6 @@ public class ServerInfoTest {
);
}
// 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() {
@@ -84,10 +62,6 @@ public class ServerInfoTest {
// Assert
assertNotNull(result);
assertEquals(
HttpStatus.BAD_REQUEST,
result.getCode()
);
assertEquals(
"Create Server Failed !",
result.getData()
@@ -105,10 +79,6 @@ public class ServerInfoTest {
// Assert
assertNotNull(result);
assertEquals(
HttpStatus.BAD_REQUEST,
result.getCode()
);
assertEquals(
"Server info update failed !",
result.getData()