Skip to content

Commit 389ce91

Browse files
committed
Ready for next version
1 parent 28820dc commit 389ce91

File tree

13 files changed

+231
-393
lines changed

13 files changed

+231
-393
lines changed
Lines changed: 149 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package com.dtsx.astra.sdk.utils;
22

3-
import com.dtsx.astra.sdk.AstraDevopsApiClient;
4-
import com.dtsx.astra.sdk.db.DatabaseClient;
53
import com.dtsx.astra.sdk.db.AstraDbClient;
4+
import com.dtsx.astra.sdk.db.DatabaseClient;
65
import com.dtsx.astra.sdk.db.domain.CloudProviderType;
76
import com.dtsx.astra.sdk.db.domain.Database;
7+
import com.dtsx.astra.sdk.db.domain.DatabaseCreationBuilder;
88
import com.dtsx.astra.sdk.db.domain.DatabaseCreationRequest;
99
import com.dtsx.astra.sdk.db.domain.DatabaseStatusType;
1010
import org.apache.hc.client5.http.classic.methods.HttpGet;
1111
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
12-
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
1312
import org.apache.hc.client5.http.impl.classic.HttpClients;
1413
import org.apache.hc.core5.http.HttpHeaders;
1514
import org.slf4j.Logger;
1615
import org.slf4j.LoggerFactory;
1716

1817
import java.io.IOException;
19-
import java.util.List;
2018
import java.util.Optional;
21-
import java.util.stream.Collectors;
2219

2320
/**
2421
* Helper for tetst.
@@ -28,25 +25,164 @@
2825
public class TestUtils {
2926

3027
/** Test constant. */
31-
public static final String TEST_DBNAME = "sdk_tests";
32-
33-
/** Test constant. */
34-
public static final String TEST_NAMESPACE = "java";
28+
public static final String TEST_REGION = "us-east1";
3529

3630
/** Test constant. */
37-
public static final String TEST_REGION = "us-east1";
31+
public static final String TEST_TIER = "serverless";
3832

3933
/** Test constant. */
4034
public static final CloudProviderType TEST_PROVIDER = CloudProviderType.GCP;
4135

42-
/** Test constant. */
43-
public static final String TEST_TIER = "serverless";
36+
/**
37+
* Logger for the class.
38+
*/
39+
static Logger logger = LoggerFactory.getLogger(TestUtils.class);
40+
4441

4542
/**
4643
* Hide default constructor
4744
*/
4845
private TestUtils() {}
4946

