BuildCapabilityProcess { public String[] updateStatusAndReturnBuildsToUpdate(Project project) throws ProjectNotFoundException, BuildNotFoundException { try { String lastBuildId = project.getBuildConnection().getLastBuildId(project.getBuildProjectId()); boolean lastBuilding = project.getBuildConnection().isBuilding(project.getBuildProjectId(), lastBuildId); String previousLastBuildId = project.getLastBuildId(); boolean previousBuilding = false; try { previousBuilding = project.getLastBuild().isBuilding(); } catch (BuildNotFoundException e) { LOG.debug("No lastBuild found to say the project was building before refresh " + project); } Build lastBuild = project.findCreatedBuild(lastBuildId); try { if (previousBuilding == false && lastBuilding == false && !lastBuildId.equals(previousLastBuildId)) { LOG.info("there is an already finished new build {} {}", lastBuildId, project); return new String[] { lastBuildId }; } if (previousBuilding == false && lastBuilding == true) { LOG.info("Build {} is now running {}", lastBuild.getBuildId(), project); Runnable finishTimeRunner = getEstimatedFinishTimeRunner(project, lastBuild); scheduler.schedule(finishTimeRunner, new Date()); } if (previousBuilding == true && lastBuilding == true) { if (!lastBuildId.equals(previousLastBuildId)) { LOG.info("Previous build {} is over and a new build {} is already running {}", new Object[] { previousLastBuildId, lastBuildId, project }); project.getBuilds().get(previousLastBuildId).setEstimatedFinishTime(null); project.getBuilds().get(previousLastBuildId).setBuilding(false); Runnable finishTimeRunner = getEstimatedFinishTimeRunner(project, lastBuild); scheduler.schedule(finishTimeRunner, new Date()); return new String[] { previousLastBuildId }; } } if (previousBuilding == true && lastBuilding == false) { project.getBuilds().get(previousLastBuildId).setEstimatedFinishTime(null); project.getBuilds().get(previousLastBuildId).setBuilding(false); if (!lastBuildId.equals(previousLastBuildId)) { LOG.info("previous build {} is over and a new build {} is also over {}", new Object[] { previousLastBuildId, lastBuildId, project }); return new String[] { previousLastBuildId, lastBuildId }; } LOG.info("Previous build {} is over and no new build ", previousLastBuildId, project); return new String[] { previousLastBuildId }; } } finally { lastBuild.setBuilding(lastBuilding); project.setLastBuildId(lastBuildId); } } catch (BuildIdNotFoundException e) { LOG.debug("No last build id found to update project " + project); } return new String[] {}; }  void updatePreviousCompletedBuild(Project project); void updateLastNotBuildingId(Project project); void updateBuild(Project project, String buildId); String[] updateStatusAndReturnBuildsToUpdate(Project project);  }
@Test public void should_return_update_needed_when_new_build_already_done_in_software() throws Exception { Project project = initProject("42", false, "43", false); String[] buildIdToUpdate = buildCapabilityProcess.updateStatusAndReturnBuildsToUpdate(project); Assert.assertArrayEquals(new String[] { "43" }, buildIdToUpdate); Assert.assertFalse(project.getLastBuild().isBuilding()); Assert.assertEquals("43", project.getLastBuildId()); Mockito.verify(buildCapabilityProcess.scheduler, Mockito.times(0)).schedule(Matchers.any(Runnable.class), Matchers.any(Date.class)); }
@Test public void should_not_return_update_needed_when_new_build_building_in_software() throws Exception { Project project = initProject("42", false, "43", true); String[] buildIdToUpdate = buildCapabilityProcess.updateStatusAndReturnBuildsToUpdate(project); Assert.assertArrayEquals(new String[] {}, buildIdToUpdate); Assert.assertTrue(project.getLastBuild().isBuilding()); Assert.assertEquals("43", project.getLastBuildId()); Mockito.verify(buildCapabilityProcess.scheduler).schedule(Matchers.any(Runnable.class), Matchers.any(Date.class)); }
@Test public void should_not_return_update_needed_when_no_new_build_in_software() throws Exception { Project project = initProject("42", false, "42", false); String[] buildIdToUpdate = buildCapabilityProcess.updateStatusAndReturnBuildsToUpdate(project); Assert.assertArrayEquals(new String[] {}, buildIdToUpdate); Assert.assertFalse(project.getLastBuild().isBuilding()); Assert.assertEquals("42", project.getLastBuildId()); Mockito.verify(buildCapabilityProcess.scheduler, Mockito.times(0)).schedule(Matchers.any(Runnable.class), Matchers.any(Date.class)); }
@Test public void should_not_return_update_needed_when_still_building_in_software() throws Exception { Project project = initProject("42", false, "42", true); String[] buildIdToUpdate = buildCapabilityProcess.updateStatusAndReturnBuildsToUpdate(project); Assert.assertArrayEquals(new String[] {}, buildIdToUpdate); Assert.assertTrue(project.getLastBuild().isBuilding()); Assert.assertEquals("42", project.getLastBuildId()); Mockito.verify(buildCapabilityProcess.scheduler).schedule(Matchers.any(Runnable.class), Matchers.any(Date.class)); }