GlobalConfigServiceImpl implements GlobalConfigService { @Override public boolean delete() throws RestException { try { client.delete().forPath(ConfigurationType.GLOBAL.getZookeeperRoot()); } catch (KeeperException.NoNodeException e) { return false; } catch (Exception e) { throw new RestException(e); } return true; } @Autowired  GlobalConfigServiceImpl(CuratorFramework client, ConfigurationsCache cache); void setCache(ConfigurationsCache cache); @Override Map<String, Object> save(Map<String, Object> globalConfig); @Override Map<String, Object> get(); @Override boolean 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 { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); when(builder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenThrow(Exception.class); assertThrows(RestException.class, () -> assertFalse(globalConfigService.delete())); }
@Test public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception { DeleteBuilder builder = mock(DeleteBuilder.class); when(curatorFramework.delete()).thenReturn(builder); assertTrue(globalConfigService.delete()); verify(curatorFramework).delete(); }