|
7 | 7 |
|
8 | 8 | package oracle.nosql.driver.iam; |
9 | 9 |
|
| 10 | +import static oracle.nosql.driver.iam.Utils.getIAMURL; |
| 11 | + |
10 | 12 | import static org.junit.Assert.assertEquals; |
11 | 13 | import static org.junit.Assert.assertNotNull; |
12 | 14 | import static org.junit.Assert.assertNull; |
13 | 15 | import static org.junit.Assert.fail; |
| 16 | +import static org.junit.Assume.assumeTrue; |
14 | 17 |
|
15 | 18 | import java.io.File; |
16 | 19 | import java.io.FileInputStream; |
|
21 | 24 | import oracle.nosql.driver.NoSQLHandleConfig; |
22 | 25 | import oracle.nosql.driver.Region; |
23 | 26 | import oracle.nosql.driver.iam.OCIConfigFileReader.OCIConfigFile; |
| 27 | +import oracle.nosql.driver.values.ArrayValue; |
| 28 | +import oracle.nosql.driver.values.FieldValue; |
| 29 | +import oracle.nosql.driver.values.JsonUtils; |
| 30 | +import oracle.nosql.driver.values.MapValue; |
24 | 31 |
|
25 | 32 | import org.junit.After; |
26 | 33 | import org.junit.Before; |
@@ -244,4 +251,123 @@ public void testRegionProvider() |
244 | 251 | } catch (IllegalArgumentException iae) { |
245 | 252 | } |
246 | 253 | } |
| 254 | + |
| 255 | + private MapValue getRealm(ArrayValue realms, String realm) { |
| 256 | + for (FieldValue fv : realms) { |
| 257 | + MapValue mv = fv.asMap(); |
| 258 | + String name = mv.get("name").asString().getValue(); |
| 259 | + if (realm.compareTo(name) == 0) { |
| 260 | + return mv; |
| 261 | + } |
| 262 | + } |
| 263 | + return null; |
| 264 | + } |
| 265 | + |
| 266 | + private String nosqlEndpoint(ArrayValue realms, MapValue mv) { |
| 267 | + String realm = mv.get("realm").asString().getValue(); |
| 268 | + MapValue realmVals = getRealm(realms, realm); |
| 269 | + if (realmVals == null) { |
| 270 | + return null; |
| 271 | + } |
| 272 | + String prefix = realmVals.get("epprefix").asString().getValue(); |
| 273 | + String suffix = realmVals.get("epsuffix").asString().getValue(); |
| 274 | + String regionId = mv.get("name").asString().getValue(); |
| 275 | + return "https://" + prefix + regionId + suffix; |
| 276 | + } |
| 277 | + |
| 278 | + private String authEndpoint(ArrayValue realms, MapValue mv) { |
| 279 | + String realm = mv.get("realm").asString().getValue(); |
| 280 | + MapValue realmVals = getRealm(realms, realm); |
| 281 | + if (realmVals == null) { |
| 282 | + return null; |
| 283 | + } |
| 284 | + String prefix = realmVals.get("authprefix").asString().getValue(); |
| 285 | + String suffix = realmVals.get("authsuffix").asString().getValue(); |
| 286 | + String regionId = mv.get("name").asString().getValue(); |
| 287 | + return "https://" + prefix + regionId + suffix; |
| 288 | + } |
| 289 | + |
| 290 | + |
| 291 | + @Test |
| 292 | + public void testAllRegionCodes() { |
| 293 | + /* only execute if we're supplied a path to regions.json file */ |
| 294 | + String jsonPath = System.getProperty("test.regionsfile"); |
| 295 | + assumeTrue("Skipping region codes test: no regionsfile given", |
| 296 | + (jsonPath != null && !jsonPath.isEmpty())); |
| 297 | + MapValue jsonMap = null; |
| 298 | + FileInputStream fis = null; |
| 299 | + try { |
| 300 | + fis = new FileInputStream(jsonPath); |
| 301 | + jsonMap = JsonUtils.createValueFromJson(fis, null).asMap(); |
| 302 | + } catch (Exception e) { |
| 303 | + fail("Can't read regions json file '" + jsonPath + "': " + e); |
| 304 | + } finally { |
| 305 | + if (fis != null) { |
| 306 | + try { |
| 307 | + fis.close(); |
| 308 | + } catch (Exception e) {} |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + /* |
| 313 | + * Regions json has two fields: "realms" and "regions": |
| 314 | + * "realms": [ |
| 315 | + * { |
| 316 | + * "name": "oc1", |
| 317 | + * "epprefix": "nosql.", |
| 318 | + * "epsuffix": ".oci.oraclecloud.com", |
| 319 | + * "authprefix": "auth.", |
| 320 | + * "authsuffix": ".oraclecloud.com" |
| 321 | + * }, |
| 322 | + * ...] |
| 323 | + * |
| 324 | + * "regions": [ |
| 325 | + * {"tlc": "jnb", "realm": "oc1", "name": "af-johannesburg-1"}, |
| 326 | + * {"tlc": "yny", "realm": "oc1", "name": "ap-chuncheon-1"}, |
| 327 | + * {"tlc": "nja", "realm": "oc8", "name": "ap-chiyoda-1"}, |
| 328 | + * ...] |
| 329 | + * |
| 330 | + * Walk all regions, and verify endpoints for each. |
| 331 | + */ |
| 332 | + ArrayValue realms = jsonMap.get("realms").asArray(); |
| 333 | + StringBuilder errs = new StringBuilder(); |
| 334 | + ArrayValue regions = jsonMap.get("regions").asArray(); |
| 335 | + for (FieldValue fv : regions) { |
| 336 | + MapValue mv = fv.asMap(); |
| 337 | + String regionId = mv.get("name").asString().getValue(); |
| 338 | + Region r = Region.fromRegionId(regionId); |
| 339 | + if (r == null) { |
| 340 | + errs.append(" Missing region '").append(regionId).append("'\n"); |
| 341 | + } else { |
| 342 | + String endpoint = r.endpoint(); |
| 343 | + String expEndpoint = nosqlEndpoint(realms, mv); |
| 344 | + if (expEndpoint.compareTo(endpoint) != 0) { |
| 345 | + errs.append(" Wrong nosql endpoint for region '") |
| 346 | + .append(regionId).append("':\n") |
| 347 | + .append(" expected=").append(expEndpoint).append("\n") |
| 348 | + .append(" observed=").append(endpoint).append("\n"); |
| 349 | + } |
| 350 | + } |
| 351 | + String expAuthURL = authEndpoint(realms, mv); |
| 352 | + |
| 353 | + String authURL = getIAMURL(regionId); |
| 354 | + if (authURL == null || expAuthURL.compareTo(authURL) != 0) { |
| 355 | + errs.append(" Wrong auth endpoint for region '") |
| 356 | + .append(regionId).append("':\n") |
| 357 | + .append(" expected=").append(expAuthURL).append("\n") |
| 358 | + .append(" observed=").append(authURL).append("\n"); |
| 359 | + } |
| 360 | + String regionCode = mv.get("tlc").asString().getValue(); |
| 361 | + authURL = getIAMURL(regionCode); |
| 362 | + if (authURL == null ||expAuthURL.compareTo(authURL) != 0) { |
| 363 | + errs.append(" Wrong auth endpoint for region code '") |
| 364 | + .append(regionCode).append("':\n") |
| 365 | + .append(" expected=").append(expAuthURL).append("\n") |
| 366 | + .append(" observed=").append(authURL).append("\n"); |
| 367 | + } |
| 368 | + } |
| 369 | + if (errs.length() > 0) { |
| 370 | + fail("\nErrors found in regions:\n" + errs.toString()); |
| 371 | + } |
| 372 | + } |
247 | 373 | } |
0 commit comments