/**
 * Copyright © 2016-2022 The Thingsboard Authors
 *
 * 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 org.thingsboard.server.dao.service.install.sql;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.thingsboard.server.dao.service.AbstractServiceTest;
import org.thingsboard.server.dao.service.DaoSqlTest;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

@DaoSqlTest
public class EntitiesSchemaSqlTest extends AbstractServiceTest {

    @Value("${spring.datasource.url}")
    private String dbUrl;
    @Value("${spring.datasource.username}")
    private String dbUserName;
    @Value("${spring.datasource.password}")
    private String dbPassword;

    @Value("${classpath:sql/schema-entities.sql}")
    private Path installScriptPath;

    @Test
    public void testRepeatedInstall() throws IOException {
        String installScript = Files.readString(installScriptPath);
        try (Connection connection = getConnection()) {
            for (int i = 1; i <= 2; i++) {
                connection.createStatement().execute(installScript);
            }
        } catch (SQLException e) {
            Assertions.fail("Failed to execute reinstall", e);
        }
    }

    @Test
    public void testRepeatedInstall_badScript() throws SQLException {
        String illegalInstallScript = "CREATE TABLE IF NOT EXISTS qwerty ();\n" +
                "ALTER TABLE qwerty ADD COLUMN first VARCHAR(10);";

        Connection connection = getConnection();
        try {
            assertDoesNotThrow(() -> {
                connection.createStatement().execute(illegalInstallScript);
            });

            assertThatThrownBy(() -> {
                connection.createStatement().execute(illegalInstallScript);
            }).hasMessageContaining("column").hasMessageContaining("already exists");
        } finally {
            connection.createStatement().execute("DROP TABLE qwerty;");
            connection.close();
        }
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
    }

}

@@ -780,20 +780,134 @@
 otation.
-Valu
+Autowired;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.jdbc.core.JdbcTemplat
 e;%0Aimpor
@@ -1105,97 +1105,8 @@
 ath;
-%0Aimport java.sql.Connection;%0Aimport java.sql.DriverManager;%0Aimport java.sql.SQLException;
 %0A%0Aim
@@ -1319,224 +1319,8 @@
  %7B%0A%0A
-    @Value(%22$%7Bspring.datasource.url%7D%22)%0A    private String dbUrl;%0A    @Value(%22$%7Bspring.datasource.username%7D%22)%0A    private String dbUserName;%0A    @Value(%22$%7Bspring.datasource.password%7D%22)%0A    private String dbPassword;%0A%0A
     
@@ -1403,16 +1403,71 @@
 tPath;%0A%0A
+    @Autowired%0A    private JdbcTemplate jdbcTemplate;%0A%0A
     @Tes
@@ -1610,50 +1610,8 @@
  try
- (Connection connection = getConnection())
  %7B%0A 
@@ -1672,36 +1672,20 @@
     
-connection.createStatement()
+jdbcTemplate
 .exe
@@ -1736,19 +1736,16 @@
  catch (
-SQL
 Exceptio
@@ -1888,36 +1888,16 @@
 Script()
- throws SQLException
  %7B%0A     
@@ -2046,75 +2046,8 @@
 %22;%0A%0A
-        Connection connection = getConnection();%0A        try %7B%0A    
     
@@ -2093,40 +2093,20 @@
     
-  
-  connection.createStatement()
+jdbcTemplate
 .exe
@@ -2141,20 +2141,16 @@
         
-    
 %7D);%0A%0A   
@@ -2150,24 +2150,34 @@
 );%0A%0A    
+   
+ try %7B%0A
         
 assertTh
@@ -2164,24 +2164,28 @@
 y %7B%0A        
+    
 assertThatTh
@@ -2219,36 +2219,20 @@
     
-connection.createStatement()
+jdbcTemplate
 .exe
@@ -2274,16 +2274,27 @@
      %7D).
+getCause().
 hasMessa
@@ -2392,36 +2392,20 @@
     
-connection.createStatement()
+jdbcTemplate
 .exe
@@ -2444,184 +2444,9 @@
     
-    connection.close();%0A        %7D%0A    %7D%0A%0A    private Connection getConnection() throws SQLException %7B%0A        return DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
+%7D
 %0A   

Use JdbcTemplate in EntitiesSchemaSqlTest