/**
 * Copyright 2017 IBM Corp. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.ibm.watson.developer_cloud.util;

import com.ibm.watson.developer_cloud.WatsonServiceTest;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Hashtable;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * The Class CredentialUtilsTest.
 */
public class CredentialUtilsTest extends WatsonServiceTest {
  private static final String SERVICE_NAME = "personality_insights";
  private static final String VCAP_SERVICES = "vcap_services.json";
  private static final String APIKEY = "apikey";
  private static final String USERNAME = "username";
  private static final String OLD_API_KEY = "api_key";
  private static final String NOT_A_USERNAME = "not-a-username";
  private static final String NOT_A_PASSWORD = "not-a-password";
  private static final String NOT_A_FREE_USERNAME = "not-a-free-username";
  private static final String VISUAL_RECOGNITION = "watson_vision_combined";

  /**
   * Setup.
   */
  @Before
  public void setup() {
    final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(VCAP_SERVICES);
    final String vcapServices = getStringFromInputStream(in);
    CredentialUtils.setServices(vcapServices);

    final Hashtable<String, String> env = new Hashtable<>();
    env.put("java.naming.factory.initial", "org.osjava.sj.SimpleContextFactory");
    env.put("org.osjava.sj.delimiter", "/");
    env.put("org.osjava.sj.root", "src/test/resources");

    CredentialUtils.setContext(env);
  }

  @Test
  public void testGetVcapValueWithNullOrEmptyService() {
    assertNull(CredentialUtils.getVcapValue(null, APIKEY));
    assertNull(CredentialUtils.getVcapValue("", APIKEY));
  }

  @Test
  public void testGetVcapValueWithPlan() {
    assertEquals(NOT_A_USERNAME, CredentialUtils.getVcapValue(SERVICE_NAME, USERNAME, CredentialUtils.PLAN_STANDARD));
  }

  @Test
  public void testGetVcapValueWithoutPlan() {
    assertEquals(NOT_A_PASSWORD, CredentialUtils.getVcapValue(VISUAL_RECOGNITION, OLD_API_KEY));
  }

  @Test
  public void testGetVcapValueWithMultiplePlans() {
    assertEquals(NOT_A_FREE_USERNAME, CredentialUtils.getVcapValue(SERVICE_NAME, USERNAME));
  }
}

@@ -886,20 +886,107 @@
 t.assert
-Null
+False;%0Aimport static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue
 ;%0A%0A/**%0A 
@@ -2901,11 +2901,798 @@
 E));%0A  %7D
+%0A%0A  @Test%0A  public void testBadCredentialChar() %7B%0A    // valid%0A    assertFalse(CredentialUtils.hasBadStartOrEndChar(%22this_is_fine%22));%0A%0A    // starting bracket%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22%7Bbad_username%22));%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22%7B%7Bstill_bad%22));%0A%0A    // ending bracket%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22invalid%7D%22));%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22also_invalid%7D%7D%22));%0A%0A    // starting quote%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22%5C%22not_allowed_either%22));%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22%5C%22%5C%22still_not%22));%0A%0A    // ending quote%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22nope%5C%22%22));%0A    assertTrue(CredentialUtils.hasBadStartOrEndChar(%22sorry%5C%22%5C%22%22));%0A  %7D
 %0A%7D%0A

Add tests for invalid creds