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 TestCyclomaticComplexityChartRestApi 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 FileMetrics 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 = "CyclomaticComplexity";
065        String params = "AverageComplexityPerMethod,10,JavaNCSS"; 
066        String defaultProj = "Default";
067        String day = "Day";
068        XMLGregorianCalendar start = Tstamp.makeTimestamp("2007-08-01");
069        XMLGregorianCalendar end = Tstamp.makeTimestamp("2007-08-04");
070        
071        TelemetryChartData chart = 
072          telemetryClient.getChart(chartName, user, defaultProj, day, start, end, params);
073        // See if this chart contains 1 stream.
074        List<TelemetryStream> streams = chart.getTelemetryStream();
075        assertEquals("Checking only 1 stream returned", 1, streams.size());
076        // Get the data points in the single returned stream.
077        List<TelemetryPoint> points = streams.get(0).getTelemetryPoint();
078        assertEquals("Checking for 4 points", 4, points.size());
079        // Check that these four points are all 20.
080        assertEquals("Checking point 1 is 20", "20.0", points.get(0).getValue());
081        assertEquals("Checking point 2 is 20", "20.0", points.get(1).getValue());
082        assertEquals("Checking point 3 is 20", "20.0", points.get(2).getValue());
083        
084        // Now check the other modes
085        params = "TotalMethods,10,JavaNCSS";
086        chart = telemetryClient.getChart(chartName, user, defaultProj, day, start, end, params);
087        streams = chart.getTelemetryStream();
088        points = streams.get(0).getTelemetryPoint();
089        assertEquals("Checking point 1 is 2", "2.0", points.get(0).getValue());
090        
091        params = "TotalLines,10,JavaNCSS";
092        chart = telemetryClient.getChart(chartName, user, defaultProj, day, start, end, params);
093        streams = chart.getTelemetryStream();
094        points = streams.get(0).getTelemetryPoint();
095        assertEquals("Checking point 1 is 10", "10.0", points.get(0).getValue());
096        
097        params = "TotalMethodsAboveComplexityThreshold,10,JavaNCSS";
098        chart = telemetryClient.getChart(chartName, user, defaultProj, day, start, end, params);
099        streams = chart.getTelemetryStream();
100        points = streams.get(0).getTelemetryPoint();
101        assertEquals("Checking point 1 is 2 ", "2.0", points.get(0).getValue());
102        
103      }
104      
105    
106      /**
107       * Creates a sample SensorData FileMetric instance given a timestamp and a user.
108       *
109       * @param tstampString The timestamp as a string
110       * @param user The user.
111       * @return The new SensorData DevEvent instance.
112       * @throws Exception If problems occur.
113       */
114      private SensorData makeData(String tstampString, String user) throws Exception {
115        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
116        String sdt = "FileMetric";
117        SensorData data = new SensorData();
118        String tool = "JavaNCSS";
119        data.setTool(tool);
120        data.setOwner(user);
121        data.setSensorDataType(sdt);
122        data.setTimestamp(tstamp);
123        data.setResource("file://foo/bar/baz.txt");
124        data.setRuntime(tstamp);
125    
126        Properties prop = new Properties();
127        prop.getProperty().add(makeProperty("TotalLines", "10"));
128        prop.getProperty().add(makeProperty("CyclomaticComplexityList", "20,20"));
129        data.setProperties(prop);
130    
131        return data;
132      }
133    
134      /**
135       * Creates and returns a Property initialized with key and value. 
136       * @param key The key. 
137       * @param value The value.
138       * @return The Property instance. 
139       */
140      private Property makeProperty(String key, String value) {
141        Property property = new Property();
142        property.setKey(key);
143        property.setValue(value);
144        return property;
145      }
146    }