<commit_before>/*
 * Copyright 2015-2016 IBM Corporation
 *
 * 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 common;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import junit.runner.Version;

/**
 * Miscellaneous utilities used in whisk test suite
 */
public class TestUtils {
    protected static final Logger logger = Logger.getLogger("basic");

    public static final int SUCCESS_EXIT   = 0;
    public static final int ERROR_EXIT     = 1;
    public static final int MISUSE_EXIT    = 2;
    public static final int DONTCARE_EXIT  = -1;  // any value is ok
    public static final int ANY_ERROR_EXIT = -2;  // any non-zero value is ok

    public static final int ACCEPTED       = 202; // 202
    public static final int BAD_REQUEST    = 144; // 400 - 256 = 144
    public static final int UNAUTHORIZED   = 145; // 401 - 256 = 145
    public static final int FORBIDDEN      = 147; // 403 - 256 = 147
    public static final int NOT_FOUND      = 148; // 404 - 256 = 148
    public static final int NOT_ALLOWED    = 149; // 405 - 256 = 149
    public static final int CONFLICT       = 153; // 409 - 256 = 153
    public static final int TOO_LARGE      = 157; // 413 - 256 = 157
    public static final int THROTTLED      = 173; // 429 (TOO_MANY_REQUESTS) - 256 = 173
    public static final int TIMEOUT        = 246; // 502 (GATEWAY_TIMEOUT) - 256 = 246

    private static final File catalogDir = WhiskProperties.getFileRelativeToWhiskHome("catalog");
    private static final File testActionsDir = WhiskProperties.getFileRelativeToWhiskHome("tests/dat/actions");
    private static final File vcapFile = WhiskProperties.getVCAPServicesFile();
    private static final String envServices = System.getenv("VCAP_SERVICES");
    private static final String loggerLevel = System.getProperty("LOG_LEVEL", Level.WARNING.toString());

    static {
        logger.setLevel(Level.parse(loggerLevel.trim()));
        System.out.println("JUnit version is: " + Version.id());
    }

    /**
     * Gets path to file relative to catalog directory.
     *
     * (@)deprecated this method will be removed in future version; use {@link #getTestActionFilename()} instead
     * @param name relative filename
     */
    public static String getCatalogFilename(String name) {
        return new File(catalogDir, name).toString();
    }

    /**
     * Gets path to test action file relative to test catalog directory.
     *
     * @param name the filename of the test action
     * @return
     */
    public static String getTestActionFilename(String name) {
        return new File(testActionsDir, name).toString();
    }

    /**
     * Gets the value of VCAP_SERVICES.
     *
     * @return VCAP_SERVICES as a JSON object
     */
    public static JsonObject getVCAPServices() {
        try {
            if (envServices != null) {
                return new JsonParser().parse(envServices).getAsJsonObject();
            } else {
                return new JsonParser().parse(new FileReader(vcapFile)).getAsJsonObject();
            }
        } catch (Throwable t) {
            System.out.println("failed to parse VCAP" + t);
            return new JsonObject();
        }
    }

    /**
     * Gets a VCAP_SERVICES credentials.
     *
     * @return VCAP credentials as a <String, String> map for each <property,
     * value> pair in credentials
     */
    public static Map<String, String> getVCAPcredentials(String vcapService) {
        try {
            JsonArray vcapArray = getVCAPServices().get(vcapService).getAsJsonArray();
            JsonObject vcapObject = vcapArray.get(0).getAsJsonObject();
            JsonObject credentials = vcapObject.get("credentials").getAsJsonObject();
            Map<String, String> map = new HashMap<String, String>();
            for (Map.Entry<String, JsonElement> entry : credentials.entrySet()) {
                map.put(entry.getKey(), credentials.get(entry.getKey()).getAsString());
            }

            return map;
        } catch (Throwable t) {
            System.out.println("failed to parse VCAP" + t);
            return Collections.emptyMap();
        }
    }

