### Question:
FailoverService { public void add(final TaskContext taskContext) { if (regCenter.getNumChildren(FailoverNode.ROOT) > env.getFrameworkConfiguration().getJobStateQueueSize()) { log.warn("Cannot add job, caused by read state queue size is larger than {}.", env.getFrameworkConfiguration().getJobStateQueueSize()); return; } String failoverTaskNodePath = FailoverNode.getFailoverTaskNodePath(taskContext.getMetaInfo().toString()); if (!regCenter.isExisted(failoverTaskNodePath) && !runningService.isTaskRunning(taskContext.getMetaInfo())) { regCenter.persist(failoverTaskNodePath, taskContext.getId()); } } FailoverService(final CoordinatorRegistryCenter regCenter); void add(final TaskContext taskContext); Collection<JobContext> getAllEligibleJobContexts(); void remove(final Collection<TaskContext.MetaInfo> metaInfoList); Optional<String> getTaskId(final TaskContext.MetaInfo metaInfo); Map<String, Collection<FailoverTaskInfo>> getAllFailoverTasks();  }### Answer:
@Test public void assertAddWhenJobIsOverQueueSize() { Mockito.when(regCenter.getNumChildren(FailoverNode.ROOT)).thenReturn(BootstrapEnvironment.getInstance().getFrameworkConfiguration().getJobStateQueueSize() + 1); TaskNode taskNode = TaskNode.builder().type(ExecutionType.FAILOVER).build(); failoverService.add(TaskContext.from(taskNode.getTaskNodeValue())); Mockito.verify(regCenter, Mockito.times(0)).persist("/state/failover/test_job/" + taskNode.getTaskNodePath(), taskNode.getTaskNodeValue()); }
@Test public void assertAddWhenExisted() { TaskNode taskNode = TaskNode.builder().type(ExecutionType.FAILOVER).build(); Mockito.when(regCenter.isExisted("/state/failover/test_job/" + taskNode.getTaskNodePath())).thenReturn(true); failoverService.add(TaskContext.from(taskNode.getTaskNodeValue())); Mockito.verify(regCenter).isExisted("/state/failover/test_job/" + taskNode.getTaskNodePath()); Mockito.verify(regCenter, Mockito.times(0)).persist("/state/failover/test_job/" + taskNode.getTaskNodePath(), taskNode.getTaskNodeValue()); }
@Test public void assertAddWhenNotExistedAndTaskIsRunning() { TaskNode taskNode = TaskNode.builder().type(ExecutionType.FAILOVER).build(); Mockito.when(regCenter.isExisted("/state/failover/test_job/" + taskNode.getTaskNodePath())).thenReturn(false); Mockito.when(runningService.isTaskRunning(TaskContext.MetaInfo.from(taskNode.getTaskNodePath()))).thenReturn(true); failoverService.add(TaskContext.from(taskNode.getTaskNodeValue())); Mockito.verify(regCenter).isExisted("/state/failover/test_job/" + taskNode.getTaskNodePath()); Mockito.verify(runningService).isTaskRunning(TaskContext.MetaInfo.from(taskNode.getTaskNodePath())); Mockito.verify(regCenter, Mockito.times(0)).persist("/state/failover/test_job/" + taskNode.getTaskNodePath(), taskNode.getTaskNodeValue()); }
@Test public void assertAddWhenNotExistedAndTaskIsNotRunning() { TaskNode taskNode = TaskNode.builder().type(ExecutionType.FAILOVER).build(); Mockito.when(regCenter.isExisted("/state/failover/test_job/" + taskNode.getTaskNodePath())).thenReturn(false); Mockito.when(runningService.isTaskRunning(TaskContext.MetaInfo.from(taskNode.getTaskNodePath()))).thenReturn(false); failoverService.add(TaskContext.from(taskNode.getTaskNodeValue())); Mockito.verify(regCenter).isExisted("/state/failover/test_job/" + taskNode.getTaskNodePath()); Mockito.verify(runningService).isTaskRunning(TaskContext.MetaInfo.from(taskNode.getTaskNodePath())); Mockito.verify(regCenter).persist("/state/failover/test_job/" + taskNode.getTaskNodePath(), taskNode.getTaskNodeValue()); }