Skip to content

Commit 7453a3a

Browse files
committed
Adding simple CqlHelper test, updating to Junit5 to use assertAll()
1 parent f9e597e commit 7453a3a

File tree

5 files changed

+90
-36
lines changed

5 files changed

+90
-36
lines changed

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<scalatest.version>3.2.12</scalatest.version>
1616
<connector.version>3.2.0</connector.version>
1717
<cassandra.version>3.11.13</cassandra.version>
18-
<junit.version>4.13.2</junit.version>
18+
<junit.version>5.9.1</junit.version>
19+
<mockito.version>2.28.2</mockito.version>
1920
</properties>
2021

2122
<distributionManagement>
@@ -102,8 +103,8 @@
102103
<scope>test</scope>
103104
</dependency>
104105
<dependency>
105-
<groupId>junit</groupId>
106-
<artifactId>junit</artifactId>
106+
<groupId>org.junit.jupiter</groupId>
107+
<artifactId>junit-jupiter-engine</artifactId>
107108
<version>${junit.version}</version>
108109
<scope>test</scope>
109110
</dependency>

src/main/java/datastax/astra/migrate/CqlHelper.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public enum CQL {
3737

3838
private boolean isJobMigrateRowsFromFile = false;
3939
private final PropertyHelper propertyHelper;
40-
private boolean isInitialized = false;
4140

4241
////////////////////////////////////////////////
4342

@@ -46,9 +45,6 @@ public CqlHelper() {
4645
}
4746

4847
public void initialize() {
49-
if (null == originSession || originSession.isClosed() || null == targetSession || targetSession.isClosed()) {
50-
throw new RuntimeException("Origin and/or Target sessions are either not set, or are cloasd");
51-
}
5248

5349
readConsistencyLevel = Util.mapToConsistencyLevel(propertyHelper.getString(KnownProperties.READ_CL));
5450
writeConsistencyLevel = Util.mapToConsistencyLevel(propertyHelper.getString(KnownProperties.WRITE_CL));;
@@ -81,8 +77,6 @@ public void initialize() {
8177
logger.info("PARAM -- ORIGIN SELECT Query used: {}", cqlMap.get(CQL.ORIGIN_SELECT));
8278
logger.info("PARAM -- TARGET INSERT Query used: {}", cqlMap.get(CQL.TARGET_INSERT));
8379
logger.info("PARAM -- TARGET SELECT Query used: {}", cqlMap.get(CQL.TARGET_SELECT_ORIGIN_BY_PK));
84-
85-
isInitialized = true;
8680
}
8781

8882
public BoundStatement bindInsert(PreparedStatement insertStatement, Row originRow, Row targetRow) {
@@ -139,31 +133,31 @@ private String cqlOriginSelect() {
139133
final StringBuilder selectTTLWriteTimeCols = new StringBuilder();
140134
if (null != getTtlCols()) {
141135
getTtlCols().forEach(col -> {
142-
selectTTLWriteTimeCols.append(",ttl(" + getOriginColumnNames().get(col) + ")");
136+
selectTTLWriteTimeCols.append(",TTL(" + getOriginColumnNames().get(col) + ")");
143137
});
144138
}
145139
if (null != getWriteTimeStampCols()) {
146140
getWriteTimeStampCols().forEach(col -> {
147-
selectTTLWriteTimeCols.append(",writetime(" + getOriginColumnNames().get(col) + ")");
141+
selectTTLWriteTimeCols.append(",WRITETIME(" + getOriginColumnNames().get(col) + ")");
148142
});
149143
}
150144

151145
String fullSelectQuery;
152146
if (!isJobMigrateRowsFromFile) {
153147
String partitionKey = propertyHelper.getAsString(KnownProperties.ORIGIN_PARTITION_KEY).trim();
154-
fullSelectQuery = "select " + propertyHelper.getAsString(KnownProperties.ORIGIN_COLUMN_NAMES) + selectTTLWriteTimeCols + " from " + getOriginKeyspaceTable() +
155-
" where token(" + partitionKey + ") >= ? and token(" + partitionKey + ") <= ? " +
148+
fullSelectQuery = "SELECT " + propertyHelper.getAsString(KnownProperties.ORIGIN_COLUMN_NAMES) + selectTTLWriteTimeCols + " FROM " + getOriginKeyspaceTable() +
149+
" WHERE TOKEN(" + partitionKey + ") >= ? AND TOKEN(" + partitionKey + ") <= ? " +
156150
getOriginFilterCondition() + " ALLOW FILTERING";
157151
} else {
158152
String keyBinds = "";
159153
for (String key : propertyHelper.getStringList(KnownProperties.TARGET_PRIMARY_KEY)) {
160154
if (keyBinds.isEmpty()) {
161-
keyBinds = key + "= ?";
155+
keyBinds = key + "=?";
162156
} else {
163-
keyBinds += " and " + key + "= ?";
157+
keyBinds += " AND " + key + "=?";
164158
}
165159
}
166-
fullSelectQuery = "select " + selectCols + selectTTLWriteTimeCols + " from " + getOriginKeyspaceTable() + " where " + keyBinds;
160+
fullSelectQuery = "SELECT " + selectCols + selectTTLWriteTimeCols + " FROM " + getOriginKeyspaceTable() + " WHERE " + keyBinds;
167161
}
168162
return fullSelectQuery;
169163
}
@@ -178,10 +172,10 @@ private String cqlTargetInsert() {
178172
if (insertBinds.isEmpty()) {
179173
insertBinds = "?";
180174
} else {
181-
insertBinds += ", ?";
175+
insertBinds += ",?";
182176
}
183177
}
184-
targetInsertQuery = "insert into " + getTargetKeyspaceTable() + " (" + propertyHelper.getAsString(KnownProperties.TARGET_COLUMN_NAMES) + ") VALUES (" + insertBinds + ")";
178+
targetInsertQuery = "INSERT INTO " + getTargetKeyspaceTable() + " (" + propertyHelper.getAsString(KnownProperties.TARGET_COLUMN_NAMES) + ") VALUES (" + insertBinds + ")";
185179
if (null != getTtlCols() && !getTtlCols().isEmpty()) {
186180
targetInsertQuery += " USING TTL ?";
187181
if (null != getWriteTimeStampCols() && !getWriteTimeStampCols().isEmpty()) {
@@ -198,13 +192,13 @@ private String cqlTargetSelectOriginByPK() {
198192
String keyBinds = "";
199193
for (String key : propertyHelper.getStringList(KnownProperties.TARGET_PRIMARY_KEY)) {
200194
if (keyBinds.isEmpty()) {
201-
keyBinds = key + "= ?";
195+
keyBinds = key + "=?";
202196
} else {
203-
keyBinds += " and " + key + "= ?";
197+
keyBinds += " AND " + key + "=?";
204198
}
205199
}
206200

207-
return "select " + propertyHelper.getAsString(KnownProperties.ORIGIN_COLUMN_NAMES) + " from " + getTargetKeyspaceTable() + " where " + keyBinds;
201+
return "SELECT " + propertyHelper.getAsString(KnownProperties.ORIGIN_COLUMN_NAMES) + " FROM " + getTargetKeyspaceTable() + " WHERE " + keyBinds;
208202
}
209203

210204
public long getLargestWriteTimeStamp(Row row) {
@@ -313,9 +307,6 @@ public void setTargetSession(CqlSession targetSession) {
313307

314308
// Getters
315309
public String getCql(CQL cql) {
316-
if (!isInitialized)
317-
throw new RuntimeException("CqlHelper not initialized. Call initialize() first.");
318-
319310
if (!cqlMap.containsKey(cql)) {
320311
switch (cql) {
321312
case ORIGIN_SELECT:
@@ -333,8 +324,7 @@ public String getCql(CQL cql) {
333324
}
334325

335326
public PreparedStatement getPreparedStatement(CQL cql) {
336-
if (!isInitialized)
337-
throw new RuntimeException("CqlHelper not initialized. Call initialize() first.");
327+
abendIfSessionsNotSet();
338328

339329
if (!preparedStatementMap.containsKey(cql)) {
340330
switch (cql) {
@@ -351,10 +341,12 @@ public PreparedStatement getPreparedStatement(CQL cql) {
351341
}
352342

353343
public CqlSession getOriginSession() {
344+
abendIfSessionsNotSet();
354345
return originSession;
355346
}
356347

357348
public CqlSession getTargetSession() {
349+
abendIfSessionsNotSet();
358350
return targetSession;
359351
}
360352

@@ -450,4 +442,10 @@ private String getTargetKeyspaceTable() {
450442
private String getOriginFilterCondition() {
451443
return propertyHelper.getString(KnownProperties.ORIGIN_FILTER_CONDITION);
452444
}
445+
446+
private void abendIfSessionsNotSet() {
447+
if (null == originSession || originSession.isClosed() || null == targetSession || targetSession.isClosed()) {
448+
throw new RuntimeException("Origin and/or Target sessions are either not set, or are closed");
449+
}
450+
}
453451
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package datastax.astra.migrate;
2+
3+
import datastax.astra.migrate.properties.KnownProperties;
4+
import datastax.astra.migrate.properties.PropertyHelper;
5+
import org.apache.spark.SparkConf;
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.assertAll;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class CqlHelperTest {
14+
15+
SparkConf sparkConf;
16+
PropertyHelper propertyHelper;
17+
CqlHelper cqlHelper;
18+
19+
@BeforeEach
20+
public void setup() {
21+
sparkConf = new SparkConf();
22+
sparkConf.set(KnownProperties.ORIGIN_CONNECT_HOST, "localhost");
23+
sparkConf.set(KnownProperties.ORIGIN_KEYSPACE_TABLE, "origin.tab1");
24+
sparkConf.set(KnownProperties.ORIGIN_COLUMN_NAMES, "key,val");
25+
sparkConf.set(KnownProperties.ORIGIN_PARTITION_KEY, "key");
26+
sparkConf.set(KnownProperties.ORIGIN_COLUMN_TYPES, "0,0");
27+
sparkConf.set(KnownProperties.TARGET_CONNECT_HOST, "localhost");
28+
sparkConf.set(KnownProperties.TARGET_KEYSPACE_TABLE, "target.tab1");
29+
sparkConf.set(KnownProperties.TARGET_PRIMARY_KEY, "key");
30+
31+
propertyHelper = PropertyHelper.getInstance();
32+
cqlHelper = new CqlHelper();
33+
}
34+
35+
@AfterEach
36+
public void tearDown() {
37+
PropertyHelper.destroyInstance();
38+
}
39+
40+
@Test
41+
public void smokeTest() {
42+
propertyHelper.initializeSparkConf(sparkConf);
43+
cqlHelper.initialize();
44+
45+
String originSelect = "SELECT key,val FROM origin.tab1 WHERE TOKEN(key) >= ? AND TOKEN(key) <= ? ALLOW FILTERING".replaceAll("\\s+"," ");
46+
String targetInsert = "INSERT INTO target.tab1 (key,val) VALUES (?,?)".replaceAll("\\s+"," ");
47+
String targetSelect = "SELECT key,val FROM target.tab1 WHERE key=?".replaceAll("\\s+"," ");
48+
49+
assertAll(
50+
() -> assertEquals(originSelect, cqlHelper.getCql(CqlHelper.CQL.ORIGIN_SELECT).replaceAll("\\s+"," ")),
51+
() -> assertEquals(targetInsert, cqlHelper.getCql(CqlHelper.CQL.TARGET_INSERT).replaceAll("\\s+"," ")),
52+
() -> assertEquals(targetSelect, cqlHelper.getCql(CqlHelper.CQL.TARGET_SELECT_ORIGIN_BY_PK).replaceAll("\\s+"," "))
53+
);
54+
}
55+
56+
}

src/test/java/datastax/astra/migrate/properties/KnownPropertiesTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package datastax.astra.migrate.properties;
22

33
import datastax.astra.migrate.MigrateDataType;
4-
import org.junit.Test;
4+
import org.junit.jupiter.api.Test;
55

66
import java.util.ArrayList;
77
import java.util.Arrays;
8-
import java.util.Set;
98

109
import static datastax.astra.migrate.properties.KnownProperties.TEST_NUMBER_LIST_DEFAULT;
11-
import static org.junit.Assert.*;
10+
import static org.junit.jupiter.api.Assertions.*;
1211

1312
public class KnownPropertiesTest {
1413

src/test/java/datastax/astra/migrate/properties/PropertyHelperTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66
import datastax.astra.migrate.MigrateDataType;
77
import org.apache.spark.SparkConf;
8-
import org.junit.After;
9-
import org.junit.Before;
10-
import org.junit.Test;
11-
12-
import static org.junit.Assert.*;
8+
import org.junit.jupiter.api.AfterEach;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import static org.junit.jupiter.api.Assertions.*;
1312

1413
public class PropertyHelperTest {
1514
PropertyHelper helper;
1615
SparkConf validSparkConf;
17-
@Before
16+
17+
@BeforeEach
1818
public void setup() {
1919
helper = PropertyHelper.getInstance();
2020
}
2121

22-
@After
22+
@AfterEach
2323
public void tearDown() {
2424
PropertyHelper.destroyInstance();
2525
validSparkConf = null;

0 commit comments

Comments
 (0)