KeysResource { @GET @Produces(MediaType.APPLICATION_JSON) public Collection<KeyValue> getAll() throws Exception { LOGGER.trace("get all keys"); try { ListenableFuture<Collection<KeyValue>> getAllFuture = distributedStore.getAll(); return getAllFuture.get(ResourceConstants.COMMAND_TIMEOUT, ResourceConstants.COMMAND_TIMEOUT_TIME_UNIT); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof NotLeaderException) { throw new CannotSubmitCommandException((NotLeaderException) cause, members); } else { throw e; } } } KeysResource(Set<ClusterMember> members, DistributedStore distributedStore); @Path("/{key:" + KEY_PATTERN + "}") KeyResource forKey(@PathParam("key") String key); @GET @Produces(MediaType.APPLICATION_JSON) Collection<KeyValue> getAll();  }
@Test public void shouldReturnACollectionOfKeyValuePairsWhenGetIsCalled() { final List<KeyValue> serverAll = ImmutableList.of( new KeyValue("KEY1", "VALUE1"), new KeyValue("KEY2", "VALUE2"), new KeyValue("KEY3", "VALUE3") ); when(distributedStore.getAll()).thenReturn(Futures.<Collection<KeyValue>>immediateFuture(serverAll)); Collection<KeyValue> clientAll = client().resource(REQUEST_URI).get(new GenericType<Collection<KeyValue>>() { }); assertThat(clientAll, containsInAnyOrder(serverAll.toArray())); }
@Test public void shouldReturnInternalServerErrorIfAnErrorIsThrownInServiceCode() { when(distributedStore.getAll()).thenThrow(new IllegalStateException()); ClientResponse response = getClientResponseForUnsuccessfulRequest(); assertThat(response.getClientResponseStatus(), equalTo(ClientResponse.Status.INTERNAL_SERVER_ERROR)); assertThat(response.getLocation(), nullValue()); }
@Test public void shouldReturnInternalServerErrorIfThereIsAnErrorInMakingTheDistributedStoresCall() { when(distributedStore.getAll()).thenReturn(Futures.<Collection<KeyValue>>immediateFailedFuture(new IllegalStateException())); ClientResponse response = getClientResponseForUnsuccessfulRequest(); assertThat(response.getClientResponseStatus(), equalTo(ClientResponse.Status.INTERNAL_SERVER_ERROR)); assertThat(response.getLocation(), nullValue()); }
@Test public void shouldReturnServiceUnavailableWithNoLocationWhenNotLeaderExceptionIsThrownAndTheLeaderIsNull() { when(distributedStore.getAll()).thenReturn(Futures.<Collection<KeyValue>>immediateFailedFuture(new NotLeaderException(self, null))); ClientResponse response = getClientResponseForUnsuccessfulRequest(); assertThat(response.getClientResponseStatus(), equalTo(ClientResponse.Status.SERVICE_UNAVAILABLE)); assertThat(response.getLocation(), nullValue()); }
@Test public void shouldReturnRedirectWithLocationWhenNotLeaderExceptionIsThrownAndTheLeaderIsSetToAValidLeader() { when(distributedStore.getAll()).thenReturn(Futures.<Collection<KeyValue>>immediateFailedFuture(new NotLeaderException(self, leader))); ClientResponse response = getClientResponseForUnsuccessfulRequest(); assertThat(response.getClientResponseStatus(), equalTo(ClientResponse.Status.MOVED_PERMANENTLY)); assertThat(response.getLocation().toString(), equalTo(leaderUrl + REQUEST_URI)); }
@Test public void shouldReturnInternalServerErrorWhenNotLeaderExceptionIsThrownAndTheLeaderIsSetToAnInvalidLeader() { when(distributedStore.getAll()).thenReturn(Futures.<Collection<KeyValue>>immediateFailedFuture(new NotLeaderException(self, "UNKNOWN_LEADER"))); ClientResponse response = getClientResponseForUnsuccessfulRequest(); assertThat(response.getClientResponseStatus(), equalTo(ClientResponse.Status.INTERNAL_SERVER_ERROR)); assertThat(response.getLocation(), nullValue()); }