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'

        PRODUCT="wso2is-5.4.0-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.0/testgrid-home'

        MYSQL_DRIVER_LOCATION='http://central.maven.org/maven2/mysql/mysql-connector-java/6.0.6/mysql-connector-java-6.0.6.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}
                curl ${TESTGRID_DIST_URL} -o ${TESTGRID_NAME}-${TESTGRID_VERSION}.zip
                unzip ${TESTGRID_NAME}-${TESTGRID_VERSION}.zip
                cd ${TESTGRID_NAME}-${TESTGRID_VERSION}
                curl -o ./lib/mysql.jar $MYSQL_DRIVER_LOCATION
                """

                sh """
                cd ${TESTGRID_NAME}-${TESTGRID_VERSION}
                ./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_VERSION}
                        ./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'
                    }
                }
            }
        }
    }

    post {
        always {
           sh """
            cd ${TESTGRID_NAME}-${TESTGRID_VERSION}
            ./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()
            }
       }
   }
}
