Skip to content

Commit 99bd24d

Browse files
committed
Add unit test for getting URL through JDNI
1 parent c225f6f commit 99bd24d

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

core/src/main/java/com/ibm/watson/developer_cloud/util/CredentialUtils.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.ibm.watson.developer_cloud.util;
1414

15+
import java.util.Hashtable;
1516
import java.util.Map.Entry;
1617
import java.util.logging.Level;
1718
import java.util.logging.Logger;
@@ -91,6 +92,9 @@ public String getUsername() {
9192
/** The services. */
9293
private static String services;
9394

95+
/** The context. */
96+
private static Context context;
97+
9498
/** The Constant USERNAME. */
9599
private static final String USERNAME = "username";
96100

@@ -120,22 +124,35 @@ private CredentialUtils() {
120124
}
121125

122126
/**
123-
* Attempt to get the Base64-encoded value through JNDI.
124-
*
125-
* This method should always return null on Android due to the javax functions being unsupported
127+
* Builds the lookup name to be searched for in JDNI
128+
* and uses it to call the overloaded JDNI method.
126129
*
127130
* @param serviceName Name of the bluemix service
128131
* @param lookupNameExtension Extension to determine which value should be retrieved through JDNI
129132
* @return The encoded desired value
130133
*/
131134
private static String getJDNIValue(String serviceName, String lookupNameExtension) {
135+
return getJDNIValue("watson-developer-cloud/" + serviceName + lookupNameExtension);
136+
}
137+
138+
/**
139+
* Attempt to get the Base64-encoded value through JNDI.
140+
*
141+
* This method should always return null on Android due to the javax functions being unsupported
142+
*
143+
* @param lookupName Key to lookup in JDNI
144+
* @return The encoded desired value
145+
*/
146+
private static String getJDNIValue(String lookupName) {
132147
if (!isClassAvailable("javax.naming.Context") || !isClassAvailable("javax.naming.InitialContext")) {
133148
log.info("JNDI string lookups is not available.");
134149
return null;
135150
}
136-
String lookupName = "watson-developer-cloud/" + serviceName + lookupNameExtension;
151+
137152
try {
138-
Context context = new InitialContext();
153+
if (context == null) {
154+
context = new InitialContext();
155+
}
139156
return (String) context.lookup(lookupName);
140157
} catch (Exception e) {
141158
log.fine("JNDI " + lookupName + " not found.");
@@ -339,4 +356,32 @@ public static String getAPIUrl(String serviceName, String plan) {
339356
public static void setServices(String services) {
340357
CredentialUtils.services = services;
341358
}
359+
360+
/**
361+
* Sets the context variable for JDNI. This is a utility method for testing.
362+
*
363+
* @param env Configuration options for the context
364+
*/
365+
public static void setContext(Hashtable<String, String> env) {
366+
try {
367+
CredentialUtils.context = new InitialContext(env);
368+
} catch (Exception e) {
369+
log.fine("Error setting up JDNI context: " + e.getMessage());
370+
}
371+
}
372+
373+
/**
374+
* Method for testing the getAPIUrl method that bypasses the VCAP
375+
* services to ensure retrieval from JDNI.
376+
*
377+
* @param serviceName the service name
378+
* @return the API URL
379+
*/
380+
public static String getAPIUrlTest(String serviceName) {
381+
if ((serviceName == null) || serviceName.isEmpty()) {
382+
return null;
383+
}
384+
385+
return getJDNIValue("jdni/watson-developer-cloud/" + serviceName + LOOKUP_NAME_EXTENSION_URL);
386+
}
342387
}

core/src/test/java/com/ibm/watson/developer_cloud/util/CredentialUtilsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.junit.Assert.assertTrue;
1818

1919
import java.io.InputStream;
20+
import java.util.Hashtable;
2021

2122
import org.junit.Assert;
2223
import org.junit.Before;
@@ -50,6 +51,8 @@ public class CredentialUtilsTest extends WatsonServiceTest {
5051

5152
private static final String VISUAL_RECOGNITION = "watson_vision_combined";
5253

54+
private static final String PERSONALITY_INSIGHTS_URL = "https://gateway.watsonplatform.net/personality-insights/api";
55+
5356
/**
5457
* Setup.
5558
*/
@@ -58,6 +61,14 @@ public void setup() {
5861
final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(VCAP_SERVICES);
5962
final String vcapServices = getStringFromInputStream(in);
6063
CredentialUtils.setServices(vcapServices);
64+
65+
final Hashtable<String, String> env = new Hashtable<>();
66+
env.put("java.naming.factory.initial", "org.osjava.sj.SimpleContextFactory");
67+
env.put("org.osjava.sj.delimiter", "/");
68+
String rootPath = System.getProperty("user.dir") + "/src/test/resources";
69+
env.put("org.osjava.sj.root", rootPath);
70+
71+
CredentialUtils.setContext(env);
6172
}
6273

6374
/**
@@ -113,4 +124,12 @@ public void testGetUserCredentialsWithPlan() {
113124
assertEquals(credentials.getUsername(), NOT_A_USERNAME);
114125
assertEquals(credentials.getPassword(), NOT_A_PASSWORD);
115126
}
127+
128+
/**
129+
* Test getting the API URL using JDNI.
130+
*/
131+
@Test
132+
public void testGetAPIUrlFromJDNI() {
133+
assertEquals(CredentialUtils.getAPIUrlTest(SERVICE_NAME), PERSONALITY_INSIGHTS_URL);
134+
}
116135
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
watson-developer-cloud/personality_insights/url=https://gateway.watsonplatform.net/personality-insights/api

0 commit comments

Comments
 (0)