KeyResource { private KeyValue set(SetValue setValue) throws Exception { LOGGER.info("set: {}->{}", key, setValue.getNewValue()); checkArgument(setValue.getNewValue() != null, "key:%s - null newValue: setValue:%s", key, setValue); try { ListenableFuture<KeyValue> setFuture = distributedStore.set(key, setValue); return setFuture.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 shouldReturnRedirectWithLocationIfSetFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToAValidLeader() { when(distributedStore.set(KEY, setValue)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new NotLeaderException(self, leader))); assertThatValidSetRequestReturnsErrorStatus(ClientResponse.Status.MOVED_PERMANENTLY); }
@Test public void shouldReturnInternalServerErrorWithNoLocationIfSetFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToAnInvalidLeader() { when(distributedStore.set(KEY, setValue)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new NotLeaderException(self, "UNKNOWN_LEADER"))); assertThatValidSetRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnBadRequestIfSetFailsBecauseNewValueIsNull() { when(distributedStore.set(KEY, setValue)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new StorageException(new SQLException("DATABASE GONE")))); SetValue nulledValue = new SetValue(); nulledValue.setNewValue(null); assertThatResourceMethodReturnsStatus( client().resource(REQUEST_URI).type(MediaType.APPLICATION_JSON_TYPE).entity(nulledValue), "PUT", ClientResponse.Status.BAD_REQUEST); }
@Test public void shouldReturnInternalServerErrorIfSetFailsBecauseADatabaseExceptionIsThrownInsideDistributedStore() { when(distributedStore.set(KEY, setValue)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new StorageException(new SQLException("DATABASE GONE")))); assertThatValidSetRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnInternalServerErrorIfSetFailsBecauseAnExceptionIsThrownInServiceCode() { when(distributedStore.set(KEY, setValue)).thenThrow(new IllegalStateException("service code terminated unexpectedly")); assertThatValidSetRequestReturnsErrorStatus(ClientResponse.Status.INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnServiceUnavailableWithNoLocationIfSetFailsBecauseNotLeaderExceptionIsThrownWithTheLeaderSetToNull() { when(distributedStore.set(KEY, setValue)).thenReturn(Futures.<KeyValue>immediateFailedFuture(new NotLeaderException(self, null))); assertThatValidSetRequestReturnsErrorStatus(ClientResponse.Status.SERVICE_UNAVAILABLE); }