BAEL-7259
This commit is contained in:
parent
1a63be9035
commit
43fd0973dd
|
@ -12,6 +12,7 @@ import org.testcontainers.containers.GenericContainer;
|
|||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.sql.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
@ -63,18 +64,22 @@ public class HarperDBLiveTest {
|
|||
}
|
||||
|
||||
private static void insertSubjectRecords() throws IOException {
|
||||
String records = "[" + "{\"id\":1, \"name\":\"English\"}," + "{\"id\":2, \"name\":\"Maths\"}," + "{\"id\":3, \"name\":\"Science\"}" + "]";
|
||||
String records = "["
|
||||
+ "{\"id\":1, \"name\":\"English\"},"
|
||||
+ "{\"id\":2, \"name\":\"Maths\"},"
|
||||
+ "{\"id\":3, \"name\":\"Science\"}"
|
||||
+ "]";
|
||||
|
||||
harperDbApiService.insertRecords("Demo", "Subject", records);
|
||||
}
|
||||
|
||||
private static void insertTeacherRecords() throws IOException {
|
||||
String records = "[" + "{\"id\":1, \"name\":\"Parthiv Pradhan\", \"joining_date\":\"04-05-2000\"}," +
|
||||
"{\"id\":2, \"name\":\"David Martinez\", \"joining_date\":\"20-10-2005\"}," +
|
||||
"{\"id\":3, \"name\":\"Liam Williams\", \"joining_date\":\"04-06-1997\"}," +
|
||||
String records = "[" + "{\"id\":1, \"name\":\"James Cameron\", \"joining_date\":\"04-05-2000\"}," +
|
||||
"{\"id\":2, \"name\":\"Joe Biden\", \"joining_date\":\"20-10-2005\"}," +
|
||||
"{\"id\":3, \"name\":\"Jessie Williams\", \"joining_date\":\"04-06-1997\"}," +
|
||||
"{\"id\":4, \"name\":\"Robin Williams\", \"joining_date\":\"01-01-2020\"}," +
|
||||
"{\"id\":5, \"name\":\"Eric Martin\", \"joining_date\":\"04-05-2022\"}," +
|
||||
"{\"id\":6, \"name\":\"Sajjan Nagendra\", \"joining_date\":\"02-02-1999\"}" + "]";
|
||||
"{\"id\":5, \"name\":\"Eric Johnson\", \"joining_date\":\"04-05-2022\"}," +
|
||||
"{\"id\":6, \"name\":\"Raghu Yadav\", \"joining_date\":\"02-02-1999\"}" + "]";
|
||||
harperDbApiService.insertRecords("Demo", "Teacher", records);
|
||||
}
|
||||
|
||||
|
@ -138,175 +143,13 @@ public class HarperDBLiveTest {
|
|||
});
|
||||
}
|
||||
|
||||
private static Connection getConnection() throws SQLException {
|
||||
String URL = "jdbc:harperdb:Server=127.0.0.1:" + port + ";User=admin;Password=password;";
|
||||
return DriverManager.getConnection(URL);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenFetchRecord_thenSuccess() throws SQLException {
|
||||
final String SQL_QUERY = "select id, name, grade from Demo.student";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(SQL_QUERY);
|
||||
while (resultSet.next()) {
|
||||
int id = resultSet.getInt("id");
|
||||
String name = resultSet.getString("name");
|
||||
int grade = resultSet.getInt("grade");
|
||||
assertNotNull(Integer.valueOf(id));
|
||||
logger.info("Student id:" + id + " Student Name:" + name + " grade:" + grade);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenInsertRecord_thenSuccess() throws SQLException {
|
||||
final String INSERT_SQL = "insert into Demo.student(id, name, grade) values (10, 'Barak', 3)";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
assertDoesNotThrow(() -> statement.execute(INSERT_SQL));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenInsertRecordInTableWithoutAttributes_thenSuccess() throws SQLException {
|
||||
final String INSERT_SQL =
|
||||
"insert into Demo.teacher(id, name, joining_date) " + "values (7, 'David Sirocco', '04-05-2004'), (8, 'Ali Azmat', '04-10-2000')";
|
||||
final String QUERY_SQL = "select name, joining_date from Demo.teacher where name = ?";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
assertDoesNotThrow(() -> statement.execute(INSERT_SQL));
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(QUERY_SQL);
|
||||
preparedStatement.setString(1, "David Sirocco");
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
String teacherName = resultSet.getString("name");
|
||||
String joinDate = resultSet.getString("joining_date");
|
||||
assertEquals("David Sirocco", teacherName);
|
||||
assertEquals("04-05-2004", joinDate);
|
||||
logger.info("name:" + teacherName + " joining date:" + joinDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenUpdateRecord_thenSuccess() throws SQLException {
|
||||
final String UPDATE_SQL = "update Demo.student set grade = 4 where id = 5";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
assertDoesNotThrow(() -> statement.execute(UPDATE_SQL));
|
||||
assertEquals(1, statement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenDeleteRecord_thenSuccess() throws SQLException {
|
||||
final String DELETE_SQL = "delete from Demo.student where id = 3";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
assertDoesNotThrow(() -> statement.execute(DELETE_SQL));
|
||||
assertEquals(1, statement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPreparedStatement_whenFetchRecord_thenSuccess() throws SQLException {
|
||||
final String SQL_QUERY = "select id, name, grade from Demo.student where name = ? and grade = ?";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(SQL_QUERY);
|
||||
preparedStatement.setString(1, "Robin");
|
||||
preparedStatement.setInt(2, 4);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
int id = resultSet.getInt("id");
|
||||
String name = resultSet.getString("name");
|
||||
int grade = resultSet.getInt("grade");
|
||||
assertNotNull(Integer.valueOf(id));
|
||||
assertEquals("Robin", name);
|
||||
logger.info("Student id:" + id + " Student Name:" + name + " grade:" + grade);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPreparedStatement_whenDeleteRecord_thenSuccess() throws SQLException {
|
||||
final String DELETE_SQL = "delete from Demo.student where name = ?";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(DELETE_SQL);
|
||||
preparedStatement.setString(1, "James");
|
||||
assertDoesNotThrow(() -> preparedStatement.execute());
|
||||
assertEquals(1, preparedStatement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPreparedStatement_whenUpdateRecord_thenSuccess() throws SQLException {
|
||||
final String UPDATE_SQL = "update Demo.student set grade = ? where id = ? and name = ?";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_SQL);
|
||||
preparedStatement.setInt(1, 5);
|
||||
preparedStatement.setInt(2, 1);
|
||||
preparedStatement.setString(3, "John");
|
||||
assertDoesNotThrow(() -> preparedStatement.execute());
|
||||
assertEquals(1, preparedStatement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
|
||||
//@Test
|
||||
void whenAddtoBatch_thenExecuteBatchIsSuccess() throws SQLException {
|
||||
final String INSERT_SQL = "insert into Demo.teacher(id, name, joining_date, subject_id)" + "values(?, ?, ?, ?)";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_SQL);
|
||||
preparedStatement.setInt(1, 9);
|
||||
preparedStatement.setString(2, "Bret Lee");
|
||||
preparedStatement.setString(3, "07-08-2002");
|
||||
preparedStatement.setString(4, "[1, 3]");
|
||||
preparedStatement.addBatch();
|
||||
|
||||
preparedStatement.setInt(1, 10);
|
||||
preparedStatement.setString(2, "Sarah Glimmer");
|
||||
preparedStatement.setString(3, "07-08-1997");
|
||||
preparedStatement.setString(4, "[1, 2]");
|
||||
|
||||
preparedStatement.addBatch();
|
||||
|
||||
assertDoesNotThrow(() -> preparedStatement.executeBatch());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenExecuteJoinQuery_thenResultSuccess() throws SQLException {
|
||||
final String JOIN_QUERY =
|
||||
"SELECT t.name as teacher_name, s.name as subject_name " + "from Demo.teacher AS t INNER JOIN Demo.subject AS s ON t.subject_id = s.id";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
|
||||
ResultSet resultSet = statement.executeQuery(JOIN_QUERY);
|
||||
while (resultSet.next()) {
|
||||
logger.info("Teacher Name:" + resultSet.getString("teacher_name") + " Subject Name:" + resultSet.getString("subject_name"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenExecuteStoredToCreateTable_thenSuccess() throws SQLException {
|
||||
final String CREATE_TABLE_SQL = "CreateTable";
|
||||
final String CREATE_TABLE_PROC = "CreateTable";
|
||||
try (Connection connection = getConnection()) {
|
||||
CallableStatement callableStatement = connection.prepareCall(CREATE_TABLE_SQL);
|
||||
CallableStatement callableStatement = connection.prepareCall(CREATE_TABLE_PROC);
|
||||
|
||||
callableStatement.setString("SchemaName", "prod"); //schema gets created too
|
||||
callableStatement.setString("SchemaName", "prod");
|
||||
callableStatement.setString("TableName", "subject");
|
||||
callableStatement.setString("PrimaryKey", "id");
|
||||
Boolean result = callableStatement.execute();
|
||||
|
@ -316,8 +159,170 @@ public class HarperDBLiveTest {
|
|||
while (resultSet.next()) {
|
||||
String tableCreated = resultSet.getString("Success");
|
||||
assertEquals("true", tableCreated);
|
||||
logger.info("result of the callable execute:" + tableCreated);
|
||||
logger.info("Table Created Successfully");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenInsertRecord_thenSuccess() throws SQLException {
|
||||
final String INSERT_SQL = "insert into Demo.Subject(id, name) values "
|
||||
+ "(4, 'Social Studies'),"
|
||||
+ "(5, 'Geography')";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
assertDoesNotThrow(() -> statement.execute(INSERT_SQL));
|
||||
assertEquals(2, statement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void givenPrepareStatement_whenAddToBatch_thenSuccess() throws SQLException {
|
||||
final String INSERT_SQL = "insert into Demo.Teacher(id, name, joining_date) values"
|
||||
+ "(?, ?, ?)";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_SQL);
|
||||
preparedStatement.setInt(1, 7);
|
||||
preparedStatement.setString(2, "Bret Lee");
|
||||
preparedStatement.setString(3, "07-08-2002");
|
||||
preparedStatement.addBatch();
|
||||
|
||||
preparedStatement.setInt(1, 8);
|
||||
preparedStatement.setString(2, "Sarah Glimmer");
|
||||
preparedStatement.setString(3, "07-08-1997");
|
||||
preparedStatement.addBatch();
|
||||
|
||||
int[] recordsInserted = preparedStatement.executeBatch();
|
||||
|
||||
assertEquals(2, Arrays.stream(recordsInserted).sum());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static Connection getConnection() throws SQLException {
|
||||
String URL = "jdbc:harperdb:Server=127.0.0.1:" + port + ";User=admin;Password=password;";
|
||||
return DriverManager.getConnection(URL);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenFetchRecord_thenSuccess() throws SQLException {
|
||||
final String SQL_QUERY = "select id, name from Demo.Subject where name = 'Maths'";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(SQL_QUERY);
|
||||
while (resultSet.next()) {
|
||||
int id = resultSet.getInt("id");
|
||||
String name = resultSet.getString("name");
|
||||
assertNotNull(Integer.valueOf(id));
|
||||
assertEquals("Maths", name);
|
||||
logger.info("Subject id:" + id + " Subject Name:" + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPreparedStatement_whenExecuteJoinQuery_thenSuccess() throws SQLException {
|
||||
final String JOIN_QUERY = "SELECT t.name as teacher_name, t.joining_date as joining_date, s.name as subject_name "
|
||||
+ "from Demo.Teacher_Details AS td "
|
||||
+ "INNER JOIN Demo.Teacher AS t ON t.id = td.teacher_id "
|
||||
+ "INNER JOIN Demo.Subject AS s on s.id = td.subject_id "
|
||||
+ "where t.name = ?";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(JOIN_QUERY);
|
||||
preparedStatement.setString(1, "Eric Johnson");
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
String teacherName = resultSet.getString("teacher_name");
|
||||
String subjectName = resultSet.getString("subject_name");
|
||||
String joiningDate = resultSet.getString("joining_date");
|
||||
assertEquals("Eric Johnson", teacherName);
|
||||
assertEquals("Maths", subjectName);
|
||||
logger.info("Teacher Name:" + teacherName + " Subject Name:" + subjectName + " Joining Date:" + joiningDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void givenPreparedStatement_whenFetchRecord_thenSuccess() throws SQLException {
|
||||
final String SQL_QUERY = "select id, name from Demo.Subject where name = ?";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(SQL_QUERY);
|
||||
preparedStatement.setString(1, "Maths");
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
int id = resultSet.getInt("id");
|
||||
String name = resultSet.getString("name");
|
||||
assertNotNull(Integer.valueOf(id));
|
||||
assertEquals("Maths", name);
|
||||
logger.info("Subject id:" + id + " Subject Name:" + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenStatement_whenUpdateRecord_thenSuccess() throws SQLException {
|
||||
final String UPDATE_SQL = "update Demo.Teacher_Details set subject_id = 2 "
|
||||
+ "where teacher_id in (2, 5)";
|
||||
final String UPDATE_SQL_WITH_SUB_QUERY = "update Demo.Teacher_Details "
|
||||
+ "set subject_id = (select id from Demo.Subject where name = 'Maths') "
|
||||
+ "where teacher_id in (select id from Demo.Teacher where name in ('Joe Biden', 'Eric Johnson'))";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
assertDoesNotThrow(() -> statement.execute(UPDATE_SQL));
|
||||
assertEquals(2, statement.getUpdateCount());
|
||||
}
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
assertThrows(SQLException.class, () -> connection.createStatement().execute(UPDATE_SQL_WITH_SUB_QUERY));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPreparedStatement_whenUpdateRecord_thenSuccess() throws SQLException {
|
||||
final String UPDATE_SQL = "update Demo.Teacher_Details set subject_id = ? "
|
||||
+ "where teacher_id in (?, ?)";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_SQL);
|
||||
preparedStatement.setInt(1, 1);
|
||||
//following is not supported by the HarperDB driver
|
||||
//Integer[] teacherIds = {4, 5};
|
||||
//Array teacherIdArray = connection.createArrayOf(Integer.class.getTypeName(), teacherIds);
|
||||
preparedStatement.setInt(2, 4);
|
||||
preparedStatement.setInt(3, 5);
|
||||
assertDoesNotThrow(() -> preparedStatement.execute());
|
||||
assertEquals(2, preparedStatement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStatement_whenDeleteRecord_thenSuccess() throws SQLException {
|
||||
final String DELETE_SQL = "delete from Demo.Teacher_Details where teacher_id = 6 and subject_id = 3";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
assertDoesNotThrow(() -> statement.execute(DELETE_SQL));
|
||||
assertEquals(1, statement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPreparedStatement_whenDeleteRecord_thenSuccess() throws SQLException {
|
||||
final String DELETE_SQL = "delete from Demo.Teacher_Details where teacher_id = ? and subject_id = ?";
|
||||
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(DELETE_SQL);
|
||||
preparedStatement.setInt(1, 6);
|
||||
preparedStatement.setInt(2, 2);
|
||||
assertDoesNotThrow(() -> preparedStatement.execute());
|
||||
assertEquals(1, preparedStatement.getUpdateCount());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue