9
9
import oracle .jdbc .provider .azure .AzureTestProperty ;
10
10
import oracle .jdbc .provider .azure .authentication .AzureAuthenticationMethod ;
11
11
import org .junit .jupiter .api .Assertions ;
12
+ import org .junit .jupiter .api .Nested ;
12
13
import org .junit .jupiter .api .Test ;
13
14
14
15
import java .sql .Connection ;
20
21
21
22
class AzureAppConfigurationProviderTest {
22
23
24
+ /**
25
+ * Label-related tests
26
+ */
27
+ @ Nested
28
+ class LabelTests {
29
+ @ Test
30
+ public void testNoLabel () throws SQLException {
31
+ String url = buildUrlWithLabel (null );
32
+ try (Connection conn = tryConnection (url )) {
33
+ } catch (IllegalArgumentException e ) {
34
+ // no connect descriptor is set for no label
35
+ Assertions .assertTrue (e .getMessage ().contains (
36
+ "Missing mandatory attributes: connect_descriptor" ));
37
+ }
38
+ }
39
+
40
+ @ Test
41
+ public void testEmptyLabel () throws SQLException {
42
+ String url = buildUrlWithLabel ("" );
43
+ try (Connection conn = tryConnection (url )) {
44
+ } catch (IllegalArgumentException e ) {
45
+ // no connect descriptor is set for empty label
46
+ Assertions .assertTrue (e .getMessage ().contains (
47
+ "Missing mandatory attributes: connect_descriptor" ));
48
+ }
49
+ }
50
+
51
+ @ Test
52
+ public void testLabelEqualsAll () throws SQLException {
53
+ String url = buildUrlWithLabel ("all" );
54
+ try (Connection conn = tryConnection (url )) {
55
+ } catch (IllegalArgumentException e ) {
56
+ Assertions .assertTrue (e .getMessage ().contains (
57
+ "Label 'all' or '*' is not allowed" ));
58
+ }
59
+ }
60
+
61
+ @ Test
62
+ public void testLabelEqualsStar () throws SQLException {
63
+ String url = buildUrlWithLabel ("*" );
64
+ try (Connection conn = tryConnection (url )) {
65
+ } catch (IllegalArgumentException e ) {
66
+ Assertions .assertTrue (e .getMessage ().contains (
67
+ "Label 'all' or '*' is not allowed" ));
68
+ }
69
+ }
70
+
71
+ @ Test
72
+ public void testMultipleLabels () throws SQLException {
73
+ String url = buildUrlWithLabel ("dev,prod" );
74
+ try (Connection conn = tryConnection (url )) {
75
+ } catch (IllegalArgumentException e ) {
76
+ Assertions .assertTrue (e .getMessage ().contains (
77
+ "Multiple labels and wildcards are not supported" ));
78
+ }
79
+ }
80
+
81
+ @ Test
82
+ public void testWildcardLabel () throws SQLException {
83
+ String url = buildUrlWithLabel ("dev*" );
84
+ try (Connection conn = tryConnection (url )) {
85
+ } catch (IllegalArgumentException e ) {
86
+ Assertions .assertTrue (e .getMessage ().contains (
87
+ "Multiple labels and wildcards are not supported" ));
88
+ }
89
+ }
90
+ }
91
+
23
92
/**
24
93
* Verify that the cache is purged after hitting 1017 error.
25
94
* Specifically, get connection to the same url twice, but modify the 'user'
26
95
* every time.
27
96
* The provided app configuration should have correct username, password and
28
97
* correct connect descriptor to connect to Database.
29
- ** /
98
+ */
30
99
@ Test
31
100
public void testCachePurged () throws SQLException {
32
101
ConfigurationClient client = getSecretCredentialClient ();
@@ -36,6 +105,7 @@ public void testCachePurged() throws SQLException {
36
105
TestProperties .getOrAbort (AzureTestProperty .AZURE_APP_CONFIG_KEY );
37
106
String APP_CONFIG_LABEL =
38
107
TestProperties .getOrAbort (AzureTestProperty .AZURE_APP_CONFIG_LABEL );
108
+
39
109
String username = "user" ;
40
110
String originalUrl =
41
111
"jdbc:oracle:thin:@config-azure://" + APP_CONFIG_NAME +
@@ -52,16 +122,18 @@ public void testCachePurged() throws SQLException {
52
122
client .setConfigurationSetting ( APP_CONFIG_KEY + username ,
53
123
APP_CONFIG_LABEL , originalKeyValue + "wrong" );
54
124
55
- // Connection fails: hit 1017
56
- SQLException exception = assertThrows (SQLException .class ,
57
- () -> tryConnection (url ), "Should throw an SQLException" );
58
- Assertions .assertEquals (exception .getErrorCode (), 1017 );
59
-
60
- // Set value of 'user' correct
61
- ConfigurationSetting result =
62
- client .setConfigurationSetting (APP_CONFIG_KEY + username ,
63
- APP_CONFIG_LABEL , originalKeyValue );
64
- Assertions .assertEquals (originalKeyValue , result .getValue ());
125
+ try {
126
+ // Connection fails: hit 1017
127
+ SQLException exception = assertThrows (SQLException .class ,
128
+ () -> tryConnection (url ), "Should throw an SQLException" );
129
+ Assertions .assertEquals (exception .getErrorCode (), 1017 );
130
+ } finally {
131
+ // Set value of 'user' correct
132
+ ConfigurationSetting result =
133
+ client .setConfigurationSetting (APP_CONFIG_KEY + username ,
134
+ APP_CONFIG_LABEL , originalKeyValue );
135
+ Assertions .assertEquals (originalKeyValue , result .getValue ());
136
+ }
65
137
66
138
// Connection succeeds
67
139
try (Connection conn = tryConnection (url )) {
@@ -76,7 +148,7 @@ public void testCachePurged() throws SQLException {
76
148
77
149
/**
78
150
* Helper function: try to get connection form specified url
79
- ** /
151
+ */
80
152
private Connection tryConnection (String url ) throws SQLException {
81
153
OracleDataSource ds = new OracleDataSource ();
82
154
ds .setURL (url );
@@ -86,7 +158,7 @@ private Connection tryConnection(String url) throws SQLException {
86
158
87
159
/**
88
160
* Similar to the method in AzureAppConfigurationProviderURLParserTest
89
- ** /
161
+ */
90
162
private static ConfigurationClient getSecretCredentialClient () {
91
163
return new ConfigurationClientBuilder ()
92
164
.credential ( new ClientSecretCredentialBuilder ()
@@ -103,7 +175,7 @@ private static ConfigurationClient getSecretCredentialClient() {
103
175
/**
104
176
* Use {@link AzureAuthenticationMethod#SERVICE_PRINCIPLE} as its
105
177
* authentication method.
106
- ** /
178
+ */
107
179
private String composeUrlWithServicePrincipleAuthentication (String originalUrl ){
108
180
String [] options = new String [] {
109
181
"AUTHENTICATION=AZURE_SERVICE_PRINCIPAL" ,
@@ -115,4 +187,21 @@ private String composeUrlWithServicePrincipleAuthentication(String originalUrl){
115
187
AzureTestProperty .AZURE_TENANT_ID )};
116
188
return String .format ("%s&%s" , originalUrl , String .join ("&" , options ));
117
189
}
190
+
191
+ /**
192
+ * Helper function: to construct a URL with label parameter.
193
+ */
194
+ private String buildUrlWithLabel (String label ) {
195
+ String APP_CONFIG_NAME =
196
+ TestProperties .getOrAbort (AzureTestProperty .AZURE_APP_CONFIG_NAME );
197
+ String APP_CONFIG_KEY =
198
+ TestProperties .getOrAbort (AzureTestProperty .AZURE_APP_CONFIG_KEY );
199
+ String baseUrl =
200
+ "jdbc:oracle:thin:@config-azure://" + APP_CONFIG_NAME +
201
+ "?key=" + APP_CONFIG_KEY ;
202
+ if (label != null ) {
203
+ baseUrl += "&label=" + label ;
204
+ }
205
+ return composeUrlWithServicePrincipleAuthentication (baseUrl );
206
+ }
118
207
}
0 commit comments