@Test
public void testBranchOrdering() throws Exception {

    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
    hudson.model.User user = User.get("alice");
    user.setFullName("Alice Cooper");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false),
        new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    String token = getJwtToken(j.jenkins, "alice", "alice");
    new RequestBuilder(baseUrl)
        .put("/organizations/jenkins/pipelines/p/branches/feature2/favorite")
        .jwtToken(token)
        .data(ImmutableMap.of("favorite", true))
        .build(Map.class);
    new RequestBuilder(baseUrl)
        .put("/organizations/jenkins/pipelines/p/branches/feature4/favorite")
        .jwtToken(token)
        .data(ImmutableMap.of("favorite", true))
        .build(Map.class);
    List l = request().get("/organizations/jenkins/pipelines/p/branches/")
        .jwtToken(token)
        .build(List.class);
    Assert.assertEquals(4,l.size());
    Map o = (Map)l.get(1);
    Map o2 = (Map)l.get(0);
    WorkflowJob j1 = findBranchProject(mp, (String)o.get("name"));
    j.waitForCompletion(j1.getLastBuild());
    j.waitForCompletion(j1.scheduleBuild2(0).waitForStart());
    WorkflowJob j2 = findBranchProject(mp, (String)o2.get("name"));
    j.waitForCompletion(j2.getLastBuild());
    List l2 = request().get("/organizations/jenkins/pipelines/p/branches/")
        .jwtToken(token)
        .build(List.class);
    Assert.assertEquals(4,l.size());
    Map o1 = (Map)l2.get(0);
    Map o3 = (Map)l2.get(1);
    Assert.assertEquals(o2.get("name"), o1.get("name"));
}