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.3.0"
        CHANNEL="public"

        INFRASTRUCTURE_REPOSITORY='https://github.com/malithie/aws-cloudformation-templates.git'
        DEPLOYMENT_REPOSITORY='https://github.com/malithie/aws-cloudformation-templates.git'
        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"
        // https://github.com/wso2/cloudformation-is

        TESTGRID_HOME = './testgrid-home'
        JMETER_HOME = './jmeter-home'
    }

    tools {
        jdk 'jdk8'
    }

    stages {
        stage('Preparation') {
            steps {
                deleteDir()
                git url: '${INFRASTRUCTURE_REPOSITORY}',
                //git ${DEPLOYMENT_REPOSITORY}  //ATM Infra and Deploy code in one repository.
                git ${SCENARIOS_REPOSITORY}
                sh 'mkdir testplans/'

                sh """
                curl ${TESTGRID_DIST_URL} -o ${TESTGRID_NAME}.zip
                unzip ${TESTGRID_NAME}.zip
                cd ${TESTGRID_NAME}-${TESTGRID_VERSION}
                """

                sh """
                java -jar test-grid-${TESTGRID_VERSION}.jar create-product-testplan \
                    --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL}
                """


            }

        }
        stage('TestPlan-01-Ubuntu-JDK7-MySQL_5.7') {
            steps {
                echo 'Running Test Plan 01'
                sh 'mkdir ${TESTGRID_HOME}'
                sh 'mkdir ${JMETER_HOME}'
                sh "java -version"
                sh """

                java -jar test-grid-${TESTGRID_VERSION}.jar run-testplan \
                    --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL} \
                    --testplan xxx/testplan.yaml \
                    --infra ${INFRA_LOCATION} \
                    --deployment ${DEPLOYMENT_LOCATION} \
                    --scenarios ${SCENARIOS_LOCATION}
                """
            }
        }

        //todo write the testplan.yaml files for each combination
        stage('TestPlan-02-Ubuntu-JDK8-MySQL_5.7') {
            steps {
                echo 'Running Test Plan 01'
                sh 'mkdir ${TESTGRID_HOME}'
                sh 'mkdir ${JMETER_HOME}'
                sh "java -version"
                sh """

                java -jar test-grid-${TESTGRID_VERSION}.jar run-testplan \
                    --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL} \
                    --testplan xxx/testplan.yaml \
                    --infra ${INFRA_LOCATION} \
                    --deployment ${DEPLOYMENT_LOCATION} \
                    --scegt narios ${SCENARIOS_LOCATION}
                """
            }
        }

        stage('TestPlan-03-Ubuntu-JDK7-Oracle') {
            steps {
                echo 'Running Test Plan 01'
                sh 'mkdir ${TESTGRID_HOME}'
                sh 'mkdir ${JMETER_HOME}'
                sh "java -version"
                sh """

                java -jar test-grid-${TESTGRID_VERSION}.jar run-testplan \
                    --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL} \
                    --testplan xxx/testplan.yaml \
                    --infra ${INFRA_LOCATION} \
                    --deployment ${DEPLOYMENT_LOCATION} \
                    --scenarios ${SCENARIOS_LOCATION}
                """
            }
        }

        stage('TestPlan-04-Ubuntu-JDK8-Oracle') {
            steps {
                echo 'Running Test Plan 01'
                sh 'mkdir ${TESTGRID_HOME}'
                sh 'mkdir ${JMETER_HOME}'
                sh "java -version"
                sh """

                java -jar test-grid-${TESTGRID_VERSION}.jar run-testplan \
                    --product ${PRODUCT} --version ${VERSION} --channel ${CHANNEL} \
                    --testplan xxx/testplan.yaml \
                    --infra ${INFRA_LOCATION} \
                    --deployment ${DEPLOYMENT_LOCATION} \
                    --scenarios ${SCENARIOS_LOCATION}
                """
            }
        }
    }
}