/*
 * Copyright 2020 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.pinpoint.test.plugin;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_6;

@RestController
public class PostgresqlPluginController {
    private static EmbeddedPostgres postgres;
    private static String url;

    @PostConstruct
    public static void start() throws Exception {
        // starting Postgres
        postgres = new EmbeddedPostgres(V9_6);
        // predefined data directory
        // final EmbeddedPostgres postgres = new EmbeddedPostgres(V9_6, "/path/to/predefined/data/directory");
        url = postgres.start("localhost", 5432, "dbName", "userName", "password");

        final Connection conn = DriverManager.getConnection(url);
        Statement selectStatement = conn.createStatement();
        conn.createStatement().execute("CREATE TABLE test (name VARCHAR(45), age int);");
        conn.createStatement().execute("CREATE TABLE member (id INT PRIMARY KEY, name CHAR(20));");
    }

    @PreDestroy
    public static void shutdown() throws Exception {
        if (postgres != null) {
            postgres.close();
        }
    }

    @GetMapping("/select")
    public Mono<String> select() throws SQLException {
        final String name = "testUser";
        final int age = 5;

        String insertQuery = "INSERT INTO test (name, age) VALUES (?, ?)";
        String selectQuery = "SELECT * FROM test";
        String deleteQuery = "DELETE FROM test";

        final Connection conn = DriverManager.getConnection(url);

        // performing some assertions
        PreparedStatement insertPreparedStatement = conn.prepareStatement(insertQuery);
        insertPreparedStatement.setString(1, name);
        insertPreparedStatement.setInt(2, age);
        insertPreparedStatement.execute();

        Statement selectStatement = conn.createStatement();
        ResultSet rs = selectStatement.executeQuery(selectQuery);

        Statement deleteStatement = conn.createStatement();
        deleteStatement.executeUpdate(deleteQuery, Statement.NO_GENERATED_KEYS);

        return Mono.just("OK");
    }
}

@@ -1769,38 +1769,38 @@
       Statement 
-select
+create
 Statement = conn
@@ -1819,37 +1819,32 @@
 ment();%0A        
-conn.
 createStatement(
@@ -1834,34 +1834,32 @@
  createStatement
-()
 .execute(%22CREATE
@@ -1910,21 +1910,16 @@
         
-conn.
 createSt
@@ -1925,18 +1925,16 @@
 tatement
-()
 .execute
@@ -1990,24 +1990,80 @@
 HAR(20));%22);
+%0A%0A        createStatement.close();%0A        conn.close();
 %0A    %7D%0A%0A    
@@ -2867,16 +2867,57 @@
 ecute();
+%0A        insertPreparedStatement.close();
 %0A%0A      
@@ -2981,23 +2981,8 @@
     
- ResultSet rs =
  sel
@@ -3020,16 +3020,49 @@
 tQuery);
+%0A        selectStatement.close();
 %0A%0A      
@@ -3195,16 +3195,72 @@
 D_KEYS);
+%0A        deleteStatement.close();%0A%0A        conn.close();
 %0A%0A      

Fix postgresql plugin testweb warning