GenericCommand extends HystrixCommand<T> { @Override protected T run() throws Exception { final HttpRequestBase request = httpCall.get(); addCommandHeaders(request); addAuthorizationHeaders(request); addLocaleHeaders(request); final Span requestSpan = addTrackingHeaders(request); try { final HttpResponse response; try { LOGGER.trace("Requesting {} {}", request.getMethod(), request.getURI()); response = client.execute(request); } catch (Exception e) { throw onError.apply(e); } commandResponseHeaders = response.getAllHeaders(); status = HttpStatus.valueOf(response.getStatusLine().getStatusCode()); Header cookies = response.getFirstHeader("Set-Cookie"); if (cookies != null) { LOGGER.warn( "request {} {}: Cookie detected in responseHeaders (check security.oauth2.resource.uri settings)", request.getMethod(), request.getURI()); } BiFunction<HttpRequestBase, HttpResponse, T> function = behavior.get(status); if (function != null) { try { return function.apply(request, response); } catch (Exception e) { throw onError.apply(e); } } if (status.is4xxClientError() || status.is5xxServerError()) { LOGGER.debug("request {} {} : response on error {}", request.getMethod(), request.getURI(), response.getStatusLine()); return callOnError(onError).apply(request, response); } else { return behavior.getOrDefault(status, missingBehavior()).apply(request, response); } } finally { tracer.close(requestSpan); } } protected  GenericCommand(final HystrixCommandGroupKey group); protected  GenericCommand(final HystrixCommandGroupKey group, final Map<String, String> headers); String getAuthenticationToken(); Header[] getCommandResponseHeaders(); Header getHeader(String name); HttpStatus getStatus(); static final HystrixCommandGroupKey DATASET_GROUP; static final HystrixCommandGroupKey PREPARATION_GROUP; static final HystrixCommandGroupKey TRANSFORM_GROUP; static final HystrixCommandGroupKey FULLRUN_GROUP; static final HystrixCommandGroupKey ASYNC_GROUP; static final HystrixCommandGroupKey USER_GROUP; }
@Test public void testSuccess() throws Exception { final GenericCommand<String> command = getCommand("http: final String result = command.run(); assertThat(result, is("success")); assertThat(lastException, nullValue()); }
@Test public void testGenericSuccess() throws Exception { final GenericCommand<String> command = getGroupedCommand("http: final String result = command.run(); assertThat(result, is("success")); assertThat(lastException, nullValue()); }
@Test public void testLanguageHeader() throws Exception { LocaleContextHolder.setLocale(Locale.JAPANESE); final GenericCommand<String> command = getCommand("http: final String result = command.run(); assertThat(result, is("ja")); }
@Test public void testSuccessWithMissingBehavior() throws Exception { final GenericCommand<String> command = getCommand( "http: final String result = command.run(); assertThat(result, nullValue()); assertThat(lastException, nullValue()); }
@Test public void testFail_With_400() throws Exception { GenericCommand<String> command = getCommand("http: try { command.run(); } catch (Exception e) { assertThat(e.getCause(), is(lastException)); assertThat(lastException, isA(TDPException.class)); assertThat(lastException.getCode().getCode(), is(BaseErrorCodes.MISSING_ACTION_SCOPE.getCode())); assertThat(lastException.getCode().getHttpStatus(), is(400)); } }
@Test public void testFail_With_404() throws Exception { GenericCommand<String> command = getCommand("http: try { command.run(); } catch (Exception e) { assertThat(e.getCause(), is(lastException)); assertThat(lastException, isA(TDPException.class)); assertThat(lastException.getCode().getCode(), is(CommonErrorCodes.UNEXPECTED_EXCEPTION.getCode())); assertThat(lastException.getCode().getHttpStatus(), is(404)); } }
@Test public void testFail_With_500() throws Exception { GenericCommand<String> command = getCommand("http: try { command.run(); } catch (Exception e) { assertThat(e.getCause(), is(lastException)); assertThat(lastException, isA(TDPException.class)); assertThat(lastException.getCode().getCode(), is(CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON.getCode())); assertThat(lastException.getCode().getHttpStatus(), is(500)); } }
@Test public void testFail_With_Unknown() throws Exception { GenericCommand<String> command = getCommand("http: try { command.run(); } catch (Exception e) { assertThat(e.getCause(), is(lastException)); assertThat(lastException, isA(TDPException.class)); assertThat(lastException.getCode().getProduct(), is(CommonErrorCodes.UNEXPECTED_EXCEPTION.getProduct())); assertThat(lastException.getCode().getCode(), is(CommonErrorCodes.UNEXPECTED_EXCEPTION.getCode())); assertThat(lastException.getCode().getHttpStatus(), is(418)); } }
@Test public void testPassThrough() throws Exception { GenericCommand<String> command = getCommand("http: try { command.run(); } catch (TDPException e) { assertThat(lastException, nullValue()); assertThat(e.getCode().getCode(), is(CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON.getCode())); } }
@Test public void testUnexpected() throws Exception { GenericCommand<String> command = getCommand("http: try { command.run(); } catch (TDPException e) { assertThat(lastException, nullValue()); assertThat(e.getCode().getCode(), is(CommonErrorCodes.UNEXPECTED_SERVICE_EXCEPTION.getCode())); final Iterator<Map.Entry<String, Object>> entries = e.getContext().entries().iterator(); assertThat(entries.hasNext(), is(true)); final Map.Entry<String, Object> next = entries.next(); assertThat(next.getKey(), is("message")); assertThat(String.valueOf(next.getValue()), is("Unable to execute an operation")); } }