001 package org.hackystat.telemetry.service.resource.chart; 002 003 import static org.junit.Assert.assertEquals; 004 import static org.junit.Assert.assertNull; 005 006 import java.util.List; 007 import javax.xml.datatype.XMLGregorianCalendar; 008 import org.hackystat.sensorbase.client.SensorBaseClient; 009 import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties; 010 import org.hackystat.sensorbase.resource.sensordata.jaxb.Property; 011 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData; 012 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas; 013 import org.hackystat.telemetry.service.client.TelemetryClient; 014 import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryChartData; 015 import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryPoint; 016 import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryStream; 017 import org.hackystat.telemetry.service.resource.chart.jaxb.YAxis; 018 import org.hackystat.telemetry.service.test.TelemetryTestHelper; 019 import org.hackystat.utilities.tstamp.Tstamp; 020 import org.junit.Test; 021 import org.junit.Before; 022 023 /** 024 * Tests the Coverage Chart processing. 025 * @author Philip Johnson 026 */ 027 public class TestCoverageChartRestApi extends TelemetryTestHelper { 028 029 /** The user for this test case. */ 030 private String user = "TestChart@hackystat.org"; 031 032 /** The telemetry client. */ 033 private TelemetryClient telemetryClient; 034 035 /** A counter so that each resource (file name) is unique in the sample data. */ 036 int counter = 0; 037 038 /** 039 * Creates UnitTests on server for use in Telemetry processing. 040 * @throws Exception If problems occur. 041 */ 042 @Before 043 public void generateData() throws Exception { 044 // [1] First, create a batch of sensor data for Day 1. 045 SensorDatas batchData = new SensorDatas(); 046 String tstampString = "2007-08-01T02:00:00"; 047 XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString); 048 // Create four instances with unique tstamps and the same runtime. 049 batchData.getSensorData().add(makeData(tstamp, user, tstamp)); 050 batchData.getSensorData().add(makeData(tstamp, user, tstamp)); 051 batchData.getSensorData().add(makeData(tstamp, user, tstamp)); 052 batchData.getSensorData().add(makeData(tstamp, user, tstamp)); 053 054 // Connect to the sensorbase and register the user. 055 SensorBaseClient.registerUser(getSensorBaseHostName(), user); 056 SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user); 057 client.authenticate(); 058 // Send the sensor data to the SensorBase. 059 client.putSensorDataBatch(batchData); 060 061 // Now connect to the Telemetry server. 062 this.telemetryClient = new TelemetryClient(getTelemetryHostName(), user, user); 063 telemetryClient.authenticate(); 064 } 065 066 067 /** 068 * Tests the Coverage chart. 069 * @throws Exception If problems occur. 070 */ 071 @Test public void testCoverageChart() throws Exception { 072 String chartName = "Coverage"; 073 String params = "Percentage,line"; 074 TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 075 Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params); 076 // See if this chart contains 1 stream. 077 List<TelemetryStream> streams = chart.getTelemetryStream(); 078 assertEquals("Checking only 1 stream returned", 1, streams.size()); 079 // Get the data points in the single returned stream. 080 List<TelemetryPoint> points = streams.get(0).getTelemetryPoint(); 081 assertEquals("Checking for 4 points", 4, points.size()); 082 // Check that these four points are 0 and null (only first day has value). 083 assertEquals("Checking point 1 is 66", "66", points.get(0).getValue()); 084 assertNull("Checking point 2 is null", points.get(1).getValue()); 085 086 // See if the chart Y-Axis provides the type, lower, and upper bound information. 087 YAxis yAxis = streams.get(0).getYAxis(); 088 assertEquals("Checking number type", "integer", yAxis.getNumberType()); 089 assertEquals("Checking lower bound", 0, yAxis.getLowerBound().intValue()); 090 assertEquals("Checking upper bound", 100, yAxis.getUpperBound().intValue()); 091 } 092 093 094 095 /** 096 * Creates a sample SensorData Coverage instance given a timestamp and a user. 097 * Only "line" coverage metrics are provided here. 098 * 099 * @param tstamp The timestamp, incremented each time to guarantee uniqueness. 100 * @param user The user. 101 * @param runtime The runtime, which should be the same for all grouped Coverage instances. 102 * @return The new SensorData instance. 103 * @throws Exception If problems occur. 104 */ 105 private SensorData makeData(XMLGregorianCalendar tstamp, String user, 106 XMLGregorianCalendar runtime) throws Exception { 107 108 String sdt = "Coverage"; 109 SensorData data = new SensorData(); 110 String tool = "Emma"; 111 data.setTool(tool); 112 data.setOwner(user); 113 data.setSensorDataType(sdt); 114 data.setTimestamp(Tstamp.incrementMinutes(tstamp, counter++)); 115 data.setResource("file://foo/bar/baz-" + counter + ".java"); 116 data.setRuntime(runtime); 117 Properties prop = new Properties(); 118 prop.getProperty().add(makeProperty("line_Uncovered", "1")); 119 prop.getProperty().add(makeProperty("line_Covered", "2")); 120 data.setProperties(prop); 121 return data; 122 } 123 124 /** 125 * Creates and returns a Property initialized with key and value. 126 * @param key The key. 127 * @param value The value. 128 * @return The Property instance. 129 */ 130 private Property makeProperty(String key, String value) { 131 Property property = new Property(); 132 property.setKey(key); 133 property.setValue(value); 134 return property; 135 } 136 }