This commit is contained in:
insanity 2018-06-28 17:26:55 +09:00
parent b704e5fa54
commit 6e4405750e
8 changed files with 141 additions and 226 deletions

View File

@ -50,7 +50,7 @@
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>commons-java</artifactId>
<version>1.0.103-SNAPSHOT</version>
<version>1.0.106-SNAPSHOT</version>
</dependency>
<dependency>

View File

@ -1,7 +1,5 @@
package com.loafle.overflow.central.module.sensor.dao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@ -15,11 +13,9 @@ import com.loafle.overflow.model.target.Target;
*/
@Repository
public interface SensorDAO extends JpaRepository<Sensor, Long> {
Page<Sensor> findAllByTargetId(Long targetID, Pageable pageable);
List<Sensor> findAllByTargetId(Long targetID);
Page<Sensor> findAllByTargetIn(List<Target> targets, Pageable pageable);
List<Sensor> findAllByTargetIn(List<Target> targets);
Page<Sensor> findAllByTargetInfraId(Long infraID, Pageable pageable);
List<Sensor> findAllByTargetInfraProbeId(Long probeId);
}

View File

@ -1,9 +1,9 @@
package com.loafle.overflow.central.module.sensor.dao;
import java.util.List;
import com.loafle.overflow.model.sensor.SensorItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@ -12,5 +12,5 @@ import org.springframework.stereotype.Repository;
*/
@Repository
public interface SensorItemDAO extends JpaRepository<SensorItem, Long> {
Page<SensorItem> findAllBySensorId(Long sensorId, Pageable pageable);
List<SensorItem> findAllBySensorId(Long sensorId);
}

View File

