FailoverService { public Map<String, Collection<FailoverTaskInfo>> getAllFailoverTasks() { if (!regCenter.isExisted(FailoverNode.ROOT)) { return Collections.emptyMap(); } List<String> jobNames = regCenter.getChildrenKeys(FailoverNode.ROOT); Map<String, Collection<FailoverTaskInfo>> result = new HashMap<>(jobNames.size(), 1); for (String each : jobNames) { Collection<FailoverTaskInfo> failoverTasks = getFailoverTasks(each); if (!failoverTasks.isEmpty()) { result.put(each, failoverTasks); } } return result; } FailoverService(final CoordinatorRegistryCenter regCenter); void add(final TaskContext taskContext); Collection<JobContext> getAllEligibleJobContexts(); void remove(final Collection<MetaInfo> metaInfoList); Optional<String> getTaskId(final MetaInfo metaInfo); Map<String, Collection<FailoverTaskInfo>> getAllFailoverTasks();  }
@Test public void assertGetAllFailoverTasksWithoutRootNode() { when(regCenter.isExisted(FailoverNode.ROOT)).thenReturn(false); assertTrue(failoverService.getAllFailoverTasks().isEmpty()); verify(regCenter).isExisted(FailoverNode.ROOT); }
@Test public void assertGetAllFailoverTasksWhenRootNodeHasNoChild() { when(regCenter.isExisted(FailoverNode.ROOT)).thenReturn(true); when(regCenter.getChildrenKeys(FailoverNode.ROOT)).thenReturn(Collections.emptyList()); assertTrue(failoverService.getAllFailoverTasks().isEmpty()); verify(regCenter).isExisted(FailoverNode.ROOT); verify(regCenter).getChildrenKeys(FailoverNode.ROOT); }
@Test public void assertGetAllFailoverTasksWhenJobNodeHasNoChild() { when(regCenter.isExisted(FailoverNode.ROOT)).thenReturn(true); when(regCenter.getChildrenKeys(FailoverNode.ROOT)).thenReturn(Collections.singletonList("test_job")); when(regCenter.getChildrenKeys(FailoverNode.getFailoverJobNodePath("test_job"))).thenReturn(Collections.emptyList()); assertTrue(failoverService.getAllFailoverTasks().isEmpty()); verify(regCenter).isExisted(FailoverNode.ROOT); verify(regCenter).getChildrenKeys(FailoverNode.ROOT); verify(regCenter).getChildrenKeys(FailoverNode.getFailoverJobNodePath("test_job")); }
@Test public void assertGetAllFailoverTasksWithRootNode() { String uuid1 = UUID.randomUUID().toString(); String uuid2 = UUID.randomUUID().toString(); String uuid3 = UUID.randomUUID().toString(); when(regCenter.isExisted(FailoverNode.ROOT)).thenReturn(true); when(regCenter.getChildrenKeys(FailoverNode.ROOT)).thenReturn(Arrays.asList("test_job_1", "test_job_2")); when(regCenter.getChildrenKeys(FailoverNode.getFailoverJobNodePath("test_job_1"))).thenReturn(Arrays.asList("test_job_1@-@0", "test_job_1@-@1")); when(regCenter.getChildrenKeys(FailoverNode.getFailoverJobNodePath("test_job_2"))).thenReturn(Collections.singletonList("test_job_2@-@0")); when(regCenter.get(FailoverNode.getFailoverTaskNodePath("test_job_1@-@0"))).thenReturn(uuid1); when(regCenter.get(FailoverNode.getFailoverTaskNodePath("test_job_1@-@1"))).thenReturn(uuid2); when(regCenter.get(FailoverNode.getFailoverTaskNodePath("test_job_2@-@0"))).thenReturn(uuid3); Map<String, Collection<FailoverTaskInfo>> result = failoverService.getAllFailoverTasks(); assertThat(result.size(), is(2)); assertThat(result.get("test_job_1").size(), is(2)); assertThat(result.get("test_job_1").toArray(new FailoverTaskInfo[]{})[0].getTaskInfo().toString(), is("test_job_1@-@0")); assertThat(result.get("test_job_1").toArray(new FailoverTaskInfo[]{})[0].getOriginalTaskId(), is(uuid1)); assertThat(result.get("test_job_1").toArray(new FailoverTaskInfo[]{})[1].getTaskInfo().toString(), is("test_job_1@-@1")); assertThat(result.get("test_job_1").toArray(new FailoverTaskInfo[]{})[1].getOriginalTaskId(), is(uuid2)); assertThat(result.get("test_job_2").size(), is(1)); assertThat(result.get("test_job_2").iterator().next().getTaskInfo().toString(), is("test_job_2@-@0")); assertThat(result.get("test_job_2").iterator().next().getOriginalTaskId(), is(uuid3)); verify(regCenter).isExisted(FailoverNode.ROOT); verify(regCenter).getChildrenKeys(FailoverNode.ROOT); verify(regCenter).getChildrenKeys(FailoverNode.getFailoverJobNodePath("test_job_1")); verify(regCenter).getChildrenKeys(FailoverNode.getFailoverJobNodePath("test_job_2")); verify(regCenter).get(FailoverNode.getFailoverTaskNodePath("test_job_1@-@0")); verify(regCenter).get(FailoverNode.getFailoverTaskNodePath("test_job_1@-@1")); verify(regCenter).get(FailoverNode.getFailoverTaskNodePath("test_job_2@-@0")); }