11/*
2- * Copyright © 2015, 2018 IBM Corp. All rights reserved.
2+ * Copyright © 2015, 2019 IBM Corp. All rights reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55 * except in compliance with the License. You may obtain a copy of the License at
1616
1717import com .cloudant .client .api .ClientBuilder ;
1818import com .cloudant .client .api .CloudantClient ;
19+ import com .cloudant .tests .extensions .IamAuthCondition ;
1920
2021import okhttp3 .mockwebserver .MockWebServer ;
2122
@@ -32,53 +33,54 @@ public abstract class CloudantClientHelper {
3233 //some tests need access to the URI with user info (e.g. replication)
3334 public static final String SERVER_URI_WITH_USER_INFO ;
3435 //some tests need access to the credentials (e.g. auth interceptors, vcap)
35- public static final String COUCH_USERNAME ;
36- public static final String COUCH_PASSWORD ;
37- public static final String COUCH_HOST ;
36+ static final String SERVER_USER ;
37+ static final String SERVER_PASSWORD ;
38+ static final String SERVER_HOST ;
3839
39- protected static final CloudantClient CLIENT_INSTANCE ;
40+ private static final CloudantClient CLIENT_INSTANCE ;
4041
41- private static final String COUCH_PORT ;
42- private static final String HTTP_PROTOCOL ;
42+ private static final String SERVER_PORT ;
43+ private static final String SERVER_PROTOCOL ;
4344 private static final URL SERVER_URL ;
4445
4546 static {
4647
4748 try {
4849 //a URL might be supplied, otherwise use the separate properties
49- String URL = System .getProperty ("test.couch .url" );
50+ String URL = System .getProperty ("test.server .url" );
5051 if (URL != null ) {
5152 URL couch = new URL (URL );
52- HTTP_PROTOCOL = couch .getProtocol ();
53- COUCH_HOST = couch .getHost ();
54- COUCH_PORT = (couch .getPort () < 0 ) ? null : Integer .toString (couch .getPort ());
53+ SERVER_PROTOCOL = couch .getProtocol ();
54+ SERVER_HOST = couch .getHost ();
55+ SERVER_PORT = (couch .getPort () < 0 ) ? null : Integer .toString (couch .getPort ());
5556 String userInfo = couch .getUserInfo ();
5657 if (userInfo != null ) {
57- COUCH_USERNAME = userInfo .substring (0 , userInfo .indexOf (":" ));
58- COUCH_PASSWORD = userInfo .substring (userInfo .indexOf (":" ) + 1 );
58+ SERVER_USER = userInfo .substring (0 , userInfo .indexOf (":" ));
59+ SERVER_PASSWORD = userInfo .substring (userInfo .indexOf (":" ) + 1 );
5960 } else {
60- COUCH_USERNAME = System .getProperty ("test.couch.username " );
61- COUCH_PASSWORD = System .getProperty ("test.couch .password" );
61+ SERVER_USER = System .getProperty ("test.server.user " );
62+ SERVER_PASSWORD = System .getProperty ("test.server .password" );
6263 }
6364 } else {
64- COUCH_USERNAME = System .getProperty ("test.couch.username " );
65- COUCH_PASSWORD = System .getProperty ("test.couch .password" );
66- COUCH_HOST = System .getProperty ("test.couch .host" , "localhost" );
67- COUCH_PORT = System .getProperty ("test.couch .port" , "5984" );
68- HTTP_PROTOCOL = System .getProperty ("test.couch.http " , "http" ); //should either be
65+ SERVER_USER = System .getProperty ("test.server.user " );
66+ SERVER_PASSWORD = System .getProperty ("test.server .password" );
67+ SERVER_HOST = System .getProperty ("test.server .host" , "localhost" );
68+ SERVER_PORT = System .getProperty ("test.server .port" , "5984" );
69+ SERVER_PROTOCOL = System .getProperty ("test.server.protocol " , "http" ); //should either be
6970 // http or https
7071 }
7172
7273 //now build the URLs
73- SERVER_URL = new URL (HTTP_PROTOCOL + "://"
74- + COUCH_HOST
75- + ((COUCH_PORT != null ) ? ":" + COUCH_PORT : "" )); //port if supplied
74+ SERVER_URL = new URL (SERVER_PROTOCOL + "://"
75+ + SERVER_HOST
76+ + ((SERVER_PORT != null ) ? ":" + SERVER_PORT : "" )); //port if supplied
7677
7778 // Ensure username and password are correctly URL encoded when included in the URI
78- SERVER_URI_WITH_USER_INFO = HTTP_PROTOCOL + "://"
79- + ((COUCH_USERNAME != null ) ? URLEncoder .encode (COUCH_USERNAME , "UTF-8" ) +
80- ":" + URLEncoder .encode (COUCH_PASSWORD , "UTF-8" ) + "@" : "" ) + COUCH_HOST + (
81- (COUCH_PORT != null ) ? ":" + COUCH_PORT : "" ); //port if supplied
79+ SERVER_URI_WITH_USER_INFO = SERVER_PROTOCOL + "://"
80+ + ((!IamAuthCondition .IS_IAM_ENABLED && SERVER_USER != null ) ?
81+ URLEncoder .encode (SERVER_USER , "UTF-8" ) +
82+ ":" + URLEncoder .encode (SERVER_PASSWORD , "UTF-8" ) + "@" : "" ) + SERVER_HOST + (
83+ (SERVER_PORT != null ) ? ":" + SERVER_PORT : "" ); //port if supplied
8284 } catch (Throwable t ) {
8385 throw new RuntimeException (t );
8486 }
@@ -116,9 +118,14 @@ public static ClientBuilder newMockWebServerClientBuilder(MockWebServer mockServ
116118 }
117119
118120 public static ClientBuilder getClientBuilder () {
119- return ClientBuilder .url (SERVER_URL )
120- .username (COUCH_USERNAME )
121- .password (COUCH_PASSWORD );
121+ ClientBuilder builder = ClientBuilder .url (SERVER_URL );
122+ if (IamAuthCondition .IS_IAM_ENABLED ) {
123+ builder .iamApiKey (IamAuthCondition .IAM_API_KEY );
124+ } else {
125+ builder .username (SERVER_USER )
126+ .password (SERVER_PASSWORD );
127+ }
128+ return builder ;
122129 }
123130
124131 static String REP_SOURCE = null ;
0 commit comments