@ -1,16 +1,14 @@
package com.loafle.overflow.central.module.sensor.service;
import com.loafle.overflow.central.commons.utils.PageUtil;
import com.loafle.overflow.central.module.sensor.dao.SensorDAO;
import com.loafle.overflow.central.module.sensor.dao.SensorItemDAO;
import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.core.model.PageParams;
import com.loafle.overflow.model.meta.MetaDisplayItemMapping;
import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.service.central.sensor.SensorItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@ -25,38 +23,39 @@ public class CentralSensorItemService implements SensorItemService {
@Autowired
private SensorItemDAO sensorItemDAO;
@Autowired
private SensorDAO sensorDAO;
private CentralSensorService sensorService;
@Transactional
public SensorItem regist(SensorItem sensorItem) throws OverflowException {
Sensor s = sensorDAO.findById(sensorItem.getSensor().getId()).get();
s.setItemCount(s.getItemCount() + 1);
this.sensorDAO.save(s);
public SensorItem regist(Sensor sensor, MetaDisplayItemMapping metaDisplayItemMapping) throws OverflowException {
SensorItem sensorItem = new SensorItem();
sensorItem.setMetaDisplayItemMapping(metaDisplayItemMapping);
sensorItem.setSensor(sensor);
return this.sensorItemDAO.save(sensorItem);
}
@Transactional
public boolean registAll(List<SensorItem> sensorItemList) throws OverflowException {
Sensor s = sensorDAO.findById(sensorItemList.get(0).getSensor().getId()).get();
s.setItemCount(sensorItemList.size());
this.sensorDAO.save(s);
this.sensorItemDAO.saveAll(sensorItemList);
return true;
}
public SensorItem read(String id) throws OverflowException {
@Override
public SensorItem read(Long id) throws OverflowException {
return this.sensorItemDAO.findById(Long.valueOf(id)).get();
}
public Page<SensorItem> readAllBySensorID(Long sensorID, PageParams pageParams) throws OverflowException {
return this.sensorItemDAO.findAllBySensorId(sensorID, PageUtil.getPageRequest(pageParams));
@Override
public List<SensorItem> readAllBySensorID(Long sensorID) throws OverflowException {
return this.sensorItemDAO.findAllBySensorId(sensorID);
}
@Transactional
public void remove(SensorItem sensorItem) throws OverflowException {
Sensor s = sensorItem.getSensor();
s.setItemCount(s.getItemCount() - 1);
this.sensorDAO.save(s);
this.sensorItemDAO.delete(sensorItem);
@Override
public void remove(Long sensorItemID) throws OverflowException {
SensorItem sensorItem = this.read(sensorItemID);
if (null == sensorItem) {
throw new OverflowException(String.format("ID[%d] of SensorItem is not valid", sensorItemID));
}
Sensor sensor = sensorItem.getSensor();
if (null == sensor) {
throw new OverflowException(String.format("ID[%d] of Sensor is not valid", sensorItem.getSensor().getId()));
}
sensor.setItemCount(sensor.getItemCount() - 1);
this.sensorService.regist(sensor);
this.sensorItemDAO.deleteById(sensorItemID);
}
}

View File

@ -1,20 +1,18 @@
package com.loafle.overflow.central.module.sensor.service;
import com.loafle.overflow.central.commons.utils.PageUtil;
import com.loafle.overflow.central.module.sensor.dao.SensorDAO;
import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.core.model.PageParams;
import com.loafle.overflow.model.meta.MetaDisplayItemMapping;
import com.loafle.overflow.model.meta.MetaSensorStatus;
import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.service.central.sensor.SensorItemService;
import com.loafle.overflow.model.target.Target;
import com.loafle.overflow.service.central.sensor.SensorService;
import com.loafle.overflow.service.central.target.TargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.transaction.Transactional;
import java.util.List;
/**
@ -24,56 +22,96 @@ import java.util.List;
public class CentralSensorService implements SensorService {
@Autowired
SensorDAO sensorDAO;
private SensorDAO sensorDAO;
@Autowired
private SensorItemService sensorItemService;
private CentralSensorItemService sensorItemService;
@Autowired
private TargetService targetService;
@Transactional
public Sensor regist(Sensor sensor, Long targetID) throws OverflowException {
@Override
public Sensor regist(Sensor sensor, List<MetaDisplayItemMapping> metaDisplayItemMappings) throws OverflowException {
Long targetID = sensor.getTarget().getId();
Target t = this.targetService.read(targetID);
if (null == t) {
throw new OverflowException(String.format("ID[%d] of Target is not valid", targetID));
}
if (null == sensor.getName()) {
String crawlerName = sensor.getMetaCrawlerMapping().getMetaCrawler().getName();
String targetTypeName = sensor.getTarget().getMetaTargetType().getName();
sensor.setName(targetTypeName + " Sensor via " + crawlerName);
}
sensor.setMetaSensorStatus(MetaSensorStatus.Enum.RUNNING.to());
Sensor s = this.sensorDAO.save(sensor);
s.setItemCount(0);
this.targetService.increaseSensorCount(targetID);
return this.sensorDAO.save(sensor);
for (MetaDisplayItemMapping metaDisplayItemMapping : metaDisplayItemMappings) {
this.sensorItemService.regist(s, metaDisplayItemMapping);
}
return s;
}
@Transactional
@Override
public Sensor modify(Long sensorID, List<MetaDisplayItemMapping> metaDisplayItemMappings) throws OverflowException {
// FIXME : not impl
return null;
}
@Override
public Sensor read(Long id) throws OverflowException {
return this.sensorDAO.findById(id).get();
}
@Transactional
public void remove(Long id, Long targertID) throws OverflowException {
this.targetService.decreaseSensorCount(targertID);
@Override
public void remove(Long id) throws OverflowException {
Sensor sensor = this.read(id);
if (null == sensor) {
throw new OverflowException(String.format("ID[%d] of Sensor is not valid", id));
}
this.targetService.decreaseSensorCount(sensor.getTarget().getId());
this.sensorDAO.deleteById(id);
}
public Page<Sensor> readAllByProbeID(Long probeID, PageParams pageParams) throws OverflowException {
// FIXME: NOT IMPLEMENTS
return null;
}
public Page<Sensor> readAllByDomainID(Long domainID, PageParams pageParams) throws OverflowException {
// FIXME: NOT IMPLEMENTS
return null;
}
public Page<Sensor> readAllByInfraID(Long infraID, PageParams pageParams) throws OverflowException {
return this.sensorDAO.findAllByTargetInfraId(infraID, PageUtil.getPageRequest(pageParams));
}
public Page<Sensor> readAllByTargetID(Long targetID, PageParams pageParams) throws OverflowException {
return this.sensorDAO.findAllByTargetId(targetID, PageUtil.getPageRequest(pageParams));
@Override
public List<Sensor> readAll() throws OverflowException {
return this.sensorDAO.findAll();
}
@Override
public List<Sensor> readAllByTargetID(Long targetID) throws OverflowException {
return this.sensorDAO.findAllByTargetId(targetID);
}
@Override
public List<Sensor> readAllByTargetIn(List<Target> targets) throws OverflowException {
return this.sensorDAO.findAllByTargetIn(targets);
}
@Override
public List<Sensor> readAllByTargetInfraProbeID(Long probeID) throws OverflowException {
return this.sensorDAO.findAllByTargetInfraProbeId(probeID);
}
public Sensor regist(Sensor sensor) throws OverflowException {
Long targetID = sensor.getTarget().getId();
Target t = this.targetService.read(targetID);
if (null == t) {
throw new OverflowException(String.format("ID[%d] of Target is not valid", targetID));
}
return this.sensorDAO.save(sensor);
}
public Sensor increaseSensorItemCount(Long id) throws OverflowException {
Sensor s = this.sensorDAO.findById(id).get();
s.setItemCount(s.getItemCount() + 1);
return this.sensorDAO.save(s);
}
@Override
public Sensor decreaseSensorItemCount(Long id) throws OverflowException {
Sensor s = this.sensorDAO.findById(id).get();
int count = s.getItemCount();
@ -83,25 +121,4 @@ public class CentralSensorService implements SensorService {
return this.sensorDAO.save(s);
}
@Transactional
public Sensor registSensorConfig(Sensor sensor, List<SensorItem> sensorItemList, String etcJson)
throws OverflowException {
this.targetService.increaseSensorCount(sensor.getTarget().getId());
this.sensorDAO.save(sensor);
for (SensorItem sensorItem : sensorItemList) {
sensorItem.setSensor(sensor);
}
this.sensorItemService.registAll(sensorItemList);
return sensor;
}
@Override
public String generateSensorConfig(Sensor sensor) throws OverflowException {
return null;
}
}

View File

@ -5,9 +5,7 @@ import static org.junit.Assert.assertNotNull;
import java.util.List;
import com.loafle.overflow.central.spring.AppConfigTest;
import com.loafle.overflow.model.domain.Domain;
import com.loafle.overflow.model.infra.InfraHostPort;
import com.loafle.overflow.model.member.Member;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -6,7 +6,6 @@ import com.loafle.overflow.model.discovery.Host;
import com.loafle.overflow.model.discovery.Port;
import com.loafle.overflow.model.discovery.Service;
import com.loafle.overflow.model.discovery.Zone;
import com.loafle.overflow.model.infra.Infra;
import com.loafle.overflow.model.meta.MetaCryptoType;
import com.loafle.overflow.model.meta.MetaIPType;
import com.loafle.overflow.model.meta.MetaPortType;
@ -23,8 +22,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
@ContextConfiguration(classes = { AppConfigTest.class })

View File

@ -1,141 +1,49 @@
// package com.loafle.overflow.central.module.sensor.service;
package com.loafle.overflow.central.module.sensor.service;
// import com.loafle.overflow.central.module.meta.model.MetaCrawler;
// import com.loafle.overflow.central.module.meta.model.MetaSensorStatus;
// import com.loafle.overflow.central.module.sensor.model.Sensor;
// import com.loafle.overflow.central.module.target.model.Target;
// import com.loafle.overflow.central.spring.AppConfigTest;
// import com.fasterxml.jackson.databind.ObjectMapper;
// import org.junit.Assert;
// import org.junit.Ignore;
// 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 com.loafle.overflow.central.spring.AppConfigTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.service.central.sensor.SensorService;
import org.junit.Assert;
import org.junit.Ignore;
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 java.io.IOException;
// import java.util.Date;
// import java.util.HashMap;
// import java.util.Map;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
// /**
// * Created by insanity on 17. 6. 28.
// */
// //@Ignore
// @RunWith(SpringJUnit4ClassRunner.class)
// @ContextConfiguration(classes = {AppConfigTest.class})
// public class SensorServiceTest {
// @Autowired
// SensorService sensorService;
/**
* Created by insanity on 17. 6. 28.
*/
//@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfigTest.class})
public class SensorServiceTest {
@Autowired
SensorService sensorService;
// @Autowired
// ObjectMapper objectMapper;
@Autowired
ObjectMapper objectMapper;
// @Ignore
// @Test
// public void regist() throws Exception {
// Sensor sensor = new Sensor();
// MetaSensorStatus status = new MetaSensorStatus((short)1);
// sensor.setStatus(status);
// MetaCrawler crawler = new MetaCrawler();
// crawler.setId((short)1);
// sensor.setCrawler(crawler);
// Target target = new Target();
// target.setId(1);
// sensor.setTarget(target);
// sensor.setCreateDate(new Date());
// Sensor res = this.sensorService.regist(sensor);
// Assert.assertNotNull(res);
// }
@Test
public void regist() throws Exception {
Sensor sensor = new Sensor();
MetaSensorStatus status = new MetaSensorStatus((short)1);
sensor.setStatus(status);
MetaCrawler crawler = new MetaCrawler();
crawler.setId((short)1);
sensor.setCrawler(crawler);
Target target = new Target();
target.setId(1);
sensor.setTarget(target);
sensor.setCreateDate(new Date());
// @Test
// public void read() throws Exception {
// Sensor res = this.sensorService.read("1");
// Assert.assertNotNull(res);
// ObjectMapper objectMapper = new ObjectMapper();
// // String json = objectMapper.writeValueAsString(res);
// String json = this.objectMapper.writeValueAsString(res);
// System.out.println(json);
// }
// @Test
// public void generateSensorConfig() throws IOException {
// Sensor sensor = new Sensor();
// sensor.setId(5);
// String result = this.sensorService.generateSensorConfig(sensor);
// System.out.println(result);
// }
// @Test
// public void testJsonToMap() throws IOException {
// String json = "{\"interval\":5}";
// ObjectMapper objectMapper = new ObjectMapper();
// Map<String, Object> mm = new HashMap<>();
// mm = objectMapper.readValue(json, HashMap.class);
// System.out.println(mm.get("interval"));
// }
// @Test
// public void stringbbb() {
// StringBuffer stringBuffer = new StringBuffer();
// stringBuffer.append("222222222222222222222222222");
// System.out.println(stringBuffer.toString());
// stringBuffer.setLength(0);
// System.out.println(stringBuffer.toString());
// stringBuffer.append("3333333333");
// System.out.println(stringBuffer.toString());
// }
// // @Test
// // public void readAllByTarget() throws Exception {
// // Target target = new Target();
// // target.setId(1);
// // List<Sensor> sensors = this.sensorService.readAllByTarget(target);
// //
// // ObjectMapper objectMapper = new ObjectMapper();
// //
// // String json = objectMapper.writeValueAsString(sensors);
// //
// // System.out.println(json);
// // }
// // @Test
// // public void readAllByTargetList() {
// //
// // Domain domain = new Domain();
// // domain.setId(1);
// //
// // List<Sensor> sl = this.sensorService.readAllByDomain(domain);
// //
// // Assert.assertNotEquals(sl.size(), 0);
// //
// // }
// // @Test
// // public void readAllByInfra() {
// //
// // Infra infra = new InfraMachine();
// // infra.setId(1);
// //
// // List<Sensor> sl = this.sensorService.readAllByInfra(1);
// //
// // Assert.assertNotEquals(sl.size(), 0);
// //
// // }
// }
Sensor res = this.sensorService.regist(sensor);
Assert.assertNotNull(res);
}
}