diff --git a/src/main/java/capstone/lip/landinformationportal/business/service/CrawlRealEstateService.java b/src/main/java/capstone/lip/landinformationportal/business/service/CrawlRealEstateService.java index e4a7005c..a4cb214b 100644 --- a/src/main/java/capstone/lip/landinformationportal/business/service/CrawlRealEstateService.java +++ b/src/main/java/capstone/lip/landinformationportal/business/service/CrawlRealEstateService.java @@ -29,6 +29,7 @@ import capstone.lip.landinformationportal.business.repository.RealEstateRepository; import capstone.lip.landinformationportal.business.repository.UserRepository; import capstone.lip.landinformationportal.business.service.Interface.ICrawlRealEstateService; +import capstone.lip.landinformationportal.business.validation.CrawlRealEstateValidation; import capstone.lip.landinformationportal.common.config.CrawlRealEstateNowJob; import capstone.lip.landinformationportal.common.config.CrawlRealEstateScheduleJob; import capstone.lip.landinformationportal.common.constant.HousesFeatureNameConstant; @@ -53,374 +54,397 @@ import capstone.lip.landinformationportal.common.entity.compositekey.RealEstateAdjacentSegmentId; @Service -public class CrawlRealEstateService implements ICrawlRealEstateService{ - - @Autowired - private RealEstateRepository realEstateRepository; - - @Autowired - private HouseRepository houseRepository; - - @Autowired - private LandRepository landRepository; - - @Autowired - private HousesFeatureRepository housesFeatureRepository; - - @Autowired - private LandsFeatureRepository landsFeatureRepository; - - @Autowired - private UserRepository userRepository; - - @Autowired - private HousesDetailRepository housesDetailRepository; - - @Autowired - private LandsDetailRepository landsDetailRepository; - - @Autowired - private ProvinceRepository provinceRepository; - - @Autowired - private RealEstateAdjacentSegmentRepository adjRepository; - - private JobKey jobKey = new JobKey("crawlerJob", "crawler"); - private TriggerKey triggerKey = new TriggerKey("crawlerTriggler", "crawler"); - private JobKey jobKeyNow = new JobKey("crawlerNowJob", "crawler"); - - @Autowired - Scheduler scheduler; - - private List listHouseFeature; - private List listLandsFeature; - - private Trigger trigger; - private JobDetail job; - - @Override - public boolean saveRealEstateCrawl(List listReoCrawl) { - try { - listHouseFeature = housesFeatureRepository.findAll(); - listLandsFeature = landsFeatureRepository.findAll(); - User user = userRepository.findByUsername("admin"); - int i = 1; - for (RealEstateObjectCrawl reoCrawl : listReoCrawl) { - RealEstate reo = new RealEstate(); - String link = reoCrawl.getLink(); - RealEstate reoSearch = realEstateRepository.findByRealEstateLink(link); - System.out.println(i+" "+link); - i++; - if (reoSearch != null) { - continue; - } - - reo.setRealEstateName(reoCrawl.getTitle()) - .setRealEstateLat(reoCrawl.getLatitude()) - .setRealEstateLng(reoCrawl.getLongitude()) - .setRealEstatePrice(reoCrawl.getPrice()) - .setRealEstateSource(reoCrawl.getDomain()) - .setRealEstateLink(reoCrawl.getLink()) - .setRealEstateAddress(reoCrawl.getAddress()) - .setUser(user) - .setRealEstateStatus(String.valueOf(StatusRealEstateConstant.CONFUSED)); - reo.setCreateDate(reoCrawl.getStartDatePost()); - if (!validateNumber(getStringCheckNull(reoCrawl.getPrice().toString()))) { - reo.setRealEstatePrice(BigDecimal.ZERO); - }else { - reo.setRealEstatePrice(reoCrawl.getPrice()); - } - reo.setCreateDate(reoCrawl.getStartDatePost()); - reo = realEstateRepository.save(reo); - - - RealEstateAdjacentSegment adj = mappingRealEstateToLocation(reo); - if (adj != null) { - adjRepository.save(adj); - - List listRealEstateByCoordinate = realEstateRepository.findByRealEstateLatAndRealEstateLng(reo.getRealEstateLat(), reo.getRealEstateLng()); - if (listRealEstateByCoordinate == null || (listRealEstateByCoordinate!= null && listRealEstateByCoordinate.isEmpty())) { - reo.setRealEstateStatus(String.valueOf(StatusRealEstateConstant.VERIFIED)); - reo = realEstateRepository.save(reo); - } - } - - - House house = new House(); - house.setRealEstate(reo) - .setHouseName(reoCrawl.getTitle()) - .setHousePrice(BigDecimal.ZERO); - Land land = new Land(); - land.setRealEstate(reo) - .setLandName(reoCrawl.getTitle()) - .setLandPrice(BigDecimal.ZERO); - - house = houseRepository.save(house); - land = landRepository.save(land); - - List listHousesDetail = parseDataToListHouseDetail(reoCrawl, house); - List listLandsDetail = parseDataToListLandDetail(reoCrawl, land); - house.setListHousesDetail(listHousesDetail); - land.setListLandsDetail(listLandsDetail); - List listHouse = new ArrayList();listHouse.add(house); - - reo.setListHouse(listHouse); - reo.setLand(land); - - housesDetailRepository.saveAll(listHousesDetail); - landsDetailRepository.saveAll(listLandsDetail); - - - } - return true; - }catch(Exception e) { - e.printStackTrace(); - return false; - } - } - private String getStringCheckNull(String string) { - if (string == null) { - return ""; - } - return string; - } - private String getStringCheckNull(Double string) { - if (string == null) { - return ""; - } - return string.toString(); - } - private boolean validateNumber(String value) { - try { - BigDecimal number = new BigDecimal(value); - if (number.compareTo(new BigDecimal(0)) == 1){ - return true; - } - return false; - }catch(Exception e) { - return false; - } - } - private List parseDataToListHouseDetail(RealEstateObjectCrawl reoCrawl, House house) { - try { - List listHouseDetail = new ArrayList(); - for (HousesFeature housesFeature: listHouseFeature) { - switch (housesFeature.getHousesFeatureName()) { - case HousesFeatureNameConstant.NUMBERFLOORS: - if (!validateNumber(getStringCheckNull(reoCrawl.getNumberFloor().toString()))) { - continue; - } - listHouseDetail.add(new HousesDetail() - .setHouse(house) - .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) - .setHousesFeature(housesFeature) - .setValue(getStringCheckNull(reoCrawl.getNumberFloor().toString()))); - break; - case HousesFeatureNameConstant.NUMBERBEDROOMS: - if (!validateNumber(getStringCheckNull(reoCrawl.getNumberBedrooms().toString()))) { - continue; - } - listHouseDetail.add(new HousesDetail() - .setHouse(house) - .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) - .setHousesFeature(housesFeature) - .setValue(getStringCheckNull(reoCrawl.getNumberBedrooms().toString()))); - break; - case HousesFeatureNameConstant.HOMEDIRECTION: - listHouseDetail.add(new HousesDetail() - .setHouse(house) - .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) - .setHousesFeature(housesFeature) - .setValue(getStringCheckNull(reoCrawl.getHomeDirection()))); - break; - case HousesFeatureNameConstant.NUMBERTOILETS: - if (!validateNumber(getStringCheckNull(reoCrawl.getNumberToilets().toString()))) { - continue; - } - listHouseDetail.add(new HousesDetail() - .setHouse(house) - .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) - .setHousesFeature(housesFeature) - .setValue(getStringCheckNull(reoCrawl.getNumberToilets().toString()))); - break; - case HousesFeatureNameConstant.BALCONYDIRECTION: - listHouseDetail.add(new HousesDetail() - .setHouse(house) - .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) - .setHousesFeature(housesFeature) - .setValue(getStringCheckNull(reoCrawl.getBalconyDirection()))); - break; - case HousesFeatureNameConstant.PROJECTOWNER: - listHouseDetail.add(new HousesDetail() - .setHouse(house) - .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) - .setHousesFeature(housesFeature) - .setValue(getStringCheckNull(reoCrawl.getProjectOwner()))); - break; - default: - break; - } - } - return listHouseDetail; - }catch(Exception e) { - e.printStackTrace(); - return null; - } - } - private List parseDataToListLandDetail(RealEstateObjectCrawl reoCrawl, Land land) { - try { - List listLandDetail = new ArrayList(); - for(LandsFeature landsFeature:listLandsFeature) { - switch (landsFeature.getLandsFeatureName()) { - case LandsFeatureNameConstant.AREA: - listLandDetail.add(new LandsDetail() - .setLand(land) - .setId(new LandsDetailId().setLandId(land.getLandId()).setLandsFeatureId(landsFeature.getLandsFeatureID())) - .setLandsFeature(landsFeature) - .setValue(getStringCheckNull(reoCrawl.getArea().toString()))); - break; - case LandsFeatureNameConstant.WARDIN: - listLandDetail.add(new LandsDetail() - .setLand(land) - .setId(new LandsDetailId().setLandId(land.getLandId()).setLandsFeatureId(landsFeature.getLandsFeatureID())) - .setLandsFeature(landsFeature) - .setValue(getStringCheckNull(reoCrawl.getWardin()))); - break; - default: - break; - } - } - return listLandDetail; - }catch(Exception e) { - e.printStackTrace(); - return null; - } - } - - public String initCrawlJob() { - try { - String timeCrawl = ""; - JobDetail jobDetail; - try { - //find current job if exist - jobDetail = scheduler.getJobDetail(jobKey); - if (jobDetail == null) return timeCrawl; - List triggers = scheduler.getTriggersOfJob(jobDetail.getKey()); - for (Trigger trigger : triggers) { - - SimpleScheduleBuilder scheduleBuilder = (SimpleScheduleBuilder)trigger.getScheduleBuilder(); - if (scheduleBuilder != null) { - - Field privateStringField = SimpleScheduleBuilder.class. - getDeclaredField("interval"); - - privateStringField.setAccessible(true); - Long fieldValue = ((Long) privateStringField.get(scheduleBuilder))/3600000; - System.out.println("fieldValue = " + fieldValue); - timeCrawl = String.valueOf(fieldValue); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - return timeCrawl; - }catch(Exception e) { - e.printStackTrace(); - return null; - } - } - - public boolean setTimeCrawlJob(int value) { - try { - trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) - .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(value).repeatForever()).build(); - - job = JobBuilder.newJob(CrawlRealEstateScheduleJob.class).withIdentity(jobKey).build(); - return true; - }catch(Exception e) { - e.printStackTrace(); - return false; - } - } - public boolean turnOffCrawler() { - try { - if (scheduler!= null) { - scheduler.deleteJob(jobKey); +public class CrawlRealEstateService implements ICrawlRealEstateService { + + @Autowired + private RealEstateRepository realEstateRepository; + + @Autowired + private HouseRepository houseRepository; + + @Autowired + private LandRepository landRepository; + + @Autowired + private HousesFeatureRepository housesFeatureRepository; + + @Autowired + private LandsFeatureRepository landsFeatureRepository; + + @Autowired + private UserRepository userRepository; + + @Autowired + private HousesDetailRepository housesDetailRepository; + + @Autowired + private LandsDetailRepository landsDetailRepository; + + @Autowired + private ProvinceRepository provinceRepository; + + @Autowired + private RealEstateAdjacentSegmentRepository adjRepository; + + private JobKey jobKey = new JobKey("crawlerJob", "crawler"); + private TriggerKey triggerKey = new TriggerKey("crawlerTriggler", "crawler"); + private JobKey jobKeyNow = new JobKey("crawlerNowJob", "crawler"); + + @Autowired + Scheduler scheduler; + + private List listHouseFeature; + private List listLandsFeature; + + private Trigger trigger; + private JobDetail job; + + @Override + public boolean saveRealEstateCrawl(List listReoCrawl) { + try { + listHouseFeature = housesFeatureRepository.findAll(); + listLandsFeature = landsFeatureRepository.findAll(); + User user = userRepository.findByUsername("admin"); + int i = 1; + for (RealEstateObjectCrawl reoCrawl : listReoCrawl) { + CrawlRealEstateValidation validReo = new CrawlRealEstateValidation(); + RealEstate reo = new RealEstate(); + String link = reoCrawl.getLink(); + String title = reoCrawl.getTitle().trim(); + RealEstate reoSearch = realEstateRepository.findByRealEstateLink(link); + System.out.println(i + " " + link); + i++; + if (reoSearch != null) { + continue; + } else if (validReo.isValidCrawlRealEstate(reoCrawl)) { + + reo.setRealEstateName(reoCrawl.getTitle()) + .setRealEstateLat(reoCrawl.getLatitude()) + .setRealEstateLng(reoCrawl.getLongitude()) + .setRealEstatePrice(reoCrawl.getPrice()) + .setRealEstateSource(reoCrawl.getDomain()) + .setRealEstateLink(reoCrawl.getLink()) + .setRealEstateAddress(reoCrawl.getAddress()) + .setUser(user) + .setRealEstateStatus(String.valueOf(StatusRealEstateConstant.CONFUSED)); + reo.setCreateDate(reoCrawl.getStartDatePost()); + if (!validateNumber(getStringCheckNull(reoCrawl.getPrice().toString()))) { + reo.setRealEstatePrice(BigDecimal.ZERO); + } else { + reo.setRealEstatePrice(reoCrawl.getPrice()); + } + reo.setCreateDate(reoCrawl.getStartDatePost()); + reo = realEstateRepository.save(reo); + + RealEstateAdjacentSegment adj = mappingRealEstateToLocation(reo); + if (adj != null) { + adjRepository.save(adj); + + List listRealEstateByCoordinate = realEstateRepository.findByRealEstateLatAndRealEstateLng(reo.getRealEstateLat(), reo.getRealEstateLng()); + if (listRealEstateByCoordinate == null || (listRealEstateByCoordinate != null && listRealEstateByCoordinate.isEmpty())) { + reo.setRealEstateStatus(String.valueOf(StatusRealEstateConstant.VERIFIED)); + reo = realEstateRepository.save(reo); + } + } + + House house = new House(); + house.setRealEstate(reo) + .setHouseName(reoCrawl.getTitle()) + .setHousePrice(BigDecimal.ZERO); + Land land = new Land(); + land.setRealEstate(reo) + .setLandName(reoCrawl.getTitle()) + .setLandPrice(BigDecimal.ZERO); + + house = houseRepository.save(house); + land = landRepository.save(land); + + List listHousesDetail = parseDataToListHouseDetail(reoCrawl, house); + List listLandsDetail = parseDataToListLandDetail(reoCrawl, land); + house.setListHousesDetail(listHousesDetail); + land.setListLandsDetail(listLandsDetail); + List listHouse = new ArrayList(); + listHouse.add(house); + + reo.setListHouse(listHouse); + reo.setLand(land); + + housesDetailRepository.saveAll(listHousesDetail); + landsDetailRepository.saveAll(listLandsDetail); + } else { + throw new Exception(); + + } + + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + private String getStringCheckNull(String string) { + if (string == null) { + return ""; + } + return string; + } + + private String getStringCheckNull(Double string) { + if (string == null) { + return ""; + } + return string.toString(); + } + + private boolean validateNumber(String value) { + try { + BigDecimal number = new BigDecimal(value); + if (number.compareTo(new BigDecimal(0)) == 1) { + return true; + } + return false; + } catch (Exception e) { + return false; + } + } + + private List parseDataToListHouseDetail(RealEstateObjectCrawl reoCrawl, House house) { + try { + List listHouseDetail = new ArrayList(); + for (HousesFeature housesFeature : listHouseFeature) { + switch (housesFeature.getHousesFeatureName()) { + case HousesFeatureNameConstant.NUMBERFLOORS: + if (!validateNumber(getStringCheckNull(reoCrawl.getNumberFloor().toString()))) { + continue; + } + listHouseDetail.add(new HousesDetail() + .setHouse(house) + .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) + .setHousesFeature(housesFeature) + .setValue(getStringCheckNull(reoCrawl.getNumberFloor().toString()))); + break; + case HousesFeatureNameConstant.NUMBERBEDROOMS: + if (!validateNumber(getStringCheckNull(reoCrawl.getNumberBedrooms().toString()))) { + continue; + } + listHouseDetail.add(new HousesDetail() + .setHouse(house) + .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) + .setHousesFeature(housesFeature) + .setValue(getStringCheckNull(reoCrawl.getNumberBedrooms().toString()))); + break; + case HousesFeatureNameConstant.HOMEDIRECTION: + listHouseDetail.add(new HousesDetail() + .setHouse(house) + .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) + .setHousesFeature(housesFeature) + .setValue(getStringCheckNull(reoCrawl.getHomeDirection()))); + break; + case HousesFeatureNameConstant.NUMBERTOILETS: + if (!validateNumber(getStringCheckNull(reoCrawl.getNumberToilets().toString()))) { + continue; + } + listHouseDetail.add(new HousesDetail() + .setHouse(house) + .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) + .setHousesFeature(housesFeature) + .setValue(getStringCheckNull(reoCrawl.getNumberToilets().toString()))); + break; + case HousesFeatureNameConstant.BALCONYDIRECTION: + listHouseDetail.add(new HousesDetail() + .setHouse(house) + .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) + .setHousesFeature(housesFeature) + .setValue(getStringCheckNull(reoCrawl.getBalconyDirection()))); + break; + case HousesFeatureNameConstant.PROJECTOWNER: + listHouseDetail.add(new HousesDetail() + .setHouse(house) + .setId(new HousesDetailId().setHouseId(house.getHouseId()).setHousesFeatureId(housesFeature.getHousesFeatureID())) + .setHousesFeature(housesFeature) + .setValue(getStringCheckNull(reoCrawl.getProjectOwner()))); + break; + default: + break; + } + } + return listHouseDetail; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + private List parseDataToListLandDetail(RealEstateObjectCrawl reoCrawl, Land land) { + try { + List listLandDetail = new ArrayList(); + for (LandsFeature landsFeature : listLandsFeature) { + switch (landsFeature.getLandsFeatureName()) { + case LandsFeatureNameConstant.AREA: + listLandDetail.add(new LandsDetail() + .setLand(land) + .setId(new LandsDetailId().setLandId(land.getLandId()).setLandsFeatureId(landsFeature.getLandsFeatureID())) + .setLandsFeature(landsFeature) + .setValue(getStringCheckNull(reoCrawl.getArea().toString()))); + break; + case LandsFeatureNameConstant.WARDIN: + listLandDetail.add(new LandsDetail() + .setLand(land) + .setId(new LandsDetailId().setLandId(land.getLandId()).setLandsFeatureId(landsFeature.getLandsFeatureID())) + .setLandsFeature(landsFeature) + .setValue(getStringCheckNull(reoCrawl.getWardin()))); + break; + default: + break; + } + } + return listLandDetail; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public String initCrawlJob() { + try { + String timeCrawl = ""; + JobDetail jobDetail; + try { + //find current job if exist + jobDetail = scheduler.getJobDetail(jobKey); + if (jobDetail == null) { + return timeCrawl; + } + List triggers = scheduler.getTriggersOfJob(jobDetail.getKey()); + for (Trigger trigger : triggers) { + + SimpleScheduleBuilder scheduleBuilder = (SimpleScheduleBuilder) trigger.getScheduleBuilder(); + if (scheduleBuilder != null) { + + Field privateStringField = SimpleScheduleBuilder.class. + getDeclaredField("interval"); + + privateStringField.setAccessible(true); + Long fieldValue = ((Long) privateStringField.get(scheduleBuilder)) / 3600000; + System.out.println("fieldValue = " + fieldValue); + timeCrawl = String.valueOf(fieldValue); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return timeCrawl; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public boolean setTimeCrawlJob(int value) { + try { + if (value == 0) { + return false; + } else { + trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) + .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(value).repeatForever()).build(); + job = JobBuilder.newJob(CrawlRealEstateScheduleJob.class).withIdentity(jobKey).build(); + return true; + } + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + public boolean turnOffCrawler() { + try { + if (scheduler != null) { + scheduler.deleteJob(jobKey); // scheduler.shutdown(); - - } - return true; - } catch (SchedulerException e) { - e.printStackTrace(); - return false; - } - } - public boolean turnOnCrawler() { - try { - if (scheduler!= null) { - if (!scheduler.isStarted()) { - scheduler.start(); - } - scheduler.scheduleJob(job, trigger); - } - return true; - } catch (SchedulerException e) { - e.printStackTrace(); - return false; - } - } - public boolean crawlNow() { - try { - if (scheduler!= null) { - if (!scheduler.isStarted()) { - scheduler.start(); - } - } - JobDetail jobNow = JobBuilder.newJob(CrawlRealEstateNowJob.class).storeDurably(true).withIdentity(jobKeyNow).build(); - scheduler.addJob(jobNow, true); - scheduler.triggerJob(jobKeyNow); - scheduler.deleteJob(jobKeyNow); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - - } - - private RealEstateAdjacentSegment mappingRealEstateToLocation(RealEstate realestate) { - List listProvince = provinceRepository.findAll(); - String address = realestate.getRealEstateAddress().toLowerCase(); - Province provinceSearch = listProvince.stream().filter(element -> address.contains(element.getProvinceName().toLowerCase())).findAny().orElse(null); - if (provinceSearch == null) return null; - List listDistrict = provinceSearch.getListDistrict(); - District districtSearch = listDistrict.stream().filter(element -> address.contains(element.getDistrictName().toLowerCase())).findAny().orElse(null); - if (districtSearch == null) return null; - List listSegment = districtSearch.getListSegmentOfStreet(); - List listStreet = listSegment.stream().map(x->x.getStreet()).distinct().collect(Collectors.toList()); - Street streetSearch = listStreet.stream().filter(element -> address.contains(element.getStreetName().toLowerCase())).findAny().orElse(null); - if (streetSearch == null) return null; - listSegment = streetSearch.getListSegmentOfStreet(); - - SegmentOfStreet segmentSelected = null; - Double distance = null; - for (SegmentOfStreet element: listSegment) { - if (segmentSelected == null) { - segmentSelected = element; - distance = Math.sqrt( Math.pow(element.getSegmentLat() - realestate.getRealEstateLat(), 2) + Math.pow(element.getSegmentLng() - realestate.getRealEstateLng(), 2)); - continue; - } - Double currentDistance = Math.sqrt(Math.pow(element.getSegmentLat() - realestate.getRealEstateLat(), 2) + Math.pow(element.getSegmentLng() - realestate.getRealEstateLng(), 2)); - if (currentDistance < distance) { - distance = currentDistance; - segmentSelected = element; - } - } - RealEstateAdjacentSegment adj = null; - if (segmentSelected != null) { - adj = new RealEstateAdjacentSegment().setId(new RealEstateAdjacentSegmentId(segmentSelected.getSegmentId(), realestate.getRealEstateId())); - } - - return adj; - } + + } + return true; + } catch (SchedulerException e) { + e.printStackTrace(); + return false; + } + } + + public boolean turnOnCrawler() { + try { + if (scheduler != null) { + if (!scheduler.isStarted()) { + scheduler.start(); + } + scheduler.scheduleJob(job, trigger); + } + return true; + } catch (SchedulerException e) { + e.printStackTrace(); + return false; + } + } + + public boolean crawlNow() { + try { + if (scheduler != null) { + if (!scheduler.isStarted()) { + scheduler.start(); + } + } + JobDetail jobNow = JobBuilder.newJob(CrawlRealEstateNowJob.class).storeDurably(true).withIdentity(jobKeyNow).build(); + scheduler.addJob(jobNow, true); + scheduler.triggerJob(jobKeyNow); + scheduler.deleteJob(jobKeyNow); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + private RealEstateAdjacentSegment mappingRealEstateToLocation(RealEstate realestate) { + List listProvince = provinceRepository.findAll(); + String address = realestate.getRealEstateAddress().toLowerCase(); + Province provinceSearch = listProvince.stream().filter(element -> address.contains(element.getProvinceName().toLowerCase())).findAny().orElse(null); + if (provinceSearch == null) { + return null; + } + List listDistrict = provinceSearch.getListDistrict(); + District districtSearch = listDistrict.stream().filter(element -> address.contains(element.getDistrictName().toLowerCase())).findAny().orElse(null); + if (districtSearch == null) { + return null; + } + List listSegment = districtSearch.getListSegmentOfStreet(); + List listStreet = listSegment.stream().map(x -> x.getStreet()).distinct().collect(Collectors.toList()); + Street streetSearch = listStreet.stream().filter(element -> address.contains(element.getStreetName().toLowerCase())).findAny().orElse(null); + if (streetSearch == null) { + return null; + } + listSegment = streetSearch.getListSegmentOfStreet(); + + SegmentOfStreet segmentSelected = null; + Double distance = null; + for (SegmentOfStreet element : listSegment) { + if (segmentSelected == null) { + segmentSelected = element; + distance = Math.sqrt(Math.pow(element.getSegmentLat() - realestate.getRealEstateLat(), 2) + Math.pow(element.getSegmentLng() - realestate.getRealEstateLng(), 2)); + continue; + } + Double currentDistance = Math.sqrt(Math.pow(element.getSegmentLat() - realestate.getRealEstateLat(), 2) + Math.pow(element.getSegmentLng() - realestate.getRealEstateLng(), 2)); + if (currentDistance < distance) { + distance = currentDistance; + segmentSelected = element; + } + } + RealEstateAdjacentSegment adj = null; + if (segmentSelected != null) { + adj = new RealEstateAdjacentSegment().setId(new RealEstateAdjacentSegmentId(segmentSelected.getSegmentId(), realestate.getRealEstateId())); + } + + return adj; + } } diff --git a/src/main/java/capstone/lip/landinformationportal/business/service/FeedbackService.java b/src/main/java/capstone/lip/landinformationportal/business/service/FeedbackService.java index e0e11ca2..03566b42 100644 --- a/src/main/java/capstone/lip/landinformationportal/business/service/FeedbackService.java +++ b/src/main/java/capstone/lip/landinformationportal/business/service/FeedbackService.java @@ -15,101 +15,109 @@ @Service public class FeedbackService implements IFeedbackService { - @Autowired - private FeedbackRepository feedbackRepository; - - @Autowired - private EmailSender emailSender; - - @Override - public Feedback save(Feedback feedback) { - try { - FeedbackValidation validate = new FeedbackValidation(); - String error = validate.isValidFeedback(feedback); - if (!error.isEmpty()) { - throw new Exception(error); - } - return feedbackRepository.save(feedback); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - - } - - @Override - public boolean delete(Feedback feedback){ - try { - if (findById(feedback.getFeedBackID())==null) { - throw new Exception("Id not found"); - } - feedbackRepository.delete(feedback); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - - } - - @Override - public Feedback findById(Long id) { - try { - Optional temp = feedbackRepository.findById(id); - if (temp.isPresent()) { - return temp.get(); - } - return null; - } catch (Exception e) { - e.printStackTrace(); - return null; - } - - } - - @Override - public long countByFeedbackStatus(String feedbackStatus) { - try { - return feedbackRepository.countByFeedbackStatus(feedbackStatus); - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - - } - - @Override - public boolean sendFeedbackReply(Feedback feedback){ - try { - FeedbackValidation validate = new FeedbackValidation(); - String error = validate.isValidFeedback(feedback); - if (error.isEmpty()) error = validate.isValidFeedbackReply(feedback); - if (!error.isEmpty()) { - throw new Exception(error); - } - feedbackRepository.save(feedback); - emailSender.sendMailFeedback(feedback); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - - } - - @Override - public Page findByFeedbackStatus(String feedbackStatus, Pageable page) { - try { - FeedbackValidation validate = new FeedbackValidation(); - String error = validate.isValidStatus(new Feedback().setFeedbackStatus(feedbackStatus)); - if (!error.isEmpty()) { - throw new Exception(error); - } - return feedbackRepository.findByFeedbackStatus(feedbackStatus, page); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } + @Autowired + private FeedbackRepository feedbackRepository; + + @Autowired + private EmailSender emailSender; + + @Override + public Feedback save(Feedback feedback) { + try { + FeedbackValidation validate = new FeedbackValidation(); + String error = validate.isValidFeedback(feedback); + if (!error.isEmpty()) { + throw new Exception(error); + } + return feedbackRepository.save(feedback); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + } + + @Override + public boolean delete(Feedback feedback) { + try { + if (findById(feedback.getFeedBackID()) == null) { +// throw new Exception("Id not found"); + return false; + } else { + feedbackRepository.delete(feedback); + return true; + } + + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + @Override + public Feedback findById(Long id) { + try { + if(id == null){ + return null; + } + Optional temp = feedbackRepository.findById(id); + if (temp.isPresent()) { + return temp.get(); + } + return null; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + } + + @Override + public long countByFeedbackStatus(String feedbackStatus) { + try { + return feedbackRepository.countByFeedbackStatus(feedbackStatus); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + + } + + @Override + public boolean sendFeedbackReply(Feedback feedback) { + try { + FeedbackValidation validate = new FeedbackValidation(); + String error = validate.isValidFeedback(feedback); + if (error.isEmpty()) { + error = validate.isValidFeedbackReply(feedback); + } + if (!error.isEmpty()) { + throw new Exception(error); + } + feedbackRepository.save(feedback); + emailSender.sendMailFeedback(feedback); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + @Override + public Page findByFeedbackStatus(String feedbackStatus, Pageable page) { + try { + FeedbackValidation validate = new FeedbackValidation(); + String error = validate.isValidStatus(new Feedback().setFeedbackStatus(feedbackStatus)); + if (!error.isEmpty()) { + throw new Exception(error); + } + return feedbackRepository.findByFeedbackStatus(feedbackStatus, page); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } } diff --git a/src/main/java/capstone/lip/landinformationportal/business/validation/CrawlRealEstateValidation.java b/src/main/java/capstone/lip/landinformationportal/business/validation/CrawlRealEstateValidation.java new file mode 100644 index 00000000..294ab051 --- /dev/null +++ b/src/main/java/capstone/lip/landinformationportal/business/validation/CrawlRealEstateValidation.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package capstone.lip.landinformationportal.business.validation; + +import capstone.lip.landinformationportal.common.dto.RealEstateObjectCrawl; +import java.math.BigDecimal; + +/** + * + * @author Admin + */ +public class CrawlRealEstateValidation extends StringValidation { + + public boolean isValidCrawlRealEstate(RealEstateObjectCrawl reoCrawl) { + if (reoCrawl == null) { + return false; + } + if (reoCrawl.getTitle().trim().equals("")) { + return false; + } else if (Double.compare(reoCrawl.getLatitude(), 0) <= 0 && Double.compare(reoCrawl.getLongitude(), 0) <= 0) { + return false; + } else if (reoCrawl.getPrice().compareTo(BigDecimal.ZERO) <= 0) { + return false; + } else if (reoCrawl.getSource().toString().equals("CONTRIBUTOR")) { + return false; + } + return true; + } +} diff --git a/src/test/java/capstone/lip/landinformationportal/common/CRUDTest.java b/src/test/java/capstone/lip/landinformationportal/common/CRUDTest.java index 6cc6888f..511e7df1 100644 --- a/src/test/java/capstone/lip/landinformationportal/common/CRUDTest.java +++ b/src/test/java/capstone/lip/landinformationportal/common/CRUDTest.java @@ -1,3 +1,4 @@ + /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates diff --git a/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/AbstractCrawlRealEstateServiceTest.java b/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/AbstractCrawlRealEstateServiceTest.java index a4cc7ca7..d7e132ab 100644 --- a/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/AbstractCrawlRealEstateServiceTest.java +++ b/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/AbstractCrawlRealEstateServiceTest.java @@ -10,8 +10,11 @@ import capstone.lip.landinformationportal.business.repository.RealEstateRepository; import capstone.lip.landinformationportal.business.service.CrawlRealEstateService; import capstone.lip.landinformationportal.common.CRUDTest; +import capstone.lip.landinformationportal.common.dto.RealEstateObjectCrawl; import capstone.lip.landinformationportal.common.entity.RealEstate; import capstone.lip.landinformationportal.common.entity.User; +import java.math.BigDecimal; +import java.sql.Timestamp; import java.util.List; import java.util.Optional; @@ -35,67 +38,80 @@ public abstract class AbstractCrawlRealEstateServiceTest extends CRUDTest { protected final String ADDRESS_STRING = "HA NOI, VIET NAM"; protected final String STATUS_STRING = "CONFUSED"; - protected final String LINK_STRING = "https://batdongsan.com.vn/"; + protected final String LINK_STRING = "http://batdongsan.com.vn/"; protected final String SOURCE_STRING = "batdongsan.com/"; protected final int NEGATIVE_TIME_CRAWL_JOB = -10; protected final int ZERO_TIME_CRAWL_JOB = 0; protected final int POSITIVE_TIME_CRAWL_JOB = 10; + protected final String DEFAULT_ID_CRAWJOB = "001"; + protected final long DEFAULT_CODE_POST = 1L; + protected final String DEFAULT_TYPEPOST = "001"; + protected final String DEFAULT_TITLE = "001"; + protected final String DEFAULT_TYPE = "001"; + protected final String DEFAULT_HOMEDIRECTION = "NAM"; + protected final String DEFAULT_BALCONYDIRECTION = "NAM"; + protected final String DEFAULT_STRING = "DEFAULT"; + protected final Timestamp DEFAULT_TIMESTAMP = new Timestamp(System.currentTimeMillis()); + protected final BigDecimal DEFAULT_BIGDECIMAL = DEFAULT_PRICE; + protected final Double DEFAULT_DOUBLE = 10.0; + protected final Double NEGATIVE_LATLONG = -10.0; + protected final Double ZERO_DOUBLE = 0.0; + protected final String CONTRIBUTOR_SOURCE = "CONTRIBUTOR"; + protected final String INVALID_STATUS = "ABC"; + + protected final long TOTAL_REALESTATE = 5L; - protected RealEstate sampleRealEstate = new RealEstate() - .setRealEstateId(DEFAULT_ID) - .setRealEstateName("SAMPLE REALESTATE") - .setRealEstateLat(DEFAULT_LAT) - .setRealEstateLng(DEFAULT_LNG) - .setRealEstateAddress(ADDRESS_STRING) - .setRealEstatePrice(DEFAULT_PRICE) - .setRealEstateStatus(STATUS_STRING) - .setRealEstateLink(LINK_STRING) - .setRealEstateSource(SOURCE_STRING) - .setUser(new User() - .setUserId(EXISTED_ID) - .setEmail(DEFAULT_EMAIL)); - - protected void testInsertSuccess(List result) { - for (RealEstate re : result) { - if (result != null) { - //Test exist in DB - Optional actual = repository.findById(re.getRealEstateId()); - if (actual.isPresent()) { - //Compare others - assertEquals(true, actual.get().equals(result)); - //Test number of records is not changed - assertEquals(TOTAL_REALESTATE + result.size(), repository.count()); - } else { - fail(); - } - } else { - fail(); - } - } - //Insert success + protected RealEstateObjectCrawl getSampleCrawlRealEstate(){ + RealEstateObjectCrawl sampleRealEstateCrawl = new RealEstateObjectCrawl(); + sampleRealEstateCrawl.setIdCrawlerJob(DEFAULT_ID_CRAWJOB); + sampleRealEstateCrawl.setCodePost(DEFAULT_CODE_POST); + sampleRealEstateCrawl.setTypePost(DEFAULT_TYPEPOST); + sampleRealEstateCrawl.setTitle(DEFAULT_TITLE); + sampleRealEstateCrawl.setPrice(DEFAULT_PRICE); + sampleRealEstateCrawl.setArea(DEFAULT_BIGDECIMAL); + sampleRealEstateCrawl.setType(DEFAULT_TITLE); + sampleRealEstateCrawl.setDate(DEFAULT_TIMESTAMP); + sampleRealEstateCrawl.setAddress(ADDRESS_STRING); + sampleRealEstateCrawl.setNumberBedrooms(POSITIVE_TIME_CRAWL_JOB); + sampleRealEstateCrawl.setNumberToilets(POSITIVE_TIME_CRAWL_JOB); + sampleRealEstateCrawl.setSizeFront(DEFAULT_DOUBLE); + sampleRealEstateCrawl.setNumberFloor(POSITIVE_TIME_CRAWL_JOB); + sampleRealEstateCrawl.setWardin(DEFAULT_DOUBLE); + sampleRealEstateCrawl.setHomeDirection(DEFAULT_TITLE); + sampleRealEstateCrawl.setBalconyDirection(DEFAULT_TITLE); + sampleRealEstateCrawl.setInterior(DEFAULT_TITLE); + sampleRealEstateCrawl.setLongitude(DEFAULT_DOUBLE); + sampleRealEstateCrawl.setLatitude(DEFAULT_DOUBLE); + sampleRealEstateCrawl.setNameOwner(DEFAULT_TITLE); + sampleRealEstateCrawl.setMobile(DEFAULT_TITLE); + sampleRealEstateCrawl.setEmail(DEFAULT_TITLE); + sampleRealEstateCrawl.setLink(DEFAULT_TITLE); + sampleRealEstateCrawl.setProjectName(DEFAULT_TITLE); + sampleRealEstateCrawl.setProjectSize(DEFAULT_TITLE); + sampleRealEstateCrawl.setProjectOwner(DEFAULT_TITLE); + sampleRealEstateCrawl.setSource(DEFAULT_TITLE); + sampleRealEstateCrawl.setDomain(DEFAULT_TITLE); + sampleRealEstateCrawl.setStartDatePost(DEFAULT_TIMESTAMP); + sampleRealEstateCrawl.setEndDatePost(DEFAULT_TIMESTAMP); + + + return sampleRealEstateCrawl; + } + + protected void testInsertSuccess(List result, boolean testResult) { + assertEquals(true, testResult); + //Test number of records is not changed + assertEquals(TOTAL_REALESTATE + result.size(), repository.count()); + } - protected void testUpdateSuccess(List result) { - for (RealEstate re : result) { - //Update success - if (result != null) { - //Test exist in DB - Optional actual = repository.findById(re.getRealEstateId()); - if (actual.isPresent()) { - //Compare others - assertEquals(true, actual.get().equals(result)); - //Test number of records is not changed - assertEquals(TOTAL_REALESTATE, repository.count()); - } else { - fail(); - } - } else { - fail(); - } - } + protected void testUpdateSuccess(List result, boolean testResult) { + assertEquals(true, testResult); + //Test number of records is not changed + assertEquals(TOTAL_REALESTATE, repository.count()); } protected void testDeleteSuccess(boolean result, long id) { diff --git a/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/CrawlRealEstateServiceTest_1.java b/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/CrawlRealEstateServiceTest_1.java index a949a434..3beb5c6d 100644 --- a/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/CrawlRealEstateServiceTest_1.java +++ b/src/test/java/capstone/lip/landinformationportal/service/crawlRealEstateService/CrawlRealEstateServiceTest_1.java @@ -5,9 +5,11 @@ */ package capstone.lip.landinformationportal.service.crawlRealEstateService; +import capstone.lip.landinformationportal.common.dto.RealEstateObjectCrawl; import capstone.lip.landinformationportal.common.entity.House; import capstone.lip.landinformationportal.common.entity.Land; import capstone.lip.landinformationportal.common.entity.RealEstate; +import capstone.lip.landinformationportal.common.entity.User; import java.util.ArrayList; import java.util.List; import org.junit.Test; @@ -27,284 +29,603 @@ public class CrawlRealEstateServiceTest_1 extends AbstractCrawlRealEstateService */ @Test public void FT_CRES_1_01() { - List result = null; + boolean result = instance.saveRealEstateCrawl(null); testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate ID is existed - * @Expected Result: Update success + * @Description: Save empty title + * @Dependency: List had 1 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_02() { - RealEstate test = new RealEstate(); - test.setRealEstateId(EXISTED_ID); - List result = new ArrayList<>(); - result.add(test); - testUpdateSuccess(result); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(EMPTY_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate ID is positive but not existed - * @Expected Result: Insert success + * @Description: Save empty title + * @Dependency: List had 3 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_03() { - RealEstate test = new RealEstate(); - test.setRealEstateId(POSITIVE_NOT_EXISTED_ID); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(EMPTY_STRING); + reoCrawl.setLink(LINK_STRING + i); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate ID is negative - * @Expected Result: Insert success + * @Description: Save null title + * @Dependency: List had 1 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_04() { - RealEstate test = new RealEstate(); - test.setRealEstateId(NEGATIVE_NOT_EXISTED_ID); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NULL_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate ID equals zero - * @Expected Result: Insert success + * @Description: Save null title + * @Dependency: List had 3 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_05() { - RealEstate test = new RealEstate(); - test.setRealEstateId(ZERO_NOT_EXISTED_ID); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NULL_STRING); + reoCrawl.setLink(LINK_STRING + i); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate ID is null + * @Description: Save alphanumeric title + * @Dependency: List had 1 real estate * @Expected Result: Insert success */ @Test public void FT_CRES_1_06() { - RealEstate test = new RealEstate(); - test.setRealEstateId(NULL_ID); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + System.out.println(repository.count()); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(ALPHABETIC_NUMERIC_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate contains a house which its ID is existed + * @Description: Save alphanumeric title + * @Dependency: List had 3 real estate * @Expected Result: Insert success */ @Test public void FT_CRES_1_07() { - RealEstate test = new RealEstate(); - List listHouse = new ArrayList<>(); - House house = new House(); - house.setHouseId(EXISTED_ID); - listHouse.add(house); - test.setListHouse(listHouse); - List result = new ArrayList<>(); - result.add(test); - testFail(result); + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(ALPHABETIC_NUMERIC_STRING); + reoCrawl.setLink(LINK_STRING + i); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate contains a house which its ID is not existed + * @Description: Save numeric Vietnamese title + * @Dependency: List had 1 real estate * @Expected Result: Insert success */ @Test public void FT_CRES_1_08() { - RealEstate test = new RealEstate(); - List listHouse = new ArrayList<>(); - House house = new House(); - house.setHouseId(NOT_EXISTED_ID); - listHouse.add(house); - test.setListHouse(listHouse); - List result = new ArrayList<>(); - result.add(test); - testFail(result); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setSource(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate contains a land which its ID is existed + * @Description: Save numeric Vietnamese title + * @Dependency: List had 3 real estate * @Expected Result: Insert success */ @Test public void FT_CRES_1_09() { - RealEstate test = new RealEstate(); - Land land = new Land(); - land.setLandId(EXISTED_ID); - test.setLand(land); - List result = new ArrayList<>(); - result.add(test); - testFail(result); + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setLink(LINK_STRING + i); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate contains a land which its ID is not existed - * @Expected Result: Insert success + * @Description: Save all space title + * @Dependency: List had 1 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_10() { - RealEstate test = new RealEstate(); - Land land = new Land(); - land.setLandId(NOT_EXISTED_ID); - test.setLand(land); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(ALL_SPACE_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate contains no house - * @Expected Result: Insert success + * @Description: Save all space title + * @Dependency: List had 3 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_11() { - RealEstate test = new RealEstate(); - List listHouse = new ArrayList<>(); - test.setListHouse(listHouse); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(ALL_SPACE_STRING); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setLink(LINK_STRING + i); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate contains no land - * @Expected Result: Insert success + * @Description: Save negative lat & lng + * @Dependency: List had 1 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_12() { - RealEstate test = new RealEstate(); - Land land = new Land(); - test.setLand(land); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setLatitude(NEGATIVE_LATLONG); + reoCrawl.setLongitude(NEGATIVE_LATLONG); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate contains no house and land - * @Expected Result: Insert success + * @Description: Save negative lat & lng + * @Dependency: List had 3 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_13() { - RealEstate test = new RealEstate(); - List listHouse = new ArrayList<>(); - test.setListHouse(listHouse); - Land land = new Land(); - test.setLand(land); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setLatitude(NEGATIVE_LATLONG); + reoCrawl.setLongitude(NEGATIVE_LATLONG); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setLink(LINK_STRING + i); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate has negative price - * @Expected Result: Insert success + * @Description: Save lat & lng equals zero + * @Dependency: List had 1 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_14() { - RealEstate test = new RealEstate(); - test.setRealEstatePrice(NEGATIVE_PRICE); - List result = new ArrayList<>(); - result.add(test); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setLatitude(ZERO_DOUBLE); + reoCrawl.setLongitude(ZERO_DOUBLE); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setSource(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate has price which equals zero - * @Expected Result: Insert success + * @Description: Save lat & lng equals zero + * @Dependency: List had 3 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_15() { - RealEstate test = new RealEstate(); - test.setRealEstatePrice(ZERO_PRICE); - List result = new ArrayList<>(); - result.add(test); - testInsertSuccess(result); + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setLatitude(ZERO_DOUBLE); + reoCrawl.setLongitude(ZERO_DOUBLE); + reoCrawl.setLink(LINK_STRING + i); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } /** - * @Description: Save list contain 1 record - * @Dependency: Real estate has null price + * @Description: Save positive lat & lng + * @Dependency: List had 1 real estate * @Expected Result: Insert success */ @Test public void FT_CRES_1_16() { - RealEstate test = new RealEstate(); - test.setRealEstatePrice(null); - List result = new ArrayList<>(); - result.add(test); - testFail(result); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLatitude(DEFAULT_DOUBLE); + reoCrawl.setLongitude(DEFAULT_DOUBLE); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); } /** - * @Description: Save list contain 3 record - * @Dependency: All real estate is existed + * @Description: Save positive lat & lng + * @Dependency: List had 3 real estate * @Expected Result: Insert success */ @Test public void FT_CRES_1_17() { - List result = new ArrayList<>(); + List testReoCrawl = new ArrayList<>(); for (int i = 0; i < 3; i++) { - RealEstate test = new RealEstate(); - test.setRealEstateId(EXISTED_IDs[i]); - result.add(test); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLatitude(DEFAULT_DOUBLE); + reoCrawl.setLongitude(DEFAULT_DOUBLE); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setLink(LINK_STRING + i); + testReoCrawl.add(reoCrawl); } - testUpdateSuccess(result); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); } - + /** - * @Description: Save list contain 3 record - * @Dependency: All real estate is not existed - * @Expected Result: Insert success + * @Description: Save negative price + * @Dependency: List had 1 real estate + * @Expected Result: Fail */ @Test public void FT_CRES_1_18() { - List result = new ArrayList<>(); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setPrice(NEGATIVE_PRICE); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); + } + + /** + * @Description: Save negative price + * @Dependency: List had 3 real estate + * @Expected Result: Fail + */ + @Test + public void FT_CRES_1_19() { + List testReoCrawl = new ArrayList<>(); for (int i = 0; i < 3; i++) { - RealEstate test = new RealEstate(); - test.setRealEstateId(NOT_EXISTED_ID); - result.add(test); + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setPrice(NEGATIVE_PRICE); + reoCrawl.setLink(LINK_STRING + i); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setSource(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); } - testInsertSuccess(result); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); } - + /** - * @Description: Save list contain 3 record - * @Dependency: There is a null real estate + * @Description: Save price equals zero + * @Dependency: List had 1 real estate + * @Expected Result: Fail + */ + @Test + public void FT_CRES_1_20() { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setPrice(ZERO_PRICE); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); + } + + /** + * @Description: Save price equals zero + * @Dependency: List had 3 real estate + * @Expected Result: Fail + */ + @Test + public void FT_CRES_1_21() { + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setPrice(ZERO_PRICE); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setLink(LINK_STRING + i); + reoCrawl.setSource(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); + } + + /** + * @Description: Save positive price + * @Dependency: List had 1 real estate * @Expected Result: Insert success */ @Test - public void FT_CRES_1_19() { - List result = new ArrayList<>(); - for (int i = 0; i < 2; i++) { - RealEstate test = new RealEstate(); - test.setRealEstateId(NOT_EXISTED_ID); - result.add(test); + public void FT_CRES_1_22() { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setPrice(DEFAULT_BIGDECIMAL); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); + } + + /** + * @Description: Save positive price + * @Dependency: List had 3 real estate + * @Expected Result: Insert success + */ + @Test + public void FT_CRES_1_23() { + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setPrice(DEFAULT_BIGDECIMAL); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setLink(LINK_STRING + i); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); + } + + /** + * @Description: Save CONTRIBUTOR source + * @Dependency: List had 1 real estate + * @Expected Result: Fail + */ + @Test + public void FT_CRES_1_24() { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setSource(CONTRIBUTOR_SOURCE); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testFail(result); + } + + /** + * @Description: Save CONTRIBUTOR source + * @Dependency: List had 3 real estate + * @Expected Result: Fail + */ + @Test + public void FT_CRES_1_25() { + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setSource(CONTRIBUTOR_SOURCE); + reoCrawl.setLink(LINK_STRING + i); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); } - RealEstate test = null; - result.add(test); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); testFail(result); } + + /** + * @Description: Save special character link + * @Dependency: List had 1 real estate + * @Expected Result: Insert success + */ + @Test + public void FT_CRES_1_26() { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLink(LINK_STRING + "1"); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setSource(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); + } + + /** + * @Description: Save special character link + * @Dependency: List had 3 real estate + * @Expected Result: Insert success + */ + @Test + public void FT_CRES_1_27() { + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLink(LINK_STRING + i); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setSource(ADDRESS_STRING); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); + } + + /** + * @Description: Save Vietnamese link + * @Dependency: List had 1 real estate + * @Expected Result: Insert Success + */ + @Test + public void FT_CRES_1_28() { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLink(VIETNAMESE_STRING); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setSource(ADDRESS_STRING); + List testReoCrawl = new ArrayList<>(); + testReoCrawl.add(reoCrawl); + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); + } + + /** + * @Description: Save Vietnamese link + * @Dependency: List had 3 real estate + * @Expected Result: Insert Success + */ + @Test + public void FT_CRES_1_29() { + List testReoCrawl = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + RealEstateObjectCrawl reoCrawl = new RealEstateObjectCrawl(); + reoCrawl.setTitle(NUMERIC_VIETNAMESE_STRING); + reoCrawl.setLink(VIETNAMESE_STRING + i); + reoCrawl.setLatitude(DEFAULT_LAT); + reoCrawl.setLongitude(DEFAULT_LNG); + reoCrawl.setPrice(DEFAULT_PRICE); + reoCrawl.setDomain(ADDRESS_STRING); + reoCrawl.setAddress(ADDRESS_STRING); + reoCrawl.setSource(ADDRESS_STRING); + testReoCrawl.add(reoCrawl); + } + boolean result = instance.saveRealEstateCrawl(testReoCrawl); + testInsertSuccess(testReoCrawl, result); + } + } diff --git a/src/test/java/capstone/lip/landinformationportal/service/feedback/FeedbackServiceTest_2.java b/src/test/java/capstone/lip/landinformationportal/service/feedback/FeedbackServiceTest_2.java index 2a356d4d..72eb9a9c 100644 --- a/src/test/java/capstone/lip/landinformationportal/service/feedback/FeedbackServiceTest_2.java +++ b/src/test/java/capstone/lip/landinformationportal/service/feedback/FeedbackServiceTest_2.java @@ -22,8 +22,9 @@ public class FeedbackServiceTest_2 extends AbstractFeedbackServiceTest { */ @Test public void FT_FS_2_01() { - boolean result = instance.delete(getSampleFeedback() - .setFeedBackID(NULL_ID)); + + boolean result = instance.delete(sampleFeedback + .setFeedBackID(NULL_NOT_EXISTED_ID)); testFail(result); } @@ -147,6 +148,7 @@ public void FT_FS_2_10() { .findById(EXISTED_ID).get() .setFeedbackAdminReply(NULL_STRING)); + testDeleteSuccess(result, EXISTED_ID); }