MongoUrlServiceImpl implements UrlService { @Override public void addDomainLinks(List<DomainLink> domainLinks) { final Timer.Context context = domainLinksTimer.time(); try { Map<String, DomainURL> domainURLs = new HashMap<>(); Map<String, Set<String>> simpleURLs = new HashMap<>(); String status = null; for (DomainLink domainLink : domainLinks) { String domain = domainLink.getDomainURL().getAddress(); domainURLs.put(domain, domainLink.getDomainURL()); Set<String> urls = simpleURLs.get(domain); if (urls == null) { urls = new HashSet<>(); simpleURLs.put(domain, urls); } SimpleURL simpleURL = domainLink.getSimpleURL(); status = simpleURL.getStatus(); urls.add(simpleURL.getUrl()); } for (DomainURL domainURL : domainURLs.values()) { addDomainUrl(domainURL.getName(), domainURL.getAddress(), domainURL.getCoolDownPeriod()); } for (Map.Entry<String, Set<String>> entry : simpleURLs.entrySet()) { DomainURL domainURL = domainURLs.get(entry.getKey()); addSimpleUrls(new ArrayList<>(entry.getValue()), status, domainURL.getAddress(), null ); } } finally { context.stop(); } }  @PostConstruct void init(); DomainURL addDomainUrl(String domainName, String domainUrl, long coolDownPeriod); @Override void addSimpleUrls(List<String> urls, String status, String domainURL, String sourceDomainName); @Override void addDomainLinks(List<DomainLink> domainLinks); @Override List<DomainURL> findDomains(Pageable pageable); @Override Collection<SimpleURL> findURLs(String address, String status, int pageNo, int pageSize); @Override Collection<SimpleURL> findExternalURLs(String address, String status, int pageNo, int pageSize); void removeSimpleUrl(String url); @Override void updateSimpleUrlsStatus(List<String> urls, String status); @Override void updateSimpleUrlErrorStatus(String url, int errorDelta); void removeDomainUrl(String domainURL); @Override void removeDomains(); @Override List<Entries> query(String query); @Override long countAllNodes(); @Override long countAllDomains(); @Override long countAllLinks(); @Override long countDomainLinks(String domainUrl); @Autowired
    public MetricsConfig metricsConfig; @Autowired
    public MongoConfig mongoConfig; public MongoCollection domainRepo; public MongoCollection urlRepo; }
@Test public void testAddDomainLinks() throws Exception { List<DomainLink> domainLinks = new ArrayList<>(); DomainURL domain1 = new DomainURL("d1", "n1"); domainLinks.add(new DomainLink(domain1, new SimpleURL("url1", "n1", "new"))); domainLinks.add(new DomainLink(domain1, new SimpleURL("url2", "n2", "new"))); DomainURL domain2 = new DomainURL("d2", "n2"); domainLinks.add(new DomainLink(domain2, new SimpleURL("url3", "n1", "new"))); domainLinks.add(new DomainLink(domain2, new SimpleURL("url4", "n2", "new"))); service.addDomainLinks(domainLinks); assertEquals(4, urlRepo.count()); assertEquals(2, domainRepo.count()); }
@Test public void testAddDuplicateDomainLinks() throws Exception { List<DomainLink> domainLinks1 = new ArrayList<>(); DomainURL domain1 = new DomainURL("d1", "n1"); domainLinks1.add(new DomainLink(domain1, new SimpleURL("url1", "n1", "new"))); domainLinks1.add(new DomainLink(domain1, new SimpleURL("url2", "n2", "new"))); DomainURL domain2 = new DomainURL("d2", "n2"); domainLinks1.add(new DomainLink(domain2, new SimpleURL("url3", "n1", "new"))); domainLinks1.add(new DomainLink(domain2, new SimpleURL("url4", "n2", "new"))); List<DomainLink> domainLinks2 = new ArrayList<>(); DomainURL domain3 = new DomainURL("d3", "n3"); domainLinks2.add(new DomainLink(domain3, new SimpleURL("url1", "n2", "new"))); domainLinks2.add(new DomainLink(domain3, new SimpleURL("url5", "n1", "new"))); domainLinks2.add(new DomainLink(domain2, new SimpleURL("url3", "n2", "new"))); domainLinks2.add(new DomainLink(domain2, new SimpleURL("url7", "n1", "new"))); service.addDomainLinks(domainLinks1); service.addDomainLinks(domainLinks2); assertEquals(6, urlRepo.count()); assertEquals(3, domainRepo.count()); assertEquals(3, domainRepo.count()); }
@Test public void testAddDomainLinks_STRESS() throws Exception { List<DomainLink> domainLinks = new ArrayList<>(); DomainURL domain1 = new DomainURL("d1", "n1"); for (int i = 0; i < 1000; i++) { domainLinks.add(new DomainLink(domain1, new SimpleURL("url"+i, "n"+1, "new"))); } service.addDomainLinks(domainLinks); assertEquals(1000, urlRepo.count()); assertEquals(1, domainRepo.count()); }