package io.monokkel.actors; import akka.actor.Props; import akka.testkit.JavaTestKit; import com.google.common.collect.Lists; import io.monokkel.messages.*; import org.junit.*; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import static akka.testkit.JavaTestKit.duration; import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; /** * Created by tarjei on 29/05/14. */ public class UrlVisitedActorTest extends ActorTest{ @Before public void setUp() throws Exception { super.before(); } @Override protected String getActorName() { return "urlVisitedActor"; } @Override protected Props getActor() { return UrlVisitedActor.props(); } @Test public void onReceive_withUrl_expectUrlIsVisitedBefore() throws Exception { subject.tell(new IsUrlVisitedBefore(Lists.newArrayList("url"), 0, 1), javaTestKit.getRef()); javaTestKit.expectMsgClass(duration("1 second"), UrlIsNotVisitedBefore.class); } @Test public void onReceive_withAddUrlMessageUsingTwoUrls_expectUrlToBeAdded() throws Exception { subject.tell(new AddUrlsToVisitedSet(Lists.newArrayList("http://url1","http://url2")), javaTestKit.getRef()); javaTestKit.expectMsgClass(duration("1 second"), UrlAdded.class); } @Test public void onReceive_withUrlWithMaxDepth_expectNoMessageToBeReceived(){ subject.tell(new IsUrlVisitedBefore(Lists.newArrayList("http://maxDepthUrl"), 1, 1), javaTestKit.getRef()); javaTestKit.expectNoMsg(duration("1 second")); } @Test public void onReceive_withUrlWithMaxDepthDisabled_expectUrlIsNotVisitedBefore(){ subject.tell(new IsUrlVisitedBefore(Lists.newArrayList("http://disabledMaxDepthUrl"), 1, -1), javaTestKit.getRef()); javaTestKit.expectMsgClass(duration("1 second"), UrlIsNotVisitedBefore.class); } @Test public void onReceive_withAddUrlMessageUsingTwoUrlsAndAskIfTheyExists_expectUrlToBeFound() throws Exception { final String expectedUrl = "http://url3"; final ArrayList<String> inputUrls = Lists.newArrayList("http://url1", "http://url2"); final ArrayList<String> testUrls = Lists.newArrayList("http://url1", "http://url2", expectedUrl); subject.tell(new AddUrlsToVisitedSet(inputUrls), javaTestKit.getRef()); subject.tell(new IsUrlVisitedBefore(testUrls, 0, 1), javaTestKit.getRef()); final List<Object> messages = Lists.newArrayList(javaTestKit.receiveN(2, duration("1 second"))); final List<UrlIsNotVisitedBefore> collect = messages.stream().filter(e -> e instanceof UrlIsNotVisitedBefore).map(e -> (UrlIsNotVisitedBefore) e).collect(Collectors.toList()); final UrlIsNotVisitedBefore urlIsNotVisitedBefore = collect.get(0); final List<String> urls = urlIsNotVisitedBefore.getUrls(); assertEquals(Lists.newArrayList(expectedUrl), urls); } @After public void tearDown() { JavaTestKit.shutdownActorSystem(system); system = null; } }