A PowerShell script for an automated Runtime test using API(v2)
One of the DATPROF Runtime features is the availability of a RESTful API.
Using it may look somewhat complicated.
In this post you will find an example of a test in a project/environment which initial does not exist and at the end is removed.
All requests, except for one, are made using Invoke-WebRequest. For reasons that are unclear to us, we were unable to use this tool for uploading a package, so curl.exe is utilized instead.
In this script:
- A project is created
- An environment is created
- An application is uploaded
- An application is installed in an environment
- A scenario is started
- The status of the run is polled for every 5 seconds.
- When finished the environment is deleted
- The project is deleted
# Path to Curl executable
$curlExe = 'D:\curl\curl.exe'
# Runtime URL
$runtimeUrl = 'http://localhost:7070'
# API Key/Token
$apiKey = '12345678-abcd-1234-abcd-123456789012'
# Name of your to be created Project
$newProject = '{ "name": "AutomatedProject" }'
# Name of your to be created Environment
$newEnvironment = '{"name": "AutomatedEnvironment"}'
# Full path to application package that must be uploaded and installed
$applicationpackage = 'D:\Documents\DATPROF\Privacy\TestAutomation\dpfgen\TestAutomation-0.0.1-prv.zip'
# Name of the scenario that must be executed
$runScenario = 'anonymize'
# Target Database Connection
$connParam = '{\"type\":\"ORACLE\",\"host\":\"192.168.10.100\",\"port\":\"1521\",\"username\":\"USER\",\"password\":\"PWD\",\"sid\":\"XE\"}'
# Create Runtime Project
try {
$resProj = Invoke-WebRequest -Uri $runtimeUrl'/api/2/projects' -Headers @{"X-Auth-Token"="$apiKey"} -Method POST -Body $newProject -ContentType "application/json" | ConvertFrom-Json
Write-Host 'Project created' $resProj.id
}
catch { $_.Exception.Response }
$projectId = $resProj.id
# Create Runtime Environment
$newEnvironment = '{ "projectId":"'+$projectId+'" , "name": "AutomatedEnvironment", "type":"ORACLE" }'
try {
$resEnv = Invoke-WebRequest -Uri $runtimeUrl'/api/2/environments' -Headers @{"X-Auth-Token"="$apiKey"} -Method POST -Body $newEnvironment -ContentType "application/json" | ConvertFrom-Json
Write-Host 'Environment created' $resEnv.id
#Add Target Connection Parameter
$newParameter = '{ "environmentId": "' + $resEnv.id + '", "name":"DPF_TARGET_CONNECTION", "value": "'+$connParam+'", "defaultValue":"'+$connParam+'", "type": "CONNECTIONSTRING", "isUserDefined": "false", "description": ""}'
$targetParam = Invoke-WebRequest -Uri $runtimeUrl'/api/2/parameters' -Headers @{"X-Auth-Token"="$apiKey"} -Method POST -Body $newParameter -ContentType "application/json" | ConvertFrom-Json
}
catch { $_.Exception.Response }
$envId = $resEnv.id
# Upload application (currently uses curl)
try {
$resApp = & $curlExe -F file=@"$applicationpackage" --header "X-auth-token: f252c96b-9fdd-4fe1-ab12-4235f68f0cbd" $runtimeUrl/api/2/applications?projectId=$projectId -X POST -s | ConvertFrom-Json
$appId = $resApp.id
Write-Host 'Application uploaded '$appId
}
catch { $_.Exception.Response }
# Install application
try {
$uri = $runtimeUrl+'/ajax/projects/'+$projectId+'/environments/'+$envId+'/installations/'+$appId
$resInstall = Invoke-WebRequest -Uri $uri -Headers @{"X-Auth-Token"="$apiKey"} -Method POST -Body $newEnvironment -ContentType "application/json" | ConvertFrom-Json
Write-Host 'Installed application' $resInstall.id
}
catch { $_.Exception.Response }
# Start a new run with given scenario
try {
$uri = $runtimeUrl+'/api/2/runs'
$scenario = '{"environmentId":'+$envId+',"scenarioName":"'+$runScenario+'"} '
Write-Host 'Starting run with scenario: ' $runScenario
$resRun = Invoke-WebRequest -Uri $uri -Headers @{"X-Auth-Token"="$apiKey"} -Method POST -Body $scenario -ContentType "application/json" | ConvertFrom-Json
$runId = $resRun.id
}
catch { $_.Exception.Response }
# Loop while run is active
Do
{
$uri = $runtimeUrl+'/api/2/runs/'+$runId
$resRunStatus = Invoke-WebRequest -Uri $uri -Headers @{"X-Auth-Token"="$apiKey"} -Method GET -ContentType "application/json" | ConvertFrom-Json
$runStatus = $resRunStatus.status
Write-Host 'Run status: '$runStatus
Start-Sleep -seconds 5
} while (($runStatus -ne 'ABORTED') -and ($runStatus -ne 'DONE') )
Write-Host 'Run is finished succesfully or is aborted by the user'
# Delete environment
try {
Write-Host "Delete environment $envID"
$uri = $runtimeUrl+'/api/2/environments/'+$envId
$res = Invoke-WebRequest -Uri $uri -Headers @{"X-Auth-Token"="$apiKey"} -Method Delete
}
catch { $_.Exception.Response }
# Delete Project
try {
Write-Host "Delete project $projectID"
$uri = $runtimeUrl+'/api/2/projects/'+$projectId
$res = Invoke-WebRequest -Uri $uri -Headers @{"X-Auth-Token"="$apiKey"} -Method Delete
}
catch { $_.Exception.Response }