001    package org.hackystat.telemetry.service.resource.chart;
002    
003    import static org.junit.Assert.assertEquals;
004    
005    import java.util.List;
006    import javax.xml.datatype.XMLGregorianCalendar;
007    import org.hackystat.sensorbase.client.SensorBaseClient;
008    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
009    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
010    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
011    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
012    import org.hackystat.telemetry.service.client.TelemetryClient;
013    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryChartData;
014    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryPoint;
015    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryStream;
016    import org.hackystat.telemetry.service.test.TelemetryTestHelper;
017    import org.hackystat.utilities.tstamp.Tstamp;
018    import org.junit.Test;
019    import org.junit.Before;
020    
021    /**
022     * Tests CodeIssue Chart processing. 
023     * @author Philip Johnson
024     */
025    public class TestCodeIssueChartRestApi extends TelemetryTestHelper {
026      
027      /** The user for this test case. */
028      private String user = "TestChart@hackystat.org";
029      
030      /** The telemetry client. */
031      private TelemetryClient telemetryClient;
032      
033      /**
034       * Creates Commits on server for use in Telemetry processing. 
035       * @throws Exception If problems occur. 
036       */
037      @Before
038      public void generateData() throws Exception { 
039      // [1] First, create a batch of sensor data.
040      SensorDatas batchData = new SensorDatas();
041      batchData.getSensorData().add(makeData("2007-08-01T02:00:00", user));
042      batchData.getSensorData().add(makeData("2007-08-02T23:55:00", user));
043      batchData.getSensorData().add(makeData("2007-08-03T00:01:00", user));
044      
045      // Connect to the sensorbase and register the user. 
046      SensorBaseClient.registerUser(getSensorBaseHostName(), user);
047      SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
048      client.authenticate();
049      // Send the sensor data to the SensorBase. 
050      client.putSensorDataBatch(batchData);
051      
052      // Now connect to the Telemetry server. 
053      this.telemetryClient = new TelemetryClient(getTelemetryHostName(), user, user);
054      telemetryClient.authenticate();
055      }
056      
057      
058      /**
059       * Tests the chart.
060       * @throws Exception If problems occur. 
061       */
062      @Test public void testTotalCodeIssue() throws Exception {
063        String chartName = "CodeIssue";
064        String params = "*,*"; 
065        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
066              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
067        // See if this chart contains 1 stream.
068        List<TelemetryStream> streams = chart.getTelemetryStream();
069        assertEquals("Checking only 1 stream returned", 1, streams.size());
070        // Get the data points in the single returned stream.
071        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
072        assertEquals("Checking for 4 points", 4, points.size());
073        // Check that these four points are 0, 0, 0, and null (last day has no data.)
074        assertEquals("Checking point 1 is 30", "30", points.get(0).getValue());
075        assertEquals("Checking point 2 is 30", "30", points.get(1).getValue());
076        assertEquals("Checking point 3 is 30", "30", points.get(2).getValue());
077        assertEquals("Checking point 4 is null", null, points.get(3).getValue());
078      }
079      
080      /**
081       * Tests the chart.
082       * @throws Exception If problems occur. 
083       */
084      @Test public void testCodeIssueType() throws Exception {
085        String chartName = "CodeIssue";
086        String params = "*,NPE"; 
087        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
088              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
089        // See if this chart contains 1 stream.
090        List<TelemetryStream> streams = chart.getTelemetryStream();
091        assertEquals("Checking only 1 stream returned", 1, streams.size());
092        // Get the data points in the single returned stream.
093        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
094        assertEquals("Checking for 4 points", 4, points.size());
095        // Check that these four points are 0, 0, 0, and null (last day has no data.)
096        assertEquals("Checking point 1 is 10", "10", points.get(0).getValue());
097        assertEquals("Checking point 2 is 10", "10", points.get(1).getValue());
098        assertEquals("Checking point 3 is 10", "10", points.get(2).getValue());
099        assertEquals("Checking point 4 is null", null, points.get(3).getValue());
100      }
101      
102    
103      /**
104       * Creates a sample SensorData CodeIssue instance given a timestamp and a user.
105       *
106       * @param tstampString The timestamp as a string
107       * @param user The user.
108       * @return The new SensorData CodeIssue instance.
109       * @throws Exception If problems occur.
110       */
111      private SensorData makeData(String tstampString, String user) throws Exception {
112        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
113        String sdt = "CodeIssue";
114        SensorData data = new SensorData();
115        String tool = "FindBugs";
116        data.setTool(tool);
117        data.setOwner(user);
118        data.setSensorDataType(sdt);
119        data.setTimestamp(tstamp);
120        data.setResource("file://foo/bar/baz.txt");
121        data.setRuntime(tstamp);
122    
123        Properties prop = new Properties();
124        prop.getProperty().add(makeProperty("Type_NPE", "10"));
125        prop.getProperty().add(makeProperty("Type_DereferenceNull", "20"));
126        data.setProperties(prop);
127    
128        return data;
129      }
130    
131      /**
132       * Creates and returns a Property initialized with key and value. 
133       * @param key The key. 
134       * @param value The value.
135       * @return The Property instance. 
136       */
137      private Property makeProperty(String key, String value) {
138        Property property = new Property();
139        property.setKey(key);
140        property.setValue(value);
141        return property;
142      }
143    }