package com.ibm.mobilefirstplatform.clientsdk.android.analytics.internal;

import com.ibm.mobilefirstplatform.clientsdk.android.analytics.internal.BMSAnalytics;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Interceptor;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

public class NetworkLoggingInterceptorTest {
  final CountDownLatch latch = new CountDownLatch(1);
  
  @Test
  public void testInterceptor() throws Exception{ 
           
    MockWebServer mockServer = new MockWebServer();
    MockResponse mresponse = new MockResponse().setResponseCode(200);
    for (int i = 0; i <= 10; i++) {
        mockServer.enqueue(mresponse);
    }
    mockServer.start();
    
    String urlString = mockServer.url("").toString();

    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(10, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    
    
    BMSAnalytics.isRecordingNetworkEvents = true;
    NetworkLoggingInterceptorLocal interceptor = new NetworkLoggingInterceptorLocal();
    
    client.interceptors().add(interceptor);

    Request request = new Request.Builder().url(urlString).build();
    
    Response response = client.newCall(request).execute();
    System.out.println(response.toString());
    mockServer.shutdown();
    
    assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
        
    assertEquals("GET",interceptor.getRequestMethod());
    assertEquals(200,interceptor.getResponseCode());
    assertEquals("network",interceptor.getCategory());                
  }  
  
  class NetworkLoggingInterceptorLocal extends NetworkLoggingInterceptor{
    public String requestMethod = "";
    public int responseCode = 0;
    public String category = "";
    @Override public com.squareup.okhttp.Response intercept(Interceptor.Chain chain) throws IOException {
      com.squareup.okhttp.Response response =  super.intercept(chain);
      return response;
    }
    @Override
    protected JSONObject generateRoundTripRequestAnalyticsMetadata(Request request, long startTime, String trackingID, Response response) throws IOException {
        JSONObject metadata = super.generateRoundTripRequestAnalyticsMetadata(request,startTime,trackingID,response);
        try{
          requestMethod = metadata.getString("$requestMethod");
          responseCode = metadata.getInt("$responseCode");
          category = metadata.getString("$category");                
        }catch(JSONException e){
          
        }
        latch.countDown();
        return metadata;
    }
    
    public String getRequestMethod(){
        return requestMethod;
    }
    
    public int getResponseCode(){
        return responseCode;
    }
    
    public String getCategory(){
        return category;
    }      
  }
}
@@ -678,102 +678,12 @@
 sert
-False;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.assertNotNull
+True
 ;%0A%0Ap
@@ -732,13 +732,16 @@
 %7B%0A  
-final
+ private
  Cou
@@ -761,32 +761,8 @@
 atch
- = new CountDownLatch(1)
 ;%0A  
@@ -821,27 +821,55 @@
 ption%7B %0A    
-   
+latch = new CountDownLatch(1);%0A
     %0A    Moc
@@ -2154,36 +2154,16 @@
  public 
-com.squareup.okhttp.
 Response
@@ -2225,36 +2225,16 @@
 %7B%0A      
-com.squareup.okhttp.
 Response

Improve Code quality