001    package org.hackystat.telemetry.service.resource.cache;
002    
003    import javax.xml.datatype.XMLGregorianCalendar;
004    import org.hackystat.sensorbase.client.SensorBaseClient;
005    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
006    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
007    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
008    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
009    import org.hackystat.telemetry.service.client.TelemetryClient;
010    import org.hackystat.telemetry.service.test.TelemetryTestHelper;
011    import org.hackystat.utilities.tstamp.Tstamp;
012    import org.junit.Test;
013    import org.junit.Before;
014    
015    /**
016     * Tests the Telemetry Cache API.
017     * @author Philip Johnson
018     */
019    public class TestCacheRestApi extends TelemetryTestHelper {
020      
021      /** The user for this test case. */
022      private String user = "TestCache@hackystat.org";
023      
024      /** The telemetry client. */
025      private TelemetryClient telemetryClient;
026      
027      /**
028       * Creates Build sensor data on server for use in Telemetry processing. 
029       * @throws Exception If problems occur. 
030       */
031      @Before
032      public void generateData() throws Exception { 
033      // [1] First, create a batch of sensor data.
034      SensorDatas batchData = new SensorDatas();
035      batchData.getSensorData().add(makeData("2007-08-01T02:00:00", user));
036      batchData.getSensorData().add(makeData("2007-08-01T02:10:00", user));
037      batchData.getSensorData().add(makeData("2007-08-02T23:55:00", user));
038      batchData.getSensorData().add(makeData("2007-08-03T00:01:00", user));
039      
040      // Connect to the sensorbase and register the user. 
041      SensorBaseClient.registerUser(getSensorBaseHostName(), user);
042      SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
043      client.authenticate();
044      // Send the sensor data to the SensorBase. 
045      client.putSensorDataBatch(batchData);
046      
047      // Now connect to the Telemetry server. 
048      this.telemetryClient = new TelemetryClient(getTelemetryHostName(), user, user);
049      telemetryClient.authenticate();
050      }
051      
052      
053      /**
054       * Tests the cache api.
055       * @throws Exception If problems occur. 
056       */
057      @Test public void testBuildChartDefault() throws Exception {  //NOPMD no assertions here.
058        // Make a chart.
059        String chartName = "Build";
060        String project = "Default";
061        //String params = "FailureCount,*,false"; // make sure no embedded spaces, or else escape them.
062        String params = "*,*,Integration,false";
063        telemetryClient.getChart(chartName, user, project, "Day", 
064              Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params);
065        
066        // Note that caching is not enabled, so these tests are simply making sure the protocol is OK.
067        telemetryClient.clearServerCache();
068        telemetryClient.clearServerCache(user, project);
069      }
070      
071      
072      /**
073       * Creates a sample SensorData UnitTest instance given a timestamp and a user.
074       *
075       * @param tstampString The timestamp as a string
076       * @param user The user.
077       * @return The new SensorData DevEvent instance.
078       * @throws Exception If problems occur.
079       */
080      private SensorData makeData(String tstampString, String user) throws Exception {
081        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
082        String sdt = "Build";
083        SensorData data = new SensorData();
084        String tool = "Ant";
085        data.setTool(tool);
086        data.setOwner(user);
087        data.setSensorDataType(sdt);
088        data.setTimestamp(tstamp);
089        data.setResource("file://foo/bar/baz.txt");
090        data.setRuntime(tstamp);
091    
092        Properties prop = new Properties();
093        prop.getProperty().add(makeProperty("Result", "Success"));
094        prop.getProperty().add(makeProperty("Type", "Integration"));
095        data.setProperties(prop);
096        return data;
097      }
098    
099      /**
100       * Creates and returns a Property initialized with key and value. 
101       * @param key The key. 
102       * @param value The value.
103       * @return The Property instance. 
104       */
105      private Property makeProperty(String key, String value) {
106        Property property = new Property();
107        property.setKey(key);
108        property.setValue(value);
109        return property;
110      }
111    }