// The service name pattern can be used to disambiguate situations where
  // VCAP_SERVICES contains multiple instances of the MQ Light service
  @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());
  }
 // Using the default values (e.g. match all service names) check that the
 // lookup fails when there is ambiguity about which service name to use.
 @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 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 golden path for 'user-provided' services.
 @Test
 public void userProvidedWorks() throws InterruptedException {
   BluemixEndpointService service =
       new MockBluemixEndpointService(userProvidedJSON, expectedUserProvidedUri, servicesJson);
   MockEndpointPromise promise = new MockEndpointPromise(Method.SUCCESS);
   service.lookup(promise);
   waitForComplete(promise);
   assertTrue("Promise should have been marked done", promise.isComplete());
 }
 private void waitForComplete(MockEndpointPromise promise) throws InterruptedException {
   // Promise completed on another thread - need to delay for a reasonable amount of time to allow
   // this to happen.
   for (int i = 0; i < 20; ++i) {
     if (promise.isComplete()) break;
     Thread.sleep(50);
   }
 }
  // The service label pattern can be used to disambiguate situations where
  // VCAP_SERVICES contains multiple different services that can match
  @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 goldenPath() throws InterruptedException {
    String vcapJson =
        "{ \"mqlight\": [ { \"name\": \"mqlsampleservice\", "
            + "\"label\": \"mqlight\", \"plan\": \"default\", "
            + "\"credentials\": { \"username\": \"jBruGnaTHuwq\", "
            + "\"connectionLookupURI\": \"http://mqlightp-lookup.ng.bluemix.net/Lookup?serviceId=ServiceId_0000000090\", "
            + "\"password\": \"xhUQve2gdgAN\", \"version\": \"2\" } } ] }";
    String expectedUri =
        "http://mqlightp-lookup.ng.bluemix.net/Lookup?serviceId=ServiceId_0000000090";
    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());

    // Expect 1st endpoint to be returned.
    assertEquals("ep1.example.org", promise.getEndoint().getHost());

    // If the test asks for another endpoint - it should receive the second.
    promise = new MockEndpointPromise(Method.SUCCESS);
    service.lookup(promise);
    assertTrue("Promise should have been marked done", promise.isComplete());
    assertEquals("ep2.example.org", promise.getEndoint().getHost());

    // Mark the second endpoint as successful - expect it to be returned again if we ask for another
    // endpoint
    service.onSuccess(promise.getEndoint());
    promise = new MockEndpointPromise(Method.SUCCESS);
    service.lookup(promise);
    assertTrue("Promise should have been marked done", promise.isComplete());
    assertEquals("ep2.example.org", promise.getEndoint().getHost());

    // Asking for another endpoint should return the 1st endpoint...
    promise = new MockEndpointPromise(Method.SUCCESS);
    service.lookup(promise);
    assertTrue("Promise should have been marked done", promise.isComplete());
    assertEquals("ep1.example.org", promise.getEndoint().getHost());

    // Asking for another endpoint should result in the test being told to wait...
    promise = new MockEndpointPromise(Method.WAIT);
    service.lookup(promise);
    assertTrue("Promise should have been marked done", promise.isComplete());
  }