Skip to content

Commit

Permalink
Advanced trending option and Support automatically finding and creati…
Browse files Browse the repository at this point in the history
…ng Test Instance (#198)

* index on master: ab83a3d Merge remote-tracking branch 'remotes/origin/5.0.1-beta-SNAPSHOT'

* Revert pom changes

* Merge branch '5.0.1-beta-SNAPSHOT' of https://github.com/hpsa/hp-application-automation-tools-plugin into development

# Conflicts:
#	.github/PULL_REQUEST_TEMPLATE.md

* Added some comments

* changes according to sonarlint

* changes for sonarlint

* Remove some comments

* Comments

*  * Fix for supporting SECURITY-170 changes
 * https://jenkins.io/blog/2016/05/11/security-update/
 * https://issues.jenkins-ci.org/browse/JENKINS-39654

* code changes for sonarlint

* Changes in PC configuration section header

* Adding support for Jenkins using proxy:  https://issues.jenkins-ci.org/browse/JENKINS-44314
Changing the HTTPS checkbox position

* Adding link under Test ID text box for browsing to the PC Server

* Added support for creating TESTINSTANCEID before running a test.
Still needs to add:
1. TestSetID is a stub, we need to check if there is one and to create if not exists
2. Need to check if a test instance id is already exists

* Adding support for createTestInstance if test instance is not available.

* SonarLint Changes

* Support auto trending options
https://issues.jenkins-ci.org/browse/JENKINS-44723

* Added Unit Testing for new Tests Rest API

* Minor change in UI

* Small change regarding the flow of the Auto Trending feature
  • Loading branch information
bamh authored and YafimK committed Jun 8, 2017
1 parent 30a59ea commit c3b8a06
Show file tree
Hide file tree
Showing 16 changed files with 534 additions and 43 deletions.
Expand Up @@ -11,7 +11,7 @@
public class PcModel {

public static final String COLLATE = "Collate Results";
public static final String COLLATE_ANALYZE = "Collate And Analyze";
public static final String COLLATE_ANALYZE = "Collate and Analyze";
public static final String DO_NOTHING = "Do Not Collate";

private final String pcServerName;
Expand All @@ -21,26 +21,29 @@ public class PcModel {
private final String almProject;
private final String testId;
private final String testInstanceId;
private final String autoTestInstanceID;
private final TimeslotDuration timeslotDuration;
private final PostRunAction postRunAction;
private final boolean vudsMode;
private final String description;
private final boolean addRunToTrendReport;
private final String trendReportId;
private final String addRunToTrendReport;
private String trendReportId;
private final boolean HTTPSProtocol;
private final String proxyOutURL;


@DataBoundConstructor
public PcModel(String pcServerName, String almUserName, String almPassword, String almDomain, String almProject,
String testId, String testInstanceId, String timeslotDurationHours, String timeslotDurationMinutes,
PostRunAction postRunAction, boolean vudsMode, String description, boolean addRunToTrendReport, String trendReportId, boolean HTTPSProtocol, String proxyOutURL) {
String testId,String autoTestInstanceID, String testInstanceId, String timeslotDurationHours, String timeslotDurationMinutes,
PostRunAction postRunAction, boolean vudsMode, String description, String addRunToTrendReport, String trendReportId, boolean HTTPSProtocol, String proxyOutURL) {

this.pcServerName = pcServerName;
this.almUserName = almUserName;
this.almPassword = setPassword(almPassword);
this.almDomain = almDomain;
this.almProject = almProject;
this.testId = testId;
this.autoTestInstanceID = autoTestInstanceID;
this.testInstanceId = testInstanceId;
this.timeslotDuration = new TimeslotDuration(timeslotDurationHours, timeslotDurationMinutes);
this.postRunAction = postRunAction;
Expand Down Expand Up @@ -90,10 +93,13 @@ public String getTestId() {
}

public String getTestInstanceId() {

return this.testInstanceId;
}

public String getAutoTestInstanceID(){
return this.autoTestInstanceID;
}

public TimeslotDuration getTimeslotDuration() {

return this.timeslotDuration;
Expand Down Expand Up @@ -122,11 +128,11 @@ public String getProxyOutURL(){
return this.proxyOutURL;
}


public static List<PostRunAction> getPostRunActions() {
return Arrays.asList(PostRunAction.values());
}


@Override
public String toString() {

Expand All @@ -136,7 +142,7 @@ public String toString() {
public String runParamsToString() {

String vudsModeString = (vudsMode) ? ", VUDsMode='true'" : "";
String trendString = (addRunToTrendReport) ? String.format(", TrendReportID = '%s'",trendReportId) : "";
String trendString = ("USE_ID").equals(addRunToTrendReport) ? String.format(", TrendReportID = '%s'",trendReportId) : "";

return String.format("[Domain='%s', Project='%s', TestID='%s', " +
"TestInstanceID='%s', TimeslotDuration='%s', PostRunAction='%s'%s%s]",
Expand All @@ -150,7 +156,11 @@ public String getTrendReportId() {
return trendReportId;
}

public boolean isAddRunToTrendReport() {
public void setTrendReportId(String trendReportId){
this.trendReportId = trendReportId;
}

public String getAddRunToTrendReport() {
return addRunToTrendReport;
}

Expand Down
Expand Up @@ -3,7 +3,7 @@
public enum PostRunAction {

COLLATE("Collate Results"),
COLLATE_AND_ANALYZE("Collate And Analyze"),
COLLATE_AND_ANALYZE("Collate and Analyze"),
DO_NOTHING("Do Not Collate");

private String value;
Expand Down
71 changes: 67 additions & 4 deletions src/main/java/com/hp/application/automation/tools/pc/PcClient.java
Expand Up @@ -73,7 +73,7 @@ public <T extends PcRestProxy> PcClient(PcModel pcModel, PrintStream logger, T p
public boolean login() {
try {
String user = model.getAlmUserName();
logger.println(String.format("Trying to login\n[PCServer='%s', User='%s']", model.getPcServerName(), user));
logger.println(String.format("Trying to login\n[PCServer='%s://%s', User='%s']",model.isHTTPSProtocol(), model.getPcServerName(), user));
loggedIn = restProxy.authenticate(user, model.getAlmPassword().toString());
} catch (PcException e) {
logger.println(e.getMessage());
Expand Down Expand Up @@ -102,9 +102,17 @@ public boolean isLoggedIn() {

public int startRun() throws NumberFormatException, ClientProtocolException, PcException, IOException {




int testID = Integer.parseInt(model.getTestId());
int testInstance = getCorrectTestInstanceID(testID);
setCorrectTrendReportID();

logger.println(String.format("\nExecuting Load Test: \n====================\nTest ID: %s \nTest Instance ID: %s \nTimeslot Duration: %s \nPost Run Action: %s \nUse VUDS: %s\n====================\n", Integer.parseInt(model.getTestId()), testInstance, model.getTimeslotDuration() ,model.getPostRunAction().getValue(),model.isVudsMode()));
// logger.println("Sending run request:\n" + model.runParamsToString());
PcRunResponse response = restProxy.startRun(Integer.parseInt(model.getTestId()),
Integer.parseInt(model.getTestInstanceId()),
PcRunResponse response = restProxy.startRun(testID,
testInstance,
model.getTimeslotDuration(),
model.getPostRunAction().getValue(),
model.isVudsMode());
Expand All @@ -113,6 +121,60 @@ public int startRun() throws NumberFormatException, ClientProtocolException, PcE
return response.getID();
}

private int getCorrectTestInstanceID(int testID) throws IOException, PcException {
if("AUTO".equals(model.getAutoTestInstanceID())){
try {


logger.println("Searching for available Test Instance");
PcTestInstances pcTestInstances = restProxy.getTestInstancesByTestId(testID);
int testInstanceID = 0;
if (pcTestInstances != null && pcTestInstances.getTestInstancesList() != null){
PcTestInstance pcTestInstance = pcTestInstances.getTestInstancesList().get(pcTestInstances.getTestInstancesList().size()-1);
testInstanceID = pcTestInstance.getInstanceId();
logger.println("Found testInstanceId: " + testInstanceID);
}else{
logger.println("Could not find available TestInstanceID, Creating Test Instance.");
logger.println("Searching for available TestSet");
// Get a random TestSet
PcTestSets pcTestSets = restProxy.GetAllTestSets();
if (pcTestSets !=null && pcTestSets.getPcTestSetsList() !=null){
PcTestSet pcTestSet = pcTestSets.getPcTestSetsList().get(pcTestSets.getPcTestSetsList().size()-1);
int testSetID = pcTestSet.TestSetID;
logger.println(String.format("Creating Test Instance with testID: %s and TestSetID: %s", testID,testSetID));
testInstanceID = restProxy.createTestInstance(testID,testSetID);
logger.println(String.format("Test Instance with ID : %s has been created successfully.", testInstanceID));
}else{
String msg = "No TestSetID available in project, please create a testset from Performance Center UI";
logger.println(msg);
throw new PcException(msg);
}
}
return testInstanceID;
} catch (Exception e){
logger.println(String.format("getCorrectTestInstanceID failed, reason: %s",e));
return Integer.parseInt(null);
}
}
return Integer.parseInt(model.getTestInstanceId());
}

private void setCorrectTrendReportID() throws IOException, PcException {
// If the user selected "Use trend report associated with the test" we want the report ID to be the one from the test
if (("ASSOCIATED").equals(model.getAddRunToTrendReport())){
PcTest pcTest = restProxy.getTestData(Integer.parseInt(model.getTestId()));
if (pcTest.getTrendReportId() > -1)
model.setTrendReportId(String.valueOf(pcTest.getTrendReportId()));
else{
String msg = "No trend report ID is associated with the test.\n" +
"Please turn Automatic Trending on for the test through Performance Center UI.\n" +
"Alternatively you can check 'Add run to trend report with ID' on Jenkins job configuration.";
throw new PcException(msg);
}
}

}

public PcRunResponse waitForRunCompletion(int runId) throws InterruptedException, ClientProtocolException, PcException, IOException {

return waitForRunCompletion(runId, 5000);
Expand Down Expand Up @@ -232,7 +294,8 @@ public PcRunEventLog getRunEventLog(int runId){
return null;
}

public void addRunToTrendReport(int runId, String trendReportId){
public void addRunToTrendReport(int runId, String trendReportId)
{

TrendReportRequest trRequest = new TrendReportRequest(model.getAlmProject(), runId, null);
logger.println("Adding run: " + runId + " to trend report: " + trendReportId);
Expand Down
Expand Up @@ -49,11 +49,14 @@
import org.apache.http.impl.conn.SchemeRegistryFactory;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.*;

import java.io.PrintStream;
Expand All @@ -69,6 +72,9 @@ public class PcRestProxy {
protected static final String AUTHENTICATION_LOGOUT_URL = BASE_PC_API_AUTHENTICATION_URL + "/logout";
protected static final String PC_API_RESOURCES_TEMPLATE = BASE_PC_API_URL + "/domains/%s/projects/%s";
protected static final String RUNS_RESOURCE_NAME = "Runs";
protected static final String TESTS_RESOURCE_NAME = "tests";
protected static final String TEST_INSTANCES_NAME = "testinstances";
protected static final String TEST_SETS_NAME = "testsets";
protected static final String RESULTS_RESOURCE_NAME = "Results";
protected static final String EVENTLOG_RESOURCE_NAME = "EventLog";
protected static final String TREND_REPORT_RESOURCE_NAME = "TrendReports";
Expand Down Expand Up @@ -164,6 +170,40 @@ public PcRunResponse startRun(int testId, int testInstaceId, TimeslotDuration ti
return PcRunResponse.xmlToObject(startRunResponse);
}


public int createTestInstance(int testId, int testSetId) throws PcException, ClientProtocolException, IOException {
HttpPost createTestInstanceRequest = new HttpPost(String.format(baseURL + "/%s", TEST_INSTANCES_NAME));
TestInstanceCreateRequest testInstanceCreateRequest = new TestInstanceCreateRequest(testId,testSetId);
createTestInstanceRequest.setEntity(new StringEntity(testInstanceCreateRequest.objectToXML(), ContentType.APPLICATION_XML));
createTestInstanceRequest.addHeader(RESTConstants.CONTENT_TYPE, CONTENT_TYPE_XML);
HttpResponse response = executeRequest(createTestInstanceRequest);
String responseXml = IOUtils.toString(response.getEntity().getContent());
int testInstanceID = 0;
try {
testInstanceID = testInstanceCreateRequest.getTestInstanceIDFromResponse(responseXml,"TestInstanceID");
} catch (SAXException|ParserConfigurationException e) {
throw new PcException("createTestInstance exception: " + e);
}
return testInstanceID;
}

public PcTestSets GetAllTestSets()throws IOException,PcException{
String getTestSetsUrl = String.format(baseURL + "/%s", TEST_SETS_NAME);
HttpGet getTestSetsRequest = new HttpGet(getTestSetsUrl);
HttpResponse response = executeRequest(getTestSetsRequest);
String testSets = IOUtils.toString(response.getEntity().getContent());
return PcTestSets.xmlToObject(testSets);
}

public PcTestInstances getTestInstancesByTestId(int testId)throws PcException,IOException{
String uri = String.format(baseURL + "/%s?%s=%s", TEST_INSTANCES_NAME,"query",URLEncoder.encode("{test-id[" + testId + "]}","UTF-8"));
HttpGet getFirtstTestInstanceByTestID = new HttpGet(uri);
HttpResponse response = executeRequest(getFirtstTestInstanceByTestID);
String testInstances = IOUtils.toString(response.getEntity().getContent());
return PcTestInstances.xmlToObject(testInstances);

}

public boolean stopRun(int runId, String stopMode) throws PcException, ClientProtocolException, IOException {
String stopUrl = String.format(baseURL + "/%s/%s/%s", RUNS_RESOURCE_NAME, runId, stopMode);
HttpPost stopRunRequest = new HttpPost(stopUrl);
Expand All @@ -181,6 +221,13 @@ public PcRunResponse getRunData(int runId) throws PcException, ClientProtocolExc
return PcRunResponse.xmlToObject(runData);
}

public PcTest getTestData(int testId) throws IOException, PcException {
HttpGet getTestDataRequest = new HttpGet(String.format(baseURL + "/%s/%s",TESTS_RESOURCE_NAME,testId ));
HttpResponse response = executeRequest(getTestDataRequest);
String testData = IOUtils.toString(response.getEntity().getContent());
return PcTestData.xmlToObject(testData);
}

public PcRunResults getRunResults(int runId) throws PcException, ClientProtocolException, IOException {
String getRunResultsUrl = String
.format(baseURL + "/%s/%s/%s", RUNS_RESOURCE_NAME, runId, RESULTS_RESOURCE_NAME);
Expand Down Expand Up @@ -278,4 +325,5 @@ public static boolean isOk (HttpResponse response) {
protected String getBaseURL() {
return baseURL;
}

}
36 changes: 36 additions & 0 deletions src/main/java/com/hp/application/automation/tools/pc/PcTest.java
@@ -0,0 +1,36 @@
package com.hp.application.automation.tools.pc;

/**
* Created by bemh on 6/5/2017.
* Partial implementation of the test xml structure
*/
public class PcTest {


private int testId;
private String testName;

private int trendReportId = -1;



public int getTestId() {
return testId;
}

public String getTestName() {
return testName;
}

public void setTrendReportId(int trendReportId) {
this.trendReportId = trendReportId;
}

public int getTrendReportId() {
return trendReportId;
}

public void setTestId(int testId){this.testId = testId;}

public void setTestName(String testName){this.testName = testName;}
}
@@ -0,0 +1,71 @@
package com.hp.application.automation.tools.pc;

import com.thoughtworks.xstream.XStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;

/**
* Created by bemh on 6/5/2017.
* Partial implementation of the test xml structure
*/
public class PcTestData {




static Document dom;
static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();


public static PcTest xmlToObject(String xml){

PcTest pcTest = new PcTest();

try {
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(new InputSource(new StringReader(xml)));
Element doc = dom.getDocumentElement();
NodeList nListTestNodes = doc.getElementsByTagName("AutomaticTrending");
if (nListTestNodes.getLength() >0 )
{
NodeList nListChild = nListTestNodes.item(0).getChildNodes();
for (int j=0;j < nListChild.getLength();j++) {
if (nListChild.item(j).getNodeName().equals("ReportId")){
pcTest.setTrendReportId(Integer.parseInt(nListChild.item(j).getTextContent()));
break;
}
}
}

nListTestNodes = doc.getElementsByTagName("ID");
pcTest.setTestId(Integer.parseInt(nListTestNodes.item(0).getFirstChild().getNodeValue()));

nListTestNodes = doc.getElementsByTagName("Name");
pcTest.setTestName(nListTestNodes.item(0).getFirstChild().getNodeValue());




}catch (ParserConfigurationException pce) {
System.out.println(pce.getMessage());
} catch (SAXException se) {
System.out.println(se.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

return pcTest;
}

}

0 comments on commit c3b8a06

Please sign in to comment.