package util;

import exception.BadRequestException;
import exception.FilestackIOException;
import exception.HandleNotFoundException;
import exception.PolicySignatureException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;

import static util.MockConstants.*;

/**
 * Tests {@link FailedResponseInterceptor FailedResponseInterceptor} class to ensure correct exceptions are thrown.
 */
public class TestFailedResponseInterceptor {
    private static OkHttpClient client;

    @BeforeClass
    public static void setupClient() {
        client = new OkHttpClient.Builder()
                .addInterceptor(new FailedResponseInterceptor())
                .addInterceptor(new MockInterceptor())
                .build();
    }

    @Test(expected = BadRequestException.class)
    public void testBadRequestResponse() throws IOException {
        Request request = new Request.Builder()
                .url(TEST_BAD_REQUEST_URL)
                .build();

        client.newCall(request).execute();
    }

    @Test(expected = PolicySignatureException.class)
    public void testForbiddenResponse() throws IOException {
        Request request = new Request.Builder()
                .url(TEST_FORBIDDEN_URL)
                .build();

        client.newCall(request).execute();
    }

    @Test(expected = HandleNotFoundException.class)
    public void testNotFoundResponse() throws IOException {
        Request request = new Request.Builder()
                .url(TEST_NOT_FOUND_URL)
                .build();

        client.newCall(request).execute();
    }

    @Test(expected = FilestackIOException.class)
    public void testUnmatchedResponse() throws IOException {
        Request request = new Request.Builder()
                .url(TEST_UNMATCHED_URL)
                .build();

        client.newCall(request).execute();
    }
}

@@ -19,244 +19,17 @@
 ort 
-exception.BadRequestException;%0Aimport exception.FilestackIOException;%0Aimport exception.HandleNotFoundException;%0Aimport exception.PolicySignatureException;%0Aimport okhttp3.OkHttpClient;%0Aimport okhttp3.Request;%0Aimport org.junit.BeforeClass
+okhttp3.*
 ;%0Aim
@@ -97,28 +97,35 @@
 tic 
-util.MockConstants.*
+org.junit.Assert.assertTrue
 ;%0A%0A/
@@ -216,16 +216,8 @@
 ure 
-correct 
 exce
@@ -288,268 +288,402 @@
 r %7B%0A
-    priva
+%0A    @Test%0A    public void 
 te
- 
 st
-atic OkHttpClient client;%0A%0A    @BeforeClass%0A    public static void setupClient() %7B%0A        client = new OkHttpClient.Builder()%0A                .addInterceptor(new FailedResponseInterceptor())%0A                .addInterceptor(new MockIntercepto
+WithBody() %7B%0A        Interceptor interceptor = new Interceptor() %7B%0A            @Override%0A            public Response intercept(Chain chain) throws IOException %7B%0A                MediaType mediaType = MediaType.parse(%22text/plain%22);%0A                ResponseBody body = ResponseBody.create(mediaType, %22test error body%22);%0A%0A                return new Response.Builde
 r()
-)
 %0A   
@@ -699,226 +699,220 @@
     
-.build();%0A    %7D%0A%0A    @Test(expected = BadRequestException.class)%0A    public void testBadRequestResponse() throws IOException %7B%0A        Request request = new Request.Builder()%0A                .url(TEST_BAD_REQUEST_URL)%0A
+        .protocol(Protocol.HTTP_1_1)%0A                        .request(chain.request())%0A                        .code(400)%0A                        .message(%22Bad Request%22)%0A                        .body(body)%0A      
     
@@ -915,32 +915,34 @@
                 
+  
 .build();%0A%0A     
@@ -931,33 +931,32 @@
   .build();%0A
-%0A
         
 client.newCa
@@ -947,163 +947,137 @@
     
-client.newCall(request).execute();%0A    %7D
+    %7D%0A        %7D;
 %0A%0A    
+ 
-@Test(expected = PolicySignatureException.class)%0A    public void testForbiddenResponse() throws IOException %7B
+   OkHttpClient httpClient = Networking.getHttpClient().newBuilder().addInterceptor(interceptor).build();%0A
 %0A   
@@ -1124,49 +1124,49 @@
 er()
-%0A                .url(TEST_FORBIDDEN_URL)
+.url(%22https://www.example.com%22).build();%0A
 %0A   
@@ -1166,32 +1166,38 @@
 );%0A%0A        
+try %7B%0A
         
 .build();%0A%0A 
@@ -1184,36 +1184,25 @@
         
-.build();%0A%0A        c
+    httpC
 lient.ne
@@ -1235,100 +1235,292 @@
     
-%7D%0A%0A
     
-@Test(expected = HandleNotFoundException.class)%0A    public void testNotFoundResponse(
+%7D catch (IOException e) %7B%0A            assertTrue(e.getMessage().equals(%22test error body%22));%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testWithoutBody() %7B%0A        Interceptor interceptor = new Interceptor() %7B%0A            @Override%0A            public Response intercept(Chain chain
 ) th
@@ -1550,37 +1550,35 @@
     
-Request request =
+        return
  new Re
-quest
+sponse
 .Bui
@@ -1604,219 +1604,349 @@
     
+     
-.url(TEST_NOT_FOUND_URL)%0A                .build();%0A%0A        client.newCall(request).execute();%0A    %7D%0A%0A    @Test(expected = FilestackIOException.class)%0A    public void testUnmatchedResponse() throws IOException %7B
+   .protocol(Protocol.HTTP_1_1)%0A                        .request(chain.request())%0A                        .code(400)%0A                        .message(%22Bad Request%22)%0A                        .build();%0A            %7D%0A        %7D;%0A%0A        OkHttpClient httpClient = Networking.getHttpClient().newBuilder().addInterceptor(interceptor).build();%0A
 %0A   
@@ -1985,32 +1985,73 @@
 st.Builder()
+.url(%22https://www.example.com%22).build();%0A
 %0A       
          .ur
@@ -2030,32 +2030,39 @@
 uild();%0A%0A       
+ try %7B%0A
          .url(TE
@@ -2058,102 +2058,175 @@
     
-.url(TEST_UNMATCHED_URL)%0A                .build();%0A%0A        client.newCall(request).execute();
+   httpClient.newCall(request).execute();%0A        %7D catch (IOException e) %7B%0A            assertTrue(e.getMessage().equals(%22www.example.com 400 Bad Request%22));%0A        %7D
 %0A   

Update test for FailedResponseInterceptor