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.Parameter;
014    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryChartData;
015    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryPoint;
016    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryStream;
017    import org.hackystat.telemetry.service.test.TelemetryTestHelper;
018    import org.hackystat.utilities.tstamp.Tstamp;
019    import org.junit.Test;
020    import org.junit.Before;
021    
022    /**
023     * Tests the UnitTest Chart processing. 
024     * @author Philip Johnson
025     */
026    public class TestUnitTestChartRestApi extends TelemetryTestHelper {
027      
028      /** The user for this test case. */
029      private String user = "TestChart@hackystat.org";
030      
031      /** The telemetry client. */
032      private TelemetryClient telemetryClient;
033      
034      /**
035       * Creates UnitTests on server for use in Telemetry processing. 
036       * @throws Exception If problems occur. 
037       */
038      @Before
039      public void generateData() throws Exception { 
040      // [1] First, create a batch of sensor data.
041      SensorDatas batchData = new SensorDatas();
042      batchData.getSensorData().add(makeData("2007-08-01T02:00:00", user));
043      batchData.getSensorData().add(makeData("2007-08-01T02:10:00", user));
044      batchData.getSensorData().add(makeData("2007-08-02T23:55:00", user));
045      batchData.getSensorData().add(makeData("2007-08-03T00:01:00", user));
046      
047      // Connect to the sensorbase and register the user. 
048      SensorBaseClient.registerUser(getSensorBaseHostName(), user);
049      SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
050      client.authenticate();
051      // Send the sensor data to the SensorBase. 
052      client.putSensorDataBatch(batchData);
053      
054      // Now connect to the Telemetry server. 
055      this.telemetryClient = new TelemetryClient(getTelemetryHostName(), user, user);
056      telemetryClient.authenticate();
057      }
058      
059      
060      /**
061       * Tests the UnitTest chart.
062       * @throws Exception If problems occur. 
063       */
064      @Test public void testUnitTestChartFailureCount() throws Exception {
065        String chartName = "UnitTest";
066        //String params = "FailureCount,*,false"; // make sure no embedded spaces, or else escape them.
067        String params = "FailureCount," + user + ",false"; 
068        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
069              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
070        // See if this chart contains 1 stream.
071        List<TelemetryStream> streams = chart.getTelemetryStream();
072        assertEquals("Checking only 1 stream returned", 1, streams.size());
073        // Get the data points in the single returned stream.
074        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
075        assertEquals("Checking for 4 points", 4, points.size());
076        // Check that the first three are zero and the last is null.
077        assertEquals("Checking point 1 is zero", "0", points.get(0).getValue());
078        assertEquals("Checking point 2 is zero", "0", points.get(1).getValue());
079        assertEquals("Checking point 3 is zero", "0", points.get(2).getValue());
080        assertEquals("Checking point 4 is null", null, points.get(3).getValue());
081        List<Parameter> parameters = chart.getParameter();
082        assertEquals("Checking first param id", "mode", parameters.get(0).getName());
083        assertEquals("Checking first param val", "FailureCount", parameters.get(0).getValue());
084        assertEquals("Checking second param id", "member", parameters.get(1).getName());
085        assertEquals("Checking second param val", user, parameters.get(1).getValue());
086      }
087      
088      /**
089       * Tests the UnitTest chart.
090       * @throws Exception If problems occur. 
091       */
092      @Test public void testMemberUnitTestChartFailureCount() throws Exception {
093        String chartName = "MemberUnitTest";
094        //String params = "FailureCount,*,false"; // make sure no embedded spaces, or else escape them.
095        String params = "FailureCount,false"; 
096        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
097              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
098        // See if this chart contains 1 stream.
099        List<TelemetryStream> streams = chart.getTelemetryStream();
100        assertEquals("Checking only 1 stream returned", 1, streams.size());
101      }
102      
103      /**
104       * Tests the UnitTest chart.
105       * @throws Exception If problems occur. 
106       */
107      @Test public void testUnitTestChartTotalCount() throws Exception {
108        String chartName = "UnitTest";
109        //String params = "FailureCount,*,false"; // make sure no embedded spaces, or else escape them.
110        String params = "TotalCount," + user + ",false"; 
111        TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 
112              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
113        // See if this chart contains 1 stream.
114        List<TelemetryStream> streams = chart.getTelemetryStream();
115        assertEquals("Checking only 1 stream returned", 1, streams.size());
116        // Get the data points in the single returned stream.
117        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
118        assertEquals("Checking for 4 points", 4, points.size());
119        // Check that these four points are 0, 0, 0, and null (last day has no data.)
120        assertEquals("Checking point 1 is 2", "2", points.get(0).getValue());
121        assertEquals("Checking point 2 is 1", "1", points.get(1).getValue());
122        assertEquals("Checking point 3 is 1", "1", points.get(2).getValue());
123        assertEquals("Checking point 4 is null", null, points.get(3).getValue());
124        List<Parameter> parameters = chart.getParameter();
125        assertEquals("Checking first param id", "mode", parameters.get(0).getName());
126        assertEquals("Checking first param val", "TotalCount", parameters.get(0).getValue());
127        assertEquals("Checking second param id", "member", parameters.get(1).getName());
128        assertEquals("Checking second param val", user, parameters.get(1).getValue());
129      }
130      
131      /**
132       * Creates a sample SensorData UnitTest instance given a timestamp and a user.
133       *
134       * @param tstampString The timestamp as a string
135       * @param user The user.
136       * @return The new SensorData DevEvent instance.
137       * @throws Exception If problems occur.
138       */
139      private SensorData makeData(String tstampString, String user) throws Exception {
140        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
141        String sdt = "UnitTest";
142        SensorData data = new SensorData();
143        String tool = "JUnit";
144        data.setTool(tool);
145        data.setOwner(user);
146        data.setSensorDataType(sdt);
147        data.setTimestamp(tstamp);
148        data.setResource("file://foo/bar/baz.txt");
149        data.setRuntime(tstamp);
150    
151        // test count, test time, success, failure
152        Properties prop = new Properties();
153    
154        prop.getProperty().add(makeProperty("Name", "testName"));
155        prop.getProperty().add(makeProperty("Result", "pass"));
156        prop.getProperty().add(makeProperty("ElapsedTime", "15"));
157        data.setProperties(prop);
158    
159        return data;
160      }
161    
162      /**
163       * Creates and returns a Property initialized with key and value. 
164       * @param key The key. 
165       * @param value The value.
166       * @return The Property instance. 
167       */
168      private Property makeProperty(String key, String value) {
169        Property property = new Property();
170        property.setKey(key);
171        property.setValue(value);
172        return property;
173      }
174    }