/**
 * Copyright 2016 Palantir Technologies
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.palantir.atlasdb.http;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import feign.Response;
import feign.RetryableException;
import feign.codec.ErrorDecoder;

public class AtlasDbErrorDecoderTest {
    private static final String EMPTY_METHOD_KEY = "";

    private static final Exception NON_RETRYABLE_EXCEPTION = new Exception();
    private static final Exception RETRYABLE_EXCEPTION = new RetryableException("MyException", new Date());

    private static final int STATUS_503 = 503;
    private static final int STATUS_NOT_503 = 511;

    ErrorDecoder defaultDecoder;
    AtlasDbErrorDecoder atlasDbDecoder;

    @Before
    public void setUp() {
        defaultDecoder = mock(ErrorDecoder.class);
        atlasDbDecoder = new AtlasDbErrorDecoder(defaultDecoder);
    }

    @Test
    public void shouldCreateNewRetryableExceptionWithNullRetryAfterWhen503AndNotRetryableException() {
        Response response = createResponse(STATUS_503);
        makeDefaultDecoderReplyWhenReceivingResponse(response, NON_RETRYABLE_EXCEPTION);

        Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response);

        assertNull(((RetryableException) exception).retryAfter());
    }

    @Test
    public void shouldDelegateToDefaultDecoderWhen503AndRetryableException() {
        Response response = createResponse(STATUS_503);
        makeDefaultDecoderReplyWhenReceivingResponse(response, RETRYABLE_EXCEPTION);

        Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response);

        assertThat(exception, is(sameInstance(RETRYABLE_EXCEPTION)));
    }

    @Test
    public void shouldDelegateToDefaultDecoderWhenNeither503NorRetryableException() {
        Response response = createResponse(STATUS_NOT_503);
        makeDefaultDecoderReplyWhenReceivingResponse(response, NON_RETRYABLE_EXCEPTION);

        Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response);

        assertThat(exception, is(sameInstance(NON_RETRYABLE_EXCEPTION)));
    }

    @Test
    public void shouldDelegateToDefaultDecoderWhenNot503AndRetryableException() {
        Response response = createResponse(STATUS_NOT_503);
        makeDefaultDecoderReplyWhenReceivingResponse(response, RETRYABLE_EXCEPTION);

        Exception exception = atlasDbDecoder.decode(EMPTY_METHOD_KEY, response);

        assertThat(exception, is(sameInstance(RETRYABLE_EXCEPTION)));
    }

    private void makeDefaultDecoderReplyWhenReceivingResponse(Response response, Exception exception) {
        when(defaultDecoder.decode(EMPTY_METHOD_KEY, response))
                .thenReturn(exception);
    }

    private static Response createResponse(int status) {
        Map<String, Collection<String>> emptyHeaders = new HashMap<>();
        String emptyReason = "";
        byte[] emptyBody = new byte[0];
        return Response.create(status, emptyReason, emptyHeaders, emptyBody);
    }
}
@@ -1876,44 +1876,8 @@
 se =
- createResponse(STATUS_503);%0A       
  mak
@@ -1914,32 +1914,34 @@
 ingResponse(
-response
+STATUS_503
 , NON_RETRYA
@@ -2233,44 +2233,8 @@
 se =
- createResponse(STATUS_503);%0A       
  mak
@@ -2271,32 +2271,34 @@
 ingResponse(
-response
+STATUS_503
 , RETRYABLE_
@@ -2596,48 +2596,8 @@
 se =
- createResponse(STATUS_NOT_503);%0A       
  mak
@@ -2638,24 +2638,30 @@
 esponse(
-response
+STATUS_NOT_503
 , NON_RE
@@ -2967,48 +2967,8 @@
 se =
- createResponse(STATUS_NOT_503);%0A       
  mak
@@ -3009,24 +3009,30 @@
 esponse(
-response
+STATUS_NOT_503
 , RETRYA
@@ -3219,20 +3219,24 @@
 private 
-void
+Response
  makeDef
@@ -3273,33 +3273,26 @@
 esponse(
-Response response
+int status
 , Except
@@ -3304,24 +3304,76 @@
 xception) %7B%0A
+        Response response = createResponse(status);%0A
         when
@@ -3424,16 +3424,16 @@
 ponse))%0A
-
         
@@ -3464,16 +3464,41 @@
 ption);%0A
+        return response;%0A
     %7D%0A%0A 

Refactor to get rid of some code reuse