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 UnitTest Chart processing. 
023     * @author Philip Johnson
024     */
025    public class TestBuildChartRestApi 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 Build sensor data 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 Build chart.
061       * @throws Exception If problems occur. 
062       */
063      @Test public void testBuildChartDefault() throws Exception {
064        String chartName = "Build";
065        //String params = "FailureCount,*,false"; // make sure no embedded spaces, or else escape them.
066        String params = "*,*,Integration,false";
067        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
068              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
069        // See if this chart contains 1 stream.
070        List<TelemetryStream> streams = chart.getTelemetryStream();
071        assertEquals("Checking only 1 stream returned", 1, streams.size());
072        // Get the data points in the single returned stream.
073        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
074        assertEquals("Checking for 4 points", 4, points.size());
075        // Check that these four points are 0, 0, 0, and null (last day has no data.)
076        assertEquals("Checking point 1 is 2", "2", points.get(0).getValue());
077        assertEquals("Checking point 2 is 1", "1", points.get(1).getValue());
078        assertEquals("Checking point 3 is 1", "1", points.get(2).getValue());
079        assertEquals("Checking point 4 is null", null, points.get(3).getValue());
080      }
081      
082      /**
083       * Tests the MemberBuild chart.
084       * @throws Exception If problems occur. 
085       */
086      @Test public void testMemberBuildChartDefault() throws Exception {
087        String chartName = "MemberBuild";
088        String params = "*,Integration,false";
089        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
090              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
091        // See if this chart contains 1 stream.
092        List<TelemetryStream> streams = chart.getTelemetryStream();
093        assertEquals("Checking only 1 stream returned", 1, streams.size());
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 = "Build";
108        SensorData data = new SensorData();
109        String tool = "Ant";
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("Result", "Success"));
119        prop.getProperty().add(makeProperty("Type", "Integration"));
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    }