/*
 * 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 io.prestosql.plugin.prometheus;

import io.airlift.log.Logger;
import io.prestosql.Session;
import io.prestosql.testing.AbstractTestQueryFramework;
import io.prestosql.testing.MaterializedResult;
import io.prestosql.testing.MaterializedRow;
import io.prestosql.testing.QueryRunner;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import static io.prestosql.plugin.prometheus.PrometheusQueryRunner.createPrometheusQueryRunner;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

/**
 * Integration tests against Prometheus container
 */
@Test(priority = 1, singleThreaded = true)
public class TestPrometheusIntegrationTests3
        extends AbstractTestQueryFramework
{
    private PrometheusServer server;
    private Session session;

    @Override
    protected QueryRunner createQueryRunner()
            throws Exception
    {
        this.server = new PrometheusServer();
        return createPrometheusQueryRunner(server);
    }

    @Test
    public void testConfirmMetricAvailableAndCheckUp()
            throws Exception
    {
        int maxTries = 60;
        int timeBetweenTriesMillis = 1000;
        session = getQueryRunner().getDefaultSession();
        int tries = 0;
        final OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .build();
        HttpUrl.Builder urlBuilder = HttpUrl.parse(server.getUri().toString()).newBuilder().encodedPath("/api/v1/query");
        urlBuilder.addQueryParameter("query", "up[1d]");
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder()
                .url(url)
                .build();
        String responseBody;
        // this seems to be a reliable way to ensure Prometheus has `up` metric data
        while (tries < maxTries) {
            responseBody = httpClient.newCall(request).execute().body().string();
            if (responseBody.contains("values")) {
                Logger log = Logger.get(TestPrometheusIntegrationTests3.class);
                log.info("prometheus response: %s", responseBody);
                break;
            }
            Thread.sleep(timeBetweenTriesMillis);
            tries++;
        }
        if (tries == maxTries) {
            fail("Prometheus container not available for metrics query in " + maxTries * timeBetweenTriesMillis + " milliseconds.");
        }
        // now we're making sure the client is ready
        tries = 0;
        while (tries < maxTries) {
            if (getQueryRunner().tableExists(session, "up")) {
                break;
            }
            Thread.sleep(timeBetweenTriesMillis);
            tries++;
        }
        if (tries == maxTries) {
            fail("Prometheus container, or client, not available for metrics query in " + maxTries * timeBetweenTriesMillis + " milliseconds.");
        }

        MaterializedResult results = computeActual(session, "SELECT * FROM prometheus.default.up LIMIT 1");
        assertEquals(results.getRowCount(), 1);
        MaterializedRow row = results.getMaterializedRows().get(0);
        assertEquals(row.getField(0).toString(), "{instance=localhost:9090, __name__=up, job=prometheus}");
    }

    @Test(priority = 2, dependsOnMethods = "testConfirmMetricAvailableAndCheckUp")
    public void testPushDown()
    {
        // default interval on the `up` metric that Prometheus records on itself is 15 seconds, so this should only yield one row
        MaterializedResult results = computeActual(session, "SELECT * FROM prometheus.default.up WHERE timestamp > (NOW() - INTERVAL '15' SECOND)");
        assertEquals(results.getRowCount(), 1);
    }
}

@@ -630,37 +630,8 @@
 er;%0A
-import io.prestosql.Session;%0A
 impo
@@ -682,16 +682,16 @@
 mework;%0A
+
 import i
@@ -1381,37 +1381,8 @@
 ver;
-%0A    private Session session;
 %0A%0A  
@@ -1753,64 +1753,8 @@
 00;%0A
-        session = getQueryRunner().getDefaultSession();%0A
     
@@ -3178,23 +3178,28 @@
 eExists(
-s
+getS
 ession
+()
 , %22up%22))
@@ -3555,33 +3555,24 @@
 mputeActual(
-session, 
 %22SELECT * FR
@@ -4088,16 +4088,16 @@
 one row%0A
+
         
@@ -4143,17 +4143,8 @@
 ual(
-session, 
 %22SEL

Remove redundant session field in a test