### Question:
JobWatcherUtil { static HealthCheckReport computeHealthCheckReport(Map<UUID, JobDefinition> jobs, Map<UUID, InstanceInfo> instances) { JobWatcherUtil.StateView v = JobWatcherUtil.computeState(jobs, instances); HealthCheckReport res = new HealthCheckReport(); for (Map.Entry<UUID, List<InstanceInfo>> e : v.jobInstances.entrySet()) { JobDefinition job = v.jobs.get(e.getKey()); if (job == null) { res.spuriousInstances().addAll(e.getValue()); continue; } Map<InstanceInfo, JobDefinitionDesiredstate> instanceState = new HashMap<>(); e.getValue().forEach(x -> instanceState.put(x, JobWatcherUtil.computeActualState(x))); List<JobDefinitionDesiredstate> desiredState = new ArrayList<>(job.getDesiredState()); List<InstanceInfo> healthyInstances = new ArrayList<>(); for (Map.Entry<InstanceInfo, JobDefinitionDesiredstate> entry : instanceState.entrySet()) { if (desiredState.contains(entry.getValue())) { desiredState.remove(entry.getValue()); healthyInstances.add(entry.getKey()); } } List<InstanceInfo> allInstances = e.getValue(); allInstances.removeAll(healthyInstances); res.instancesWithDifferentParameters().addAll(allInstances); if (!desiredState.isEmpty()) { res.instancesToStart().put( new ExtendedJobDefinition().uuid(e.getKey()) .definition(job), desiredState); } } return res; } private  JobWatcherUtil();   }### Answer:
@Test public void testMismatchedParameter() { UUID instId = UUID.randomUUID(); UUID jobId = UUID.randomUUID(); InstanceInfo instance = mockInstanceInfo("foo", 1, 4096, jobId, InstanceStatus.StateEnum.RUNNING); JobDefinition job = mockJobWithSingleInstance("foo", 1, 2048); HealthCheckReport report = JobWatcherUtil.computeHealthCheckReport( Collections.singletonMap(jobId, job), Collections.singletonMap(instId, instance)); assertTrue(report.spuriousInstances().isEmpty()); assertEquals(1, report.instancesWithDifferentParameters().size()); assertEquals(1, report.instancesToStart().size()); }
@Test public void testStartJob() { UUID jobId = UUID.randomUUID(); JobDefinition job = mockJobWithSingleInstance("foo", 1, 2048); HealthCheckReport report = JobWatcherUtil.computeHealthCheckReport( Collections.singletonMap(jobId, job), Collections.emptyMap()); assertTrue(report.spuriousInstances().isEmpty()); assertTrue(report.instancesWithDifferentParameters().isEmpty()); assertEquals(1, report.instancesToStart().size()); }
@Test public void testKillingSpuriousInstances() { UUID instId = UUID.randomUUID(); UUID jobId = UUID.randomUUID(); InstanceInfo instance = mockInstanceInfo("foo", 1, 4096, jobId, InstanceStatus.StateEnum.ACCEPTED); HealthCheckReport report = JobWatcherUtil.computeHealthCheckReport( Collections.emptyMap(), Collections.singletonMap(instId, instance)); assertEquals(1, report.spuriousInstances().size()); instance = mockInstanceInfo("foo", 1, 4096, jobId, InstanceStatus.StateEnum.KILLED); report = JobWatcherUtil.computeHealthCheckReport( Collections.emptyMap(), Collections.singletonMap(instId, instance)); assertTrue(report.spuriousInstances().isEmpty()); }
@Test public void testHealthyInstance() { UUID instId = UUID.randomUUID(); UUID jobId = UUID.randomUUID(); InstanceInfo instance = mockInstanceInfo("foo", 1, 4096, jobId, InstanceStatus.StateEnum.RUNNING); JobDefinition job = mockJobWithSingleInstance("foo", 1, 4096); HealthCheckReport report = JobWatcherUtil.computeHealthCheckReport( Collections.singletonMap(jobId, job), Collections.singletonMap(instId, instance)); assertTrue(report.spuriousInstances().isEmpty()); assertTrue(report.instancesWithDifferentParameters().isEmpty()); assertTrue(report.instancesToStart().isEmpty()); }