package com.filestack.util;

import com.google.gson.JsonObject;
import okhttp3.*;
import okio.Buffer;
import org.junit.Assert;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Generic interceptor that matches a URL regex, checks request body params, and sends a JSON response.
 */
public class GenericInterceptor implements Interceptor {
    private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/response");

    private String urlRegex;
    private Map<String, String> params;
    private JsonObject response;

    private GenericInterceptor() {
        this.params = new HashMap<>();
        this.response = new JsonObject();
    }

    private void validateBody(Request request) throws IOException {
        RequestBody body = request.body();
        Assert.assertNotNull(body);
        Buffer buffer = new Buffer();
        body.writeTo(buffer);
        String bodyString = buffer.readUtf8();

        for (String key : params.keySet()) {
            String value = params.get(key);
            Assert.assertTrue("Missing key: " + key, bodyString.contains("name=\"" + key + "\""));
            if (value != null)
                Assert.assertTrue("Missing key: " + value, bodyString.contains(value));
        }
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String url = request.url().toString();

        if (!url.matches(urlRegex))
            return chain.proceed(request);

        validateBody(request);

        ResponseBody body = ResponseBody.create(JSON_MEDIA_TYPE, response.toString());

        return new Response.Builder()
                .protocol(Protocol.HTTP_1_1)
                .request(request)
                .code(200)
                .message("Okay")
                .body(body)
                .build();
    }

    public static class Builder {
        private GenericInterceptor interceptor = new GenericInterceptor();

        public Builder urlRegex(String urlRegex) {
            interceptor.urlRegex = urlRegex;
            return this;
        }

        public Builder addEmptyParam(String... keys) {
            for (String key : keys)
                interceptor.params.put(key, null);
            return this;
        }

        public Builder addParam(String key, String value) {
            interceptor.params.put(key, value);
            return this;
        }

        public Builder addEmptyResponse(String... keys) {
            for (String key : keys)
                interceptor.response.addProperty(key, "test");
            return this;
        }

        public Builder addEmptyResponseObject(String key, String... objectKeys) {
            JsonObject object = new JsonObject();
            for (String objectKey : objectKeys)
                object.addProperty(objectKey, "test");
            interceptor.response.add(key, object);
            return this;
        }

        public Builder addResponse(String key, Object value) {
            interceptor.response.addProperty(key, value.toString());
            return this;
        }

        public Builder addResponse(String key, JsonObject object) {
            interceptor.response.add(key, object);
            return this;
        }

        public GenericInterceptor build() {
            return interceptor;
        }
    }
}

@@ -68,72 +68,8 @@
 ort 
-okhttp3.*;%0Aimport okio.Buffer;%0Aimport org.junit.Assert;%0A%0Aimport 
 java
@@ -131,16 +131,246 @@
 util.Map
+;%0Aimport okhttp3.Interceptor;%0Aimport okhttp3.MediaType;%0Aimport okhttp3.Protocol;%0Aimport okhttp3.Request;%0Aimport okhttp3.RequestBody;%0Aimport okhttp3.Response;%0Aimport okhttp3.ResponseBody;%0Aimport okio.Buffer;%0Aimport org.junit.Assert
 ;%0A%0A/**%0A 
@@ -436,23 +436,16 @@
 est body
- params
 , and se
@@ -1251,16 +1251,36 @@
 %22 + key,
+%0A                   
  bodyStr
@@ -1348,16 +1348,18 @@
 != null)
+ %7B
 %0A       
@@ -1435,24 +1435,38 @@
 ns(value));%0A
+            %7D%0A
         %7D%0A  
@@ -1674,16 +1674,18 @@
 lRegex))
+ %7B
 %0A       
@@ -1711,32 +1711,42 @@
 roceed(request);
+%0A        %7D
 %0A%0A        valida
@@ -2323,32 +2323,99 @@
 his;%0A        %7D%0A%0A
+        /** Add params that will just be checked for existence. */%0A
         public B
@@ -2480,32 +2480,34 @@
 ring key : keys)
+ %7B
 %0A               
@@ -2542,16 +2542,30 @@
  null);%0A
+            %7D%0A
         
@@ -2728,32 +2728,107 @@
 his;%0A        %7D%0A%0A
+        /** Add fields to the JSON response with default %22test%22 values. */%0A
         public B
@@ -2900,24 +2900,26 @@
  key : keys)
+ %7B
 %0A           
@@ -2962,32 +2962,46 @@
 y(key, %22test%22);%0A
+            %7D%0A
             retu
@@ -3012,32 +3012,281 @@
 his;%0A        %7D%0A%0A
+        /**%0A         * Add a child JSON element to the response.%0A         * Each field of the child element will have default %22test%22 values.%0A         * @param key for the element field%0A         * @param objectKeys for the element itself%0A         */%0A
         public B
@@ -3448,16 +3448,18 @@
 ectKeys)
+ %7B
 %0A       
@@ -3498,32 +3498,46 @@
 ctKey, %22test%22);%0A
+            %7D%0A
             inte

Fix linter warnings for GenericInterceptor