Skip to content

Instantly share code, notes, and snippets.

@tridungle
Forked from MaximPedich/CrawlersServiceImpl
Created May 11, 2021 07:44
Show Gist options
  • Save tridungle/9ec11aaf2e6f0f994604d50ec2ac6d03 to your computer and use it in GitHub Desktop.
Save tridungle/9ec11aaf2e6f0f994604d50ec2ac6d03 to your computer and use it in GitHub Desktop.
package com.gd.ashylin.crawler.service;
import com.gd.ashylin.crawler.CrawlerResult;
import com.gd.ashylin.crawler.CrawlerResults;
import com.gd.ashylin.crawler.CrawlerStatus;
import com.gd.ashylin.crawler.Sorting;
import com.gd.ashylin.crawler.logic.Crawler;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Alexander Shylin
*/
public class CrawlersServiceImpl implements CrawlersService<Long> {
private Map<Long, Crawler> crawlers = new HashMap<>();
private CrawlerRequestValidator requestValidator = new CrawlerRequestValidatorImpl();
private InstanceProvider<Crawler> instanceProvider = new CrawlerInstanceProviderImpl();
@Override
public Long runCrawlerJob(String url, Long delay) {
requestValidator.validateRunCrawlerJobReq(url, delay);
Crawler crawler = instanceProvider.getInstance(url, delay);
Long jobId = crawler.getJobId();
crawlers.put(jobId, crawler);
crawler.start();
return jobId;
}
public void setRequestValidator(CrawlerRequestValidator requestValidator) {
this.requestValidator = requestValidator;
}
public void setInstanceProvider(InstanceProvider<Crawler> instanceProvider) {
this.instanceProvider = instanceProvider;
}
// for unit-testing
Map<Long, Crawler> getCrawlers() {
return Collections.unmodifiableMap(crawlers);
}
}
package com.gd.ashylin.crawler.service;
import com.gd.ashylin.crawler.logic.Crawler;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class CrawlersServiceImplTest {
@InjectMocks
CrawlersServiceImpl crawlersService = new CrawlersServiceImpl();
@Mock
InstanceProvider<Crawler> instanceProvider;
@Mock
CrawlerRequestValidator validator;
@Mock
Crawler crawler;
@Test
public void testRunCrawlerJob() throws Exception {
//given
String url = "http://google.com.ua";
long delay = 0L;
//when
Mockito.when(crawler.getJobId()).thenReturn(Long.MAX_VALUE);
Mockito.when(instanceProvider.getInstance(url, delay)).thenReturn(crawler);
Long jobId = crawlersService.runCrawlerJob(url, delay);
//than
Mockito.verify(validator).validateRunCrawlerJobReq(url, delay);
Mockito.verify(instanceProvider).getInstance(url, delay);
Mockito.verify(crawler).start();
int actualSize = crawlersService.getCrawlers().size();
Assert.assertEquals(Long.valueOf(Long.MAX_VALUE), jobId);
Assert.assertEquals(1, actualSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment