package com.ibm.watson.developer_cloud.service;

import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class AuthenticationTest {
  private static final String APIKEY_USERNAME = "apikey";
  private static final String APIKEY = "12345";
  private static final String ICP_APIKEY = "icp-12345";

  public class TestService extends WatsonService {
    private static final String SERVICE_NAME = "test";

    public TestService() {
      super(SERVICE_NAME);
    }
  }

  @Test
  public void authenticateWithApiKeyAsUsername() {
    TestService service = new TestService();
    service.setUsernameAndPassword(APIKEY_USERNAME, APIKEY);
    assertTrue(service.isTokenManagerSet());
  }

  @Test
  public void authenticateWithIcp() {
    TestService service = new TestService();
    service.setUsernameAndPassword(APIKEY_USERNAME, ICP_APIKEY);
    assertFalse(service.isTokenManagerSet());
  }
}

@@ -66,16 +66,106 @@
 .Test;%0A%0A
+import java.lang.reflect.Field;%0Aimport java.lang.reflect.Modifier;%0Aimport java.util.Map;%0A%0A
 import s
@@ -199,16 +199,16 @@
 rtTrue;%0A
-
 import s
@@ -439,16 +439,76 @@
 -12345%22;
+%0A  private static final String BASIC_USERNAME = %22basicUser%22;
 %0A%0A  publ
@@ -1036,32 +1036,32 @@
 E, ICP_APIKEY);%0A
-
     assertFalse(
@@ -1093,11 +1093,1442 @@
 ());%0A  %7D
+%0A%0A  @Test%0A  public void multiAuthenticationWithMultiBindSameServiceOnVcapService() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException %7B%0A    Field theCaseInsensitiveEnvironment =  Class.forName(%22java.lang.ProcessEnvironment%22).getDeclaredField(%22theCaseInsensitiveEnvironment%22);%0A    theCaseInsensitiveEnvironment.setAccessible(true);%0A%0A    Field field = Field.class.getDeclaredField(%22modifiers%22);%0A    field.setAccessible(true);%0A    field.setInt(theCaseInsensitiveEnvironment,theCaseInsensitiveEnvironment.getModifiers() & ~Modifier.PRIVATE & ~Modifier.FINAL);%0A%0A    Map%3CString,String%3E seytemEnv = (Map%3CString, String%3E) theCaseInsensitiveEnvironment.get(null);%0A    seytemEnv.put(%22VCAP_SERVICES%22,%22%7B%5Cn%22 +%0A        %22  %5C%22test%5C%22: %5B%5Cn%22 +%0A        %22    %7B%5Cn%22 +%0A        %22      %5C%22credentials%5C%22: %7B%5Cn%22 +%0A        %22        %5C%22apikey%5C%22: %5C%22%22+ APIKEY +%22%5C%22,%5Cn%22 +%0A        %22        %5C%22url%5C%22: %5C%22https://gateway.watsonplatform.net/discovery/api%5C%22%5Cn%22 +%0A        %22      %7D,%5Cn%22 +%0A        %22      %5C%22plan%5C%22: %5C%22lite%5C%22%5Cn%22 +%0A        %22    %7D%5Cn%22 +%0A        %22  %5D%5Cn%22 +%0A        %22%7D%5Cn%22);%0A%0A    theCaseInsensitiveEnvironment.set(null,seytemEnv);%0A%0A    TestService serviceA = new TestService();%0A    serviceA.setUsernameAndPassword(APIKEY_USERNAME, APIKEY);%0A%0A    TestService serviceB = new TestService();%0A    serviceB.setUsernameAndPassword(BASIC_USERNAME, APIKEY);%0A%0A    assertTrue(serviceA.isTokenManagerSet());%0A    assertFalse(serviceB.isTokenManagerSet());%0A  %7D
 %0A%7D%0A

create test