Setting up the repository
You need a JSON file generated from Talend Cloud API Tester to describe your tests and a pom.xml to configure them. You can find sample files in the jenkins.zip archive.
The POM file
The POM file's configuration can be parametrized by JVM arguments.
<file>${test_file}</file>
<accountId>${account_id}</accountId>
<variables>
<!-- The variable API_KEY's value will be overwritten with the JVM argument api_key -->
<property>
<name>API_KEY</name>
<value>${api_key}</value>
</property>
</variables>
Here you can see that the test file is parametrized, this allows you to put your tests in separate files and call the tests individually by specifying the test file in the Maven command.
The Account ID has also been parametrized. It is very important, for security reasons, that the Account ID is not pushed to a GitHub repository. As both the POM file and the Jenkinsfile are on your public GitHub repository, you must parametrize the build and inject the Account ID here via Jenkins credentials.
The variable API_KEY is a bit trickier. It is one of the variables from your Talend Cloud API Tester environment in the JSON test file. It has been overwritten manually here because it contains sensitive information that you do not want to push to GitHub. This mean you can have environments with a valid value here to run the tests in Talend Cloud API Tester and a safe environment without sensitive information in it for CI where you manually add the sensitive data.
The Jenkinsfile
Jenkins pipelines are written in groovy but, you do not need to know the language to create them, they are quite descriptive.
Here is the pipeline you are going to run:
#!/usr/bin/env groovy
node {
stage('Checkout') {
checkout scm
}
stage('Build') {
echo 'Building and unit testing the API.'
// this step is faked
echo 'API built and unit-tested.'
}
stage('Deploy') {
echo 'Deploying the API.'
// this step is faked
echo 'API deployed.'
}
stage('Test') {
echo 'Testing the API with Talend Cloud API Tester Maven plugin'
withCredentials([
string(credentialsId: 'TALEND_ACCOUNT_ID', variable: 'TALEND_ACCOUNT_ID'),
string(credentialsId: 'CONTACTS_API_KEY', variable: 'CONTACTS_API_KEY')]) {
// Maven installation declared in the Jenkins "Global Tool Configuration"
withMaven(maven: 'Maven 3.5.0') {
sh "mvn clean test -B \
-Dapi_key=\"$CONTACTS_API_KEY\" \
-Dselected_environment='dev' \
-Dtest_file='src/test/resources/contacts_api_tests.json' \
-Daccount_id=\"$TALEND_ACCOUNT_ID\""
}
echo 'API tested.'
}
}
}
The build and deploy stages are fake here. In a real build job, you would put your build and deploy code there. This tutorial focuses on the testing phase.
Credentials: You can see in the Test stage that the block withCredentials is used to retrieve the API key and the Talend Account ID and put them in variables of the same name. They are then reused as JVM arguments to provide values to the parametrized part of the POM file.
Maven: The block withMaven retrieves the Maven path with the name we set earlier in the Maven configuration.