pipeline {
    agent any
    environment {
        TESTGRID_VERSION = '0.9.0-SNAPSHOT'
        TESTGRID_NAME = 'WSO2-TestGrid'
        TESTGRID_DIST_URL = 'https://wso2.org/jenkins/job/platform-builds/job/wso2-test-grid/' +
                'lastSuccessfulBuild/artifact/distribution/target/${TESTGRID_NAME}-${TESTGRID_VERSION}.zip'

        PRODUCT="wso2is"
        VERSION="5.4.0"
        CHANNEL="LTS"

        INFRASTRUCTURE_REPOSITORY='https://github.com/azinneera/cloudformation-is'
        DEPLOYMENT_REPOSITORY='https://github.com/harshanL/cloudformation-is'
        SCENARIOS_REPOSITORY='https://github.com/sameerawickramasekara/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'
    }

    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}.zip
                unzip ${TESTGRID_NAME}.zip
                cd ${TESTGRID_NAME}-${TESTGRID_VERSION}
                """

                sh """
                cd ${TESTGRID_NAME}-${TESTGRID_VERSION}
                java -jar test-grid-${TESTGRID_VERSION}.jar generate-test-plan \
                    --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL} \
                    -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}
                        java -jar test-grid-${TESTGRID_VERSION}.jar run-testplan \
                            --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL} \
                            -ir ${PWD}/${INFRA_LOCATION}/cloudformation-templates/pattern-1 \
                            -sr ${PWD}/${SCENARIOS_LOCATION} \
                            -t ${PWD}/${SCENARIOS_LOCATION}/TestPlans/single-node-testplan.yaml
                        """
                        currentBuild.result = 'SUCCESS'
                    } catch (Exception err) {
                        currentBuild.result = 'SUCCESS'
                    }
                }
                echo "RESULT: ${currentBuild.result}"
            }
        }
    }

    post {
        always {
           sh """
            cd ${TESTGRID_NAME}-${TESTGRID_VERSION}
            java -jar test-grid-${TESTGRID_VERSION}.jar generate-report \
            --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL} \
            --groupBy deployment \
            """

           publishHTML([
           allowMissing: false,
           alwaysLinkToLastBuild: true,
           keepAll: true,
           reportDir: "${TESTGRID_HOME}",
           reportFiles: '*.html',
           reportName: 'HTML Report',
           reportTitles: ''])
       }
   }
}
