Use JdbcTemplate in EntitiesSchemaSqlTest
This commit is contained in:
parent
6182ae7415
commit
b396aa4a38
@ -17,16 +17,15 @@ package org.thingsboard.server.dao.service.install.sql;
|
|||||||
|
|
||||||
import org.assertj.core.api.Assertions;
|
import org.assertj.core.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.thingsboard.server.dao.service.AbstractServiceTest;
|
import org.thingsboard.server.dao.service.AbstractServiceTest;
|
||||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
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.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
@ -34,50 +33,40 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|||||||
@DaoSqlTest
|
@DaoSqlTest
|
||||||
public class EntitiesSchemaSqlTest extends AbstractServiceTest {
|
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}")
|
@Value("${classpath:sql/schema-entities.sql}")
|
||||||
private Path installScriptPath;
|
private Path installScriptPath;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRepeatedInstall() throws IOException {
|
public void testRepeatedInstall() throws IOException {
|
||||||
String installScript = Files.readString(installScriptPath);
|
String installScript = Files.readString(installScriptPath);
|
||||||
try (Connection connection = getConnection()) {
|
try {
|
||||||
for (int i = 1; i <= 2; i++) {
|
for (int i = 1; i <= 2; i++) {
|
||||||
connection.createStatement().execute(installScript);
|
jdbcTemplate.execute(installScript);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (Exception e) {
|
||||||
Assertions.fail("Failed to execute reinstall", e);
|
Assertions.fail("Failed to execute reinstall", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRepeatedInstall_badScript() throws SQLException {
|
public void testRepeatedInstall_badScript() {
|
||||||
String illegalInstallScript = "CREATE TABLE IF NOT EXISTS qwerty ();\n" +
|
String illegalInstallScript = "CREATE TABLE IF NOT EXISTS qwerty ();\n" +
|
||||||
"ALTER TABLE qwerty ADD COLUMN first VARCHAR(10);";
|
"ALTER TABLE qwerty ADD COLUMN first VARCHAR(10);";
|
||||||
|
|
||||||
Connection connection = getConnection();
|
assertDoesNotThrow(() -> {
|
||||||
try {
|
jdbcTemplate.execute(illegalInstallScript);
|
||||||
assertDoesNotThrow(() -> {
|
});
|
||||||
connection.createStatement().execute(illegalInstallScript);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
try {
|
||||||
assertThatThrownBy(() -> {
|
assertThatThrownBy(() -> {
|
||||||
connection.createStatement().execute(illegalInstallScript);
|
jdbcTemplate.execute(illegalInstallScript);
|
||||||
}).hasMessageContaining("column").hasMessageContaining("already exists");
|
}).getCause().hasMessageContaining("column").hasMessageContaining("already exists");
|
||||||
} finally {
|
} finally {
|
||||||
connection.createStatement().execute("DROP TABLE qwerty;");
|
jdbcTemplate.execute("DROP TABLE qwerty;");
|
||||||
connection.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection getConnection() throws SQLException {
|
|
||||||
return DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user