@SuppressWarnings("unchecked") @Test public void testStart() 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(framework.create()).thenReturn(createBuilder); when(framework.getState()).thenReturn(CuratorFrameworkState.STARTED); 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(os).forPath(eq("/services/myservice/nodes/foo:4321"), eq(payload)); }
@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)); }
public static void createPath(CuratorFramework client, String path, String content, CreateMode mode) { try { ((ACLBackgroundPathAndBytesable) client.create().creatingParentsIfNeeded().withMode(mode)) .forPath(path, List2StringUtil.toBytes(content)); } catch (Exception e) { LOGGER.error("ZKUtil-->>createPath(CuratorFramework client, String path, String content, CreateMode mode) error,", e); } }
public static void setPath(CuratorFramework client, String path, String content, CreateMode mode) { try { if (client.checkExists().forPath(path) == null) { ((ACLBackgroundPathAndBytesable) client.create().creatingParentsIfNeeded().withMode(mode)) .forPath(path, List2StringUtil.toBytes(content)); } else { client.setData().forPath(path, List2StringUtil.toBytes(content)); } } catch (Exception e) { LOGGER.error("ZKUtil-->>setPath(CuratorFramework client, String path, String content, CreateMode mode) error,", e); } }
public static String createEphemeralSequential(CuratorFramework client, String path, byte[] payload) { try { return (String) ((ACLBackgroundPathAndBytesable) client.create().withProtection() .withMode(CreateMode.EPHEMERAL_SEQUENTIAL)) .forPath(path, payload); } catch (Exception e) { LOGGER.error("ZKUtil-->>createEphemeralSequential", e); } return null; }
/** * test createNode method * @throws Exception */ @Test public void testCreateNode() throws Exception { CuratorStateManager spyStateManager = spy(new CuratorStateManager()); CuratorFramework mockClient = mock(CuratorFramework.class); CreateBuilder mockCreateBuilder = mock(CreateBuilder.class); // Mockito doesn't support mock type-parametrized class, thus suppress the warning @SuppressWarnings("rawtypes") ACLBackgroundPathAndBytesable mockPath = spy(ACLBackgroundPathAndBytesable.class); final byte[] data = new byte[10]; doReturn(mockClient) .when(spyStateManager).getCuratorClient(); doReturn(true) .when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class)); doReturn(mockCreateBuilder) .when(mockClient).create(); doReturn(mockPath) .when(mockCreateBuilder).withMode(any(CreateMode.class)); spyStateManager.initialize(config); // Verify the node is created successfully ListenableFuture<Boolean> result = spyStateManager.createNode(PATH, data, false); verify(mockCreateBuilder).withMode(any(CreateMode.class)); verify(mockPath).forPath(PATH, data); assertTrue(result.get()); }
public ACLBackgroundPathAndBytesable<T> withMode(CreateMode createMode) { throw new UnsupportedOperationException("Not implemented in MockCurator"); }
public ACLBackgroundPathAndBytesable<String> withMode(CreateMode createMode) { this.createMode = createMode; return this; }