@Test
  public void shouldNotRebootMultipleInstancesDueToAnInvalidUser() {
    // setup

    setupScatterGather(instanceIds);

    // act
    rebootInstanceServiceHelper.rebootInstances("nonOwner", instanceIds, apiApplicationManager);

    // verify
    verify(instance123, never()).setRestartRequested(eq(true));
    verify(instance456, never()).setRestartRequested(eq(true));
    verify(instanceManagerMessageContext, never())
        .routePiMessageToApplication(
            eq(nodeId),
            eq(EntityMethod.UPDATE),
            eq(instance123),
            eq(InstanceManagerApplication.APPLICATION_NAME));
    verify(secondMessageContext, never())
        .routePiMessageToApplication(
            eq(nodeId),
            eq(EntityMethod.UPDATE),
            eq(instance456),
            eq(InstanceManagerApplication.APPLICATION_NAME));
  }
  @Test
  public void shouldRebootMultipleInstances() {
    // setup
    final Collection<String> instanceIds = Arrays.asList(new String[] {"i-123", "i-456"});
    when(instance123.getNodeId()).thenReturn(nodeIdStr);
    when(instance456.getNodeId()).thenReturn(nodeIdStr);
    when(piIdBuilder.getNodeIdFromNodeId(anyString())).thenReturn(nodeId);
    setupScatterGather(instanceIds);

    // act
    rebootInstanceServiceHelper.rebootInstances(ownerId, instanceIds, apiApplicationManager);

    // verify
    verify(instance123).setRestartRequested(eq(true));
    verify(instance456).setRestartRequested(eq(true));
    verify(instanceManagerMessageContext)
        .routePiMessageToApplication(
            eq(nodeId),
            eq(EntityMethod.UPDATE),
            eq(instance123),
            eq(InstanceManagerApplication.APPLICATION_NAME));
    verify(secondMessageContext)
        .routePiMessageToApplication(
            eq(nodeId),
            eq(EntityMethod.UPDATE),
            eq(instance456),
            eq(InstanceManagerApplication.APPLICATION_NAME));
  }
  @SuppressWarnings("unchecked")
  @Test
  public void shouldRebootSingleInstance() {
    // setup
    instanceIds = Arrays.asList(new String[] {"i-123"});

    // act
    rebootInstanceServiceHelper.rebootInstances(ownerId, instanceIds, apiApplicationManager);

    // assert
    verify(scatterGatherContinuationRunner, times(1))
        .execute(anyCollection(), anyLong(), isA(TimeUnit.class));
  }
  @Before
  public void before() {
    when(koalaIdFactory.buildPId(eq(nodeIdStr))).thenReturn(nodeId);
    doAnswer(
            new Answer<PId>() {
              @Override
              public PId answer(InvocationOnMock invocation) throws Throwable {
                String id = (String) invocation.getArguments()[0];
                if (id.equals(Instance.getUrl("i-123"))) return id123;
                if (id.equals(Instance.getUrl("i-456"))) return id456;
                return null;
              }
            })
        .when(koalaIdFactory)
        .buildId(isA(String.class));

    when(piIdBuilder.getPIdForEc2AvailabilityZone(anyString())).thenReturn(id123).thenReturn(id456);

    when(apiApplicationManager.newMessageContext())
        .thenReturn(instanceManagerMessageContext)
        .thenReturn(secondMessageContext);
    when(apiApplicationManager.getKoalaIdFactory()).thenReturn(koalaIdFactory);

    AvailabilityZones zones = new AvailabilityZones();
    zones.addAvailabilityZone(
        new AvailabilityZone(
            AVAILABILITY_ZONE_1_NAME,
            AVAILABILITY_ZONE_1_CODE,
            REGION_ZONE_1_CODE,
            AVAILABILITY_ZONE_1_NAME));
    when(apiApplicationManager.getAvailabilityZonesRecord()).thenReturn(zones);

    when(koalaIdFactory.getRegion()).thenReturn(REGION_ZONE_1_CODE);

    setupDht();

    rebootInstanceServiceHelper.setPiIdBuilder(piIdBuilder);
  }