Skip to content

Commit 4528363

Browse files
authored
Merge pull request #160 from datastax/feature/feature/CDM-63
CDM-63 Target will default to using origin keyspace & table values if…
2 parents 209b232 + e08b175 commit 4528363

File tree

3 files changed

+105
-14
lines changed

3 files changed

+105
-14
lines changed
Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package com.datastax.cdm.schema;
22

3-
import com.datastax.oss.driver.api.core.type.DataType;
43
import com.datastax.cdm.data.CqlConversion;
4+
import com.datastax.cdm.properties.IPropertyHelper;
55
import com.datastax.cdm.properties.KnownProperties;
6-
import com.datastax.cdm.properties.PropertyHelper;
6+
import com.datastax.oss.driver.api.core.type.DataType;
7+
import org.apache.commons.lang3.StringUtils;
78

9+
import javax.validation.constraints.NotNull;
810
import java.util.List;
911

1012
public class BaseTable implements Table {
11-
protected final PropertyHelper propertyHelper;
13+
protected final IPropertyHelper propertyHelper;
1214
protected final boolean isOrigin;
13-
1415
protected String keyspaceName;
1516
protected String tableName;
1617
protected List<String> columnNames;
1718
protected List<DataType> columnCqlTypes;
1819
protected List<CqlConversion> cqlConversions;
1920

20-
public BaseTable(PropertyHelper propertyHelper, boolean isOrigin) {
21+
public BaseTable(IPropertyHelper propertyHelper, boolean isOrigin) {
2122
this.propertyHelper = propertyHelper;
2223
this.isOrigin = isOrigin;
2324

24-
String keyspaceTableString = (this.isOrigin ? propertyHelper.getString(KnownProperties.ORIGIN_KEYSPACE_TABLE) : propertyHelper.getString(KnownProperties.TARGET_KEYSPACE_TABLE)).trim();
25+
String keyspaceTableString = getKeyspaceTableAsString(propertyHelper, isOrigin);
2526
if (keyspaceTableString.contains(".")) {
2627
String[] keyspaceTable = keyspaceTableString.split("\\.");
2728
this.keyspaceName = keyspaceTable[0];
@@ -32,11 +33,47 @@ public BaseTable(PropertyHelper propertyHelper, boolean isOrigin) {
3233
}
3334
}
3435

35-
public String getKeyspaceName() {return this.keyspaceName;}
36-
public String getTableName() {return this.tableName;}
37-
public String getKeyspaceTable() {return this.keyspaceName + "." + this.tableName;}
38-
public List<String> getColumnNames(boolean format) { return this.columnNames; }
39-
public List<DataType> getColumnCqlTypes() {return this.columnCqlTypes;}
40-
public List<CqlConversion> getConversions() {return this.cqlConversions;}
41-
public boolean isOrigin() {return this.isOrigin;}
42-
}
36+
@NotNull
37+
private String getKeyspaceTableAsString(IPropertyHelper propertyHelper, boolean isOrigin) {
38+
String keyspaceTableString = (isOrigin ? propertyHelper.getString(KnownProperties.ORIGIN_KEYSPACE_TABLE) :
39+
propertyHelper.getString(KnownProperties.TARGET_KEYSPACE_TABLE));
40+
41+
// Use origin keyspaceTable property if target not specified
42+
if (!isOrigin && StringUtils.isBlank(keyspaceTableString)) {
43+
keyspaceTableString = propertyHelper.getString(KnownProperties.ORIGIN_KEYSPACE_TABLE);
44+
}
45+
if (StringUtils.isBlank(keyspaceTableString)) {
46+
throw new RuntimeException("Value for required property " + KnownProperties.ORIGIN_KEYSPACE_TABLE + " not provided!!");
47+
}
48+
49+
return keyspaceTableString.trim();
50+
}
51+
52+
public String getKeyspaceName() {
53+
return this.keyspaceName;
54+
}
55+
56+
public String getTableName() {
57+
return this.tableName;
58+
}
59+
60+
public String getKeyspaceTable() {
61+
return this.keyspaceName + "." + this.tableName;
62+
}
63+
64+
public List<String> getColumnNames(boolean format) {
65+
return this.columnNames;
66+
}
67+
68+
public List<DataType> getColumnCqlTypes() {
69+
return this.columnCqlTypes;
70+
}
71+
72+
public List<CqlConversion> getConversions() {
73+
return this.cqlConversions;
74+
}
75+
76+
public boolean isOrigin() {
77+
return this.isOrigin;
78+
}
79+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.datastax.cdm.schema;
22

33
import com.datastax.oss.driver.api.core.type.DataType;
4+
45
import java.util.List;
56

67
public interface Table {
78
String getKeyspaceName();
9+
810
String getTableName();
11+
912
List<String> getColumnNames(boolean asCql);
13+
1014
List<DataType> getColumnCqlTypes();
1115
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.datastax.cdm.schema;
2+
3+
import com.datastax.cdm.cql.CommonMocks;
4+
import com.datastax.cdm.properties.KnownProperties;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.mockito.Mockito.when;
12+
13+
public class BaseTableTest extends CommonMocks {
14+
public Logger logger = LoggerFactory.getLogger(this.getClass().getName());
15+
16+
@BeforeEach
17+
public void setup() {
18+
defaultClassVariables();
19+
commonSetupWithoutDefaultClassVariables();
20+
}
21+
22+
@Test
23+
public void useOriginWhenTargetAbsent() {
24+
when(propertyHelper.getString(KnownProperties.ORIGIN_KEYSPACE_TABLE)).thenReturn("origin_ks.origin_table");
25+
BaseTable bt = new BaseTable(propertyHelper, false);
26+
27+
assertAll(
28+
() -> assertEquals("origin_ks", bt.getKeyspaceName()),
29+
() -> assertEquals("origin_table", bt.getTableName())
30+
);
31+
}
32+
33+
@Test
34+
public void useTargetWhenTargetPresent() {
35+
when(propertyHelper.getString(KnownProperties.ORIGIN_KEYSPACE_TABLE)).thenReturn("origin_ks.origin_table");
36+
when(propertyHelper.getString(KnownProperties.TARGET_KEYSPACE_TABLE)).thenReturn("target_ks.target_table");
37+
BaseTable bt = new BaseTable(propertyHelper, false);
38+
39+
assertAll(
40+
() -> assertEquals("target_ks", bt.getKeyspaceName()),
41+
() -> assertEquals("target_table", bt.getTableName())
42+
);
43+
}
44+
45+
@Test
46+
public void failWhenKsTableAbsent() {
47+
RuntimeException thrown = assertThrows(RuntimeException.class, () -> new BaseTable(propertyHelper, false));
48+
assertTrue(thrown.getMessage().contentEquals("Value for required property " + KnownProperties.ORIGIN_KEYSPACE_TABLE + " not provided!!"));
49+
}
50+
}

0 commit comments

Comments
 (0)