Merge pull request #15319 from parthiv39731/PR-7259

BAEL-7259
This commit is contained in:
davidmartinezbarua 2023-12-02 14:14:23 -03:00 committed by GitHub
commit c55fe1ddb6
8 changed files with 604 additions and 0 deletions

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>java-harperdb</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>java-harperdb</artifactId>
<version>${haperdb-driver.version}</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/cdata.jdbc.harperdb.jar</systemPath>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>1.19.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<haperdb-driver.version>4.2</haperdb-driver.version>
</properties>
</project>

View File

@ -0,0 +1,41 @@
@startuml
'https://gist.github.com/QuantumGhost/0955a45383a0b6c0bc24f9654b3cb561
' uncomment the line below if you're using computer with a retina display
' skinparam dpi 300
!theme sketchy-outline
!define Table(name,desc) class name as "desc" << (T, #63b175) >>
' we use bold for primary key
' green color for unique
' and underscore for not_null
!define primary_key(x) <b>x</b>
!define unique(x) <color:green>x</color>
!define not_null(x) <u>x</u>
' other tags available:
' <i></i>
' <back:COLOR></color>, where color is a color name or html color code
' (#FFAACC)
' see: http://plantuml.com/classes.html#More
hide methods
hide stereotypes
' entities
Table(Teacher, "Teacher") {
primary_key(Id) Number
Name Varchar
joining_date Date
}
Table(Subject, "Subject") {
primary_key(Id) Number
Name Varchar
}
Table(Teacher_Details, "Teacher_Details") {
primary_key(Id) Number
teacher_id Number
subject_id Number
}
Teacher_Details "*" -left-> "1" Subject: " "
Teacher "1" -left-> "*" Teacher_Details: " "
@enduml

View File

@ -0,0 +1,78 @@
package com.baeldung.harperdb;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.classic.methods.HttpPost;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.ClassicHttpRequest;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpEntity;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.io.entity.StringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Base64;
public class HarperDBApiService {
private static final Logger LOGGER = LoggerFactory.getLogger(HarperDBApiService.class);
private String url;
private String base64auth;
public HarperDBApiService(String url, String user, String password) {
this.url = url;
this.base64auth = Base64.getEncoder()
.encodeToString(new StringBuilder().append(user)
.append(":")
.append(password)
.toString()
.getBytes());
}
public void createSchema(String schema) throws IOException {
String requestBody = "{\"operation\":\"create_schema\", \"" + "schema\":\"" + schema + "\"" + "}";
executeHttpPostRequest(requestBody);
}
public void createTable(String schema, String table, String ... attributes) throws IOException {
String createTableReq = "{\"operation\":\"create_table\","
+ "\"schema\":\"" + schema
+ "\",\"table\":\"" + table
+ "\",\"hash_attribute\":\"id\""
+ "}";
executeHttpPostRequest(createTableReq);
LOGGER.info("created table:" + table);
for (String attribute : attributes) {
String createAttrReq = "{\"operation\":\"create_attribute\",\"schema\":\""
+ schema + "\",\"table\":\""
+ table + "\",\"attribute\":\""
+ attribute + "\""
+ "}";
executeHttpPostRequest(createAttrReq);
LOGGER.info("created attribute:" + attribute + " in table:" + table);
}
}
public void insertRecords(String schema, String table, String records) throws IOException {
String requestBody = "{\"table\":" + "\"" + table + "\","
+ "\"schema\":" + "\"" + schema + "\"" + ","
+ "\"operation\":" + "\"" + "insert" + "\"" + ","
+ "\"records\":" + records
+ "}";
executeHttpPostRequest(requestBody);
}
private void executeHttpPostRequest(String httpRequest) throws IOException {
LOGGER.info("Post request body:" + httpRequest);
try (CloseableHttpClient closeableHttpClient = HttpClientBuilder.create()
.build()) {
HttpPost request = new HttpPost(this.url);
request.addHeader("Authorization", "Basic " + this.base64auth);
request.addHeader("Content-Type", "application/json");
request.setEntity((HttpEntity) new StringEntity(httpRequest));
CloseableHttpResponse response = closeableHttpClient.execute((ClassicHttpRequest) request);
LOGGER.info("REST API response:" + response.toString());
assert (200 == response.getCode());
}
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.harperdb;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
public class HarperDBContainer {
private final static Logger LOGGER = LoggerFactory.getLogger(HarperDBContainer.class);
private static final Map<String, String> DEFAULT_ENV_MAP = Map.of("HDB_ADMIN_USERNAME", "admin", "HDB_ADMIN_PASSWORD", "password",
"OPERATIONSAPI_NETWORK_PORT", "9925", "ROOTPATH", "/home/harperdb/hdb", "LOGGING_STDSTREAMS", "true");
private static final Integer[] DEFAULT_EXPOSED_PORTS = { 9925, 9926 };
private static final String HARPER_DOCKER_IMAGE = "harperdb/harperdb:latest";
public void stop() {
harperDBContainer.stop();
}
GenericContainer harperDBContainer;
public GenericContainer installHarperDB() {
harperDBContainer = new GenericContainer(HARPER_DOCKER_IMAGE).withEnv(DEFAULT_ENV_MAP)
.withExposedPorts(DEFAULT_EXPOSED_PORTS);
return harperDBContainer;
}
public GenericContainer installHarperDB(String dockerImgage, final Map<String, String> envMap, final Integer... exposedPorts) {
return new GenericContainer(dockerImgage).withEnv(envMap)
.withExposedPorts(exposedPorts);
}
}

View File

@ -0,0 +1,379 @@
package com.baeldung.harperdb;
import cdata.jdbc.harperdb.HarperDBConnectionPoolDataSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.*;
public class HarperDBLiveTest {
private static final Logger logger = LoggerFactory.getLogger(HarperDBLiveTest.class);
private static Integer port;
private static HarperDBContainer harperDBContainer;
private static HarperDBApiService harperDbApiService;
@BeforeAll
static void setupHarperDB() throws IOException, InterruptedException, URISyntaxException {
installHarperDB();
initHarperDBApiService();
setupDB();
}
private static void installHarperDB() {
harperDBContainer = new HarperDBContainer();
GenericContainer genericContainer = harperDBContainer.installHarperDB();
genericContainer.start();
port = genericContainer.getFirstMappedPort();
}
private static void initHarperDBApiService() {
String url = "http://localhost:" + port + "/";
harperDbApiService = new HarperDBApiService(url, "admin", "password");
}
private static void setupDB() throws IOException {
harperDbApiService.createSchema("Demo");
harperDbApiService.createTable("Demo", "Subject", "name");
harperDbApiService.createTable("Demo", "Teacher", "name", "joining_date", "subject_id");
harperDbApiService.createTable("Demo", "Teacher_Details", "teacher_id", "subject_id");
insertSubjectRecords();
insertTeacherRecords();
insertTeacherDetailsRecords();
}
private static void insertSubjectRecords() throws IOException {
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\":\"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 Johnson\", \"joining_date\":\"04-05-2022\"}," +
"{\"id\":6, \"name\":\"Raghu Yadav\", \"joining_date\":\"02-02-1999\"}" + "]";
harperDbApiService.insertRecords("Demo", "Teacher", records);
}
private static void insertTeacherDetailsRecords() throws IOException {
String records = "[" + "{\"id\":1, \"teacher_id\":1, \"subject_id\":1}," + "{\"id\":2, \"teacher_id\":1, \"subject_id\":2}," +
"{\"id\":3, \"teacher_id\":2, \"subject_id\":3 }," + "{\"id\":4, \"teacher_id\":3, \"subject_id\":1}," +
"{\"id\":5, \"teacher_id\":3, \"subject_id\":3}," + "{\"id\":6, \"teacher_id\":4, \"subject_id\":2}," +
"{\"id\":7, \"teacher_id\":5, \"subject_id\":3}," + "{\"id\":8, \"teacher_id\":6, \"subject_id\":1}," +
"{\"id\":9, \"teacher_id\":6, \"subject_id\":2}," + "{\"id\":15, \"teacher_id\":6, \"subject_id\":3}" + "]";
harperDbApiService.insertRecords("Demo", "Teacher_Details", records);
}
@AfterAll
static void stopHarperDB() {
harperDBContainer.stop();
}
@Test
void whenConnectionInfoInURL_thenConnectSuccess() {
assertDoesNotThrow(() -> {
final String JDBC_URL = "jdbc:harperdb:Server=127.0.0.1:" + port + ";User=admin;Password=password;";
try (Connection connection = DriverManager.getConnection(JDBC_URL)) {
connection.createStatement()
.executeQuery("select 1");
logger.info("Connection Successful");
}
});
}
@Test
void whenConnectionInfoInProperties_thenConnectSuccess() {
assertDoesNotThrow(() -> {
Properties prop = new Properties();
prop.setProperty("Server", "127.0.0.1:" + port);
prop.setProperty("User", "admin");
prop.setProperty("Password", "password");
try (Connection connection = DriverManager.getConnection("jdbc:harperdb:", prop)) {
connection.createStatement()
.executeQuery("select 1");
logger.info("Connection Successful");
}
});
}
@Test
void whenConnectionPooling_thenConnectSuccess() {
assertDoesNotThrow(() -> {
HarperDBConnectionPoolDataSource harperdbPoolDataSource = new HarperDBConnectionPoolDataSource();
final String JDBC_URL = "jdbc:harperdb:UseConnectionPooling=true;PoolMaxSize=2;Server=127.0.0.1:" + port + ";User=admin;Password=password;";
harperdbPoolDataSource.setURL(JDBC_URL);
try (Connection connection = harperdbPoolDataSource.getPooledConnection()
.getConnection()) {
connection.createStatement()
.executeQuery("select 1");
logger.info("Connection Pool Successful");
}
});
}
@Test
void whenExecuteStoredToCreateTable_thenSuccess() throws SQLException {
final String CREATE_TABLE_PROC = "CreateTable";
try (Connection connection = getConnection()) {
CallableStatement callableStatement = connection.prepareCall(CREATE_TABLE_PROC);
callableStatement.setString("SchemaName", "prod");
callableStatement.setString("TableName", "subject");
callableStatement.setString("PrimaryKey", "id");
Boolean result = callableStatement.execute();
ResultSet resultSet = callableStatement.getResultSet();
while (resultSet.next()) {
String tableCreated = resultSet.getString("Success");
assertEquals("true", 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);
}
private static Connection getConnection(Map<String, String> properties) throws SQLException {
Properties prop = new Properties();
prop.setProperty("Server", "127.0.0.1:" + port);
prop.setProperty("User", "admin");
prop.setProperty("Password", "password");
for (Map.Entry<String, String> entry : properties.entrySet()) {
prop.setProperty(entry.getKey(), entry.getValue());
}
return DriverManager.getConnection("jdbc:harperdb:", prop);
}
@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()) {
Integer id = resultSet.getInt("id");
String name = resultSet.getString("name");
assertNotNull(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 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());
}
}
@Test
void givenTempTable_whenInsertIntoSelectTempTable_thenSuccess() throws SQLException {
try (Connection connection = getConnection()) {
Statement statement = connection.createStatement();
assertDoesNotThrow(() -> {
statement.execute("insert into Teacher#TEMP(id, name, joining_date) "
+ "values('12', 'David Flinch', '04-04-2014')");
statement.execute("insert into Teacher#TEMP(id, name, joining_date) "
+ "values('13', 'Stephen Hawkins', '04-07-2017')");
statement.execute("insert into Teacher#TEMP(id, name, joining_date) "
+ "values('14', 'Albert Einstein', '12-08-2020')");
statement.execute("insert into Teacher#TEMP(id, name, joining_date) "
+ "values('15', 'Leo Tolstoy', '20-08-2022')");
});
assertDoesNotThrow(() -> statement.execute("insert into Demo.Teacher(id, name, joining_date) "
+ "select id, name, joining_date from Teacher#TEMP"));
ResultSet resultSet = statement.executeQuery("select count(id) as rows from Demo.Teacher where id in"
+ " (12, 13, 14, 15)");
resultSet.next();
int totalRows = resultSet.getInt("rows");
logger.info("total number of rows fetched:" + totalRows);
assertEquals(4, totalRows);
}
}
@Test
void givenUserDefinedView_whenQueryView_thenSuccess() throws SQLException {
URL url = ClassLoader.getSystemClassLoader().getResource("UserDefinedViews.json");
String folderPath = url.getPath().substring(0, url.getPath().lastIndexOf('/'));
try(Connection connection = getConnection(Map.of("Location", folderPath))) {
PreparedStatement preparedStatement = connection.prepareStatement("select teacher_name,subject_name"
+ " from UserViews.View_Teacher_Details where subject_name = ?");
preparedStatement.setString(1, "Science");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
assertEquals("Science", resultSet.getString("subject_name"));
logger.info("Science Teacher Name:" + resultSet.getString("teacher_name"));
}
}
}
@Test
void givenAutoCache_whenQuery_thenSuccess() throws SQLException {
URL url = ClassLoader.getSystemClassLoader().getResource("test.db");
String folderPath = url.getPath().substring(0, url.getPath().lastIndexOf('/'));
logger.info("Cache Location:" + folderPath);
try(Connection connection = getConnection(Map.of("AutoCache", "true", "CacheLocation", folderPath))) {
PreparedStatement preparedStatement = connection.prepareStatement("select id, name from Demo.Subject");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
logger.info("Subject Name:" + resultSet.getString("name"));
}
}
}
}

View File

@ -0,0 +1,5 @@
{
"View_Teacher_Details": {
"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"
}
}

View File

@ -817,6 +817,7 @@
<module>pdf</module>
<module>performance-tests</module>
<module>persistence-modules</module>
<module>persistence-modules/java-harperdb</module>
<module>persistence-modules/spring-data-neo4j</module>
<module>protobuffer</module>
<module>quarkus-modules</module>