/*
 * Copyright 2019 NAVER Corp.
 *
 * Licensed 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 com.navercorp.pinpoint.plugin.hbase;

import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.pluginit.utils.AgentPath;
import com.navercorp.pinpoint.test.plugin.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.annotation;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event;
import static org.mockito.Mockito.doReturn;

@RunWith(PinpointPluginTestSuite.class)
@PinpointAgent(AgentPath.PATH)
@JvmVersion(8)
@Dependency({"org.apache.hbase:hbase-shaded-client:[1.2.6.1]", "org.mockito:mockito-core:2.7.22"})
@ImportPlugin("com.navercorp.pinpoint:pinpoint-hbase-plugin")
@PinpointConfig("hbase/pinpoint-hbase-test.config")
public class HbaseClientIT {

    @Mock
    private ClusterConnection connection;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testAdmin() throws Exception {

        doReturn(new Configuration()).when(connection).getConfiguration();

        Admin admin = new HBaseAdmin(connection);

        try {
            admin.tableExists(TableName.valueOf("test"));
        } catch (Exception ignore) {
            //
        }

        PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();

        verifier.printCache();

        verifier.verifyTrace(event("HBASE_CLIENT_ADMIN", HBaseAdmin.class.getDeclaredMethod("tableExists", TableName.class),
                annotation("hbase.client.params", "[test]")));

        verifier.verifyTraceCount(0);
    }

    @Test
    public void testTable() throws Exception {

        doReturn(new Configuration()).when(connection).getConfiguration();

        Table table = new HTable(TableName.valueOf("test"), connection);

        Put put = new Put("row".getBytes());

        try {
            table.put(put);
        } catch (Exception ignore) {
            //
        }

        PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();

        verifier.printCache();

        verifier.verifyTrace(event("HBASE_CLIENT_TABLE", HTable.class.getDeclaredMethod("put", Put.class),
                annotation("hbase.client.params", "rowKey: row")));

        verifier.verifyTraceCount(0);
    }
}

@@ -1353,24 +1353,112 @@
 ito.
-Mockito.doReturn
+ArgumentMatchers.any;%0Aimport static org.mockito.Mockito.doReturn;%0Aimport static org.mockito.Mockito.mock
 ;%0A%0A@
@@ -1838,16 +1838,63 @@
 ction;%0A%0A
+    private final String tableName = %22Table%22;%0A%0A
     @Bef
@@ -2221,22 +2221,25 @@
 valueOf(
-%22test%22
+tableName
 ));%0A    
@@ -2390,37 +2390,38 @@
   verifier.print
-Cache
+Method
 ();%0A%0A        ver
@@ -2588,16 +2588,40 @@
 s%22, 
-%22%5Btest%5D%22
+String.format(%22%5B%25s%5D%22, tableName)
 )));
@@ -2737,72 +2737,246 @@
     
-doReturn(new Configuration()).when(connection).getConfiguration(
+Configuration configuration = new Configuration();%0A        doReturn(configuration).when(connection).getConfiguration();%0A        doReturn(mock(BufferedMutatorImpl.class)).when(connection).getBufferedMutator(any(BufferedMutatorParams.class)
 );%0A%0A
@@ -3030,14 +3030,17 @@
 eOf(
-%22test%22
+tableName
 ), c
@@ -3111,102 +3111,22 @@
    t
-ry %7B%0A            table.put(put);%0A        %7D catch (Exception ignore) %7B%0A            //%0A        %7D
+able.put(put);
 %0A%0A  
@@ -3228,13 +3228,14 @@
 rint
-Cache
+Method
 ();%0A
@@ -3406,16 +3406,75 @@
 y: row%22)
+,%0A                annotation(%22hbase.table.name%22, tableName)
 ));%0A%0A   

Fix it-test failure