BluemixEndpointService extends EndpointServiceImpl { @Override public void lookup(EndpointPromise future) { final String methodName = "lookup"; logger.entry(this, methodName, future); try { String lookupUri; Endpoint endpoint = null; boolean retry = false; synchronized(state) { if (state.lookupUri == null) { parseVCAPJson(); } lookupUri = state.lookupUri; if (state.lookupUri != null) { if (state.endpoints == null) { doHttpLookup(state.lookupUri, future); } else if (state.nextEndpointIndex >= state.endpoints.size()) { state.endpoints = null; retry = true; } else { endpoint = state.endpoints.get(state.nextEndpointIndex++); } } } if (lookupUri == null) { final ClientException exception = new ClientException("Could not locate a valid IBM Bluemix VCAP_SERVICES environment variable. Check 'service' parameter to NonBlockingClient.create(...) method."); logger.data(this, methodName, exception); future.setFailure(exception); } else if (retry) { doRetry(future); } else if (endpoint != null) { future.setSuccess(endpoint); } } catch(JsonParseException e) { final ClientException exception = new ClientException("Could not parse the JSON present in the IBM Bluemix VCAP_SERVICES environment variable. See linked exception for more information", e); logger.data(this, methodName, exception); future.setFailure(exception); } catch(ClientException e) { logger.data(this, methodName, e); future.setFailure(e); } logger.exit(this, methodName); } BluemixEndpointService(Pattern label, Pattern name); @Override void lookup(EndpointPromise future); @Override void onSuccess(Endpoint endpoint);  }
@Test public void noVcapServices() { BluemixEndpointService service = new MockBluemixEndpointService(null, "", ""); MockEndpointPromise promise = new MockEndpointPromise(Method.FAILURE); service.lookup(promise); assertTrue("Promise should have been marked done", promise.isComplete()); }
@Test public void ambigiousServiceNameFails() { BluemixEndpointService service = new MockBluemixEndpointService(twoMQLightServicesJSON, "", ""); MockEndpointPromise promise = new MockEndpointPromise(Method.FAILURE); service.lookup(promise); assertTrue("Promise should have been marked done", promise.isComplete()); }
@Test public void specificServiceCanBePickedByServiceName() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(null, Pattern.compile(Pattern.quote("MQ Light-9n")), twoMQLightServicesJSON, expectedTwoMQLightServicesUri1, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); service = new MockBluemixEndpointService(null, Pattern.compile("MQ Light\\-gs"), twoMQLightServicesJSON, expectedTwoMQLightServicesUri2, servicesJson); promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); }
@Test public void ambigiousServiceLabelFails() { BluemixEndpointService service = new MockBluemixEndpointService(multipleMatchingLabelsJSON, "", ""); MockEndpointPromise promise = new MockEndpointPromise(Method.FAILURE); service.lookup(promise); assertTrue("Promise should have been marked done", promise.isComplete()); }
@Test public void specificServiceCanBePickedByLabelName() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(Pattern.compile("mqlight"), null, multipleMatchingLabelsJSON, expectedMultipleMatchingUri1, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise (1) should have been marked done", promise.isComplete()); service = new MockBluemixEndpointService(Pattern.compile("messagehubincubator"), null, multipleMatchingLabelsJSON, expectedMultipleMatchingUri2, servicesJson); promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise (2) should have been marked done", promise.isComplete()); service = new MockBluemixEndpointService(Pattern.compile("user-provided"), null, multipleMatchingLabelsJSON, expectedMultipleMatchingUri3, servicesJson); promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise (3) should have been marked done", promise.isComplete()); }
@Test public void userProvidedWorks() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(userProvidedJSONUsername, expectedUserProvidedUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); }
@Test public void userProvidedIgnoresNonMQLight() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(userProvidedWithNonMQLightJSON, expectedUserProvidedWithNonMQLightUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); assertEquals("User name", "nvAN7VgCXSFS", promise.getEndoint().getUser()); }
@Test public void goldenPathUserNotUsername() throws InterruptedException { String vcapJson = "{ \"mqlight\": [ { \"name\": \"mqlsampleservice\", " + "\"label\": \"mqlight\", \"plan\": \"default\", " + "\"credentials\": { \"user\": \"jBruGnaTHuwq\", " + "\"connectionLookupURI\": \"http: "\"password\": \"xhUQve2gdgAN\", \"version\": \"2\" } } ] }"; String expectedUri = "http: BluemixEndpointService service = new MockBluemixEndpointService(vcapJson, expectedUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); assertEquals("ep1.example.org", promise.getEndoint().getHost()); assertEquals("jBruGnaTHuwq", promise.getEndoint().getUser()); }
@Test public void userProvidedWorksWithUserNotUsername() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(userProvidedJSONUser, expectedUserProvidedUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); assertEquals("User name", "DkcWc9aSldLs", promise.getEndoint().getUser()); }
@Test public void userProvidedIgnoredIfBothUserAndUsernameSpecified() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(userProvidedJSONUserAndUsername, expectedUserProvidedUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.FAILURE); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); }
@Test public void goldenPathDifferentLookupUri() throws InterruptedException { String vcapJson = "{ \"mqlight\": [ { \"name\": \"mqlsampleservice\", " + "\"label\": \"mqlight\", \"plan\": \"default\", " + "\"credentials\": { \"user\": \"jBruGnaTHuwq\", " + "\"mqlight_lookup_url\": \"http: "\"password\": \"xhUQve2gdgAN\", \"version\": \"2\" } } ] }"; String expectedUri = "http: BluemixEndpointService service = new MockBluemixEndpointService(vcapJson, expectedUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); assertEquals("ep1.example.org", promise.getEndoint().getHost()); assertEquals("jBruGnaTHuwq", promise.getEndoint().getUser()); }
@Test public void userProvidedWorksWithDifferentLookupUri() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(userProvidedJSONLookup, expectedUserProvidedUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); assertEquals("User name", "DkcWc9aSldLs", promise.getEndoint().getUser()); }
@Test public void userProvidedIgnoredIfBothLookupUriSpecified() throws InterruptedException { BluemixEndpointService service = new MockBluemixEndpointService(userProvidedJSONBothLookup, expectedUserProvidedUri, servicesJson); MockEndpointPromise promise = new MockEndpointPromise(Method.FAILURE); service.lookup(promise); waitForComplete(promise); assertTrue("Promise should have been marked done", promise.isComplete()); }