From 8ca70fd7a108e064e7411f619033e17b3ecdd7d3 Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 16 Nov 2016 18:24:18 +0900 Subject: [PATCH] added discovery zone --- .../DiscoveriyHistoryController.java | 2 +- .../DiscoveryHistory.java | 4 +- .../DiscoveryHistoryRepository.java | 4 +- .../bridge/discoveryzone/DiscoveryZone.java | 48 +++++++ .../DiscoveryZoneController.java | 24 ++++ .../DiscoveryZoneRepository.java | 7 +- .../bridge/discoveryzone/HostScanHistory.java | 103 +++++++++++++++ .../bridge/discoveryzone/ResultType.java | 16 +++ .../DiscoveryHistoryRepositoryTest.java | 4 +- .../DiscoveryZoneRepositoryTest.java | 122 ++++++++++++++++++ 10 files changed, 325 insertions(+), 9 deletions(-) rename src/main/java/com/loafle/bridge/{discoveryHistory => discoveryhistory}/DiscoveriyHistoryController.java (95%) rename src/main/java/com/loafle/bridge/{discoveryHistory => discoveryhistory}/DiscoveryHistory.java (92%) rename src/main/java/com/loafle/bridge/{discoveryHistory => discoveryhistory}/DiscoveryHistoryRepository.java (77%) create mode 100644 src/main/java/com/loafle/bridge/discoveryzone/HostScanHistory.java create mode 100644 src/main/java/com/loafle/bridge/discoveryzone/ResultType.java create mode 100644 src/test/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepositoryTest.java diff --git a/src/main/java/com/loafle/bridge/discoveryHistory/DiscoveriyHistoryController.java b/src/main/java/com/loafle/bridge/discoveryhistory/DiscoveriyHistoryController.java similarity index 95% rename from src/main/java/com/loafle/bridge/discoveryHistory/DiscoveriyHistoryController.java rename to src/main/java/com/loafle/bridge/discoveryhistory/DiscoveriyHistoryController.java index 1d9659a..d458254 100644 --- a/src/main/java/com/loafle/bridge/discoveryHistory/DiscoveriyHistoryController.java +++ b/src/main/java/com/loafle/bridge/discoveryhistory/DiscoveriyHistoryController.java @@ -1,4 +1,4 @@ -package com.loafle.bridge.discoveryHistory; +package com.loafle.bridge.discoveryhistory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; diff --git a/src/main/java/com/loafle/bridge/discoveryHistory/DiscoveryHistory.java b/src/main/java/com/loafle/bridge/discoveryhistory/DiscoveryHistory.java similarity index 92% rename from src/main/java/com/loafle/bridge/discoveryHistory/DiscoveryHistory.java rename to src/main/java/com/loafle/bridge/discoveryhistory/DiscoveryHistory.java index 64bd487..ab01b1b 100644 --- a/src/main/java/com/loafle/bridge/discoveryHistory/DiscoveryHistory.java +++ b/src/main/java/com/loafle/bridge/discoveryhistory/DiscoveryHistory.java @@ -1,4 +1,4 @@ -package com.loafle.bridge.discoveryHistory; +package com.loafle.bridge.discoveryhistory; import javax.persistence.*; import java.util.Date; @@ -19,7 +19,7 @@ public class DiscoveryHistory @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", updatable = false) private Date endDate; - @Column(name="RESULT", nullable=false) + @Column(nullable=false) private Boolean result; // private null DiscoveryZone; diff --git a/src/main/java/com/loafle/bridge/discoveryHistory/DiscoveryHistoryRepository.java b/src/main/java/com/loafle/bridge/discoveryhistory/DiscoveryHistoryRepository.java similarity index 77% rename from src/main/java/com/loafle/bridge/discoveryHistory/DiscoveryHistoryRepository.java rename to src/main/java/com/loafle/bridge/discoveryhistory/DiscoveryHistoryRepository.java index f58434b..7ebabd4 100644 --- a/src/main/java/com/loafle/bridge/discoveryHistory/DiscoveryHistoryRepository.java +++ b/src/main/java/com/loafle/bridge/discoveryhistory/DiscoveryHistoryRepository.java @@ -1,6 +1,4 @@ -package com.loafle.bridge.discoveryHistory; - -import com.loafle.bridge.discoveryHistory.DiscoveryHistory; +package com.loafle.bridge.discoveryhistory; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; diff --git a/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZone.java b/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZone.java index 9f38b31..31a8b21 100644 --- a/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZone.java +++ b/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZone.java @@ -1,9 +1,57 @@ package com.loafle.bridge.discoveryzone; +import javax.persistence.*; +import java.util.List; + /** * Created by root on 16. 11. 15. */ +@Entity public class DiscoveryZone { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @Column(nullable=false) + private long ip; + + @Column(nullable=false) + private long cidr; + + @OneToMany(mappedBy = "zone") + private List hostScanHistories; + + public List getHostScanHistories() { + return hostScanHistories; + } + + public void setHostScanHistories(List hostScanHistories) { + this.hostScanHistories = hostScanHistories; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public long getIp() { + return ip; + } + + public void setIp(long ip) { + this.ip = ip; + } + + public long getCidr() { + return cidr; + } + + public void setCidr(long cidr) { + this.cidr = cidr; + } } diff --git a/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneController.java b/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneController.java index e3613eb..74f5542 100644 --- a/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneController.java +++ b/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneController.java @@ -1,7 +1,31 @@ package com.loafle.bridge.discoveryzone; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + /** * Created by root on 16. 11. 15. */ +@RestController public class DiscoveryZoneController { + + @Autowired + DiscoveryZoneRepository discoveryZoneRepository; + + @RequestMapping(value = "discoveryZone/{id}", method = RequestMethod.GET) + public DiscoveryZone getDiscoveryZone(@PathVariable(name = "id")long id) throws Exception { + return this.discoveryZoneRepository.findOne(id); + } + + @RequestMapping(value = "discoveryZone/", method = RequestMethod.GET) + public List getDiscoveryZoneList() throws Exception { + return this.discoveryZoneRepository.findAll(); + } + + } diff --git a/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepository.java b/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepository.java index f3ad74e..2db1c16 100644 --- a/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepository.java +++ b/src/main/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepository.java @@ -1,7 +1,12 @@ package com.loafle.bridge.discoveryzone; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + /** * Created by root on 16. 11. 15. */ -public class DiscoveryZoneRepository { +@RepositoryRestResource(collectionResourceRel = "discoveryZone", path = "discoveryZone") +public interface DiscoveryZoneRepository extends JpaRepository { } diff --git a/src/main/java/com/loafle/bridge/discoveryzone/HostScanHistory.java b/src/main/java/com/loafle/bridge/discoveryzone/HostScanHistory.java new file mode 100644 index 0000000..eec4a5c --- /dev/null +++ b/src/main/java/com/loafle/bridge/discoveryzone/HostScanHistory.java @@ -0,0 +1,103 @@ +package com.loafle.bridge.discoveryzone; + +import javax.persistence.*; +import java.util.Date; + + +/** + * Created by snoop on 16. 11. 16. + */ +@Entity +public class HostScanHistory { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @Column(nullable=false) + private long ip; + + @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", nullable = false, updatable = false) + private Date createDate; + + @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", nullable = false, updatable = false) + private Date sendDate; + + @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", nullable = false, updatable = false) + private Date resultDate; + + @Column(nullable = false) + @Enumerated(EnumType.STRING) + private ResultType resultType; + + @Column + private String description; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn + private DiscoveryZone zone; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public long getIp() { + return ip; + } + + public void setIp(long ip) { + this.ip = ip; + } + + public Date getCreateDate() { + return createDate; + } + + public void setCreateDate(Date createDate) { + this.createDate = createDate; + } + + public Date getSendDate() { + return sendDate; + } + + public void setSendDate(Date sendDate) { + this.sendDate = sendDate; + } + + public Date getResultDate() { + return resultDate; + } + + public void setResultDate(Date resultDate) { + this.resultDate = resultDate; + } + + public ResultType getResultType() { + return resultType; + } + + public void setResultType(ResultType resultType) { + this.resultType = resultType; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public DiscoveryZone getZone() { + return zone; + } + + public void setZone(DiscoveryZone zone) { + this.zone = zone; + } +} diff --git a/src/main/java/com/loafle/bridge/discoveryzone/ResultType.java b/src/main/java/com/loafle/bridge/discoveryzone/ResultType.java new file mode 100644 index 0000000..4abb362 --- /dev/null +++ b/src/main/java/com/loafle/bridge/discoveryzone/ResultType.java @@ -0,0 +1,16 @@ +package com.loafle.bridge.discoveryzone; + +/** + * Created by snoop on 16. 11. 16. + */ +public enum ResultType { + Success("S"), + Timeout("T"); + + private String stringValue; + ResultType(String string) {stringValue = string;} + @Override + public String toString() { + return stringValue; + } +} diff --git a/src/test/java/com/loafle/bridge/discoveryHistory/repository/DiscoveryHistoryRepositoryTest.java b/src/test/java/com/loafle/bridge/discoveryHistory/repository/DiscoveryHistoryRepositoryTest.java index 5c4c4c8..6e04afd 100644 --- a/src/test/java/com/loafle/bridge/discoveryHistory/repository/DiscoveryHistoryRepositoryTest.java +++ b/src/test/java/com/loafle/bridge/discoveryHistory/repository/DiscoveryHistoryRepositoryTest.java @@ -1,8 +1,8 @@ package com.loafle.bridge.discoveryHistory.repository; import com.loafle.bridge.Application; -import com.loafle.bridge.discoveryHistory.DiscoveryHistory; -import com.loafle.bridge.discoveryHistory.DiscoveryHistoryRepository; +import com.loafle.bridge.discoveryhistory.DiscoveryHistory; +import com.loafle.bridge.discoveryhistory.DiscoveryHistoryRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/test/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepositoryTest.java b/src/test/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepositoryTest.java new file mode 100644 index 0000000..1cd2285 --- /dev/null +++ b/src/test/java/com/loafle/bridge/discoveryzone/DiscoveryZoneRepositoryTest.java @@ -0,0 +1,122 @@ +package com.loafle.bridge.discoveryzone; + +import com.loafle.bridge.Application; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Created by snoop on 16. 11. 16. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Application.class) +public class DiscoveryZoneRepositoryTest { + + @Autowired + DiscoveryZoneRepository discoveryZoneRepository; + + + + @Before + public void setUp() throws Exception { + DiscoveryZone discoveryZone = new DiscoveryZone(); + + + String cidr = "827452358680"; + String ip = "3232235983"; + + + discoveryZone.setCidr(Long.parseLong(cidr)); + discoveryZone.setIp(Long.parseLong(ip)); + + discoveryZoneRepository.save(discoveryZone); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void InsertZone() throws Exception { + + DiscoveryZone discoveryZone = new DiscoveryZone(); + + + String cidr = "827452358680"; + String ip = "3232235983"; + + + discoveryZone.setCidr(Long.parseLong(cidr)); + discoveryZone.setIp(Long.parseLong(ip)); + + discoveryZoneRepository.save(discoveryZone); + + + + + } + + @Test + @Transactional + public void InsertHostHistory() throws Exception { + + DiscoveryZone zone = discoveryZoneRepository.findOne((long)1); + + HostScanHistory hostScanHistory = null; + + System.out.println("!!!!!!!!!!!!!!!!!!!!!= " + zone.getCidr()); + + List hostScanHistories = new ArrayList(); + + String ip = "3232235983"; + + for( int indexI = 0 ; indexI < 5; ++indexI) { + + hostScanHistory = new HostScanHistory(); + + hostScanHistory.setIp(Long.parseLong(ip)); + + hostScanHistory.setCreateDate(new Date()); + hostScanHistory.setSendDate(new Date()); + hostScanHistory.setResultDate(new Date()); + hostScanHistory.setDescription("hgihihihih"); + + hostScanHistory.setResultType(ResultType.Success); + hostScanHistory.setZone(zone); + + + hostScanHistories.add(hostScanHistory); + } + + + zone.setHostScanHistories(hostScanHistories); + discoveryZoneRepository.save(zone); + + +// discoveryZoneRepository.flush(); + + DiscoveryZone zzzz = discoveryZoneRepository.findOne(zone.getId()); + + if(zzzz.getHostScanHistories() != null && zzzz.getHostScanHistories().size() > 0) { + System.out.println("zzzz. = " + zzzz.getHostScanHistories().size()); + } + else { + System.out.println("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"); + } + + } + +} \ No newline at end of file