public void delete(String path, boolean isDeleteChildren) { try { if (!exist(path)) { return; } DeleteBuilder builder = client.delete(); if (isDeleteChildren) { builder.guaranteed().deletingChildrenIfNeeded().forPath(path); } else { builder.guaranteed().forPath(path); } } catch (Throwable e) { throw checkException(String.format("Fail to delete node of path: %s", path), e); } }
public void deleteWithoutGuaranteed(String path, boolean isDeleteChildren) { try { if (!exist(path)) { return; } DeleteBuilder builder = client.delete(); if (isDeleteChildren) { builder.deletingChildrenIfNeeded().forPath(path); } else { builder.forPath(path); } } catch (Throwable e) { throw checkException(String.format("Fail to delete node of path: %s", path), e); } }
@Test public void shouldDeleteSetupInProgressNodeAfterCompletion() throws Exception { Set<SetupStep> steps = new LinkedHashSet<>(); SetupStep setupStep1 = mock(SetupStep.class); steps.add(setupStep1); when(configuration. getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)). thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT); when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("digest:user:pwd"); List<ACL> aclList = Arrays.asList(new ACL(ZooDefs.Perms.ALL, new Id("digest", "user:pwd"))); setupServerIdSelectionMocks(); DeleteBuilder deleteBuilder = setupSetupInProgressPathMocks(aclList).getRight(); InterProcessMutex lock = mock(InterProcessMutex.class); when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)). thenReturn(lock); SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration); setupSteps.runSetup(); verify(deleteBuilder).forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE); }
@SuppressWarnings("unchecked") @Test public void testDeletesEphemeralNode() throws Exception { CuratorFramework framework = mockFramework(); ExistsBuilder ceBuilder = mock(ExistsBuilder.class); CreateBuilder createBuilder = mock(CreateBuilder.class); when(framework.checkExists()).thenReturn(ceBuilder); when(ceBuilder.forPath("/services/myservice/nodes")).thenReturn(mock(Stat.class)); when(ceBuilder.forPath("/services/myservice/nodes/foo:4321")).thenReturn(mock(Stat.class)); when(framework.create()).thenReturn(createBuilder); when(framework.getState()).thenReturn(CuratorFrameworkState.STARTED); DeleteBuilder deleteBuilder = mock(DeleteBuilder.class); when(framework.delete()).thenReturn(deleteBuilder); ACLBackgroundPathAndBytesable<String> os = mock(ACLBackgroundPathAndBytesable.class); when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(os); DiscoService service = new DiscoService(framework, "myservice"); byte[] payload = "foo bar baz bingo".getBytes(); service.start("foo", 4321, true, payload); verify(deleteBuilder).forPath("/services/myservice/nodes/foo:4321"); verify(os).forPath(eq("/services/myservice/nodes/foo:4321"), eq(payload)); }
/** * Tests that if we attempt to delete a node that doesnt actually exist * just silently returns. * * To simulate a race condition we do this using mocks. */ @Test public void testDeleteNodeIfNoChildren_withNodeThatDoesntExist() throws Exception { final String basePath = "/testDeleteNodeIfNoChildren_withNodeThatDoesntExist"; final CuratorFramework mockCurator = mock(CuratorFramework.class); // Exists builder should return true saying our basePath exists. final ExistsBuilder mockExistsBuilder = mock(ExistsBuilder.class); when(mockExistsBuilder.forPath(eq(basePath))).thenReturn(new Stat()); when(mockCurator.checkExists()).thenReturn(mockExistsBuilder); // When we look for children, make sure it returns an empty list. final GetChildrenBuilder mockGetChildrenBuilder = mock(GetChildrenBuilder.class); when(mockGetChildrenBuilder.forPath(eq(basePath))).thenReturn(new ArrayList<>()); when(mockCurator.getChildren()).thenReturn(mockGetChildrenBuilder); // When we go to delete the actual node, we toss a no-node exception. // This effectively simulates a race condition between checking if the node exists (our mock above says yes) // and it being removed before we call delete on it. final DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class); when(mockDeleteBuilder.forPath(eq(basePath))).thenThrow(new KeeperException.NoNodeException()); when(mockCurator.delete()).thenReturn(mockDeleteBuilder); // Now create our helper final CuratorHelper curatorHelper = new CuratorHelper(mockCurator); // Call our method curatorHelper.deleteNodeIfNoChildren(basePath); }
@Override public void remove(String uuid, Handler<AsyncResult<Record>> resultHandler) { Objects.requireNonNull(uuid, "No registration id in the record"); Context context = Vertx.currentContext(); ensureConnected(x -> { if (x.failed()) { resultHandler.handle(Future.failedFuture(x.cause())); } else { getRecordById(context, uuid, record -> { if (record == null) { resultHandler.handle(Future.failedFuture("Unknown registration " + uuid)); } else { try { DeleteBuilder delete = client.delete(); if (guaranteed) { delete.guaranteed(); } delete .deletingChildrenIfNeeded() .inBackground((curatorFramework, curatorEvent) -> callback(context, record, resultHandler, curatorEvent)) .withUnhandledErrorListener((s, throwable) -> resultHandler.handle(Future.failedFuture(throwable))) .forPath(getPath(uuid)); } catch (Exception e) { resultHandler.handle(Future.failedFuture(e)); } } }); } }); }
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls, Stat stat) throws Exception { when(curatorFactory.clientInstance()).thenReturn(client); CreateBuilder createBuilder = mock(CreateBuilder.class); when(createBuilder.withACL(acls)).thenReturn(createBuilder); when(client.create()).thenReturn(createBuilder); DeleteBuilder deleteBuilder = mock(DeleteBuilder.class); when(client.delete()).thenReturn(deleteBuilder); Pair<CreateBuilder, DeleteBuilder> pair = Pair.of(createBuilder, deleteBuilder); ExistsBuilder existsBuilder = mock(ExistsBuilder.class); when(client.checkExists()).thenReturn(existsBuilder); when(existsBuilder.forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE)). thenReturn(stat); return pair; }
@Test public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.INDEXING.getZookeeperRoot() + "/bro")).thenThrow(KeeperException.NoNodeException.class); assertFalse(sensorIndexingConfigService.delete("bro")); }
@Test public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception { exception.expect(RestException.class); DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.INDEXING.getZookeeperRoot() + "/bro")).thenThrow(Exception.class); assertFalse(sensorIndexingConfigService.delete("bro")); }
@Test public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.INDEXING.getZookeeperRoot() + "/bro")).thenReturn(null); assertTrue(sensorIndexingConfigService.delete("bro")); verify(curatorFramework).delete(); }
@Test public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenThrow(KeeperException.NoNodeException.class); assertFalse(globalConfigService.delete()); }
@Test public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception { exception.expect(RestException.class); DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenThrow(Exception.class); assertFalse(globalConfigService.delete()); }
@Test public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenReturn(null); assertTrue(globalConfigService.delete()); verify(curatorFramework).delete(); }
@Test public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.PARSER.getZookeeperRoot() + "/bro")).thenThrow(KeeperException.NoNodeException.class); assertFalse(sensorParserConfigService.delete("bro")); }
@Test public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception { exception.expect(RestException.class); DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.PARSER.getZookeeperRoot() + "/bro")).thenThrow(Exception.class); assertFalse(sensorParserConfigService.delete("bro")); }
@Test public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.PARSER.getZookeeperRoot() + "/bro")).thenReturn(null); assertTrue(sensorParserConfigService.delete("bro")); verify(curatorFramework).delete(); }
@Test public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.ENRICHMENT.getZookeeperRoot() + "/bro")).thenThrow(KeeperException.NoNodeException.class); assertFalse(sensorEnrichmentConfigService.delete("bro")); }
@Test public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception { exception.expect(RestException.class); DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.ENRICHMENT.getZookeeperRoot() + "/bro")).thenThrow(Exception.class); assertFalse(sensorEnrichmentConfigService.delete("bro")); }
@Test public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.ENRICHMENT.getZookeeperRoot() + "/bro")).thenReturn(null); assertTrue(sensorEnrichmentConfigService.delete("bro")); verify(curatorFramework).delete(); }
/** * Test deleteNode method * @throws Exception */ @Test public void testDeleteNode() throws Exception { CuratorStateManager spyStateManager = spy(new CuratorStateManager()); CuratorFramework mockClient = mock(CuratorFramework.class); DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class); // Mockito doesn't support mock type-parametrized class, thus suppress the warning @SuppressWarnings("rawtypes") BackgroundPathable mockBackPathable = mock(BackgroundPathable.class); doReturn(mockClient) .when(spyStateManager).getCuratorClient(); doReturn(true) .when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class)); doReturn(mockDeleteBuilder) .when(mockClient).delete(); doReturn(mockBackPathable) .when(mockDeleteBuilder).withVersion(-1); spyStateManager.initialize(config); ListenableFuture<Boolean> result = spyStateManager.deleteExecutionState(PATH); // Verify the node is deleted correctly verify(mockDeleteBuilder).withVersion(-1); assertTrue(result.get()); }
@Test public void testStop() throws Exception { Stat stat = new Stat(); String path = "/services/myservice/nodes/foo:1234"; CuratorFramework framework = mockFramework(); ExistsBuilder existsBuilder = mock(ExistsBuilder.class); when(existsBuilder.forPath(path)).thenReturn(stat); when(framework.checkExists()).thenReturn(existsBuilder); DeleteBuilder deleteBuilder = mock(DeleteBuilder.class); when(framework.delete()).thenReturn(deleteBuilder); DiscoService manager = new DiscoService(framework, "myservice"); manager.node = path; manager.stop(); verify(deleteBuilder).forPath(path); }
@Override public DeleteBuilder delete() { return new MockDeleteBuilder(); }
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls) throws Exception { return setupSetupInProgressPathMocks(acls, null); }
@Override public DeleteBuilder delete() { return namespaceDelegate().delete(); }