1414 * limitations under the License.
1515 */
1616
17- package com .google .cloud .spanner .connection ;
17+ package com .google .cloud .spanner .connection . it ;
1818
19- import static org .junit .Assert .assertEquals ;
20- import static org .junit .Assert .assertTrue ;
21- import static org .junit .Assert .fail ;
19+ import static org .junit .Assert .*;
2220import static org .junit .Assume .assumeTrue ;
2321
2422import com .google .auth .oauth2 .GoogleCredentials ;
2523import com .google .auth .oauth2 .ServiceAccountCredentials ;
26- import com .google .cloud .spanner .ErrorCode ;
27- import com .google .cloud .spanner .ResultSet ;
28- import com .google .cloud .spanner .SerialIntegrationTest ;
29- import com .google .cloud .spanner .SpannerException ;
30- import com .google .cloud .spanner .Statement ;
24+ import com .google .cloud .spanner .*;
25+
3126import java .io .IOException ;
3227import java .io .InputStream ;
3328import java .nio .file .Files ;
3429import java .nio .file .Paths ;
3530import java .util .ArrayList ;
3631import java .util .List ;
32+
33+ import com .google .cloud .spanner .admin .database .v1 .DatabaseAdminClient ;
34+ import com .google .cloud .spanner .connection .Connection ;
35+ import com .google .cloud .spanner .connection .ConnectionOptions ;
36+ import com .google .cloud .spanner .connection .ITAbstractSpannerTest ;
37+ import com .google .cloud .spanner .connection .MutableCredentials ;
38+ import com .google .spanner .admin .database .v1 .Database ;
39+ import com .google .spanner .admin .database .v1 .DatabaseName ;
40+ import com .google .spanner .admin .database .v1 .InstanceName ;
3741import org .junit .Test ;
3842import org .junit .experimental .categories .Category ;
3943import org .junit .runner .RunWith ;
4448public class ITMutableCredentialsTest extends ITAbstractSpannerTest {
4549 private static final String INVALID_KEY_FILE =
4650 ITMutableCredentialsTest .class .getResource ("test-key.json" ).getPath ();
51+ /*private static final String VALID_KEY_FILE =
52+ ITMutableCredentialsTest.class.getResource("test-key-cloud-storage.json").getPath();
53+ */
4754
4855 @ Test
4956 public void testMutableCredentialsUpdateAuthorizationForRunningClient () throws IOException {
50- assumeTrue ("This test requires a service account key file" , hasValidKeyFile ());
5157
5258 GoogleCredentials credentialsFromFile ;
53- try (InputStream stream = Files .newInputStream (Paths .get (getKeyFile () ))) {
59+ try (InputStream stream = Files .newInputStream (Paths .get (GceTestEnvConfig . GCE_CREDENTIALS_FILE ))) {
5460 credentialsFromFile = GoogleCredentials .fromStream (stream );
5561 }
5662 assumeTrue (
@@ -66,23 +72,19 @@ public void testMutableCredentialsUpdateAuthorizationForRunningClient() throws I
6672 List <String > scopes = new ArrayList <>(getTestEnv ().getTestHelper ().getOptions ().getScopes ());
6773 MutableCredentials mutableCredentials = new MutableCredentials (validCredentials , scopes );
6874
69- StringBuilder uri =
70- extractConnectionUrl (getTestEnv ().getTestHelper ().getOptions (), getDatabase ());
71- ConnectionOptions options =
72- ConnectionOptions .newBuilder ()
73- .setUri (uri .toString ())
75+ SpannerOptions options =
76+ SpannerOptions .newBuilder ()
7477 .setCredentials (mutableCredentials )
7578 .build ();
7679
77- try (Connection connection = options .getConnection ()) {
78- try (ResultSet rs = connection .executeQuery (Statement .of ("SELECT 1" ))) {
79- assertTrue (rs .next ());
80- }
81-
82- mutableCredentials .updateCredentials (invalidCredentials );
83-
84- try (ResultSet rs = connection .executeQuery (Statement .of ("SELECT 2" ))) {
85- rs .next ();
80+ try (Spanner spanner = options .getService ();
81+ DatabaseAdminClient databaseAdminClient = spanner .createDatabaseAdminClient ()) {
82+ String dbName = DatabaseName .of (GceTestEnvConfig .GCE_PROJECT_ID , getTestEnv ().getTestHelper ().getInstanceId ().getInstance (), "TEST" ).toString ();
83+ Database database = databaseAdminClient .getDatabase (dbName );
84+ assertNotNull (database );
85+ try {
86+ mutableCredentials .updateCredentials (invalidCredentials );
87+ databaseAdminClient .getDatabase (dbName );
8688 fail ("Expected UNAUTHENTICATED after switching to invalid credentials" );
8789 } catch (SpannerException e ) {
8890 assertEquals (ErrorCode .UNAUTHENTICATED , e .getErrorCode ());
0 commit comments