pipeline {
    agent any
    environment {
        TESTGRID_VERSION = '0.9.0-SNAPSHOT'
        TESTGRID_NAME = 'WSO2-TestGrid'
        TESTGRID_DIST_URL = 'https://wso2.org/jenkins/job/testgrid/job/testgrid/' +
                'lastSuccessfulBuild/artifact/distribution/target/${TESTGRID_NAME}-${TESTGRID_VERSION}.zip'
        TESTGRID_DIST_LOCATION = '/testgrid/jenkins-home/workspace/'

        PRODUCT="wso2is-5.4.1-LTS"

        INFRASTRUCTURE_REPOSITORY='https://github.com/azinneera/cloudformation-is'
        DEPLOYMENT_REPOSITORY='https://github.com/harshanL/cloudformation-is'
        SCENARIOS_REPOSITORY='https://github.com/wso2-incubator/identity-test-integration.git'

        INFRA_LOCATION="aws-cloudformation-templates"
        DEPLOYMENT_LOCATION="aws-cloudformation-templates"
        SCENARIOS_LOCATION="identity-test-integration"
        INFRA_CONFIGS_LOCATION="${INFRA_LOCATION}/infrastructure-build-plans"

        AWS_ACCESS_KEY_ID=credentials('AWS_ACCESS_KEY_ID')
        AWS_SECRET_ACCESS_KEY=credentials('AWS_SECRET_ACCESS_KEY')
        WUM_USERNAME=credentials('WUM_USERNAME')
        WUM_PASSWORD=credentials('WUM_PASSWORD')
        PWD=pwd()
        TESTGRID_HOME='/testgrid/jenkins-home/workspace/wso2is-5.4.1/testgrid-home'

        MYSQL_DRIVER_LOCATION='http://central.maven.org/maven2/mysql/mysql-connector-java/6.0.6/mysql-connector-java-6.0.6.jar'

        // Jmeter dependencies
        SELENIUM_JAR='https://search.maven.org/remotecontent?filepath=org/seleniumhq/selenium/selenium-java/3.8.1/selenium-java-3.8.1.jar'
        HTML_UNIT_DRIVER='https://search.maven.org/remotecontent?filepath=org/seleniumhq/selenium/htmlunit-driver/2.29.0/htmlunit-driver-2.29.0-jar-with-dependencies.jar'
    }

    tools {
        jdk 'jdk8'
    }

    stages {
        stage('Preparation') {
            steps {
              deleteDir()

                // Clone scenario repo
                sh "mkdir ${SCENARIOS_LOCATION}"
                dir("${SCENARIOS_LOCATION}"){
                    git url: "${SCENARIOS_REPOSITORY}"
                }

                // Clone infra repo
                sh "mkdir ${INFRA_LOCATION}"
                dir("${INFRA_LOCATION}"){
                    git url:"${INFRASTRUCTURE_REPOSITORY}"
                }

                sh """
                echo ${TESTGRID_NAME}

                unzip ${TESTGRID_DIST_LOCATION}/${TESTGRID_NAME}.zip
                cd ${TESTGRID_NAME}
                curl -o ./lib/mysql.jar $MYSQL_DRIVER_LOCATION
                curl -o ./lib/selenium-java.jar $SELENIUM_JAR
                curl -o ./lib/html-unit-driver.jar $HTML_UNIT_DRIVER
                """

                sh """
                cd ${TESTGRID_NAME}
                ./testgrid generate-test-plan \
                    --product ${PRODUCT} \
                    -tc ${PWD}/${INFRA_CONFIGS_LOCATION}/single-node-infra.yaml
                """
            }
        }

        stage('TestPlan-01-Infra-1') {
            steps {
                script {
                    try {
                        echo 'Running Test Plan 01'
                        sh "java -version"

                        sh """
                        cd ${TESTGRID_NAME}
                        ./testgrid run-testplan \
                            --product ${PRODUCT} \
                            -ir ${PWD}/${INFRA_LOCATION}/cloudformation-templates/pattern-1 \
                            -dr ${PWD}/${INFRA_LOCATION}/cloudformation-templates/pattern-1 \
                            -sr ${PWD}/${SCENARIOS_LOCATION}
                        """
                    } catch (Exception err) {
                        echo "Error : ${err}"
                        currentBuild.result = 'FAILURE'
                    }
                }
                echo "RESULT: ${currentBuild.result}"
            }
        }
    }

    post {
        always {
           sh """
            cd ${TESTGRID_NAME}
            ./testgrid generate-report \
            --product ${PRODUCT} \
            --groupBy scenario
            """

            // Archive artifacts
            withAWS(credentials:'TESTGRID_BOT_CREDENTIALS') {
              // Upload artifacts to S3
              s3Upload(workingDir:"${TESTGRID_HOME}", includePathPattern:"**/*.log, **/*.html", bucket:"jenkins-testrun-artifacts", path:"artifacts/")
            }

            // Delete logs and reports after upload
            dir("${TESTGRID_HOME}/${PRODUCT}") {
                deleteDir()
            }
       }
  }
}
