/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.apache.hive.jdbc.miniHS2;

import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.hive.common.metrics.common.MetricsFactory;
import org.apache.hadoop.hive.common.metrics.metrics2.CodahaleMetrics;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hive.jdbc.HttpBasicAuthInterceptor;
import org.apache.hive.service.rpc.thrift.TCLIService;
import org.apache.hive.service.rpc.thrift.TCloseSessionReq;
import org.apache.hive.service.rpc.thrift.TOpenSessionReq;
import org.apache.hive.service.rpc.thrift.TOpenSessionResp;
import org.apache.hive.service.rpc.thrift.TSessionHandle;
import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransport;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test the connection metrics using an HttpClient, when HS2 is start in http mode.
 */
public class TestHs2ConnectionMetricsHttp extends Hs2ConnectionMetrics {

  @BeforeClass
  public static void setup() throws Exception {
    confOverlay.clear();
    confOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname, "http");
    confOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH.varname, "cliservice");
    Hs2ConnectionMetrics.setup();
  }

  @AfterClass
  public static void tearDown() {
    Hs2ConnectionMetrics.tearDown();
  }

  @Test
  public void testOpenConnectionMetrics() throws Exception {
    CodahaleMetrics metrics = (CodahaleMetrics) MetricsFactory.getInstance();

    TCLIService.Client httpClient = getHttpClient();
    TOpenSessionReq openSessionReq = new TOpenSessionReq();
    TOpenSessionResp tOpenSessionResp = httpClient.OpenSession(openSessionReq);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 1);
    TSessionHandle sessionHandle = tOpenSessionResp.getSessionHandle();

    TCloseSessionReq closeSessionReq = new TCloseSessionReq(sessionHandle);
    httpClient.CloseSession(closeSessionReq);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 2);

    tOpenSessionResp = httpClient.OpenSession(openSessionReq);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 3);
    sessionHandle = tOpenSessionResp.getSessionHandle();

    closeSessionReq = new TCloseSessionReq(sessionHandle);
    httpClient.CloseSession(closeSessionReq);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 4);

  }

  private TCLIService.Client getHttpClient() throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    Map<String, String> headers = new HashMap<>();
    headers.put("Connection", "close");
    httpClient.addRequestInterceptor(new BasicHttpRequestInterceptor(USERNAME, PASSWORD, null,
            null, false, headers));

    TTransport transport = new THttpClient(getHttpUrl(), httpClient);
    TProtocol protocol = new TBinaryProtocol(transport);
    return new TCLIService.Client(protocol);
  }

  private String getHttpUrl() {
    return "http://" + miniHS2.getHost() + ":" + miniHS2.getHttpPort() + "/cliservice/";

  }

  private class BasicHttpRequestInterceptor extends HttpBasicAuthInterceptor {
    BasicHttpRequestInterceptor(String userName, String password, CookieStore cookieStore,
                                String cn, boolean isSSL,
                                Map<String, String> additionalHeaders) {
      super(userName, password, cookieStore, cn, isSSL, additionalHeaders, null);
    }
  }

}

@@ -1761,24 +1761,49 @@
 eforeClass;%0A
+import org.junit.Ignore;%0A
 import org.j
@@ -2373,16 +2373,76 @@
 );%0A  %7D%0A%0A
+  @Ignore(%22Flaky test. Should be re-enabled in HIVE-20578%22)%0A
   @Test%0A

Disable org.apache.hive.jdbc.miniHS2.TestHs2ConnectionMetricsHttp.testOpenConnectionMetrics (Vaibhav Gumashta reviewed by Daniel Dai)