|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.ing.data.cassandra.jdbc; |
| 15 | + |
| 16 | +import com.datastax.oss.driver.api.core.type.DataTypes; |
| 17 | +import com.datastax.oss.driver.api.querybuilder.SchemaBuilder; |
| 18 | +import com.dtsx.astra.sdk.db.AstraDbClient; |
| 19 | +import com.dtsx.astra.sdk.db.domain.DatabaseStatusType; |
| 20 | +import com.dtsx.astra.sdk.utils.TestUtils; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.Assertions; |
| 23 | +import org.junit.jupiter.api.BeforeAll; |
| 24 | +import org.junit.jupiter.api.Order; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.TestMethodOrder; |
| 27 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import java.sql.DriverManager; |
| 32 | +import java.sql.ResultSet; |
| 33 | +import java.sql.SQLException; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test JDBC Driver against DbAAS Astra. |
| 37 | + * To run this test define environment variable ASTRA_DB_APPLICATION_TOKEN |
| 38 | + * but not having any token does not block the build. |
| 39 | + */ |
| 40 | +@TestMethodOrder(org.junit.jupiter.api.MethodOrderer.OrderAnnotation.class) |
| 41 | +class DbaasAstraIntegrationTest { |
| 42 | + |
| 43 | + private static final Logger log = LoggerFactory.getLogger(DbaasAstraIntegrationTest.class); |
| 44 | + private static final String DATABASE_NAME = "test_cassandra_jdbc"; |
| 45 | + private static final String KEYSPACE_NAME = "test"; |
| 46 | + static CassandraConnection sqlConnection = null; |
| 47 | + |
| 48 | + @BeforeAll |
| 49 | + static void setupAstra() throws Exception { |
| 50 | + if (System.getenv("ASTRA_DB_APPLICATION_TOKEN") != null) { |
| 51 | + log.debug("ASTRA_DB_APPLICATION_TOKEN is provided, Astra Test is executed"); |
| 52 | + |
| 53 | + |
| 54 | + /* |
| 55 | + * Devops API Client (create database, resume, delete) |
| 56 | + */ |
| 57 | + AstraDbClient astraDbClient = new AstraDbClient(TestUtils.getAstraToken()); |
| 58 | + log.debug("Connected the dbaas API"); |
| 59 | + |
| 60 | + /* |
| 61 | + * Set up a Database in Astra : create if not exist, resume if needed |
| 62 | + * Vector Database is Cassandra DB with vector support enabled. |
| 63 | + * It can take up to 1 min to create the database if not exists |
| 64 | + */ |
| 65 | + String dbId = TestUtils.setupVectorDatabase(DATABASE_NAME, KEYSPACE_NAME); |
| 66 | + Assertions.assertTrue(astraDbClient.findById(dbId).isPresent()); |
| 67 | + Assertions.assertEquals(DatabaseStatusType.ACTIVE, astraDbClient.findById(dbId).get().getStatus()); |
| 68 | + log.debug("Database ready"); |
| 69 | + |
| 70 | + /* |
| 71 | + * Download cloud secure bundle to connect to the database. |
| 72 | + * - Saved in /tmp |
| 73 | + * - Single region = we can use default region |
| 74 | + */ |
| 75 | + astraDbClient |
| 76 | + .database(dbId) |
| 77 | + .downloadDefaultSecureConnectBundle("/tmp/" + DATABASE_NAME + "_scb.zip"); |
| 78 | + log.debug("Connection bundle downloaded."); |
| 79 | + |
| 80 | + /* |
| 81 | + * Building jdbcUrl and sqlConnection. |
| 82 | + * Note: Astra can be access with only a token (username='token') |
| 83 | + */ |
| 84 | + sqlConnection = (CassandraConnection) DriverManager.getConnection( |
| 85 | + "jdbc:cassandra://dbaas/" + KEYSPACE_NAME + |
| 86 | + "?user=" + "token" + |
| 87 | + "&password=" + TestUtils.getAstraToken() + // env var ASTRA_DB_APPLICATION_TOKEN |
| 88 | + "&consistency=" + "LOCAL_QUORUM" + |
| 89 | + "&secureconnectbundle=/tmp/" + DATABASE_NAME + "_scb.zip"); |
| 90 | + } else { |
| 91 | + log.debug("ASTRA_DB_APPLICATION_TOKEN is not defined, skipping ASTRA test"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @Order(1) |
| 97 | + @EnabledIfEnvironmentVariable(named = "ASTRA_DB_APPLICATION_TOKEN", matches = "Astra.*") |
| 98 | + void givenConnection_whenCreateTable_shouldTableExist() throws SQLException { |
| 99 | + // Given |
| 100 | + Assertions.assertNotNull(sqlConnection); |
| 101 | + // When |
| 102 | + sqlConnection.createStatement().execute(SchemaBuilder |
| 103 | + .createTable("simple_table") |
| 104 | + .ifNotExists() |
| 105 | + .withPartitionKey("email", DataTypes.TEXT) |
| 106 | + .withColumn("firstname", DataTypes.TEXT) |
| 107 | + .withColumn("lastname", DataTypes.TEXT) |
| 108 | + .build().getQuery()); |
| 109 | + // Then |
| 110 | + Assertions.assertTrue(tableExist("simple_table")); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + @Order(2) |
| 115 | + @EnabledIfEnvironmentVariable(named = "ASTRA_DB_APPLICATION_TOKEN", matches = "Astra.*") |
| 116 | + void givenTable_whenInsert_shouldRetrieveData() throws Exception { |
| 117 | + // Given |
| 118 | + Assertions.assertTrue(tableExist("simple_table")); |
| 119 | + // When |
| 120 | + String insertSimpleCQL = "INSERT INTO simple_table (email, firstname, lastname) VALUES(?,?,?)"; |
| 121 | + final CassandraPreparedStatement prepStatement = sqlConnection.prepareStatement(insertSimpleCQL); |
| 122 | + prepStatement. setString( 1, "[email protected]"); |
| 123 | + prepStatement.setString(2, "pierre"); |
| 124 | + prepStatement.setString(2, "feuille"); |
| 125 | + prepStatement.execute(); |
| 126 | + // Then (warning on Cassandra expected) |
| 127 | + Assertions.assertEquals(1, countRecords("simple_table")); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + @Order(3) |
| 132 | + @EnabledIfEnvironmentVariable(named = "ASTRA_DB_APPLICATION_TOKEN", matches = "Astra.*") |
| 133 | + void givenConnection_whenCreateTableVector_shouldTableExist() throws Exception { |
| 134 | + // When |
| 135 | + sqlConnection.createStatement().execute("" + |
| 136 | + "CREATE TABLE IF NOT EXISTS pet_supply_vectors (" + |
| 137 | + " product_id TEXT PRIMARY KEY," + |
| 138 | + " product_name TEXT," + |
| 139 | + " product_vector vector<float, 14>)"); |
| 140 | + // Then |
| 141 | + Assertions.assertTrue(tableExist("pet_supply_vectors")); |
| 142 | + sqlConnection.createStatement().execute("" + |
| 143 | + "CREATE CUSTOM INDEX IF NOT EXISTS idx_vector " + |
| 144 | + "ON pet_supply_vectors(product_vector) " + |
| 145 | + "USING 'StorageAttachedIndex'"); |
| 146 | + // When |
| 147 | + sqlConnection.createStatement().execute("" + |
| 148 | + "INSERT INTO pet_supply_vectors (product_id, product_name, product_vector) " + |
| 149 | + "VALUES ('pf1843','HealthyFresh - Chicken raw dog food',[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])"); |
| 150 | + sqlConnection.createStatement().execute("" + |
| 151 | + "INSERT INTO pet_supply_vectors (product_id, product_name, product_vector) " + |
| 152 | + "VALUES ('pf1844','HealthyFresh - Beef raw dog food',[1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0])"); |
| 153 | + sqlConnection.createStatement().execute("" + |
| 154 | + "INSERT INTO pet_supply_vectors (product_id, product_name, product_vector) " + |
| 155 | + "VALUES ('pt0021','Dog Tennis Ball Toy',[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0])"); |
| 156 | + sqlConnection.createStatement().execute("" + |
| 157 | + "INSERT INTO pet_supply_vectors (product_id, product_name, product_vector) " + |
| 158 | + "VALUES ('pt0041','Dog Ring Chew Toy',[0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0])"); |
| 159 | + sqlConnection.createStatement().execute("" + |
| 160 | + "INSERT INTO pet_supply_vectors (product_id, product_name, product_vector) " + |
| 161 | + "VALUES ('pf7043','PupperSausage Bacon dog Treats',[0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1])"); |
| 162 | + sqlConnection.createStatement().execute("" + |
| 163 | + "INSERT INTO pet_supply_vectors (product_id, product_name, product_vector) " + |
| 164 | + "VALUES ('pf7044','PupperSausage Beef dog Treats',[0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0])"); |
| 165 | + // Then (warning on Cassandra expected) |
| 166 | + Assertions.assertEquals(6, countRecords("pet_supply_vectors")); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + @Order(4) |
| 171 | + @EnabledIfEnvironmentVariable(named = "ASTRA_DB_APPLICATION_TOKEN", matches = "Astra.*") |
| 172 | + void givenVectorTable_whenSimilaritySearch_shouldReturnResults() throws Exception { |
| 173 | + // Given |
| 174 | + Assertions.assertTrue(tableExist("pet_supply_vectors")); |
| 175 | + Assertions.assertEquals(6, countRecords("pet_supply_vectors")); |
| 176 | + // When |
| 177 | + final CassandraPreparedStatement prepStatement = sqlConnection.prepareStatement("" + |
| 178 | + "SELECT\n" + |
| 179 | + " product_id, product_vector,\n" + |
| 180 | + " similarity_dot_product(product_vector,[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]) as similarity\n" + |
| 181 | + "FROM pet_supply_vectors\n" + |
| 182 | + "ORDER BY product_vector\n" + |
| 183 | + "ANN OF [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" + |
| 184 | + "LIMIT 2;"); |
| 185 | + java.sql.ResultSet rs = prepStatement.executeQuery(); |
| 186 | + // A result has been found |
| 187 | + Assertions.assertTrue(rs.next()); |
| 188 | + // Parsing Results |
| 189 | + Assertions.assertNotNull(rs.getObject("product_vector")); |
| 190 | + Assertions.assertEquals(3.0d, rs.getDouble("similarity")); |
| 191 | + } |
| 192 | + |
| 193 | + private boolean tableExist(String tableName) throws SQLException { |
| 194 | + String existTableCql = "select table_name,keyspace_name from system_schema.tables where keyspace_name=? and table_name=?"; |
| 195 | + final CassandraPreparedStatement prepStatement = sqlConnection.prepareStatement(existTableCql); |
| 196 | + prepStatement.setString(1, KEYSPACE_NAME); |
| 197 | + prepStatement.setString(2, tableName); |
| 198 | + return prepStatement.executeQuery().next(); |
| 199 | + } |
| 200 | + |
| 201 | + private int countRecords(String tablename) throws SQLException { |
| 202 | + String countRecordsCql = "select count(*) from " + tablename; |
| 203 | + final CassandraPreparedStatement prepStatement = sqlConnection.prepareStatement(countRecordsCql); |
| 204 | + final ResultSet resultSet = prepStatement.executeQuery(); |
| 205 | + resultSet.next(); |
| 206 | + return resultSet.getInt(1); |
| 207 | + } |
| 208 | + |
| 209 | + @AfterAll |
| 210 | + static void closeSql() throws SQLException { |
| 211 | + if (sqlConnection != null) { |
| 212 | + sqlConnection.close(); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | +} |
| 217 | + |
| 218 | + |
0 commit comments