001    package org.hackystat.telemetry.service.resource.chart;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertTrue;
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.Parameter;
015    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryChartData;
016    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryPoint;
017    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryStream;
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 DevTime part of the Telemetry REST API. 
025     * @author Philip Johnson
026     */
027    public class TestDevEventChartRestApi 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      /**
036       * Creates DevEvents on server for use in Telemetry processing. 
037       * @throws Exception If problems occur. 
038       */
039      @Before
040      public void generateData() throws Exception { 
041      // [1] First, create a batch of sensor data.
042      SensorDatas batchData = new SensorDatas();
043      batchData.getSensorData().add(makeData("2007-08-01T02:00:00", user));
044      batchData.getSensorData().add(makeData("2007-08-01T02:10:00", user));
045      batchData.getSensorData().add(makeData("2007-08-02T23:55:00", user));
046      batchData.getSensorData().add(makeData("2007-08-03T00:01:00", user));
047      
048      // Connect to the sensorbase and register the user. 
049      SensorBaseClient.registerUser(getSensorBaseHostName(), user);
050      SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
051      client.authenticate();
052      // Send the sensor data to the SensorBase. 
053      client.putSensorDataBatch(batchData);
054      
055      // Now connect to the Telemetry server. 
056      this.telemetryClient = new TelemetryClient(getTelemetryHostName(), user, user);
057      telemetryClient.authenticate();
058      }
059      
060      
061      
062      /**
063       * Tests the DevTime chart.
064       * @throws Exception If problems occur. 
065       */
066      @Test public void testChartParams() throws Exception {
067        String chartName = "DevTime";
068        String params = user + ",false"; // make sure no embedded spaces, or else escape them.
069        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
070              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-03"), params);
071        // See if this chart contains 1 stream with 3 data points of 10, 15, and 20.
072        List<TelemetryStream> streams = chart.getTelemetryStream();
073        assertEquals("Checking only 1 stream returned", 1, streams.size());
074        // Get the data points in the single returned stream.
075        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
076        assertEquals("Checking for 3 points", 3, points.size());
077        // Check that these three points are 10, 15, and 20.
078        assertTrue("Checking point 1 is 0.16", points.get(0).getValue().startsWith("0.16"));
079        assertTrue("Checking point 2 is 0.08", points.get(1).getValue().startsWith("0.08"));
080        assertTrue("Checking point 3 is 0.08", points.get(2).getValue().startsWith("0.08"));
081        List<Parameter> parameters = chart.getParameter();
082        assertEquals("Checking first param id", "member", parameters.get(0).getName());
083        assertEquals("Checking first param val", user, parameters.get(0).getValue());
084        assertEquals("Checking second param id", "cumulative", parameters.get(1).getName());
085        assertEquals("Checking second param val", "false", parameters.get(1).getValue());
086      }
087      
088      /**
089       * Tests the MemberDevTime chart.
090       * @throws Exception If problems occur. 
091       */
092      @Test public void testMemberChartParams() throws Exception {
093        String chartName = "MemberDevTime";
094        String params = "false"; // make sure no embedded spaces, or else escape them.
095        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
096              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-03"), params);
097        // See if this chart contains 1 stream with 3 data points of 10, 15, and 20.
098        List<TelemetryStream> streams = chart.getTelemetryStream();
099        assertEquals("Checking only 1 stream returned", 1, streams.size());
100      }
101      
102      
103      /**
104       * Creates a sample SensorData instance given a timestamp and a user. 
105       * @param tstampString The timestamp as a string
106       * @param user The user.
107       * @return The new SensorData DevEvent instance.
108       * @throws Exception If problems occur. 
109       */
110      private static SensorData makeData(String tstampString, String user) throws Exception {
111        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
112        String sdt = "DevEvent";
113        SensorData data = new SensorData();
114        String tool = "Emacs";
115        data.setTool(tool);
116        data.setOwner(user);
117        data.setSensorDataType(sdt);
118        data.setTimestamp(tstamp);
119        data.setResource("file://foo/bar/baz.txt");
120        data.setRuntime(tstamp);
121        Property property = new Property();
122        property.setKey("Type");
123        property.setValue("StateChange");
124        Properties properties = new Properties();
125        properties.getProperty().add(property);
126        data.setProperties(properties);
127        return data;
128      }
129    
130    }