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 the Churn Chart processing. 
023     * @author Philip Johnson
024     */
025    public class TestChurnChartRestApi 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 UnitTests 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-01T02:10:00", user));
043      batchData.getSensorData().add(makeData("2007-08-02T23:55:00", user));
044      batchData.getSensorData().add(makeData("2007-08-03T00:01:00", user));
045      
046      // Connect to the sensorbase and register the user. 
047      SensorBaseClient.registerUser(getSensorBaseHostName(), user);
048      SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
049      client.authenticate();
050      // Send the sensor data to the SensorBase. 
051      client.putSensorDataBatch(batchData);
052      
053      // Now connect to the Telemetry server. 
054      this.telemetryClient = new TelemetryClient(getTelemetryHostName(), user, user);
055      telemetryClient.authenticate();
056      }
057      
058      
059      /**
060       * Tests the chart.
061       * @throws Exception If problems occur. 
062       */
063      @Test public void testChart() throws Exception {
064        String chartName = "Churn";
065        String params = "*,true"; 
066        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
067              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
068        // See if this chart contains 1 stream.
069        List<TelemetryStream> streams = chart.getTelemetryStream();
070        assertEquals("Checking only 1 stream returned", 1, streams.size());
071        // Get the data points in the single returned stream.
072        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
073        assertEquals("Checking for 4 points", 4, points.size());
074        // Check that these four points are 0, 0, 0, and null (last day has no data.)
075        assertEquals("Checking point 1 is 60", "60", points.get(0).getValue());
076        assertEquals("Checking point 2 is 90", "90", points.get(1).getValue());
077        assertEquals("Checking point 3 is 90", "120", points.get(2).getValue());
078        assertEquals("Checking point 4 is 90", "120", points.get(3).getValue());
079      }
080      
081      /**
082       * Tests the chart.
083       * @throws Exception If problems occur. 
084       */
085      @Test public void testMemberChart() throws Exception {
086        String chartName = "MemberChurn";
087        String params = "true"; 
088        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
089              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
090        // See if this chart contains 1 stream.
091        List<TelemetryStream> streams = chart.getTelemetryStream();
092        assertEquals("Checking only 1 stream returned", 1, streams.size());
093      }
094      
095      
096    
097      /**
098       * Creates a sample SensorData UnitTest instance given a timestamp and a user.
099       *
100       * @param tstampString The timestamp as a string
101       * @param user The user.
102       * @return The new SensorData DevEvent instance.
103       * @throws Exception If problems occur.
104       */
105      private SensorData makeData(String tstampString, String user) throws Exception {
106        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
107        String sdt = "Commit";
108        SensorData data = new SensorData();
109        String tool = "Subversion";
110        data.setTool(tool);
111        data.setOwner(user);
112        data.setSensorDataType(sdt);
113        data.setTimestamp(tstamp);
114        data.setResource("file://foo/bar/baz.txt");
115        data.setRuntime(tstamp);
116    
117        Properties prop = new Properties();
118        prop.getProperty().add(makeProperty("linesAdded", "10"));
119        prop.getProperty().add(makeProperty("linesDeleted", "20"));
120        data.setProperties(prop);
121    
122        return data;
123      }
124    
125      /**
126       * Creates and returns a Property initialized with key and value. 
127       * @param key The key. 
128       * @param value The value.
129       * @return The Property instance. 
130       */
131      private Property makeProperty(String key, String value) {
132        Property property = new Property();
133        property.setKey(key);
134        property.setValue(value);
135        return property;
136      }
137    }