47+
/**
48+
* Read Token for tests.
49+
*
50+
* @return
51+
* token for test or error
52+
*/
53+
public static String getAstraToken() {
54+
String token = null;
55+
if (AstraRc.isDefaultConfigFileExists()) {
56+
token = new AstraRc()
57+
.getSectionKey(AstraRc.ASTRARC_DEFAULT, AstraRc.ASTRA_DB_APPLICATION_TOKEN)
58+
.orElse(null);
59+
}
60+
return Optional.ofNullable(Utils
61+
.readEnvVariable(AstraRc.ASTRA_DB_APPLICATION_TOKEN).
62+
orElse(token))
63+
.orElseThrow(() -> new IllegalStateException(
64+
"ASTRA_DB_APPLICATION_TOKEN is not defined as env variable or present in file ~/.astrarc"));
65+
}
66+
67+
/**
68+
* Initialize databases for tests.
69+
*
70+
* @param dbName
71+
* database name
72+
* @param keyspace
73+
* expected keyspace
74+
* @return
75+
* the database id
76+
*/
77+
public static String setupVectorDatabase(String dbName, String keyspace) {
78+
return setupDatabase(getAstraToken(), AstraEnvironment.PROD, dbName, keyspace, true);
79+
}
80+
81+
/**
82+
* Initialize databases for tests.
83+
*
84+
* @param dbName
85+
* database name
86+
* @param keyspace
87+
* expected keyspace
88+
* @return
89+
* the database id
90+
*/
91+
public static String setupVectorDatabase(AstraEnvironment env, String dbName, String keyspace) {
92+
return setupDatabase(getAstraToken(), env, dbName, keyspace, true);
93+
}
94+
95+
/**
96+
* Initialize databases for tests.
97+
*
98+
* @param dbName
99+
* database name
100+
* @param keyspace
101+
* expected keyspace
102+
* @return
103+
* the database id
104+
*/
105+
public static String setupDatabase(AstraEnvironment env, String dbName, String keyspace) {
106+
return setupDatabase(getAstraToken(), env, dbName, keyspace, false);
107+
}
108+
109+
/**
110+
* Initialize databases for tests.
111+
*
112+
* @param dbName
113+
* database name
114+
* @param keyspace
115+
* expected keyspace
116+
* @return
117+
* the database id
118+
*/
119+
public static String setupDatabase(String dbName, String keyspace) {
120+
return setupDatabase(getAstraToken(), AstraEnvironment.PROD, dbName, keyspace, false);
121+
}
122+
123+
/**
124+
* Initialize databases for tests.
125+
*
126+
* @param dbName
127+
* database name
128+
* @param keyspace
129+
* expected keyspace
130+
* @return
131+
* the database id
132+
*/
133+
public static String setupDatabase(AstraEnvironment env, String dbName, String keyspace, boolean vector) {
134+
return setupDatabase(getAstraToken(), env, dbName, keyspace, vector);
135+
}
136+
137+
/**
138+
* Initialize databases for tests.
139+
*
140+
* @param token
141+
* token for the organization
142+
* @param dbName
143+
* database name
144+
* @param keyspace
145+
* expected keyspace
146+
* @return
147+
* the database id
148+
*/
149+
public static String setupDatabase(String token, AstraEnvironment env, String dbName, String keyspace, boolean vector) {
150+
AstraDbClient devopsDbCli = new AstraDbClient(getAstraToken(), env);
151+
Optional<Database> optDb = devopsDbCli.findByName(dbName).findAny();
152+
if (optDb.isPresent()) {
153+
// Db is present, should we resume it ?
154+
Database db = optDb.get();
155+
DatabaseClient dbClient = devopsDbCli.database(db.getId());
156+
if (db.getStatus().equals(DatabaseStatusType.HIBERNATED)) {
157+
logger.info("Resume DB {} as HIBERNATED ", dbName);
158+
resumeDb(optDb.get());
159+
waitForDbStatus(dbClient, DatabaseStatusType.ACTIVE, 500);
160+
}
161+
// Db is active, should I add a keyspace ?
162+
if (!dbClient.keyspaces().findAll().contains(keyspace)) {
163+
dbClient.keyspaces().create(keyspace);
164+
waitForDbStatus(dbClient, DatabaseStatusType.ACTIVE, 100);
165+
}
166+
return db.getId();
167+
} else {
168+
// Db is not present...creation
169+
DatabaseCreationBuilder builder = DatabaseCreationRequest
170+
.builder()
171+
.name(dbName)
172+
.tier(TEST_TIER)
173+
.cloudProvider(TEST_PROVIDER)
174+
.cloudRegion(TEST_REGION)
175+
.keyspace(keyspace);
176+
if (vector) {
177+
builder = builder.withVector();
178+
}
179+
String serverlessDbId = devopsDbCli.create(builder.build());
180+
DatabaseClient dbc = new DatabaseClient(devopsDbCli.getToken(), serverlessDbId);
181+
waitForDbStatus(dbc, DatabaseStatusType.ACTIVE, 180);
182+
return serverlessDbId;
183+
}
184+
}
185+
50186
/**
51187
* Wait for db to have proper status.
52188
*
@@ -78,42 +214,6 @@ public static void waitForDbStatus(DatabaseClient dbc, DatabaseStatusType status
78214
public static void waitForSeconds(int seconds) {
79215
try {Thread.sleep(seconds * 1000);} catch (InterruptedException e) {}
80216
}
81-
82-
/**
83-
* Initialize databases for tests.
84-
*
85-
* @param devopsDbCli
86-
* devops database API.
87-
* @param dbName
88-
* database name
89-
* @param keyspace
90-
* expected keyspace
91-
* @return
92-
* the database id
93-
*/
94-
public static String createDbAndKeyspaceIfNotExist(AstraDbClient devopsDbCli, String dbName, String keyspace) {
95-
List<Database> dbs = devopsDbCli.findByName(dbName).collect(Collectors.toList());
96-
if (dbs.size() > 0) {
97-
Database db = dbs.get(0);
98-
DatabaseClient dbc = new DatabaseClient(devopsDbCli.getToken(), db.getId());
99-
if (!db.getInfo().getKeyspaces().contains(keyspace)) {
100-
dbc.keyspaces().create(keyspace);
101-
waitForDbStatus(dbc, DatabaseStatusType.ACTIVE, 60);
102-
} return db.getId();
103-
} else {
104-
String serverlessDbId = devopsDbCli.create(DatabaseCreationRequest
105-
.builder()
106-
.name(dbName)
107-
.tier(TEST_TIER)
108-
.cloudProvider(TEST_PROVIDER)
109-
.cloudRegion(TEST_REGION)
110-
.keyspace(keyspace)
111-
.build());
112-
DatabaseClient dbc = new DatabaseClient(devopsDbCli.getToken(), serverlessDbId);
113-
waitForDbStatus(dbc, DatabaseStatusType.ACTIVE, 120);
114-
return serverlessDbId;
115-
}
116-
}
117217

