1+ /*
2+ * Copyright 2026 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com .google .cloud .spanner .connection ;
18+
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertFalse ;
21+ import static org .junit .Assert .assertSame ;
22+ import static org .junit .Assert .assertTrue ;
23+ import static org .mockito .ArgumentMatchers .any ;
24+ import static org .mockito .Mockito .mock ;
25+ import static org .mockito .Mockito .times ;
26+ import static org .mockito .Mockito .verify ;
27+ import static org .mockito .Mockito .when ;
28+
29+ import com .google .auth .oauth2 .ServiceAccountCredentials ;
30+ import java .io .IOException ;
31+ import java .net .URI ;
32+ import java .util .Arrays ;
33+ import java .util .Collections ;
34+ import java .util .List ;
35+ import java .util .Map ;
36+ import org .junit .Test ;
37+ import org .junit .runner .RunWith ;
38+ import org .junit .runners .JUnit4 ;
39+
40+ @ RunWith (JUnit4 .class )
41+ public class MutableCredentialsTest {
42+ ServiceAccountCredentials initialCredentials = mock (ServiceAccountCredentials .class );
43+ ServiceAccountCredentials initialScopedCredentials = mock (ServiceAccountCredentials .class );
44+ ServiceAccountCredentials updatedCredentials = mock (ServiceAccountCredentials .class );
45+ ServiceAccountCredentials updatedScopedCredentials = mock (ServiceAccountCredentials .class );
46+ List <String > scopes = Arrays .asList ("scope-a" , "scope-b" );
47+ Map <String , List <String >> initialMetadata =
48+ Collections .singletonMap ("Authorization" , Collections .singletonList ("v1" ));
49+ Map <String , List <String >> updatedMetadata =
50+ Collections .singletonMap ("Authorization" , Collections .singletonList ("v2" ));
51+ String initialAuthType = "auth-1" ;
52+ String updatedAuthType = "auth-2" ;
53+
54+ @ Test
55+ public void testCreateMutableCredentialsAndUpdate () throws IOException {
56+ setupInitialCredentials ();
57+ setupUpdatedCredentials ();
58+
59+ MutableCredentials credentials = new MutableCredentials (initialCredentials , scopes );
60+
61+ assertEquals (initialAuthType , credentials .getAuthenticationType ());
62+ assertTrue (credentials .hasRequestMetadata ());
63+ assertTrue (credentials .hasRequestMetadataOnly ());
64+ assertEquals (initialMetadata , credentials .getRequestMetadata (URI .create ("https://spanner.googleapis.com" )));
65+
66+ credentials .refresh ();
67+
68+ verify (initialScopedCredentials , times (1 )).refresh ();
69+
70+ credentials .updateCredentials (updatedCredentials );
71+
72+ assertEquals (updatedAuthType , credentials .getAuthenticationType ());
73+ assertFalse (credentials .hasRequestMetadata ());
74+ assertFalse (credentials .hasRequestMetadataOnly ());
75+ assertSame (updatedMetadata , credentials .getRequestMetadata (URI .create ("https://example.com" )));
76+
77+ credentials .refresh ();
78+
79+ verify (updatedScopedCredentials , times (1 )).refresh ();
80+ }
81+
82+ private void setupInitialCredentials () throws IOException {
83+ when (initialCredentials .createScoped (scopes )).thenReturn (initialScopedCredentials );
84+ when (initialScopedCredentials .getAuthenticationType ()).thenReturn (initialAuthType );
85+ when (initialScopedCredentials .getRequestMetadata (any (URI .class )))
86+ .thenReturn (initialMetadata );
87+ when (initialScopedCredentials .hasRequestMetadata ()).thenReturn (true );
88+ when (initialScopedCredentials .hasRequestMetadataOnly ()).thenReturn (true );
89+ }
90+
91+ private void setupUpdatedCredentials () throws IOException {
92+ when (updatedCredentials .createScoped (scopes )).thenReturn (updatedScopedCredentials );
93+ when (updatedScopedCredentials .getAuthenticationType ()).thenReturn (updatedAuthType );
94+ when (updatedScopedCredentials .getRequestMetadata (any (URI .class ))).thenReturn (updatedMetadata );
95+ when (updatedScopedCredentials .hasRequestMetadata ()).thenReturn (false );
96+ when (updatedScopedCredentials .hasRequestMetadataOnly ()).thenReturn (false );
97+ }
98+ }
0 commit comments