Jagger
 All Classes Namespaces Files Functions Variables Groups Pages
Custom listener examples code

Listeners main page

Custom test listener Custom test suite listener Custom invocation listener
/* begin: following section is used for docu generation - custom test listener */
public class ProviderOfTestListener extends ServicesAware implements Provider<TestListener> {
private static final Logger log = LoggerFactory.getLogger(ProviderOfTestListener.class);
private String commentString = "";
// Method will be executed single time, when listener provider is initiated
@Override
protected void init() {
// begin: following section is used for docu generation - example of metric creation
// In this example, we will create custom metric to collect internal metrics of SUT
// Approach is the following:
// - Declare custom metric
// - Run test. SUT will usually collect some internal data (metrics)
// - After test is finished, get all internal data from SUT and save to custom metric
// In this case internal data will be save to result DB, displayed in test report and Web UI,
// you can use it in decision maker when running Continues Integration
MetricDescription metricDescription = new MetricDescription("internalData");
metricDescription.plotData(false).showSummary(true).addAggregator(new AvgMetricAggregatorProvider());
getMetricService().createMetric(metricDescription);
// end: following section is used for docu generation - example of metric creation
}
// Method will provide custom test listener to Jagger
@Override
public TestListener provide() {
return new TestListener() {
// Method will be executed before starting of test
@Override
public void onStart(TestInfo testInfo) {
// We will take no actions in this example
}
// Method will be executed after finishing test
@Override
public void onStop(TestInfo testInfo) {
long someMetric = 0;
// Test is finished and now we are ready to get some internal data from SUT
// Unfortunately our example is very simple and no data is available
// So we will just ask what Google knows about Jagger and save number of found results as metric
// Send request to google
String url = "https://www.google.com/search?q=griddynamics+jagger";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
// Parse response
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
if (line.contains("id=\"resultStats\"")) {
// How many results google have found
Pattern pattern = Pattern.compile("id=\"resultStats\">About(.*?)results</div>");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
someMetric = Long.parseLong(matcher.group(1).replaceAll(",","").replace(" ",""));
}
}
}
}
catch (Exception e) {}
// Save metric
getMetricService().saveValue("internalData",someMetric);
}
// Method will be executed periodically during test run
@Override
public void onRun(TestInfo status) {
// We will take no actions in this example
}
};
}
}
/* end: following section is used for docu generation - custom test listener */
/* begin: following section is used for docu generation - custom test suite listener */
public class ProviderOfTestSuiteListener extends ServicesAware implements Provider<TestSuiteListener> {
private static final Logger log = LoggerFactory.getLogger(ProviderOfTestSuiteListener.class);
// Method will be executed single time, when listener provider is initiated
@Override
protected void init() {
// In this example, we will take version of Jagger artifact from pom file
// and save this value to Session comment
// In the same way you can save version(s) of SUT artifacts into session comment
String POM_JAGGER_VERSION = "jagger.version";
String pomfile = "../../pom.xml";
MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
try {
FileReader reader = new FileReader(pomfile);
Model model = mavenXpp3Reader.read(reader);
getSessionInfoService().appendToComment("jagger.version: " + model.getProperties().get(POM_JAGGER_VERSION));
}
catch(Exception e){
log.warn("Cant get pom info {}", e);
}
}
// Method will provide custom test suite listener to Jagger
@Override
public TestSuiteListener provide() {
return new TestSuiteListener() {
// Method will be executed before starting of test suite
@Override
public void onStart(TestSuiteInfo testSuiteInfo) {
/* begin: following section is used for docu generation - work with session comments */
// Here you can f.e.:
// - provide smoke tests to check if your SUT is responding
// - check system properties required for correct SUT functionality
// In this example we will append names of nodes participating in test to session comment
// We will also print OS name and CPU model
String comment = "";
for (Map.Entry<NodeId,GeneralNodeInfo> entry : testSuiteInfo.getGeneralNodeInfo().entrySet()) {
comment += "\nNode: " + entry.getKey() + " (OS: " + entry.getValue().getOsName() + ", " + entry.getValue().getCpuModel() + ")";
}
getSessionInfoService().appendToComment(comment);
/* end: following section is used for docu generation - work with session comments */
}
// Method will be executed after finishing test suite
@Override
public void onStop(TestSuiteInfo testSuiteInfo) {
/* begin: following section is used for docu generation - work with session tags */
// In this example we will create tags
getSessionInfoService().saveOrUpdateTag("SERVICE_NAME", "Tag to mark sessions, testing some particular service");
getSessionInfoService().saveOrUpdateTag("PASS", "Tag to mark sessions with pass results");
getSessionInfoService().saveOrUpdateTag("FAIL", "Tag to mark sessions with fail results");
// Mark session with some tag from source code
getSessionInfoService().markSessionWithTag("SERVICE_NAME");
/* end: following section is used for docu generation - work with session tags */
}
};
}
}
/* end: following section is used for docu generation - custom test suite listener */
/* begin: following section is used for docu generation - custom invocation listener */
public class ProviderOfInvocationListener extends ServicesAware implements Provider<InvocationListener> {
private static final Logger log = LoggerFactory.getLogger(ProviderOfInvocationListener.class);
private List<Integer> codes = new ArrayList<Integer>();
// Method will be executed single time, when listener provider is initiated
@Override
protected void init() {
// We will take no actions in this example
}
// Method will provide custom test suite listener to Jagger
@Override
public InvocationListener provide() {
return new InvocationListener() {
// In this example we will create listener that will check what
// Http codes are returned by SUT and calculate how often every code is returned
// As result you will get following info in report:
// - number of invocations per each code
// - number of invocations per each code vs time
// Method will be executed before invoke
@Override
public void onStart(InvocationInfo invocationInfo) {
// We will take no actions in this example
}
// Method will be executed after successful invoke
@Override
public void onSuccess(InvocationInfo invocationInfo) {
saveResult(invocationInfo);
}
// Method will be executed when some invocation exception happens or some validator failed
@Override
public void onFail(InvocationInfo invocationInfo, InvocationException e) {
saveResult(invocationInfo);
}
// Method is executed when invocation was interrupted by some error
@Override
public void onError(InvocationInfo invocationInfo, Throwable error) {
saveResult(invocationInfo);
}
private void saveResult(InvocationInfo invocationInfo) {
// if failed with exception => result will be null
// if failed by validator => result will contain error code and we will save it
if (invocationInfo.getResult() != null) {
HttpResponse myResult = (HttpResponse) invocationInfo.getResult();
int code = myResult.getStatusCode();
String metricId = "http_return_code_" + code;
// IMPORTANT: saveResult method will be executed during every invocation = every request to SUT
// Try to avoid slow operations in invocation listener code. They will slow down your workload
// Creating new metric is time consuming opreration. We are creating metrics dynamically in this example,
// because we don't know beforehand what HTTP codes will be returned => what metric ids will be used.
// As soon as you know metric ids in advance, create your metrics in init() method above!
// Create new metric if it doesn't exist
if (!codes.contains(code)) {
codes.add(code);
MetricDescription metricDescription = new MetricDescription(metricId);
metricDescription.plotData(true).showSummary(true).displayName("Http return code " + code + " ").addAggregator(new SumMetricAggregatorProvider());
getMetricService().createMetric(metricDescription);
}
// Save metric. After every invoke we will add 1 to some code bin
// During aggregation we will just summarize these value to get info
// how many invokes per code were executed
getMetricService().saveValue(metricId,1);
}
}
};
}
}
/* end: following section is used for docu generation - custom invocation listener */