Skip to content

Commit ce9b5f9

Browse files
committed
add new QLDB test to check data types for UPDATE queries
1 parent 4269b8a commit ce9b5f9

File tree

5 files changed

+215
-26
lines changed

5 files changed

+215
-26
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
ADDITIONAL_MVN_ARGS ?= -DskipTests -q
2+
export AWS_DEFAULT_REGION ?= us-east-1
3+
export AWS_REGION ?= us-east-1
4+
export SERVICES ?= serverless,kinesis,sns,sqs,iam,cloudwatch
25

36
usage: ## Show this help
47
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
@@ -13,12 +16,10 @@ publish-maven: ## Publish artifacts to Maven Central
1316
ADDITIONAL_MVN_TARGETS=deploy ADDITIONAL_MVN_ARGS="-DskipTests -Pawssdkv1,awssdkv2" make build
1417

1518
test-v1:
16-
USE_SSL=1 SERVICES=serverless,kinesis,sns,sqs,iam,cloudwatch mvn $(MVN_TEST_ARGS) -Pawssdkv1 \
17-
-Dtest="cloud.localstack.awssdkv1.*Test" test
19+
USE_SSL=1 mvn $(MVN_TEST_ARGS) -Pawssdkv1 -Dtest="cloud.localstack.awssdkv1.*Test" test
1820

1921
test-v2:
20-
USE_SSL=1 SERVICES=serverless,kinesis,sns,sqs,iam,cloudwatch mvn $(MVN_TEST_ARGS) -Pawssdkv2 \
21-
-Dtest="cloud.localstack.awssdkv2.*Test" test
22+
USE_SSL=1 mvn $(MVN_TEST_ARGS) -Pawssdkv2 -Dtest="cloud.localstack.awssdkv2.*Test" test
2223

2324
test: ## Run Java/JUnit tests for AWS SDK v1 and v2
2425
make test-v2

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@
160160
<version>3.9.0</version>
161161
<scope>test</scope>
162162
</dependency>
163+
<dependency>
164+
<groupId>com.fasterxml.jackson.dataformat</groupId>
165+
<artifactId>jackson-dataformat-ion</artifactId>
166+
<version>2.12.1</version>
167+
<scope>test</scope>
168+
</dependency>
163169
</dependencies>
164170

165171
<profiles>

src/main/java/cloud/localstack/deprecated/TestUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.Map;
3939
import java.util.stream.Stream;
4040

41-
import static cloud.localstack.awssdkv1.TestUtils.getCredentialsProvider;
4241

4342
@Deprecated
4443
@SuppressWarnings("all")

