package com.ibm.watson.developer_cloud.service;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;

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";
  private static final String BASIC_USERNAME = "basicUser";

  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());
  }

  @Test
  public void multiAuthenticationWithMultiBindSameServiceOnVcapService()
      throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {

    Class<?> clazz = Class.forName("java.lang.ProcessEnvironment");

    Field theCaseInsensitiveEnvironment =  clazz.getDeclaredField("theCaseInsensitiveEnvironment");

    theCaseInsensitiveEnvironment.setAccessible(true);

    Field field = Field.class.getDeclaredField("modifiers");
    field.setAccessible(true);
    field.setInt(theCaseInsensitiveEnvironment,
        theCaseInsensitiveEnvironment.getModifiers() & ~Modifier.PRIVATE & ~Modifier.FINAL);

    Map<String, String> systemEnv = (Map<String, String>) theCaseInsensitiveEnvironment.get(null);
    systemEnv.put("VCAP_SERVICES", "{\n"
        + "  \"test\": [\n"
        + "    {\n"
        + "      \"credentials\": {\n"
        + "        \"apikey\": \"" + APIKEY + "\",\n"
        + "        \"url\": \"https://gateway.watsonplatform.net/discovery/api\"\n"
        + "      },\n"
        + "      \"plan\": \"lite\"\n"
        + "    }\n"
        + "  ]\n"
        + "}\n");

    theCaseInsensitiveEnvironment.set(null, systemEnv);

    TestService serviceA = new TestService();
    serviceA.setUsernameAndPassword(APIKEY_USERNAME, APIKEY);

    TestService serviceB = new TestService();
    serviceB.setUsernameAndPassword(BASIC_USERNAME, APIKEY);

    assertTrue(serviceA.isTokenManagerSet());
    assertFalse(serviceB.isTokenManagerSet());
  }
}

@@ -53,112 +53,82 @@
 ort 
-org.junit.Test;%0A%0Aimport java.lang.reflect.Field;%0Aimport java.lang.reflect.Modifier;%0Aimport java.util.Map
+com.ibm.watson.developer_cloud.util.CredentialUtils;%0Aimport org.junit.Test
 ;%0A%0Ai
@@ -1149,688 +1149,44 @@
 ce()
-%0A      throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException %7B%0A%0A    Class%3C?%3E clazz = Class.forName(%22java.lang.ProcessEnvironment%22);%0A%0A    Field theCaseInsensitiveEnvironment =  clazz.getDeclaredField(%22theCaseInsensitiveEnvironment%22);%0A%0A    theCaseInsensitiveEnvironment.setAccessible(true);%0A%0A    Field field = Field.class.getDeclaredField(%22modifiers%22);%0A    field.setAccessible(true);%0A    field.setInt(theCaseInsensitiveEnvironment,%0A        theCaseInsensitiveEnvironment.getModifiers() & ~Modifier.PRIVATE & ~Modifier.FINAL);%0A%0A    Map%3CString, String%3E systemEnv = (Map%3CString, String%3E) theCaseInsensitiveEnvironment.get(null);%0A    systemEnv.put(%22VCAP_SERVICES%22, 
+ %7B%0A%0A    CredentialUtils.setServices(
 %22%7B%5Cn
@@ -1535,65 +1535,8 @@
 );%0A%0A
-    theCaseInsensitiveEnvironment.set(null, systemEnv);%0A%0A
     

fix test simplify.