diff --git a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java index 6a10d15d..f85636cf 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java @@ -56,10 +56,10 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { if (tDev != null) { // 如果设备是删除状态,则更新设备信息 - if (dev.getStatus() == ObjectStatus.DELETED) { + if (tDev.getStatus() == ObjectStatus.DELETED) { // 重置状态 - dev.setStatus(ObjectStatus.NORMAL); - return upgradeDisposeDevice(dev); + tDev.setStatus(ObjectStatus.NORMAL); + return upgradeDisposeDevice(tDev); } return new MulReturnType<>(ErrorCode.ERR_DEVICEEXISTS, tDev.getId()); @@ -150,10 +150,13 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { // 更新设备数据库 disposeDeviceMapper.upgradeDisposeDevice(tDev); - // 清楚能力数据库中旧的能力信息 + // 清除能力数据库中旧的能力信息 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()); } @@ -195,7 +198,7 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { if (ret != 1) { return new MulReturnType<>(ErrorCode.ERR_DATABASE, dev.getStatus()); } else { - return new MulReturnType<>(ErrorCode.ERR_OK, dev.getStatus()); + return new MulReturnType<>(ErrorCode.ERR_OK, disposeDeviceMapper.getDeviceById(id).getStatus()); } } } diff --git a/src/test/java/com/dispose/test/manager/DisposeDeviceManagerTest.java b/src/test/java/com/dispose/test/manager/DisposeDeviceManagerTest.java index 1dbacb12..8cbae645 100644 --- a/src/test/java/com/dispose/test/manager/DisposeDeviceManagerTest.java +++ b/src/test/java/com/dispose/test/manager/DisposeDeviceManagerTest.java @@ -1,14 +1,19 @@ package com.dispose.test.manager; import com.dispose.common.DisposeDeviceType; +import com.dispose.common.ErrorCode; import com.dispose.common.HttpType; import com.dispose.common.ObjectStatus; +import com.dispose.manager.DisposeDeviceManager; import com.dispose.mapper.DisposeDeviceMapper; 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.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; @@ -19,6 +24,7 @@ import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.lang.reflect.InvocationTargetException; import java.util.List; /** @@ -45,14 +51,20 @@ public class DisposeDeviceManagerTest { @Resource private DisposeDeviceMapper disposeDeviceMapper; + /** + * The Dispose device manager. + */ + @Resource + private DisposeDeviceManager disposeDeviceManager; + /** * A 1 add dispose device. */ @Test - public void a1_addDisposeDevice() { + public void a1_addDisposeDevice() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { - for(int i = 1; i < 256; i++) { - String ipAddr = "192.168.0." + String.valueOf(i); + for (int i = 1; i < 3; i++) { + String ipAddr = "192.168.0." + i; DisposeDevice dev = DisposeDevice.builder() .ipAddr(ipAddr) @@ -71,7 +83,20 @@ public class DisposeDeviceManagerTest { .status(ObjectStatus.NORMAL) .build(); - disposeDeviceMapper.addNewDisposeDevice(dev); + MulReturnType 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()); } + /** + * 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 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 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); + } + } + }); + } } diff --git a/src/test/java/com/dispose/test/service/DisposeDeviceManagerServiceTest.java b/src/test/java/com/dispose/test/service/DisposeDeviceManagerServiceTest.java new file mode 100644 index 00000000..d8bc231c --- /dev/null +++ b/src/test/java/com/dispose/test/service/DisposeDeviceManagerServiceTest.java @@ -0,0 +1,219 @@ +package com.dispose.test.service; + +import com.dispose.common.*; +import com.dispose.pojo.entity.DisposeDevice; +import com.dispose.pojo.po.MulReturnType; +import com.dispose.service.DisposeDeviceManagerService; +import com.dispose.service.UserAccountService; +import com.dispose.test.Global.InitTestEnvironment; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; + +/** + * The type User account service test. + * + * @author + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Transactional +@Rollback +public class DisposeDeviceManagerServiceTest extends InitTestEnvironment { + /** + * The constant token. + */ + private static String token = ""; + /** + * The User account service. + */ + @Resource + private UserAccountService userAccountService; + /** + * The dispose device manager service. + */ + @Resource + private DisposeDeviceManagerService disposeDeviceManagerService; + /** + * The Object mapper. + */ + @Resource + private ObjectMapper objectMapper; + /** + * User login test. + * + * @throws NoSuchAlgorithmException the no such algorithm exception + */ + @Before + public void userLoginTest() throws NoSuchAlgorithmException { + MulReturnType ret = userAccountService.loginService(getUSER_NAME(), + getPASSWORD()); + + if (ret.getFirstParam() == ErrorCode.ERR_OK) { + DisposeDeviceManagerServiceTest.token = ret.getSecondParam(); + } + } + + /** + * A 1 Add dispose device list. + */ + @Test + public void a1_addDisposeDeviceTest() { + List devList = new ArrayList<>(); + + 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(); + + devList.add(dev); + } + + List> ret = disposeDeviceManagerService.addDisposeDevice(devList); + + ret.forEach(v->{ + if (v.getFirstParam() == ErrorCode.ERR_OK) { + Assert.assertNotNull(v.getSecondParam()); + Assert.assertNotNull(v.getSecondParam().getId()); + } else if (v.getFirstParam() == ErrorCode.ERR_DEVICEEXISTS) { + Assert.assertNotNull(v.getSecondParam()); + Assert.assertNotNull(v.getSecondParam().getId()); + Assert.assertEquals(v.getSecondParam().getStatus(), ObjectStatus.NORMAL); + } else { + Assert.assertNull(v.getSecondParam()); + } + }); + } + + /** + * A 2 Add dispose device list + */ + @Test + public void a2_upgradeDisposeDeviceTest() throws JsonProcessingException { + List devList = new ArrayList<>(); + + 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(); + + devList.add(dev); + } + + disposeDeviceManagerService.addDisposeDevice(devList); + log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(devList)); + + devList.forEach(dev->{ + dev.setDeviceName("测试清洗设备"); + dev.setManufacturer("Unknown"); + }); + + List> ret = disposeDeviceManagerService.upgradeDisposeDevice(devList); + + ret.forEach(v->{ + if (v.getFirstParam() == ErrorCode.ERR_OK) { + Assert.assertNotNull(v.getSecondParam()); + Assert.assertEquals(v.getSecondParam().getDeviceName(), "测试清洗设备"); + Assert.assertEquals(v.getSecondParam().getManufacturer(), "Unknown"); + } else { + Assert.assertNull(v.getSecondParam()); + } + }); + } + + /** + * A 3 remove dispose device test. + */ + @Test + public void a3_removeDisposeDeviceTest() throws JsonProcessingException { + List devList = new ArrayList<>(); + List removeIds = new ArrayList<>(); + + 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(); + + devList.add(dev); + } + + disposeDeviceManagerService.addDisposeDevice(devList); + log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(devList)); + + devList.forEach(dev-> removeIds.add(dev.getId())); + + List> ret = disposeDeviceManagerService.removeDisposeDevice(removeIds); + + ret.forEach(v->{ + if (v.getFirstParam() == ErrorCode.ERR_OK) { + Assert.assertNotNull(v.getSecondParam()); + Assert.assertEquals(v.getSecondParam().getStatus(), ObjectStatus.DELETED); + } else { + Assert.assertNotNull(v.getSecondParam()); + Assert.assertNotEquals(v.getSecondParam().getStatus(), ObjectStatus.DELETED); + } + }); + } +}