This commit is contained in:
jackdaw 2016-11-15 16:00:53 +09:00
parent 897286b0fe
commit a204032fe4
6 changed files with 263 additions and 23 deletions

View File

@ -0,0 +1,9 @@
package com.loafle.bridge.discoveryport.constant;
/**
* Created by root on 16. 11. 15.
*/
public enum DirectionType {
S, // Send
R // Recv
}

View File

@ -0,0 +1,9 @@
package com.loafle.bridge.discoveryport.constant;
/**
* Created by root on 16. 11. 15.
*/
public enum PortType {
TCP,UDP
}

View File

@ -1,32 +1,60 @@
package com.loafle.bridge.discoveryport.entity;
import com.loafle.bridge.discoveryhost.entity.DiscoveryHost;
import com.loafle.bridge.discoveryport.constant.PortType;
import javax.persistence.*;
import java.util.Date;
/**
* Created by root on 16. 11. 15.
*/
import java.util.List;
@Entity
public class DiscoveryPort {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String type;
@ManyToOne
@JoinColumn(name = "HOST_ID", nullable = false)
private DiscoveryHost host;
@Column(nullable = false)
@Column(name = "PORT_TYPE",nullable = false)
@Enumerated(EnumType.STRING)
private PortType type;
@Column(name = "PORT_NUMBER",nullable = false)
private short portNumber;
@Temporal(value = TemporalType.TIMESTAMP)
private Date createDate;
@OneToMany(mappedBy = "port" , fetch = FetchType.LAZY)
private List<ServiceScanHistory> histories;
@Temporal(value = TemporalType.TIMESTAMP)
private Date updateDate;
@Column(name = "CREATE_AT",nullable = false,updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createAt;
public DiscoveryPort() {
@Column(name = "UPDATE_AT",nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date updateAt;
public DiscoveryPort() {}
public DiscoveryHost getHost() {
return host;
}
public void setHost(DiscoveryHost host) {
this.host = host;
}
public DiscoveryPort(PortType type, DiscoveryHost host, short port, Date createAt, Date updateAt) {
this.host = host;
this.type = type;
this.portNumber = port;
this.createAt = createAt;
this.updateAt = updateAt;
}
public long getId() {
@ -37,11 +65,11 @@ public class DiscoveryPort {
this.id = id;
}
public String getType() {
public PortType getType() {
return type;
}
public void setType(String type) {
public void setType(PortType type) {
this.type = type;
}
@ -53,19 +81,27 @@ public class DiscoveryPort {
this.portNumber = portNumber;
}
public Date getCreateDate() {
return createDate;
public Date getCreateAt() {
return createAt;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
public void setCreateAt(Date createAt) {
this.createAt = createAt;
}
public Date getUpdateDate() {
return updateDate;
public Date getUpdateAt() {
return updateAt;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
public void setUpdateAt(Date updateAt) {
this.updateAt = updateAt;
}
public List<ServiceScanHistory> getHistories() {
return histories;
}
public void setHistories(List<ServiceScanHistory> histories) {
this.histories = histories;
}
}

View File

@ -1,7 +1,96 @@
package com.loafle.bridge.discoveryport.entity;
import com.loafle.bridge.discoveryport.constant.DirectionType;
import javax.persistence.*;
import java.util.Date;
/**
* Created by root on 16. 11. 15.
*/
@Entity
public class ServiceScanHistory {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PORT_ID")
private DiscoveryPort port;
@Column(name = "SERVICE_NAME", nullable = false)
private String serviceName;
@Column(name = "DIRECTION", nullable = false)
@Enumerated(EnumType.STRING)
private DirectionType direction;
@Column(name = "CREATE_AT",nullable = false,updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createAt;
@Lob
@Column(name = "PACKET", nullable = false)
private byte[] packet;
public ServiceScanHistory() {}
public ServiceScanHistory(DiscoveryPort port, String serviceName, DirectionType direction, byte[] packet, Date createAt) {
this.port = port;
this.serviceName = serviceName;
this.direction = direction;
this.packet = packet;
this.createAt = createAt;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public DiscoveryPort getPort() {
return port;
}
public void setPort(DiscoveryPort port) {
this.port = port;
}
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public DirectionType getDirection() {
return direction;
}
public void setDirection(DirectionType direction) {
this.direction = direction;
}
public Date getCreateAt() {
return createAt;
}
public void setCreateAt(Date createAt) {
this.createAt = createAt;
}
public byte[] getPacket() {
return packet;
}
public void setPacket(byte[] packet) {
this.packet = packet;
}
}

View File

@ -10,6 +10,6 @@ spring.datasource.driver-class-name=org.h2.Driver
## Hibernate configuration
spring.jpa.database=h2
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql=true

View File

@ -0,0 +1,97 @@
package com.loafle.bridge.discoveryport.repository;
import com.loafle.bridge.Application;
import com.loafle.bridge.discoveryport.constant.DirectionType;
import com.loafle.bridge.discoveryport.constant.PortType;
import com.loafle.bridge.discoveryport.entity.DiscoveryPort;
import com.loafle.bridge.discoveryport.entity.ServiceScanHistory;
import org.apache.log4j.Logger;
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.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.assertEquals;
/**
* Created by root on 16. 11. 15.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
public class DiscoveryPortRepositoryTest {
@Autowired
private DiscoveryPortRepository repo;
Logger l = Logger.getLogger(this.getClass());
@Before
public void Before() {
repo.save(new DiscoveryPort(PortType.TCP,null ,(short)1, new Date(), new Date()));
repo.save(new DiscoveryPort(PortType.UDP,null , (short)2, new Date(), new Date()));
repo.save(new DiscoveryPort(PortType.TCP,null , (short)3, new Date(), new Date()));
repo.save(new DiscoveryPort(PortType.UDP,null , (short)4, new Date(), new Date()));
repo.save(new DiscoveryPort(PortType.TCP,null , (short)5, new Date(), new Date()));
}
@After
public void After() {
repo.deleteAll();
}
@Test
public void TestInsertPort() {
DiscoveryPort p = new DiscoveryPort(PortType.TCP, null ,(short)1, new Date(), new Date());
repo.save(p);
DiscoveryPort compare = repo.findOne(p.getId());
assertEquals(p.getId(),compare.getId());
l.debug(p.getId());
l.debug(p.getType());
l.debug(p.getPortNumber());
l.debug(p.getCreateAt());
l.debug(p.getUpdateAt());
}
@Test
@Transactional
public void TestInsertServiceHistory() {
List<DiscoveryPort> list = repo.findAll();
DiscoveryPort p = list.get(0);
List<ServiceScanHistory> h = new ArrayList<ServiceScanHistory>();
byte [] a = {10,20,30,40,50};
h.add(new ServiceScanHistory(p,"testservice1", DirectionType.S,a, new Date()));
h.add(new ServiceScanHistory(p,"testservice2", DirectionType.R,a, new Date()));
h.add(new ServiceScanHistory(p,"testservice3", DirectionType.S,a, new Date()));
h.add(new ServiceScanHistory(p,"testservice4", DirectionType.R,a, new Date()));
h.add(new ServiceScanHistory(p,"testservice5", DirectionType.S,a, new Date()));
p.setHistories(h);
repo.save(p);
repo.flush();
DiscoveryPort find = repo.findOne(p.getId());
List<ServiceScanHistory> l1 = p.getHistories();
List<ServiceScanHistory> l2 = find.getHistories();
assertEquals(l1.size(), l2.size());
}
}