JobOperateAPIImpl implements JobOperateAPI { @Override public Collection<String> remove(final Optional<String> jobName, final Optional<String> serverIp) { return jobOperatorTemplate.operate(jobName, serverIp, new JobOperateCallback() { @Override public boolean doOperate(final String jobName, final String serverIp) { JobNodePath jobNodePath = new JobNodePath(jobName); if (registryCenter.isExisted(jobNodePath.getServerNodePath(serverIp, JobNodePath.STATUS_NODE))) { return false; } registryCenter.remove(jobNodePath.getServerNodePath(serverIp)); if (registryCenter.getChildrenKeys(jobNodePath.getServerNodePath()).isEmpty()) { registryCenter.remove("/" + jobName); } return true; } }); } JobOperateAPIImpl(final CoordinatorRegistryCenter registryCenter); @Override void trigger(final Optional<String> jobName, final Optional<String> serverIp); @Override void pause(final Optional<String> jobName, final Optional<String> serverIp); @Override void resume(final Optional<String> jobName, final Optional<String> serverIp); @Override void disable(final Optional<String> jobName, final Optional<String> serverIp); @Override void enable(final Optional<String> jobName, final Optional<String> serverIp); @Override void shutdown(final Optional<String> jobName, final Optional<String> serverIp); @Override Collection<String> remove(final Optional<String> jobName, final Optional<String> serverIp);  }
@Test public void assertRemoveWithJobNameAndServerIpWhenJobStillAlive() { when(registryCenter.isExisted("/testJob/servers/localhost/status")).thenReturn(true); when(registryCenter.getChildrenKeys("/testJob/servers")).thenReturn(Collections.<String>emptyList()); assertThat(jobOperateAPI.remove(Optional.of("testJob"), Optional.of("localhost")), Is.<Collection<String>>is(Collections.singletonList("localhost"))); verify(registryCenter).isExisted("/testJob/servers/localhost/status"); }
@Test public void assertRemoveWithJobNameAndServerIpWhenIsLastInstance() { when(registryCenter.isExisted("/testJob/servers/localhost/status")).thenReturn(false); when(registryCenter.getChildrenKeys("/testJob/servers")).thenReturn(Collections.<String>emptyList()); assertThat(jobOperateAPI.remove(Optional.of("testJob"), Optional.of("localhost")), Is.<Collection<String>>is(Collections.<String>emptyList())); verify(registryCenter).isExisted("/testJob/servers/localhost/status"); verify(registryCenter).getChildrenKeys("/testJob/servers"); verify(registryCenter).remove("/testJob/servers/localhost"); verify(registryCenter).remove("/testJob"); }
@Test public void assertRemoveWithJobNameAndServerIpWhenIsNotLastInstance() { when(registryCenter.isExisted("/testJob/servers/localhost/status")).thenReturn(false); when(registryCenter.getChildrenKeys("/testJob/servers")).thenReturn(Collections.singletonList("other_host")); assertThat(jobOperateAPI.remove(Optional.of("testJob"), Optional.of("localhost")), Is.<Collection<String>>is(Collections.<String>emptyList())); verify(registryCenter).isExisted("/testJob/servers/localhost/status"); verify(registryCenter).getChildrenKeys("/testJob/servers"); verify(registryCenter).remove("/testJob/servers/localhost"); verify(registryCenter, times(0)).remove("/testJob"); }
@Test public void assertRemoveWithJobName() { when(registryCenter.isExisted("/testJob/servers/ip1/status")).thenReturn(false); when(registryCenter.isExisted("/testJob/servers/ip2/status")).thenReturn(true); when(registryCenter.getChildrenKeys("/testJob/servers")).thenReturn(Arrays.asList("ip1", "ip2")); assertThat(jobOperateAPI.remove(Optional.of("testJob"), Optional.<String>absent()), Is.<Collection<String>>is(Collections.singletonList("ip2"))); verify(registryCenter).isExisted("/testJob/servers/ip1/status"); verify(registryCenter).isExisted("/testJob/servers/ip2/status"); verify(registryCenter, times(2)).getChildrenKeys("/testJob/servers"); verify(registryCenter).remove("/testJob/servers/ip1"); verify(registryCenter, times(0)).remove("/testJob/servers/ip2"); }
@Test public void assertRemoveWithServerIp() { when(registryCenter.isExisted("/testJob1/servers/localhost/status")).thenReturn(false); when(registryCenter.isExisted("/testJob2/servers/localhost/status")).thenReturn(true); when(registryCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("testJob1", "testJob2")); assertThat(jobOperateAPI.remove(Optional.<String>absent(), Optional.of("localhost")), Is.<Collection<String>>is(Collections.singletonList("testJob2"))); verify(registryCenter).getChildrenKeys("/"); verify(registryCenter).isExisted("/testJob1/servers/localhost/status"); verify(registryCenter).isExisted("/testJob2/servers/localhost/status"); verify(registryCenter).remove("/testJob1/servers/localhost"); verify(registryCenter, times(0)).remove("/testJob2/servers/localhost"); }