    /**
     * Creates a JUnit test watcher. Use with @rule.
     * @return a junit {@link TestWatcher} that prints a message when each test starts and ends
     */
    public static TestWatcher makeTestWatcher() {<commit_msg>Add the method to get the credentials as a json object<commit_after>/*
 * Copyright 2015-2016 IBM Corporation
 *
 * 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 common;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import junit.runner.Version;

/**
 * Miscellaneous utilities used in whisk test suite
 */
public class TestUtils {
    protected static final Logger logger = Logger.getLogger("basic");

    public static final int SUCCESS_EXIT   = 0;
    public static final int ERROR_EXIT     = 1;
    public static final int MISUSE_EXIT    = 2;
    public static final int DONTCARE_EXIT  = -1;  // any value is ok
    public static final int ANY_ERROR_EXIT = -2;  // any non-zero value is ok

    public static final int ACCEPTED       = 202; // 202
    public static final int BAD_REQUEST    = 144; // 400 - 256 = 144
    public static final int UNAUTHORIZED   = 145; // 401 - 256 = 145
    public static final int FORBIDDEN      = 147; // 403 - 256 = 147
    public static final int NOT_FOUND      = 148; // 404 - 256 = 148
    public static final int NOT_ALLOWED    = 149; // 405 - 256 = 149
    public static final int CONFLICT       = 153; // 409 - 256 = 153
    public static final int TOO_LARGE      = 157; // 413 - 256 = 157
    public static final int THROTTLED      = 173; // 429 (TOO_MANY_REQUESTS) - 256 = 173
    public static final int TIMEOUT        = 246; // 502 (GATEWAY_TIMEOUT) - 256 = 246

    private static final File catalogDir = WhiskProperties.getFileRelativeToWhiskHome("catalog");
    private static final File testActionsDir = WhiskProperties.getFileRelativeToWhiskHome("tests/dat/actions");
    private static final File vcapFile = WhiskProperties.getVCAPServicesFile();
    private static final String envServices = System.getenv("VCAP_SERVICES");
    private static final String loggerLevel = System.getProperty("LOG_LEVEL", Level.WARNING.toString());

    static {
        logger.setLevel(Level.parse(loggerLevel.trim()));
        System.out.println("JUnit version is: " + Version.id());
    }

    /**
     * Gets path to file relative to catalog directory.
     *
     * (@)deprecated this method will be removed in future version; use {@link #getTestActionFilename()} instead
     * @param name relative filename
     */
    public static String getCatalogFilename(String name) {
        return new File(catalogDir, name).toString();
    }

    /**
     * Gets path to test action file relative to test catalog directory.
     *
     * @param name the filename of the test action
     * @return
     */
    public static String getTestActionFilename(String name) {
        return new File(testActionsDir, name).toString();
    }

    /**
     * Gets the value of VCAP_SERVICES.
     *
     * @return VCAP_SERVICES as a JSON object
     */
    public static JsonObject getVCAPServices() {
        try {
            if (envServices != null) {
                return new JsonParser().parse(envServices).getAsJsonObject();
            } else {
                return new JsonParser().parse(new FileReader(vcapFile)).getAsJsonObject();
            }
        } catch (Throwable t) {
            System.out.println("failed to parse VCAP" + t);
            return new JsonObject();
        }
    }

    /**
     * Gets a VCAP_SERVICES credentials.
     *
     * @return VCAP credentials as a <String, String> map for each <property,
     * value> pair in credentials
     */
    public static Map<String, String> getVCAPcredentials(String vcapService) {
        try {
            JsonObject credentials = getCredentials(vcapService);
            Map<String, String> map = new HashMap<String, String>();
            for (Map.Entry<String, JsonElement> entry : credentials.entrySet()) {
                map.put(entry.getKey(), credentials.get(entry.getKey()).getAsString());
            }

            return map;
        } catch (Throwable t) {
            System.out.println("failed to parse VCAP" + t);
            return Collections.emptyMap();
        }
    }

    /**
     * Gets a VCAP_SERVICES credentials as the json objects.
     *
     * @return VCAP credentials as a json object.
     */
    public static JsonObject getCredentials(String vcapService) {
        JsonArray vcapArray = getVCAPServices().get(vcapService).getAsJsonArray();
        JsonObject vcapObject = vcapArray.get(0).getAsJsonObject();
        JsonObject credentials = vcapObject.get("credentials").getAsJsonObject();
        return credentials;
    }

    /**
     * Creates a JUnit test watcher. Use with @rule.
     * @return a junit {@link TestWatcher} that prints a message when each test starts and ends
     */
    public static TestWatcher makeTestWatcher() {
{'fraction_non_alphanumeric': 0.09377785701551078, 'fraction_numerical': 0.023533606703512212, 'mean_word_length': 2.7940480216435577, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2340406', 'n_tokens_mistral': 3321, 'n_tokens_neox': 3062, 'n_words': 1031}