118218
/**
119219
* Terminate database if needed.
@@ -131,22 +231,6 @@ public static void terminateDatabaseByName(AstraDbClient devopsDbCli, String dbN
131231
}
132232
}
133233

134-
/**
135-
* Read Token for tests.
136-
*
137-
* @return
138-
* token for test or error
139-
*/
140-
public static String readToken() {
141-
String token = null;
142-
if (AstraRc.isDefaultConfigFileExists()) {
143-
token = new AstraRc()
144-
.getSectionKey(AstraRc.ASTRARC_DEFAULT, AstraRc.ASTRA_DB_APPLICATION_TOKEN)
145-
.orElse(null);
146-
}
147-
return Utils.readEnvVariable(AstraRc.ASTRA_DB_APPLICATION_TOKEN).orElse(token);
148-
}
149-
150234
/**
151235
* Database name.
152236
*
@@ -159,59 +243,12 @@ private static void resumeDb(Database db) {
159243
.getApiRestEndpoint(db.getId(), db.getInfo().getRegion()) +
160244
"/v2/schemas/keyspace");
161245
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
162-
request.setHeader("X-Cassandra-Token", readToken());
246+
request.setHeader("X-Cassandra-Token", getAstraToken());
163247
request.setHeader("Content-Type", "application/json");
164248
httpClient.execute(request).close();
165249
} catch (IOException e) {
166250
throw new IllegalStateException("Cannot resume DB", e);
167251
}
168252
}
169253

170-
/**
171-
* Logger for the class.
172-
*/
173-
static Logger logger = LoggerFactory.getLogger(TestUtils.class);
174-
175-
/**
176-
* Create DB if not exist
177-
*
178-
* @param dbName
179-
* database name
180-
* @param keyspace
181-
* keyspace name
182-
* @return
183-
* database client
184-
* @throws InterruptedException
185-
* wait for db availability interrupted
186-
*/
187-
public static String setupDatabase(String dbName, String keyspace)
188-
throws InterruptedException {
189-
AstraDbClient astraDb = new AstraDbClient(readToken());
190-
Optional<Database> optDb = astraDb.findByName(dbName).findAny();
191-
String dbId = null;
192-
if (!optDb.isPresent()) {
193-
logger.info("Creating database '{}' as it does not exist, this operation takes about 90s, please wait... ", dbName);
194-
dbId = astraDb.create(DatabaseCreationRequest
195-
.builder().name(dbName)
196-
.keyspace(keyspace)
197-
.cloudRegion(TEST_REGION)
198-
.withVector()
199-
.build());
200-
} else {
201-
dbId = optDb.get().getId();
202-
DatabaseClient dbClient = astraDb.database(dbId);
203-
if (optDb.get().getStatus().equals(DatabaseStatusType.HIBERNATED)) {
204-
logger.info("Resume DB {} as HIBERNATED ", dbName);
205-
resumeDb(optDb.get());
206-
waitForDbStatus(dbClient, DatabaseStatusType.ACTIVE, 500);
207-
}
208-
if (!dbClient.keyspaces().findAll().contains(keyspace)) {
209-
dbClient.keyspaces().create(keyspace);
210-
}
211-
}
212-
TestUtils.waitForDbStatus(astraDb.database(dbId), DatabaseStatusType.ACTIVE, 500);
213-
return dbId;
214-
}
215-
216-
217254
}

astra-sdk-vector/src/test/java/com/dtsx/astra/sdk/cassio/MetadataVectorTableTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.concurrent.atomic.AtomicInteger;
3131
import java.util.stream.Collectors;
3232

33-
import static com.dtsx.astra.sdk.utils.TestUtils.readToken;
33+
import static com.dtsx.astra.sdk.utils.TestUtils.getAstraToken;
3434
import static com.dtsx.astra.sdk.utils.TestUtils.setupDatabase;
3535

3636
/**
@@ -70,7 +70,7 @@ public static void setupEnvironment() throws InterruptedException {
7070

7171
// Open Cassandra Connection
7272
cqlSession = AstraClient.builder()
73-
.withToken(readToken())
73+
.withToken(getAstraToken())
7474
.withCqlKeyspace(ASTRA_DB_KEYSPACE)
7575
.withDatabaseId(databaseId).withDatabaseRegion(TestUtils.TEST_REGION)
7676
.enableCql()

astra-sdk/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,6 @@
8282
<artifactId>junit-jupiter-engine</artifactId>
8383
<scope>test</scope>
8484
</dependency>
85-
</dependencies>
85+
86+
</dependencies>
8687
</project>

astra-sdk/src/test/java/com/datastax/astra/sdk/AstraRcTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.datastax.astra.sdk;
1818

19+
import com.dtsx.astra.sdk.utils.AstraEnvironment;
1920
import com.dtsx.astra.sdk.utils.AstraRc;
2021
import org.junit.jupiter.api.Assertions;
2122

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.datastax.astra.sdk;
2+
3+
public interface AstraSdkTest {
4+
5+
String TEST_DATABASE_NAME = "test_sdk_java";
6+
}

0 commit comments

Comments
 (0)