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 Commit Chart processing. 
023     * @author Philip Johnson
024     */
025    public class TestCommitChartRestApi 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-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 testCommitChartFailureCount() throws Exception {
064        String chartName = "Commit";
065        String params = user + ",false"; 
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 2", "2", points.get(0).getValue());
076        assertEquals("Checking point 2 is 1", "1", points.get(1).getValue());
077        assertEquals("Checking point 3 is 1", "1", points.get(2).getValue());
078        assertEquals("Checking point 4 is null", null, points.get(3).getValue());
079      }
080      
081      /**
082       * Tests that the member chart can be invoked. 
083       * @throws Exception If problems occur. 
084       */
085      @Test public void testMemberCommitChartFailureCount() throws Exception {
086        String chartName = "MemberCommit";
087        String params = "false"; 
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       * Creates a sample SensorData UnitTest instance given a timestamp and a user.
098       *
099       * @param tstampString The timestamp as a string
100       * @param user The user.
101       * @return The new SensorData DevEvent instance.
102       * @throws Exception If problems occur.
103       */
104      private SensorData makeData(String tstampString, String user) throws Exception {
105        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
106        String sdt = "Commit";
107        SensorData data = new SensorData();
108        String tool = "Subversion";
109        data.setTool(tool);
110        data.setOwner(user);
111        data.setSensorDataType(sdt);
112        data.setTimestamp(tstamp);
113        data.setResource("file://foo/bar/baz.txt");
114        data.setRuntime(tstamp);
115    
116        Properties prop = new Properties();
117        prop.getProperty().add(makeProperty("linesAdded", "10"));
118        prop.getProperty().add(makeProperty("linesDeleted", "20"));
119        data.setProperties(prop);
120    
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    }