|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -import com.datastax.oss.driver.api.core.CqlSession; |
5 | | -import com.datastax.oss.driver.api.core.config.DriverConfigLoader; |
6 | | -import com.example.keyspace.ScenarioKeyspaces; |
7 | | -import com.google.gson.Gson; |
8 | | -import org.junit.jupiter.api.DisplayName; |
9 | | -import org.junit.jupiter.api.Nested; |
| 4 | + |
| 5 | +import com.example.keyspace.HelloKeyspaces; |
10 | 6 | import org.junit.jupiter.api.Tag; |
11 | 7 | import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; |
12 | 8 | import org.junit.jupiter.api.TestInstance; |
|
17 | 13 | import org.junit.jupiter.api.Order; |
18 | 14 | import software.amazon.awssdk.regions.Region; |
19 | 15 | import software.amazon.awssdk.services.keyspaces.KeyspacesClient; |
20 | | -import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; |
21 | | -import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest; |
22 | | -import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse; |
23 | | -import java.io.IOException; |
24 | | -import java.time.ZoneOffset; |
25 | | -import java.time.ZonedDateTime; |
26 | | -import java.util.Date; |
27 | | -import java.util.Random; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
28 | 17 |
|
29 | 18 | @TestInstance(TestInstance.Lifecycle.PER_METHOD) |
30 | 19 | @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
31 | 20 | public class KeyspaceTest { |
32 | | - public static final String DASHES = new String(new char[80]).replace("\0", "-"); |
33 | | - private static String fileName = ""; |
34 | | - private static String keyspaceName = ""; |
35 | | - private static String titleUpdate = "The Family"; |
36 | | - private static int yearUpdate = 2013; |
37 | | - private static String tableName = "Movie"; |
38 | | - private static String tableNameRestore = "MovieRestore"; |
39 | 21 | private static KeyspacesClient keyClient; |
40 | | - private static CqlSession session; |
41 | 22 |
|
42 | 23 | @BeforeAll |
43 | 24 | public static void setUp() { |
44 | | - Random rand = new Random(); |
45 | | - int randomNum = rand.nextInt((10000 - 1) + 1) + 1; |
46 | | - keyspaceName = "key" + randomNum; |
47 | 25 | Region region = Region.US_EAST_1; |
48 | 26 | keyClient = KeyspacesClient.builder() |
49 | 27 | .region(region) |
50 | 28 | .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) |
51 | 29 | .build(); |
52 | | - |
53 | | - DriverConfigLoader loader = DriverConfigLoader.fromClasspath("application.conf"); |
54 | | - session = CqlSession.builder() |
55 | | - .withConfigLoader(loader) |
56 | | - .build(); |
57 | | - |
58 | | - // Get the values to run these tests from AWS Secrets Manager. |
59 | | - Gson gson = new Gson(); |
60 | | - String json = getSecretValues(); |
61 | | - SecretValues values = gson.fromJson(json, SecretValues.class); |
62 | | - fileName = values.getFileName(); |
63 | | - keyspaceName = values.getKeyspaceName() + randomNum; |
64 | 30 | } |
65 | 31 |
|
66 | 32 | @Test |
67 | 33 | @Tag("IntegrationTest") |
68 | 34 | @Order(1) |
69 | | - public void scenarioTest() throws InterruptedException, IOException { |
70 | | - System.out.println(DASHES); |
71 | | - System.out.println("1. Create a keyspace."); |
72 | | - ScenarioKeyspaces.createKeySpace(keyClient, keyspaceName); |
73 | | - System.out.println(DASHES); |
74 | | - |
75 | | - System.out.println(DASHES); |
76 | | - Thread.sleep(5000); |
77 | | - System.out.println("2. Check for keyspace existence."); |
78 | | - ScenarioKeyspaces.checkKeyspaceExistence(keyClient, keyspaceName); |
79 | | - System.out.println(DASHES); |
80 | | - |
81 | | - System.out.println(DASHES); |
82 | | - System.out.println("3. List keyspaces using a paginator."); |
83 | | - ScenarioKeyspaces.listKeyspacesPaginator(keyClient); |
84 | | - System.out.println(DASHES); |
85 | | - |
86 | | - System.out.println(DASHES); |
87 | | - System.out.println("4. Create a table with a simple movie data schema, and enable point-in-time recovery."); |
88 | | - ScenarioKeyspaces.createTable(keyClient, keyspaceName, tableName); |
89 | | - System.out.println(DASHES); |
90 | | - |
91 | | - System.out.println(DASHES); |
92 | | - System.out.println("5. Check for the table to be in an Active state."); |
93 | | - Thread.sleep(6000); |
94 | | - ScenarioKeyspaces.checkTable(keyClient, keyspaceName, tableName); |
95 | | - System.out.println(DASHES); |
96 | | - |
97 | | - System.out.println(DASHES); |
98 | | - System.out.println("6. Use a Cassandra driver to insert some records into the Movie table."); |
99 | | - Thread.sleep(6000); |
100 | | - ScenarioKeyspaces.loadData(session, fileName, keyspaceName); |
101 | | - System.out.println(DASHES); |
102 | | - |
103 | | - System.out.println(DASHES); |
104 | | - System.out.println("7. Get all records from the Movie table."); |
105 | | - ScenarioKeyspaces.getMovieData(session, keyspaceName); |
106 | | - System.out.println(DASHES); |
107 | | - |
108 | | - System.out.println(DASHES); |
109 | | - System.out.println("8. Get a specific Movie."); |
110 | | - ScenarioKeyspaces.getSpecificMovie(session, keyspaceName); |
111 | | - System.out.println(DASHES); |
112 | | - |
113 | | - System.out.println(DASHES); |
114 | | - System.out.println("9. Get a UTC timestamp for the current time."); |
115 | | - ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC); |
116 | | - System.out.println("DATETIME = " + Date.from(utc.toInstant())); |
117 | | - System.out.println(DASHES); |
118 | | - |
119 | | - System.out.println(DASHES); |
120 | | - System.out.println("10. Update the table schema to add a watched Boolean column."); |
121 | | - ScenarioKeyspaces.updateTable(keyClient, keyspaceName, tableName); |
122 | | - System.out.println(DASHES); |
123 | | - |
124 | | - System.out.println(DASHES); |
125 | | - System.out.println("11. Update an item as watched."); |
126 | | - Thread.sleep(10000); // Wait 10 secs for the update. |
127 | | - ScenarioKeyspaces.updateRecord(session, keyspaceName, titleUpdate, yearUpdate); |
128 | | - System.out.println(DASHES); |
129 | | - |
130 | | - System.out.println(DASHES); |
131 | | - System.out.println("12. Query for items with watched = True."); |
132 | | - ScenarioKeyspaces.getWatchedData(session, keyspaceName); |
133 | | - System.out.println(DASHES); |
134 | | - |
135 | | - System.out.println(DASHES); |
136 | | - System.out.println("13. Restore the table back to the previous state using the timestamp."); |
137 | | - System.out.println("Note that the restore operation can take up to 20 minutes."); |
138 | | - ScenarioKeyspaces.restoreTable(keyClient, keyspaceName, utc); |
139 | | - System.out.println(DASHES); |
140 | | - |
141 | | - System.out.println(DASHES); |
142 | | - System.out.println("14. Check for completion of the restore action."); |
143 | | - Thread.sleep(5000); |
144 | | - ScenarioKeyspaces.checkRestoredTable(keyClient, keyspaceName, "MovieRestore"); |
145 | | - System.out.println(DASHES); |
146 | | - |
147 | | - System.out.println(DASHES); |
148 | | - System.out.println("15. Delete both tables."); |
149 | | - ScenarioKeyspaces.deleteTable(keyClient, keyspaceName, tableName); |
150 | | - ScenarioKeyspaces.deleteTable(keyClient, keyspaceName, tableNameRestore); |
151 | | - System.out.println(DASHES); |
152 | | - |
153 | | - System.out.println(DASHES); |
154 | | - System.out.println("16. Confirm that the table was deleted."); |
155 | | - ScenarioKeyspaces.checkTableDelete(keyClient, keyspaceName, tableName); |
156 | | - ScenarioKeyspaces.checkTableDelete(keyClient, keyspaceName, tableNameRestore); |
157 | | - System.out.println(DASHES); |
158 | | - |
159 | | - System.out.println(DASHES); |
160 | | - System.out.println("17. Delete the keyspace."); |
161 | | - ScenarioKeyspaces.deleteKeyspace(keyClient, keyspaceName); |
162 | | - System.out.println(DASHES); |
163 | | - } |
164 | | - |
165 | | - public static String getSecretValues() { |
166 | | - // Get the Amazon RDS creds from Secrets Manager. |
167 | | - SecretsManagerClient secretClient = SecretsManagerClient.builder() |
168 | | - .region(Region.US_EAST_1) |
169 | | - .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) |
170 | | - .build(); |
171 | | - String secretName = "test/keyspace"; |
172 | | - |
173 | | - GetSecretValueRequest valueRequest = GetSecretValueRequest.builder() |
174 | | - .secretId(secretName) |
175 | | - .build(); |
176 | | - |
177 | | - GetSecretValueResponse valueResponse = secretClient.getSecretValue(valueRequest); |
178 | | - return valueResponse.secretString(); |
179 | | - } |
180 | | - |
181 | | - @Nested |
182 | | - @DisplayName("A class used to get test values from test/keyspace (an AWS Secrets Manager secret)") |
183 | | - class SecretValues { |
184 | | - private String fileName; |
185 | | - private String keyspaceName; |
186 | | - |
187 | | - public String getFileName() { |
188 | | - return fileName; |
189 | | - } |
190 | | - |
191 | | - public String getKeyspaceName() { |
192 | | - return keyspaceName; |
193 | | - } |
| 35 | + public void KeyspaceTest() { |
| 36 | + assertDoesNotThrow(() -> HelloKeyspaces.listKeyspaces(keyClient), |
| 37 | + "Failed to list namespaces."); |
| 38 | + System.out.println("Test passed"); |
194 | 39 | } |
195 | 40 | } |
0 commit comments