33import cloud .localstack .Constants ;
44import cloud .localstack .LocalstackTestRunner ;
55import cloud .localstack .docker .annotation .LocalstackDockerProperties ;
6- import cloud .localstack .sample .LambdaHandler ;
7- import cloud .localstack .utils .LocalTestUtil ;
86import 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 ;
1012import software .amazon .awssdk .services .qldb .*;
1113import software .amazon .awssdk .services .qldb .model .*;
1214import software .amazon .qldb .*;
1618import org .junit .*;
1719import org .junit .runner .RunWith ;
1820
21+ import java .io .IOException ;
22+ import java .io .UncheckedIOException ;
1923import java .net .*;
2024import 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 )
2432public 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 \n SET 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