CoffeeHouse extends AbstractLoggingActor { public static Props props(int caffeineLimit) { return Props.create(CoffeeHouse.class, () -> new CoffeeHouse(caffeineLimit)); } CoffeeHouse(int caffeineLimit); @Override Receive createReceive(); static Props props(int caffeineLimit); @Override SupervisorStrategy supervisorStrategy();  }
@Test public void shouldLogMessageWhenCreated() { new JavaTestKit(system) {{ interceptDebugLogMessage(this, ".*[Oo]pen.*", 1, () -> system.actorOf(CoffeeHouse.props(Integer.MAX_VALUE), "coffee-house")); }}; }
@Test public void shouldCreateChildActorCalledBaristaWhenCreated() { new JavaTestKit(system) {{ system.actorOf(CoffeeHouse.props(Integer.MAX_VALUE), "create-barista"); expectActor(this, "/user/create-barista/waiter"); }}; }
@Test public void shouldCreateChildActorCalledWaiterWhenCreated() { new JavaTestKit(system) {{ system.actorOf(CoffeeHouse.props(Integer.MAX_VALUE), "create-waiter"); expectActor(this, "/user/create-waiter/waiter"); }}; }
@Test public void shouldCreateGuestActorsWhenCreateGuestMessageSent() { new JavaTestKit(system) {{ ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(Integer.MAX_VALUE), "create-guest"); coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE), ActorRef.noSender()); expectActor(this, "/user/create-guest/$*"); }}; }
@Test public void sendingApproveCoffeeShouldResultInLoggingStatusMessageWhenLimitReached() { new JavaTestKit(system) {{ ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(1), "caffeine-limit"); coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE), ActorRef.noSender()); ActorRef guest = expectActor(this, "/user/caffeine-limit/$*"); interceptInfoLogMessage(this, ".*[Ss]orry.*", 1, () -> coffeeHouse.tell( new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest), ActorRef.noSender())); }}; }
@Test public void sendingApproveCoffeeShouldResultInStoppingGuestWhenLimitReached() { new JavaTestKit(system) {{ ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(1), "guest-terminated"); coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE), ActorRef.noSender()); ActorRef guest = expectActor(this, "/user/guest-terminated/$*"); watch(guest); coffeeHouse.tell(new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest), ActorRef.noSender()); expectTerminated(guest); }}; }
@Test public void onTerminationOfGuestCoffeeHouseShouldRemoveGuestFromBookkeeper() { new JavaTestKit(system) {{ ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(1), "guest-removed"); coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE), ActorRef.noSender()); ActorRef guest = expectActor(this, "/user/guest-removed/$*"); interceptDebugLogMessage(this, ".*[Rr]emoved.*", 1, () -> { coffeeHouse.tell(new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest), ActorRef.noSender()); }); }}; }
@Test public void shouldStopGuestOnFailure() { new JavaTestKit(system) {{ ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(1), "guest-stopped"); coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), 0), ActorRef.noSender()); ActorRef guest = expectActor(this, "/user/guest-stopped/$*"); watch(guest); coffeeHouse.tell(new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest), ActorRef.noSender()); expectTerminated(guest); }}; }
@Test public void shouldLogMessageWhenCreated() { new JavaTestKit(system) {{ interceptDebugLogMessage(this, ".*[Oo]pen.*", 1, () -> system.actorOf(CoffeeHouse.props(Integer.MAX_VALUE))); }}; }