BAEL-7259
This commit is contained in:
		
							parent
							
								
									7284c7462c
								
							
						
					
					
						commit
						1a63be9035
					
				| @ -14,17 +14,20 @@ 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()); | ||||
|             .encodeToString(new StringBuilder().append(user) | ||||
|                 .append(":") | ||||
|                 .append(password) | ||||
|                 .toString() | ||||
|                 .getBytes()); | ||||
|     } | ||||
| 
 | ||||
|     private static final Logger LOGGER = LoggerFactory.getLogger(HarperDBApiService.class); | ||||
| 
 | ||||
|     public void createSchema(String schema) throws IOException { | ||||
|         String requestBody = "{\"operation\":\"create_schema\", \"" + "schema\":\"" + schema + "\"" + "}"; | ||||
|         executeHttpPostRequest(requestBody); | ||||
| @ -61,7 +64,8 @@ public class HarperDBApiService { | ||||
|     private void executeHttpPostRequest(String httpRequest) throws IOException { | ||||
|         LOGGER.info("Post request body:" + httpRequest); | ||||
| 
 | ||||
|         try (CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build()) { | ||||
|         try (CloseableHttpClient closeableHttpClient = HttpClientBuilder.create() | ||||
|             .build()) { | ||||
|             HttpPost request = new HttpPost(this.url); | ||||
|             request.addHeader("Authorization", "Basic " + this.base64auth); | ||||
|             request.addHeader("Content-Type", "application/json"); | ||||
|  | ||||
| @ -1,23 +1,17 @@ | ||||
| package com.baledung.harperdb; | ||||
| 
 | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import org.testcontainers.containers.GenericContainer; | ||||
| 
 | ||||
| import java.util.Map; | ||||
| 
 | ||||
| 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 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 }; | ||||
| 
 | ||||
| @ -26,17 +20,17 @@ public class HarperDBContainer { | ||||
|     public void stop() { | ||||
|         harperDBContainer.stop(); | ||||
|     } | ||||
| 
 | ||||
|     GenericContainer harperDBContainer; | ||||
| 
 | ||||
|     public GenericContainer installHarperDB() { | ||||
|         harperDBContainer = new GenericContainer(HARPER_DOCKER_IMAGE) | ||||
|                 .withEnv(DEFAULT_ENV_MAP) | ||||
|         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) | ||||
|         return new GenericContainer(dockerImgage).withEnv(envMap) | ||||
|             .withExposedPorts(exposedPorts); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| package com.baledung.harperdb; | ||||
| 
 | ||||
| import cdata.jdbc.harperdb.HarperDBConnectionPoolDataSource; | ||||
| 
 | ||||
| import org.junit.jupiter.api.AfterAll; | ||||
| import org.junit.jupiter.api.BeforeAll; | ||||
| import org.junit.jupiter.api.Test; | ||||
| @ -16,6 +17,7 @@ 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 String host; | ||||
| @ -61,40 +63,27 @@ 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\"}," | ||||
|                 + "{\"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\"}" | ||||
|                 + "]"; | ||||
|         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\"}," + | ||||
|             "{\"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\"}" + "]"; | ||||
|         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}" | ||||
|                 + "]"; | ||||
|         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); | ||||
|     } | ||||
| @ -110,7 +99,8 @@ public class HarperDBLiveTest { | ||||
|             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"); | ||||
|                 connection.createStatement() | ||||
|                     .executeQuery("select 1"); | ||||
|                 logger.info("Connection Successful"); | ||||
|             } | ||||
|         }); | ||||
| @ -125,7 +115,8 @@ public class HarperDBLiveTest { | ||||
|             prop.setProperty("Password", "password"); | ||||
| 
 | ||||
|             try (Connection connection = DriverManager.getConnection("jdbc:harperdb:", prop)) { | ||||
|                 connection.createStatement().executeQuery("select 1"); | ||||
|                 connection.createStatement() | ||||
|                     .executeQuery("select 1"); | ||||
|                 logger.info("Connection Successful"); | ||||
|             } | ||||
|         }); | ||||
| @ -135,12 +126,13 @@ public class HarperDBLiveTest { | ||||
|     void whenConnectionPooling_thenConnectSuccess() throws SQLException { | ||||
|         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;"; | ||||
|             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"); | ||||
|             try (Connection connection = harperdbPoolDataSource.getPooledConnection() | ||||
|                 .getConnection()) { | ||||
|                 connection.createStatement() | ||||
|                     .executeQuery("select 1"); | ||||
|                 logger.info("Connection Pool Successful"); | ||||
|             } | ||||
|         }); | ||||
| @ -180,8 +172,8 @@ public class HarperDBLiveTest { | ||||
| 
 | ||||
|     @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 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()) { | ||||
| @ -270,8 +262,7 @@ public class HarperDBLiveTest { | ||||
| 
 | ||||
|     //@Test | ||||
|     void whenAddtoBatch_thenExecuteBatchIsSuccess() throws SQLException { | ||||
|         final String INSERT_SQL = "insert into Demo.teacher(id, name, joining_date, subject_id)" | ||||
|                 + "values(?, ?, ?, ?)"; | ||||
|         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); | ||||
| @ -292,23 +283,19 @@ public class HarperDBLiveTest { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @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"; | ||||
|         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")); | ||||
|                 logger.info("Teacher Name:" + resultSet.getString("teacher_name") + " Subject Name:" + resultSet.getString("subject_name")); | ||||
|             } | ||||
| 
 | ||||
| 
 | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user