/*
 * Copyright (c) 2011-2015 Spotify AB
 *
 * 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.spotify.google.cloud.pubsub.client.integration;

import com.google.api.client.repackaged.com.google.common.base.Throwables;
import com.google.common.base.Splitter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.stream.Stream;

import javax.net.ssl.SSLContext;

import static com.google.common.base.CharMatcher.WHITESPACE;
import static java.lang.System.getProperty;

class Util {

  static final String TEST_NAME_PREFIX = "async-google-pubsub-client-test-";

  private static File CONFIG_PATH = new File(getProperty("user.home"), ".config");
  private static File GCLOUD_CONFIG_PATH = new File(CONFIG_PATH, "gcloud");
  private static File PROPERTIES_PATH = new File(GCLOUD_CONFIG_PATH, "properties");

  private static String defaultProject = System.getenv("GOOGLE_CLOUD_PROJECT");

  static String defaultProject() {

    if (defaultProject != null) {
      return defaultProject;
    }

    // Try reading $HOME/.config/gcloud/properties

    final List<String> lines;
    try {
      lines = Files.readAllLines(PROPERTIES_PATH.toPath());
    } catch (IOException e) {
      throw new RuntimeException("failed to get default project");
    }

    defaultProject = lines.stream()
        .filter(line -> line.contains("project")).findFirst()
        .map(line -> Splitter.on(WHITESPACE).splitToList(line))
        .map(tokens -> tokens.size() > 2 ? tokens.get(2) : "").orElse("");

    if (defaultProject == null) {
      throw new RuntimeException("failed to get default project");
    }

    return defaultProject;
  }

  /**
   * Return a list of non-GCM ciphers. GCM performance in Java 8 (pre 8u60) is unusably bad and currently worse than
   * CBC in >= 8u60.
   *
   * https://bugs.openjdk.java.net/browse/JDK-8069072
   */
  static String[] nonGcmCiphers() {
    final SSLContext sslContext;
    try {
      sslContext = SSLContext.getDefault();
    } catch (NoSuchAlgorithmException e) {
      throw Throwables.propagate(e);
    }

    final String[] defaultCiphers = sslContext.getDefaultSSLParameters().getCipherSuites();

    return Stream.of(defaultCiphers)
        .filter(cipher -> !cipher.contains("GCM"))
        .toArray(String[]::new);
  }
}

@@ -762,43 +762,22 @@
 mon.
-base.Splitter;%0A%0Aimport java.io.File
+io.CharStreams
 ;%0Aim
@@ -818,22 +818,28 @@
 ava.
-n
 io.
-file.Files
+InputStreamReader
 ;%0Aim
@@ -887,31 +887,8 @@
 on;%0A
-import java.util.List;%0A
 impo
@@ -915,17 +915,16 @@
 Stream;%0A
-%0A
 import j
@@ -953,114 +953,8 @@
 t;%0A%0A
-import static com.google.common.base.CharMatcher.WHITESPACE;%0Aimport static java.lang.System.getProperty;%0A%0A
 clas
@@ -1062,659 +1062,287 @@
 tic 
-File CONFIG_PATH = new File(getProperty(%22user.home%22), %22.config%22);%0A  private static File GCLOUD_CONFIG_PATH = new File(CONFIG_PATH, %22gcloud%22);%0A  private static File PROPERTIES_PATH = new File(GCLOUD_CONFIG_PATH, %22properties%22);%0A%0A  private static String defaultProject = System.getenv(%22GOOGLE_CLOUD_PROJECT%22);%0A%0A  static String defaultProject() %7B%0A%0A    if (defaultProject != null) %7B%0A      return defaultProject;%0A    %7D%0A%0A    // Try reading $HOME/.config/gcloud/properties%0A%0A    final List%3CString%3E lines;%0A    try %7B%0A      lines = Files.readAllLines(PROPERTIES_PATH.toPath());%0A    %7D catch (IOException e) %7B%0A      throw new RuntimeException(%22failed to get default
+String defaultProject = System.getenv(%22GOOGLE_CLOUD_PROJECT%22);%0A%0A  static String defaultProject() %7B%0A%0A    if (defaultProject != null) %7B%0A      return defaultProject;%0A    %7D%0A%0A    // Try asking gcloud%0A    try %7B%0A      Process process = Runtime.getRuntime().exec(%22gcloud config get-value
  pro
@@ -1347,29 +1347,24 @@
 roject%22);%0A  
-  %7D%0A%0A
     defaultP
@@ -1376,223 +1376,191 @@
 t = 
-lines.stream()%0A        .filter(line -%3E line.contains(%22project%22)).findFirst
+CharStreams.toString(new InputStreamReader(process.getInputStream())).trim
 ()
+;
 %0A    
-    .map(line -%3E Splitter.on(WHITESPACE).splitToList(line))%0A        .map(tokens -%3E tokens.size() %3E 2 ? tokens.get(2) : %22%22).orElse(%22%22);
+%7D catch (IOException e) %7B%0A      throw new RuntimeException(%22failed to get default project%22, e);%0A    %7D
 %0A%0A  
@@ -1583,16 +1583,18 @@
 ject
- == null
+.isEmpty()
 ) %7B%0A
@@ -1623,37 +1623,33 @@
 eException(%22
-failed to get
+got empty
  default pro

fix default project lookup