src/test/java/cloud/localstack/awssdkv1/SQSMessagingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ public void testGetQueueRedrivePolicy() {
219219

220220
String resultPolicy = result.getAttributes().get("RedrivePolicy");
221221
ObjectMapper mapper = new ObjectMapper();
222-
HashMap<String, Object> map = new HashMap<String, Object>();
222+
Map<String, Object> map = new HashMap<String, Object>();
223223

224224
try {
225225
map = mapper.readValue(resultPolicy, new TypeReference<Map<String, Object>>(){});
226-
Assert.assertEquals( map.get("maxReceiveCount"), maxReceiveCount);
227-
Assert.assertEquals( map.get("deadLetterTargetArn"), dlQueueArn);
226+
Assert.assertEquals(map.get("maxReceiveCount"), maxReceiveCount);
227+
Assert.assertEquals(map.get("deadLetterTargetArn"), dlQueueArn);
228228
} catch (Exception e) {
229229
throw new RuntimeException("No RedrivePolicy found");
230230
}

src/test/java/cloud/localstack/awssdkv2/ProFeaturesSDKV2Test.java

Lines changed: 201 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import cloud.localstack.Constants;
44
import cloud.localstack.LocalstackTestRunner;
55
import cloud.localstack.docker.annotation.LocalstackDockerProperties;
6-
import cloud.localstack.sample.LambdaHandler;
7-
import cloud.localstack.utils.LocalTestUtil;
86
import cloud.localstack.Localstack;
97

8+
import com.amazon.ion.system.IonSystemBuilder;
9+
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
10+
import com.fasterxml.jackson.dataformat.ion.ionvalue.IonValueMapper;
11+
import com.google.common.collect.ImmutableMap;
1012
import software.amazon.awssdk.services.qldb.*;
1113
import software.amazon.awssdk.services.qldb.model.*;
1214
import software.amazon.qldb.*;
@@ -16,44 +18,44 @@
1618
import org.junit.*;
1719
import org.junit.runner.RunWith;
1820

21+
import java.io.IOException;
22+
import java.io.UncheckedIOException;
1923
import java.net.*;
2024
import java.util.*;
25+
import java.util.logging.Logger;
26+
import java.util.stream.Collectors;
27+
import java.util.stream.StreamSupport;
28+
2129

2230
@RunWith(LocalstackTestRunner.class)
2331
@LocalstackDockerProperties(ignoreDockerRunErrors=true)
2432
public class ProFeaturesSDKV2Test {
33+
public static final IonSystem SYSTEM = IonSystemBuilder.standard().build();
34+
public static final IonObjectMapper MAPPER = new IonValueMapper(SYSTEM);
35+
36+
private static final Logger LOG = Logger.getLogger(ProFeaturesSDKV2Test.class.getName());
2537

2638
@Test
27-
public void testQueryQLDBLedger() throws Exception {
28-
if (System.getenv(Constants.ENV_LOCALSTACK_API_KEY) == null) {
39+
public void testCreateListTables() throws Exception {
40+
if (!isProEnabled()) {
2941
return;
3042
}
3143

32-
QldbAsyncClient client = TestUtils.getClientQLDBAsyncV2();
33-
3444
String ledgerName = "l123";
35-
CreateLedgerRequest request = CreateLedgerRequest.builder().name(ledgerName).build();
36-
CreateLedgerResponse ledger = client.createLedger(request).get();
37-
Assert.assertEquals(ledger.name(), ledgerName);
38-
39-
QldbDriver driver = QldbDriver.builder().ledger(ledgerName)
40-
.sessionClientBuilder(
41-
QldbSessionClient.builder().endpointOverride(new URI(Localstack.INSTANCE.getEndpointQLDB()))
42-
).build();
45+
QldbAsyncClient client = TestUtils.getClientQLDBAsyncV2();
4346

44-
// create tables
4547
String tableName1 = "table1";
4648
String tableName2 = "table2";
47-
driver.execute(txn -> { return txn.execute("CREATE TABLE " + tableName1); });
48-
driver.execute(txn -> { return txn.execute("CREATE TABLE " + tableName2); });
49+
createLedgerAndTables(ledgerName, tableName1, tableName2);
50+
QldbDriver driver = getDriver(ledgerName);
4951

5052
// list tables
5153
List<String> tableNames = new ArrayList<String>();
5254
driver.getTableNames().forEach(tableNames::add);
5355
Assert.assertTrue(tableNames.contains(tableName1));
5456
Assert.assertTrue(tableNames.contains(tableName2));
5557

56-
// list tables via que
58+
// list tables via query
5759
String query = "SELECT VALUE name FROM information_schema.user_tables WHERE status = 'ACTIVE'";
5860
Result result = driver.execute(txn -> { return txn.execute(query); });
5961
Assert.assertNotNull(result);
@@ -63,6 +65,187 @@ public void testQueryQLDBLedger() throws Exception {
6365
result.forEach(tableNames2::add);
6466
Assert.assertTrue(tableNames2.contains(tableName1));
6567
Assert.assertTrue(tableNames2.contains(tableName2));
68+
69+
// clean up
70+
client.deleteLedger(DeleteLedgerRequest.builder().name(ledgerName).build());
71+
}
72+
73+
@Test
74+
public void testCreateListIndexes() throws Exception {
75+
if (!isProEnabled()) {
76+
return;
77+
}
78+
String ledgerName = "l123";
79+
String tableName1 = "table1";
80+
81+
QldbDriver driver = getDriver(ledgerName);
82+
createLedgerAndTables(ledgerName, tableName1);
83+
84+
String query1 = "CREATE INDEX on " + tableName1 + "(attr1)";
85+
driver.execute(txn -> { return txn.execute(query1); });
86+
87+
String query2 = "SELECT VALUE indexes FROM information_schema.user_tables info, info.indexes indexes";
88+
Result indexQueryResult = driver.execute(txn -> {
89+
return txn.execute(query2);
90+
});
91+
92+
Set<String> result = StreamSupport.stream(indexQueryResult.spliterator(), false)
93+
.map(v -> (IonStruct) v)
94+
.map(s -> s.get("expr").toString())
95+
.collect(Collectors.toSet());
96+
Assert.assertEquals(new HashSet<String>(Arrays.asList("\"[attr1]\"")), result);
97+
98+
// clean up
99+
cleanUp(ledgerName);
100+
}
101+
102+
@Test
103+
public void testUpdateQueryDataTypes() throws Exception {
104+
if (!isProEnabled()) {
105+
return;
106+
}
107+
LOG.info("Running testUpdateQueryDataTypes to check QLDB query data types...");
108+
109+
String tableName1 = "Wallet";
110+
String ledgerName = "l123";
111+
createLedgerAndTables(ledgerName, tableName1);
112+
QldbDriver driver = getDriver(ledgerName);
113+
114+
Wallet wallet = new Wallet();
115+
wallet.setId("1");
116+
wallet.setDescription("my personal wallet");
117+
wallet.setBalance(25d);
118+
wallet.setTags(ImmutableMap.of("meta", "true"));
119+
wallet.setType(WalletType.PERSONAL);
120+
121+
driver.execute(txn -> {
122+
try {
123+
txn.execute("INSERT INTO Wallet ?", MAPPER.writeValueAsIonValue(wallet));
124+
} catch (IOException e) {
125+
throw new UncheckedIOException(e);
126+
}
127+
});
128+
129+
wallet.setDescription("my business wallet");
130+
wallet.setBalance(26.12d);
131+
wallet.setTags(ImmutableMap.of());
132+
wallet.setType(WalletType.BUSINESS);
133+
134+
String query = "UPDATE Wallet \nSET description = ?,\n balance = ?,\n tags = ?,\n type = ?\n WHERE id = ?";
135+
driver.execute(txn -> {
136+
try {
137+
return txn.execute(query,
138+
MAPPER.writeValueAsIonValue(wallet.getDescription()),
139+
MAPPER.writeValueAsIonValue(wallet.getBalance()),
140+
MAPPER.writeValueAsIonValue(wallet.getTags()),
141+
MAPPER.writeValueAsIonValue(wallet.getType()),
142+
MAPPER.writeValueAsIonValue(wallet.getId()));
143+
} catch (IOException e) {
144+
throw new UncheckedIOException(e);
145+
}
146+
});
147+
148+
Result queryResult = driver.execute(txn -> {
149+
try {
150+
return txn.execute("SELECT * FROM Wallet WHERE id = ?", MAPPER.writeValueAsIonValue(wallet.getId()));
151+
} catch (IOException e) {
152+
throw new UncheckedIOException(e);
153+
}
154+
});
155+
Set<String> result = StreamSupport.stream(queryResult.spliterator(), false)
156+
.map(v -> (IonStruct) v)
157+
.map(s -> s.get("balance").toString())
158+
.collect(Collectors.toSet());
159+
Assert.assertEquals(new HashSet<String>(Arrays.asList("26.12")), result);
160+
161+
// clean up
162+
cleanUp(ledgerName);
163+
}
164+
165+
// UTIL FUNCTIONS AND CLASSES BELOW
166+
167+
public static class Wallet {
168+
String id;
169+
String description;
170+
double balance;
171+
Map<String, String> tags;
172+
WalletType type;
173+
174+
public String getId() {
175+
return id;
176+
}
177+
178+
public void setId(String id) {
179+
this.id = id;
180+
}
181+
182+
public String getDescription() {
183+
return description;
184+
}
185+
186+
public void setDescription(String description) {
187+
this.description = description;
188+
}
189+
190+
public double getBalance() {
191+
return balance;
192+
}
193+
194+
public void setBalance(double balance) {
195+
this.balance = balance;
196+
}
197+
198+
public Map<String, String> getTags() {
199+
return tags;
200+
}
201+
202+
public void setTags(Map<String, String> tags) {
203+
this.tags = tags;
204+
}
205+
206+
public WalletType getType() {
207+
return type;
208+
}
209+
210+
public void setType(
211+
WalletType type) {
212+
this.type = type;
213+
}
66214
}
67215

216+
public enum WalletType {
217+
PERSONAL,
218+
BUSINESS
219+
}
220+
221+
private void createLedgerAndTables(String ledgerName, String ... tableNames) throws Exception {
222+
QldbAsyncClient client = TestUtils.getClientQLDBAsyncV2();
223+
224+
CreateLedgerRequest request = CreateLedgerRequest.builder().name(ledgerName).build();
225+
CreateLedgerResponse ledger = client.createLedger(request).get();
226+
Assert.assertEquals(ledger.name(), ledgerName);
227+
228+
QldbDriver driver = getDriver(ledgerName);
229+
230+
// create tables
231+
for (String tableName : tableNames) {
232+
driver.execute(txn -> { return txn.execute("CREATE TABLE " + tableName); });
233+
}
234+
}
235+
236+
private QldbDriver getDriver(String ledgerName) throws Exception {
237+
return QldbDriver.builder().ledger(ledgerName)
238+
.sessionClientBuilder(
239+
QldbSessionClient.builder().endpointOverride(new URI(Localstack.INSTANCE.getEndpointQLDB()))
240+
).build();
241+
}
242+
243+
private void cleanUp(String ledgerName) {
244+
QldbAsyncClient client = TestUtils.getClientQLDBAsyncV2();
245+
client.deleteLedger(DeleteLedgerRequest.builder().name(ledgerName).build());
246+
}
247+
248+
private boolean isProEnabled() {
249+
return System.getenv(Constants.ENV_LOCALSTACK_API_KEY) != null;
250+
}
68251
}

0 commit comments

Comments
 (0)