diff --git a/assembly.xml b/assembly.xml index 7ff9a0f4..6160f3ca 100644 --- a/assembly.xml +++ b/assembly.xml @@ -11,7 +11,7 @@ ${project.build.directory} ${file.separator} - phoenix-boot.jar + phoenix_ddos_handle.jar diff --git a/config/application.properties b/config/application.properties index 07f154c2..b2d2a5a5 100644 --- a/config/application.properties +++ b/config/application.properties @@ -1 +1 @@ -spring.profiles.active=local \ No newline at end of file +spring.profiles.active=test \ No newline at end of file diff --git a/pom.xml b/pom.xml index 75685c4e..a6b6e257 100644 --- a/pom.xml +++ b/pom.xml @@ -10,9 +10,9 @@ com.dispose - phoenix-boot + phoenix_ddos_handle 1.0.0 - phoenix-boot + phoenix_ddos_handle Demo project for Spring Boot UTF-8 @@ -212,7 +212,7 @@ - phoenix-boot + phoenix_ddos_handle org.apache.maven.plugins diff --git a/src/main/java/com/dispose/manager/DisposeDeviceManager.java b/src/main/java/com/dispose/manager/DisposeDeviceManager.java new file mode 100644 index 00000000..63551c0e --- /dev/null +++ b/src/main/java/com/dispose/manager/DisposeDeviceManager.java @@ -0,0 +1,54 @@ +package com.dispose.manager; + +import java.util.List; + +import com.dispose.pojo.entity.DisposeDevice; + +/** + * + * + * @author phoenix + * @date 2020年4月27日 + */ +public interface DisposeDeviceManager { + + /** + * + * @Description: TODO(这里用一句话描述这个方法的作用) + * @param dd + * @return + */ + int saveDisposeDevice(DisposeDevice dd); + + /** + * + * @Description: TODO(这里用一句话描述这个方法的作用) + * @param ip + * @return + */ + DisposeDevice findDisposeDeviceByIp(String ip); + + /** + * + * @Description: TODO(这里用一句话描述这个方法的作用) + * @param id + * @return + */ + DisposeDevice findDisposeDeviceById(Long id); + + /** + * + * @Description: TODO(这里用一句话描述这个方法的作用) + * @return + */ + List findAllDisposeDevice(); + + /** + * + * @Description: TODO(这里用一句话描述这个方法的作用) + * @param ip + * @return + */ + int delDisposeDeviceByIp(String ip); + +} diff --git a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java new file mode 100644 index 00000000..beb7f500 --- /dev/null +++ b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java @@ -0,0 +1,73 @@ +package com.dispose.manager.impl; + +import java.util.List; + +import javax.annotation.Resource; + +import org.springframework.stereotype.Component; + +import com.dispose.manager.DisposeDeviceManager; +import com.dispose.mapper.DisposeDeviceMapper; +import com.dispose.pojo.entity.DisposeDevice; + +import cn.hutool.core.collection.CollectionUtil; +import lombok.extern.slf4j.Slf4j; +import tk.mybatis.mapper.entity.Example; +import tk.mybatis.mapper.entity.Example.Criteria; + +/** + * + * + * @author phoenix + * @date 2020年4月27日 + */ +@Component +@Slf4j +public class DisposeDeviceManagerImpl implements DisposeDeviceManager { + + @Resource + private DisposeDeviceMapper disposeDeviceMapper; + + @Override + public int saveDisposeDevice(DisposeDevice dd) { + // TODO Auto-generated method stub + return disposeDeviceMapper.insert(dd); + } + + @Override + public DisposeDevice findDisposeDeviceByIp(String ip) { + // TODO Auto-generated method stub + log.info("根据IP {} 查询处置设备信息", ip); + Example example = new Example(DisposeDevice.class); + Criteria c = example.createCriteria(); + c.andEqualTo("ipAddr", ip); + List list = disposeDeviceMapper.selectByExample(example); + if (CollectionUtil.isNotEmpty(list)) { + return list.get(0); + } + return null; + } + + @Override + public DisposeDevice findDisposeDeviceById(Long id) { + // TODO Auto-generated method stub + return disposeDeviceMapper.selectByPrimaryKey(id); + } + + @Override + public int delDisposeDeviceByIp(String ip) { + // TODO Auto-generated method stub + log.info("根据IP {} 删除处置设备信息", ip); + Example example = new Example(DisposeDevice.class); + Criteria c = example.createCriteria(); + c.andEqualTo("ipAddr", ip); + return disposeDeviceMapper.deleteByExample(example); + } + + @Override + public List findAllDisposeDevice() { + // TODO Auto-generated method stub + return disposeDeviceMapper.selectAll(); + } + +} diff --git a/src/main/java/com/dispose/mapper/DisposeDeviceMapper.java b/src/main/java/com/dispose/mapper/DisposeDeviceMapper.java index 23376831..704c2375 100644 --- a/src/main/java/com/dispose/mapper/DisposeDeviceMapper.java +++ b/src/main/java/com/dispose/mapper/DisposeDeviceMapper.java @@ -8,49 +8,5 @@ import tk.mybatis.mapper.common.MySqlMapper; * The interface Dispose device mapper. */ public interface DisposeDeviceMapper extends Mapper, MySqlMapper { - /** - * Add new dispose device. - * - * @param dev the dev - */ - void addNewDisposeDevice(DisposeDevice dev); - /** - * Is device exists by ip int. - * - * @param ipAddr the ip addr - * @return the int - */ - int isDeviceExistsByIp(String ipAddr); - - /** - * Is device exists by id int. - * - * @param id the id - * @return the int - */ - int isDeviceExistsById(Long id); - - /** - * Gets device by ip. - * - * @param ipAddr the ip addr - * @return the device by ip - */ - DisposeDevice getDeviceByIp(String ipAddr); - - /** - * Gets device by id. - * - * @param id the id - * @return the device by id - */ - DisposeDevice getDeviceById(Long id); - - /** - * Del dispose device by ip. - * - * @param ipAddr the ip addr - */ - void delDisposeDeviceByIp(String ipAddr); } diff --git a/src/main/java/com/dispose/pojo/entity/DisposeDevice.java b/src/main/java/com/dispose/pojo/entity/DisposeDevice.java index 9ebe30da..570f980d 100644 --- a/src/main/java/com/dispose/pojo/entity/DisposeDevice.java +++ b/src/main/java/com/dispose/pojo/entity/DisposeDevice.java @@ -1,19 +1,23 @@ package com.dispose.pojo.entity; +import java.io.Serializable; +import java.util.List; + +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Transient; + import com.dispose.dispose.po.DeviceInfo; import com.dispose.pojo.po.DisposeDeviceCapacity; import com.fasterxml.jackson.annotation.JsonInclude; -import java.io.Serializable; -import java.util.List; -import javax.persistence.Id; -import javax.persistence.Table; -import javax.persistence.Transient; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; +import tk.mybatis.mapper.annotation.KeySql; import tk.mybatis.mapper.annotation.NameStyle; import tk.mybatis.mapper.code.Style; @@ -39,7 +43,7 @@ public class DisposeDevice implements Serializable { * The Id. */ @Id - //@KeySql(useGeneratedKeys = true) + @KeySql(useGeneratedKeys = true) private Long id; /** diff --git a/src/main/java/com/dispose/service/DisposeNodeManager.java b/src/main/java/com/dispose/service/DisposeNodeManager.java index 84d84676..83c0b461 100644 --- a/src/main/java/com/dispose/service/DisposeNodeManager.java +++ b/src/main/java/com/dispose/service/DisposeNodeManager.java @@ -70,4 +70,12 @@ public interface DisposeNodeManager { * @return the dispose device */ DisposeDevice getDisposeDevice(String ipAddr, DeviceCapacity capacity); + + /** + * + * @Description: TODO(这里用一句话描述这个方法的作用) + * @param ip + * @return + */ + boolean isExistsDisposeDeviceByIp(String ip); } diff --git a/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java b/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java index 470fa619..159d035d 100644 --- a/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java @@ -5,13 +5,15 @@ import com.dispose.common.ErrorCode; import com.dispose.common.IPAddrType; import com.dispose.dispose.DeviceRouter; import com.dispose.dispose.DisposeEntryManager; -import com.dispose.mapper.DisposeDeviceMapper; +import com.dispose.manager.DisposeDeviceManager; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.po.DisposeDeviceCapacity; import com.dispose.pojo.po.MReturnType; import com.dispose.service.DisposeNodeManager; import com.dispose.service.UserAccountService; import inet.ipaddr.AddressStringException; +import lombok.extern.slf4j.Slf4j; + import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -24,28 +26,35 @@ import org.springframework.stereotype.Service; * The type Dispose node manager. */ @Service +@Slf4j public class DisposeNodeManagerImpl implements DisposeNodeManager { + + @Resource + private DisposeDeviceManager disposeDeviceManager; /** * The Dispose dev map. */ private final ConcurrentHashMap disposeDevMap = new ConcurrentHashMap<>(); - /** - * The Dispose device mapper. - */ - @Resource - private DisposeDeviceMapper disposeDeviceMapper; + /** * The User account service. */ @Resource private UserAccountService userAccountService; + + @Override + public boolean isExistsDisposeDeviceByIp(String ip) { + // TODO Auto-generated method stub + log.info("查询该处置设备是否存在 {}", ip); + return disposeDeviceManager.findDisposeDeviceByIp(ip) != null; + } /** * Load dispose node from db. */ @Override public void loadDisposeNodeFromDB() { - List devList = disposeDeviceMapper.selectAll(); + List devList = disposeDeviceManager.findAllDisposeDevice(); if (devList == null) { return; @@ -73,8 +82,7 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager { */ @Override public ErrorCode delDisposeDeviceById(Long id) { - DisposeEntryManager dp; - DisposeDevice dev = disposeDeviceMapper.getDeviceById(id); + DisposeDevice dev = disposeDeviceManager.findDisposeDeviceById(id); if (dev == null) { return ErrorCode.ERR_NOSUCHDEVICE; @@ -91,9 +99,7 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager { */ @Override public ErrorCode delDisposeDeviceByIp(String ipAddr) { - DisposeEntryManager dp; - - if (disposeDeviceMapper.isDeviceExistsByIp(ipAddr) == 0 + if (!isExistsDisposeDeviceByIp(ipAddr) || !disposeDevMap.containsKey(ipAddr)) { return ErrorCode.ERR_NOSUCHDEVICE; } @@ -104,7 +110,7 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager { return ErrorCode.ERR_NOSUCHDEVICE; } - disposeDeviceMapper.delDisposeDeviceByIp(ipAddr); + disposeDeviceManager.delDisposeDeviceByIp(ipAddr); disposeDevMap.remove(ipAddr); return ErrorCode.ERR_OK; @@ -127,7 +133,7 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager { return new MReturnType<>(ErrorCode.ERR_NOSUCHDEVICE, String.valueOf(-1)); } - DisposeDevice dbDev = disposeDeviceMapper.getDeviceByIp(dev.getIpAddr()); + DisposeDevice dbDev = disposeDeviceManager.findDisposeDeviceByIp(dev.getIpAddr()); if (dbDev != null) { return new MReturnType<>(ErrorCode.ERR_DEVICEEXISTS, String.valueOf(dbDev.getId())); @@ -145,7 +151,7 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager { dev.setDevInfo(dp.getDeviceInfo()); - disposeDeviceMapper.addNewDisposeDevice(dev); + disposeDeviceManager.saveDisposeDevice(dev); disposeDevMap.put(dev.getIpAddr(), dev); return new MReturnType<>(ErrorCode.ERR_OK, dev.getId().toString()); diff --git a/src/main/resources/mappers/DisposeDevice.xml b/src/main/resources/mappers/DisposeDevice.xml deleted file mode 100644 index 84e183a5..00000000 --- a/src/main/resources/mappers/DisposeDevice.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - - - INSERT IGNORE INTO - dispose_device(ipAddr, type, - areaCode, name, manufacturer, - model, version, readme, status) - VALUES - (#{ipAddr}, #{type}, - #{areaCode}, #{name}, #{manufacturer}, - #{model}, #{version}, #{readme}, 0) - - - - - - - - - - DELETE - dispose_device - FROM - dispose_device - WHERE - dispose_device.ipAddr = #{ipAddr} - - - DELETE - dispose_device - FROM - dispose_device - WHERE - dispose_device.id = #{id} - - \ No newline at end of file diff --git a/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java b/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java index 83f332a8..5c33cc57 100644 --- a/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java +++ b/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java @@ -9,7 +9,6 @@ import com.dispose.controller.DeviceNodeManagerControllerTest; import com.dispose.controller.TaskControllerTest; import com.dispose.dptech.DPTechInterfaceTestCase; import com.dispose.manager.UserAccountManagerTest; -import com.dispose.mapper.DisposeDeviceMapperTest; import com.dispose.mapper.DisposeTaskMapperTest; import com.dispose.mapper.UserAccountMapperTest; import com.dispose.service.DisposeNodeManagerTest; @@ -29,7 +28,6 @@ import org.junit.runners.Suite; DPTechInterfaceTestCase.class, UserAccountMapperTest.class, UserAccountManagerTest.class, - DisposeDeviceMapperTest.class, AuthControllerTest.class, DeviceNodeManagerControllerTest.class, DeviceNodeInfoControllerTest.class, diff --git a/src/test/java/com/dispose/manager/DisposeDeviceManagerTest.java b/src/test/java/com/dispose/manager/DisposeDeviceManagerTest.java new file mode 100644 index 00000000..5d9c0e28 --- /dev/null +++ b/src/test/java/com/dispose/manager/DisposeDeviceManagerTest.java @@ -0,0 +1,62 @@ +package com.dispose.manager; + +import java.util.List; + +import javax.annotation.Resource; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.util.ReflectionTestUtils; + +import com.dispose.mapper.DisposeDeviceMapper; +import com.dispose.pojo.entity.DisposeDevice; + +import junit.framework.TestCase; +import lombok.extern.slf4j.Slf4j; + +/** + * + * + * @author phoenix + * @date 2020年4月27日 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +public class DisposeDeviceManagerTest { + + @Resource + private DisposeDeviceMapper disposeDeviceMapper; + + @Resource + private DisposeDeviceManager disposeDeviceManager; + + @Before + public void setUp() { + ReflectionTestUtils.setField(disposeDeviceManager, "disposeDeviceMapper", disposeDeviceMapper); + } + + @Test + public void testDisposeDeviceManager() { + log.info("DisposeDeviceManager 单元测试"); + String ip = "ip18867101080"; + DisposeDevice dd = DisposeDevice.builder().areaCode(1).ipAddr(ip).linkStatus(1).manufacturer("厂商") + .model("model").name("name").readme("readme").status(1).type(1).version("v1").build(); + int res = disposeDeviceManager.saveDisposeDevice(dd); + TestCase.assertEquals(res, 1); + DisposeDevice deviceIp = disposeDeviceManager.findDisposeDeviceByIp(ip); + TestCase.assertNotNull(deviceIp); + DisposeDevice deviceId = disposeDeviceManager.findDisposeDeviceById(dd.getId()); + TestCase.assertNotNull(deviceId); + List deviceList = disposeDeviceManager.findAllDisposeDevice(); + TestCase.assertTrue(deviceList.size() > 0); + int delRes = disposeDeviceManager.delDisposeDeviceByIp(ip); + TestCase.assertEquals(delRes, 1); + deviceId = disposeDeviceManager.findDisposeDeviceById(dd.getId()); + TestCase.assertNull(deviceId); + } + +} diff --git a/src/test/java/com/dispose/mapper/DisposeDeviceMapperTest.java b/src/test/java/com/dispose/mapper/DisposeDeviceMapperTest.java deleted file mode 100644 index c10bb672..00000000 --- a/src/test/java/com/dispose/mapper/DisposeDeviceMapperTest.java +++ /dev/null @@ -1,154 +0,0 @@ -package com.dispose.mapper; - -import com.dispose.Global.InitTestEnvironment; -import com.dispose.common.DisposeDeviceType; -import com.dispose.pojo.entity.DisposeDevice; -import com.dispose.pojo.po.DisposeDeviceCapacity; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Resource; -import lombok.extern.slf4j.Slf4j; -import org.junit.Assert; -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; - -/** - * The type Dispose device mapper test. - */ -@RunWith(SpringRunner.class) -@SpringBootTest -@Slf4j -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -@Transactional -@Rollback -public class DisposeDeviceMapperTest extends InitTestEnvironment { - /** - * The Object mapper. - */ - @Resource - private ObjectMapper objectMapper; - - /** - * The Dispose device mapper. - */ - @Resource - private DisposeDeviceMapper disposeDeviceMapper; - - /** - * T 1 add new dispose device. - * - * @throws JsonProcessingException the json processing exception - */ - @Test - public void t1_addNewDisposeDevice() throws JsonProcessingException { - Long devId = -1L; - DisposeDevice dev = new DisposeDevice(); - List devCaps = new ArrayList<>(); - dev.setId(devId); - dev.setIpAddr("10.88.77.15"); - dev.setType(DisposeDeviceType.DPTECH_UMC.getCode()); - dev.setName("中移杭研实验室清洗设备"); - dev.setManufacturer("DPTech"); - dev.setModel("UMC"); - dev.setVersion("5.7.13"); - dev.setReadme("实验室测试设备"); - dev.setAreaCode(0); - dev.setDevCaps(devCaps); - - disposeDeviceMapper.addNewDisposeDevice(dev); - - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev)); - - Assert.assertNotEquals(disposeDeviceMapper.isDeviceExistsByIp(dev.getIpAddr()), 0); - - List dp = disposeDeviceMapper.selectAll(); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dp)); - } - - /** - * T 2 is device exists by ip. - * - * @throws JsonProcessingException the json processing exception - */ - @Test - public void t2_isDeviceExistsByIp() throws JsonProcessingException { - List dp = disposeDeviceMapper.selectAll(); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dp)); - - dp.forEach(v -> Assert.assertNotEquals(disposeDeviceMapper.isDeviceExistsByIp(v.getIpAddr()), 0)); - } - - /** - * T 3 is device exists by id. - * - * @throws JsonProcessingException the json processing exception - */ - @Test - public void t3_isDeviceExistsById() throws JsonProcessingException { - List dp = disposeDeviceMapper.selectAll(); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dp)); - - dp.forEach(v -> Assert.assertNotEquals(disposeDeviceMapper.isDeviceExistsById(v.getId()), 0)); - } - - /** - * T 4 get device exists by ip. - * - * @throws JsonProcessingException the json processing exception - */ - @Test - public void t4_getDeviceExistsByIp() throws JsonProcessingException { - List dp = disposeDeviceMapper.selectAll(); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dp)); - - dp.forEach(v -> { - DisposeDevice dev = disposeDeviceMapper.getDeviceByIp(v.getIpAddr()); - Assert.assertNotNull(dp); - Assert.assertEquals(dev.getIpAddr(), v.getIpAddr()); - }); - } - - /** - * T 5 get device exists by id. - * - * @throws JsonProcessingException the json processing exception - */ - @Test - public void t5_getDeviceExistsById() throws JsonProcessingException { - List dp = disposeDeviceMapper.selectAll(); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dp)); - - dp.forEach(v -> { - DisposeDevice dev = disposeDeviceMapper.getDeviceById(v.getId()); - Assert.assertNotNull(dp); - Assert.assertEquals(dev.getId(), v.getId()); - }); - } - - /** - * T 0 del dispose device by ip. - * - * @throws JsonProcessingException the json processing exception - */ - @Test - public void t0_delDisposeDeviceByIp() throws JsonProcessingException { - String ipAddr = "10.88.77.15"; - disposeDeviceMapper.delDisposeDeviceByIp(ipAddr); - - List dp = disposeDeviceMapper.selectAll(); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dp)); - dp.forEach(v -> { - if (v.getIpAddr().equals(ipAddr)) { - Assert.fail(); - } - }); - } -}