@NotNull
  protected List<StageExecution> getRunCanaryStages(@Nonnull StageExecution stage) {
    // Collect the Run Canary Stages where the parent id is itself
    // Sorting by number after the # in the name
    return stage.getExecution().getStages().stream()
        .filter(s -> s.getType().equals(RunCanaryStage.STAGE_TYPE))
        .sorted(
            Comparator.comparing(
                s -> Integer.valueOf(StringUtils.substringAfterLast(s.getName(), "#"))))
        .collect(Collectors.toList());
  }
@Test
  public void
      test_that_getRunCanaryStages_returns_the_expected_sorted_list_of_stages_sorted_by_the_number_in_the_stage_name() {
    StageExecution stage = mock(StageExecution.class);
    PipelineExecution execution = mock(PipelineExecution.class);
    when(stage.getExecution()).thenReturn(execution);
    when(execution.getStages())
        .thenReturn(
            ImmutableList.of(
                new StageExecutionImpl(
                    null, STAGE_TYPE, "foo #1", Maps.newHashMap(ImmutableMap.of("index", "0"))),
                new StageExecutionImpl(
                    null, STAGE_TYPE, "foo #3", Maps.newHashMap(ImmutableMap.of("index", "2"))),
                new StageExecutionImpl(
                    null, STAGE_TYPE, "foo #2", Maps.newHashMap(ImmutableMap.of("index", "1"))),
                new StageExecutionImpl(
                    null, STAGE_TYPE, "foo #4", Maps.newHashMap(ImmutableMap.of("index", "3")))));
    List<StageExecution> actual = task.getRunCanaryStages(stage);
    for (int i = 0; i < 4; i++) {
      assertEquals(String.valueOf(i), actual.get(i).getContext().get("index"));
    }
  }