Last active
May 11, 2021 07:44
-
-
Save MaximPedich/a3034870149f80ff1f38c82c7460fe78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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