1515package com .microsoft .azure .storage ;
1616
1717import static org .junit .Assert .*;
18+ import static org .junit .Assume .assumeNotNull ;
1819
1920import java .io .ByteArrayInputStream ;
2021import java .io .File ;
3940import javax .xml .parsers .DocumentBuilderFactory ;
4041import javax .xml .parsers .ParserConfigurationException ;
4142
43+ import org .junit .AssumptionViolatedException ;
4244import org .w3c .dom .DOMException ;
4345import org .w3c .dom .Document ;
4446import org .w3c .dom .Node ;
@@ -58,6 +60,9 @@ public class TestHelper {
5860 private static Tenant tenant ;
5961 private static StorageCredentialsAccountAndKey credentials ;
6062 private static CloudStorageAccount account ;
63+ private static Tenant premiumBlobTenant ;
64+ private static StorageCredentialsAccountAndKey premiumBlobCredentials ;
65+ private static CloudStorageAccount premiumBlobAccount ;
6166
6267 private final static boolean enableFiddler = true ;
6368 private final static boolean requireSecondaryEndpoint = false ;
@@ -67,6 +72,11 @@ public static CloudBlobClient createCloudBlobClient() throws StorageException {
6772 return client ;
6873 }
6974
75+ public static CloudBlobClient createPremiumCloudBlobClient () throws StorageException {
76+ CloudBlobClient client = getPremiumBlobAccount ().createCloudBlobClient ();
77+ return client ;
78+ }
79+
7080 public static CloudBlobClient createCloudBlobClient (SharedAccessAccountPolicy policy , boolean useHttps )
7181 throws StorageException , InvalidKeyException , URISyntaxException {
7282
@@ -328,12 +338,12 @@ private static CloudStorageAccount getAccount() throws StorageException {
328338 account = CloudStorageAccount .parse (cloudAccount );
329339 }
330340 else if (accountConfig != null ) {
331- tenant = readTestConfigsFromXml (new File (accountConfig ));
341+ readTestConfigsFromXml (new File (accountConfig ), false );
332342 setAccountAndCredentials ();
333343 }
334344 else {
335345 URL localTestConfig = TestHelper .class .getClassLoader ().getResource ("TestConfigurations.xml" );
336- tenant = readTestConfigsFromXml (new File (localTestConfig .getPath ()));
346+ readTestConfigsFromXml (new File (localTestConfig .getPath ()), false );
337347 setAccountAndCredentials ();
338348 }
339349 }
@@ -344,6 +354,47 @@ else if (accountConfig != null) {
344354 return account ;
345355 }
346356
357+ private static CloudStorageAccount getPremiumBlobAccount () throws StorageException {
358+ // Only do this the first time TestBase is called as storage account is static
359+ if (premiumBlobAccount == null ) {
360+ //enable fiddler
361+ if (enableFiddler )
362+ enableFiddler ();
363+
364+ // try to get the environment variable with the test configuration file path
365+ String accountConfig ;
366+ try {
367+ accountConfig = System .getenv ("storageTestConfiguration" );
368+ }
369+ catch (SecurityException e ) {
370+ accountConfig = null ;
371+ }
372+
373+ // if storageConnection is set, use that as an account string
374+ // if storageTestConfiguration is set, use that as a path to the configurations file
375+ // if neither are set, use the local configurations file at TestConfigurations.xml
376+ try {
377+ if (accountConfig != null ) {
378+ readTestConfigsFromXml (new File (accountConfig ), true );
379+ setAccountAndCredentials ();
380+ }
381+ else {
382+ URL localTestConfig = TestHelper .class .getClassLoader ().getResource ("TestConfigurations.xml" );
383+ readTestConfigsFromXml (new File (localTestConfig .getPath ()), true );
384+ setAccountAndCredentials ();
385+ }
386+ }
387+ catch (AssumptionViolatedException e ) {
388+ throw e ;
389+ }
390+ catch (Exception e ) {
391+ throw StorageException .translateClientException (e );
392+ }
393+ }
394+
395+ return premiumBlobAccount ;
396+ }
397+
347398 private static void setAccountAndCredentials () {
348399 if (requireSecondaryEndpoint )
349400 tenant .assertSecondaryEndpoint ();
@@ -353,9 +404,17 @@ private static void setAccountAndCredentials() {
353404 tenant .getQueueServiceSecondaryEndpoint ()), new StorageUri (tenant .getTableServiceEndpoint (),
354405 tenant .getTableServiceSecondaryEndpoint ()), new StorageUri (tenant .getFileServiceEndpoint (),
355406 tenant .getFileServiceSecondaryEndpoint ()));
407+
408+ if (premiumBlobTenant != null ) {
409+ premiumBlobCredentials = new StorageCredentialsAccountAndKey (premiumBlobTenant .getAccountName (), premiumBlobTenant .getAccountKey ());
410+ premiumBlobAccount = new CloudStorageAccount (premiumBlobCredentials , new StorageUri (premiumBlobTenant .getBlobServiceEndpoint (), premiumBlobTenant .getBlobServiceSecondaryEndpoint ()),
411+ null ,
412+ null ,
413+ null );
414+ }
356415 }
357416
358- private static Tenant readTestConfigsFromXml (File testConfigurations ) throws ParserConfigurationException ,
417+ private static void readTestConfigsFromXml (File testConfigurations , boolean premiumBlob ) throws ParserConfigurationException ,
359418 SAXException , IOException , DOMException , URISyntaxException {
360419
361420 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance ();
@@ -372,7 +431,14 @@ private static Tenant readTestConfigsFromXml(File testConfigurations) throws Par
372431 throw new IllegalArgumentException ("No TargetTestTenant specified." );
373432 }
374433
375- Tenant tenant = null ;
434+ Node premiumBlobTenantNode = testConfigs .getElementsByTagName ("TargetPremiumBlobTenant" ).item (0 );
435+ String premiumBlobTenantName = null ;
436+ if (premiumBlobTenantNode != null ) {
437+ premiumBlobTenantName = premiumBlobTenantNode .getTextContent ();
438+ }
439+
440+ tenant = null ;
441+ premiumBlobTenant = null ;
376442 final NodeList tenantNodes = testConfigs .getElementsByTagName ("TenantName" );
377443 for (int i = 0 ; i < tenantNodes .getLength (); i ++) {
378444 if (tenantNodes .item (i ).getTextContent ().equals (targetTenant )) {
@@ -436,18 +502,50 @@ else if (name.equals("TableHttpsPortOverride")) {
436502 else if (name .equals ("FileHttpsPortOverride" )) {
437503 tenant .setFileHttpsPortOverride (Integer .parseInt (node .getTextContent ()));
438504 }
439- else {
505+ else if (! premiumBlob ) {
440506 throw new IllegalArgumentException (String .format (
441507 "Invalid child of TenantConfiguration with name: %s" , name ));
442508 }
443509 }
444510 }
445511 }
512+
513+ if (tenantNodes .item (i ).getTextContent ().equals (premiumBlobTenantName )) {
514+ premiumBlobTenant = new Tenant ();
515+ Node parent = tenantNodes .item (i ).getParentNode ();
516+ final NodeList childNodes = parent .getChildNodes ();
517+ for (int j = 0 ; j < childNodes .getLength (); j ++) {
518+ final Node node = childNodes .item (j );
519+
520+ if (node .getNodeType () != Node .ELEMENT_NODE ) {
521+ // do nothing
522+ } else {
523+ final String name = node .getNodeName ();
524+
525+ if (name .equals ("TenantName" )) {
526+ premiumBlobTenant .setTenantName (node .getTextContent ());
527+ } else if (name .equals ("TenantType" )) {
528+ // do nothing, we don't track this field
529+ } else if (name .equals ("AccountName" )) {
530+ premiumBlobTenant .setAccountName (node .getTextContent ());
531+ } else if (name .equals ("AccountKey" )) {
532+ premiumBlobTenant .setAccountKey (node .getTextContent ());
533+ } else if (name .equals ("BlobServiceEndpoint" )) {
534+ premiumBlobTenant .setBlobServiceEndpoint (new URI (node .getTextContent ()));
535+ } else if (name .equals ("BlobServiceSecondaryEndpoint" )) {
536+ premiumBlobTenant .setBlobServiceSecondaryEndpoint (new URI (node .getTextContent ()));
537+ }
538+ }
539+ }
540+ }
446541 }
447542
448- if (tenant == null ) {
543+ if (tenant == null && ! premiumBlob ) {
449544 throw new IllegalArgumentException ("TargetTestTenant specified did not exist in TenantConfigurations." );
450545 }
451- return tenant ;
546+
547+ if (premiumBlobTenant == null && premiumBlob ) {
548+ assumeNotNull (premiumBlobTenant );
549+ }
452550 }
453551}
0 commit comments