// Copyright 2017 The gRPC Authors
//
//  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.
description = 'gRPC: gae interop testing (jdk7)'

buildscript {
    // Configuration for building
    repositories {
        jcenter()    // Bintray's repository - a fast Maven Central mirror & more
        maven { // The google mirror is less flaky than mavenCentral()
            url "https://maven-central.storage-download.googleapis.com/repos/central/data/" }
    }
    dependencies {
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.3.5'
        classpath 'com.squareup.okhttp:okhttp:2.5.0'
    }
}

repositories {
    // repositories for Jar's you access in your code
    mavenLocal()
    maven { // The google mirror is less flaky than mavenCentral()
        url "https://maven-central.storage-download.googleapis.com/repos/central/data/" }
    jcenter()
}

apply plugin: 'java'                              // standard Java tasks
apply plugin: 'war'                               // standard Web Archive plugin
apply plugin: 'com.google.cloud.tools.appengine'  // App Engine tasks

dependencies {
    providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5'
    compile 'com.google.appengine:appengine-api-1.0-sdk:1.9.59'
    // Deps needed by all gRPC apps in GAE
    compile libraries.google_api_protos
    compile project(":grpc-okhttp")
    compile project(":grpc-protobuf")
    compile project(":grpc-stub")
    compile (project(":grpc-interop-testing")) {
        exclude group: 'io.opencensus', module: 'opencensus-impl'
    }
    // The lite version of opencensus is required for jdk7 GAE
    runtime libraries.opencensus_impl_lite
}

compileJava {
    // Disable "No processor claimed any of these annotations: org.junit.Ignore"
    options.compilerArgs += ["-Xlint:-processing"]
}

// [START model]
appengine {
    // App Engine tasks configuration
    run {      // local (dev_appserver) configuration (standard environments only)
        port = 8080                 // default
    }

    deploy {
        // deploy configuration
        // default - stop the current version
        stopPreviousVersion = System.getProperty('gaeStopPreviousVersion') ?: true
        // default - make this the current version
        promote = System.getProperty('gaePromote') ?: true
        // Use -DgaeDeployVersion if set, otherwise the version is null and the plugin will generate it
        version = System.getProperty('gaeDeployVersion')
    }
}
// [END model]

group = 'io.grpc'   // Generated output GroupId
version = '1.0-SNAPSHOT'          // Version in generated output

sourceCompatibility = 1.7
targetCompatibility = 1.7

/** Returns the service name. */
String getGaeProject() {
    def stream = new ByteArrayOutputStream()
    exec {
        executable 'gcloud'
        args = [
            'config',
            'get-value',
            'project'
        ]
        standardOutput = stream
    }
    return stream.toString().trim()
}

String getService(java.nio.file.Path projectPath) {
    Node xml = new XmlParser().parse(projectPath.resolve("src/main/webapp/WEB-INF/appengine-web.xml").toFile())
    if (xml.service.isEmpty()) {
        return null
    } else {
        return xml.service.text()
    }
}

String getAppUrl(String project, String service, String version) {
    if (version != null && service != null) {
        return "http://${version}.${service}.${project}.appspot.com"
    } else {
        return "http://${project}.appspot.com"
    }
}

task runInteropTestRemote(dependsOn: 'appengineDeploy') {
    doLast {
        // give remote app some time to settle down
        sleep(20000)

        def appUrl = getAppUrl(
                getGaeProject(),
                getService(project.getProjectDir().toPath()),
                System.getProperty('gaeDeployVersion'))
        logger.log(LogLevel.INFO, "the appURL=" + appUrl)
        def client = new com.squareup.okhttp.OkHttpClient()
        // The test suite can take a while to run
        client.setReadTimeout(3, java.util.concurrent.TimeUnit.MINUTES)
        // The '?jdk8' argument is ignored by the server, it exists only to tag the request log entry
        def interopRequest = new com.squareup.okhttp.Request.Builder()
                .url("${appUrl}/?jdk7").build()

        // Retry in case GAE is slow and times out
        int maxRetries = 5
        String result = null
        Throwable caught = null
        for (int attempt = 0; attempt < maxRetries; attempt++) {
            try {
                def response = client.newCall(interopRequest).execute()
                result = response.body().string()
                if (response.code() == 200) {
                    return
                }
            } catch (Throwable t) {
                caught = t
                logger.log(LogLevel.ERROR, "caught exception. will retry if possible", t)
            }
        }
        throw new GradleException("Interop test failed:\nresponse: ${result}\nthrowable:${caught}")
    }
}
