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 Coupling Chart processing. 
023     * @author Philip Johnson
024     */
025    public class TestCouplingChartRestApi 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 = "Coupling";
065        String params = "All,Average,class,10,DependencyFinder"; 
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 3", "3.0", points.get(0).getValue());
081        assertEquals("Checking point 2 is 3", "3.0", points.get(1).getValue());
082        assertEquals("Checking point 3 is 3", "3.0", points.get(2).getValue());
083        
084        // Now check the other modes
085        params = "Afferent,Total,class,10,DependencyFinder";
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 1", "1.0", points.get(0).getValue());
090        
091        params = "Efferent,TotalInstancesAboveThreshold,class,1,DependencyFinder";
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 1", "1.0", points.get(0).getValue());
096      }
097      
098    
099      /**
100       * Creates a sample SensorData FileMetric instance given a timestamp and a user.
101       *
102       * @param tstampString The timestamp as a string
103       * @param user The user.
104       * @return The new SensorData DevEvent instance.
105       * @throws Exception If problems occur.
106       */
107      private SensorData makeData(String tstampString, String user) throws Exception {
108        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
109        String sdt = "Coupling";
110        SensorData data = new SensorData();
111        String tool = "DependencyFinder";
112        data.setTool(tool);
113        data.setOwner(user);
114        data.setSensorDataType(sdt);
115        data.setTimestamp(tstamp);
116        data.setResource("file://foo/bar/baz.txt");
117        data.setRuntime(tstamp);
118    
119        Properties prop = new Properties();
120        prop.getProperty().add(makeProperty("Afferent", "1"));
121        prop.getProperty().add(makeProperty("Efferent", "2"));
122        prop.getProperty().add(makeProperty("Type", "class"));
123        data.setProperties(prop);
124    
125        return data;
126      }
127    
128      /**
129       * Creates and returns a Property initialized with key and value. 
130       * @param key The key. 
131       * @param value The value.
132       * @return The Property instance. 
133       */
134      private Property makeProperty(String key, String value) {
135        Property property = new Property();
136        property.setKey(key);
137        property.setValue(value);
138        return property;
139      }
140    }