KeyResource { @GET @Produces(MediaType.APPLICATION_JSON) public KeyValue get() throws Exception { LOGGER.info("get: {}", key); try { ListenableFuture<KeyValue> getFuture = distributedStore.get(key); return getFuture.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 if (cause instanceof KeyNotFoundException) { throw (KeyNotFoundException) 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 shouldReturnNotFoundIfGetFailsBecauseAKeyNotFoundExceptionIsThrown() { when(distributedStore.get(KEY)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new KeyNotFoundException(KEY))); assertThatValidGetRequestReturnsErrorStatus(ClientResponse.Status.NOT_FOUND); }
@Test public void shouldReturnInternalServerErrorIfGetFailsBecauseADatabaseExceptionIsThrownInsideDistributedStore() { when(distributedStore.get(KEY)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new StorageException(new SQLException("DATABASE GONE")))); assertThatValidGetRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnInternalServerErrorIfGetFailsBecauseAnExceptionIsThrownInServiceCode() { when(distributedStore.get(KEY)).thenThrow(new IllegalStateException("service code terminated unexpectedly")); assertThatValidGetRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnServiceUnavailableWithNoLocationIfGetFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToNull() { when(distributedStore.get(KEY)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new NotLeaderException(self, null))); assertThatValidGetRequestReturnsErrorStatus(ClientResponse.Status.SERVICE_UNAVAILABLE); }
@Test public void shouldReturnRedirectWithLocationIfGetFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToAValidLeader() { when(distributedStore.get(KEY)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new NotLeaderException(self, leader))); assertThatValidGetRequestReturnsErrorStatus(ClientResponse.Status.MOVED_PERMANENTLY); }
@Test public void shouldReturnInternalServerErrorWithNoLocationIfGetFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToAnInvalidLeader() { when(distributedStore.get(KEY)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new NotLeaderException(self, "UNKNOWN_LEADER"))); assertThatValidGetRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }