REM:
1. 修改Manager层增加设备和修改设备方法
2. 编写manager层更新设备和改变设备状态的单元测试
This commit is contained in:
chenlinghy 2020-08-06 16:13:11 +08:00
parent f288e01197
commit c67a2ab9e9
2 changed files with 106 additions and 9 deletions

View File

@ -53,10 +53,10 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
if (tDev != null) { if (tDev != null) {
// 如果设备是删除状态则更新设备信息 // 如果设备是删除状态则更新设备信息
if (dev.getStatus() == ObjectStatus.DELETED) { if (tDev.getStatus() == ObjectStatus.DELETED) {
// 重置状态 // 重置状态
dev.setStatus(ObjectStatus.NORMAL); tDev.setStatus(ObjectStatus.NORMAL);
return upgradeDisposeDevice(dev); return upgradeDisposeDevice(tDev);
} }
return new MulReturnType<>(ErrorCode.ERR_DEVICEEXISTS, tDev.getId()); return new MulReturnType<>(ErrorCode.ERR_DEVICEEXISTS, tDev.getId());
@ -147,10 +147,13 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
// 更新设备数据库 // 更新设备数据库
disposeDeviceMapper.upgradeDisposeDevice(tDev); disposeDeviceMapper.upgradeDisposeDevice(tDev);
// 能力数据库中旧的能力信息 // 能力数据库中旧的能力信息
disposeCapacityMapper.delDeviceDisposeCapacity(tDev.getId()); disposeCapacityMapper.delDeviceDisposeCapacity(tDev.getId());
// 新增能力信息完成更新 // 新增能力信息完成更新
disposeCapacityMapper.addNewDisposeCapacity(tDev.getDevCapacity()); if(tDev.getDevCapacity().size() != 0){
disposeCapacityMapper.addNewDisposeCapacity(tDev.getDevCapacity());
}
return new MulReturnType<>(ErrorCode.ERR_OK, tDev.getId()); return new MulReturnType<>(ErrorCode.ERR_OK, tDev.getId());
} }

View File

@ -1,14 +1,19 @@
package com.dispose.test.manager; package com.dispose.test.manager;
import com.dispose.common.DisposeDeviceType; import com.dispose.common.DisposeDeviceType;
import com.dispose.common.ErrorCode;
import com.dispose.common.HttpType; import com.dispose.common.HttpType;
import com.dispose.common.ObjectStatus; import com.dispose.common.ObjectStatus;
import com.dispose.manager.DisposeDeviceManager;
import com.dispose.mapper.DisposeDeviceMapper; import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.entity.DisposeDevice;
import com.dispose.pojo.po.MulReturnType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -19,6 +24,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
/** /**
@ -45,14 +51,20 @@ public class DisposeDeviceManagerTest {
@Resource @Resource
private DisposeDeviceMapper disposeDeviceMapper; private DisposeDeviceMapper disposeDeviceMapper;
/**
* The Dispose device manager.
*/
@Resource
private DisposeDeviceManager disposeDeviceManager;
/** /**
* A 1 add dispose device. * A 1 add dispose device.
*/ */
@Test @Test
public void a1_addDisposeDevice() { public void a1_addDisposeDevice() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
for(int i = 1; i < 256; i++) { for (int i = 1; i < 3; i++) {
String ipAddr = "192.168.0." + String.valueOf(i); String ipAddr = "192.168.0." + i;
DisposeDevice dev = DisposeDevice.builder() DisposeDevice dev = DisposeDevice.builder()
.ipAddr(ipAddr) .ipAddr(ipAddr)
@ -71,7 +83,20 @@ public class DisposeDeviceManagerTest {
.status(ObjectStatus.NORMAL) .status(ObjectStatus.NORMAL)
.build(); .build();
disposeDeviceMapper.addNewDisposeDevice(dev); MulReturnType<ErrorCode, Long> ret = disposeDeviceManager.addDisposeDevice(dev);
DisposeDevice device = disposeDeviceMapper.getDeviceByAddress(dev.getIpAddr(), dev.getIpPort());
if (ret.getFirstParam() == ErrorCode.ERR_OK) {
Assert.assertNotNull(device);
Assert.assertNotNull(device.getId());
} else if (ret.getFirstParam() == ErrorCode.ERR_DEVICEEXISTS) {
Assert.assertNotNull(device);
Assert.assertNotNull(device.getId());
Assert.assertEquals(device.getStatus(), ObjectStatus.NORMAL);
} else {
Assert.assertNull(device);
}
} }
} }
@ -91,4 +116,73 @@ public class DisposeDeviceManagerTest {
log.debug("Page Info: total {}, page {}", pageInfo.getTotal(), pageInfo.getPageNum()); log.debug("Page Info: total {}, page {}", pageInfo.getTotal(), pageInfo.getPageNum());
} }
/**
* A 2 upgrade dispose device.
*/
@Test
public void a2_upgradeDisposeDevice() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, JsonProcessingException {
for (int i = 1; i < 3; i++) {
String ipAddr = "192.168.0." + i;
DisposeDevice dev = DisposeDevice.builder()
.ipAddr(ipAddr)
.ipPort("")
.deviceType(DisposeDeviceType.DPTECH_UMC)
.areaCode(0)
.deviceName("中移杭研实验室迪普清洗设备")
.manufacturer("DPTech")
.model("UMC")
.version("5.7.13")
.userName("admin")
.password("UMCAdministrator")
.urlPath("UMC/service/AbnormalFlowCleaningService")
.urlType(HttpType.HTTP)
.readme("实验室测试设备")
.status(ObjectStatus.NORMAL)
.build();
disposeDeviceManager.addDisposeDevice(dev);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev));
dev.setDeviceName("测试清洗设备");
dev.setManufacturer("Unknown");
MulReturnType<ErrorCode, Long> ret = disposeDeviceManager.upgradeDisposeDevice(dev);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev));
DisposeDevice upgradeDev = disposeDeviceMapper.getDeviceByAddress(dev.getIpAddr(), dev.getIpPort());
if (ret.getFirstParam() == ErrorCode.ERR_OK) {
Assert.assertNotNull(upgradeDev);
Assert.assertEquals(upgradeDev.getDeviceName(), "测试清洗设备");
Assert.assertEquals(upgradeDev.getManufacturer(), "Unknown");
} else {
Assert.assertNull(upgradeDev);
}
}
}
/**
* A 3 change dispose device status.
*/
@Test
public void a3_changeDisposeDeviceStatus() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, JsonProcessingException {
disposeDeviceMapper.selectAll().forEach(v -> {
for (ObjectStatus obj : ObjectStatus.values()
) {
MulReturnType<ErrorCode, ObjectStatus> ret = disposeDeviceManager.changeDisposeDeviceStatus(v.getId(), obj);
DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(), v.getIpPort());
if (ret.getFirstParam() == ErrorCode.ERR_OK) {
Assert.assertNotNull(dev);
Assert.assertEquals(dev.getStatus(), obj);
}
}
});
}
} }