KeyResource { @DELETE public void delete() throws Exception { LOGGER.info("delete: {}", key); try { ListenableFuture<Void> deleteFuture = distributedStore.delete(key); deleteFuture.get(ResourceConstants.COMMAND_TIMEOUT, ResourceConstants.COMMAND_TIMEOUT_TIME_UNIT); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof NotLeaderException) { throw createCannotSubmitCommandException((NotLeaderException) cause); } else { throw e; } } } KeyResource(String key, Set<ClusterMember> members, DistributedStore distributedStore); @GET @Produces(MediaType.APPLICATION_JSON) KeyValue get(); @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Nullable KeyValue update(SetValue setValue); @DELETE void delete();  }
@Test public void shouldCompleteSuccessfullyIfDistributedStoreDeleteCompletesSuccessfully() { when(distributedStore.delete(KEY)).thenReturn(Futures.<Void>immediateFuture(null)); ClientResponse response = client().resource(REQUEST_URI).delete(ClientResponse.class); assertThat(response.getClientResponseStatus(), equalTo(ClientResponse.Status.NO_CONTENT)); }
@Test public void shouldReturnInternalServerErrorIfDeleteFailsBecauseADatabaseExceptionIsThrownInsideDistributedStore() { when(distributedStore.delete(KEY)).thenReturn(Futures.<Void>immediateFailedFuture(new StorageException(new SQLException("DATABASE GONE")))); assertThatValidDeleteRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnInternalServerErrorIfDeleteFailsBecauseAnExceptionIsThrownInServiceCode() { when(distributedStore.delete(KEY)).thenThrow(new IllegalStateException("service code terminated unexpectedly")); assertThatValidDeleteRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnServiceUnavailableWithNoLocationIfDeleteFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToNull() { when(distributedStore.delete(KEY)).thenReturn(Futures.<Void>immediateFailedFuture(new NotLeaderException(self, null))); assertThatValidDeleteRequestReturnsErrorStatus(ClientResponse.Status.SERVICE_UNAVAILABLE); }
@Test public void shouldReturnRedirectWithLocationIfDeleteFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToAValidLeader() { when(distributedStore.delete(KEY)).thenReturn(Futures.<Void>immediateFailedFuture(new NotLeaderException(self, leader))); assertThatValidDeleteRequestReturnsErrorStatus(ClientResponse.Status.MOVED_PERMANENTLY); }
@Test public void shouldReturnInternalServerErrorWithNoLocationIfDeleteFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToAnInvalidLeader() { when(distributedStore.delete(KEY)).thenReturn(Futures.<Void>immediateFailedFuture(new NotLeaderException(self, "UNKNOWN_LEADER"))); assertThatValidDeleteRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }