001    package org.hackystat.dailyprojectdata.resource.filemetric;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertTrue;
005    
006    import javax.xml.datatype.XMLGregorianCalendar;
007    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
008    import org.hackystat.dailyprojectdata.resource.filemetric.jaxb.FileMetricDailyProjectData;
009    import org.hackystat.dailyprojectdata.test.DailyProjectDataTestHelper;
010    import org.hackystat.sensorbase.client.SensorBaseClient;
011    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
012    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
014    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
015    import org.hackystat.utilities.tstamp.Tstamp;
016    import org.junit.Test;
017    
018    /**
019     * Tests the FileMetric DPD. 
020     * @author Philip Johnson
021     */
022    public class TestFileMetricRestApi extends DailyProjectDataTestHelper {
023    
024      /** The user for this test case. */
025      private String user = "TestFileMetric@hackystat.org";
026      
027      /** Used to guarantee tstamp uniqueness. */
028      private int counter = 0;
029    
030      /**
031       * Test that GET {host}/filemetric/{user}/default/{starttime}/TotalLines works properly.
032       * First, it creates a test user and sends some sample FileMetric data to the
033       * SensorBase. Then, it invokes the GET request and checks to see that it
034       * obtains the right answer. Finally, it deletes the data and the user.
035       *
036       * @throws Exception If problems occur.
037       */
038      @Test
039      public void getDefaultFileMetric() throws Exception {
040        // First, create a batch of DevEvent sensor data.
041        SensorDatas batchData = new SensorDatas();
042        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-04-30T02:00:00");
043        batchData.getSensorData().add(makeFileMetric(tstamp, 111));
044        batchData.getSensorData().add(makeFileMetric(tstamp, 89));
045        
046        // Connect to the sensorbase and register the test user.
047        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
048        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
049        client.authenticate();
050        // Send the FileMetric sensor data to the SensorBase.
051        client.putSensorDataBatch(batchData);
052    
053        // Now connect to the DPD server.
054        DailyProjectDataClient dpdClient = new DailyProjectDataClient(getDailyProjectDataHostName(),
055            user, user);
056        dpdClient.authenticate();
057        String size = "TotalLines";
058        FileMetricDailyProjectData fileMetric = dpdClient.getFileMetric(user, "Default", tstamp, size);
059        assertEquals("Checking default size", 200, fileMetric.getTotal(), 0.01);
060        fileMetric = dpdClient.getFileMetric(user, "Default", Tstamp.makeTimestamp("2007-05-01"), size);
061        assertTrue("Checking empty data", fileMetric.getFileData().isEmpty());
062      }
063    
064      /**
065       * Creates a sample SensorData FileMetric instance given a timestamp and a size.
066       *
067       * @param tstamp The timestamp, used as the Runtime and auto-incremented for the tstamp.
068       * @param size The size used as the value of the TotalLines property.
069       * @return The new SensorData FileMetric instance.
070       * @throws Exception If problems occur.
071       */
072      private SensorData makeFileMetric(XMLGregorianCalendar tstamp, int size) throws Exception {
073        String sdt = "FileMetric";
074        SensorData data = new SensorData();
075        String tool = "SCLC";
076        data.setTool(tool);
077        data.setOwner(user);
078        data.setSensorDataType(sdt);
079        data.setTimestamp(Tstamp.incrementMinutes(tstamp, counter++));
080        data.setRuntime(tstamp);
081        data.setResource("/users/johnson/Foo-" + counter + ".java");
082        Property property = new Property();
083        property.setKey("TotalLines");
084        property.setValue(String.valueOf(size));
085        Properties properties = new Properties();
086        properties.getProperty().add(property);
087        data.setProperties(properties);
088        return data;
089      }
090    
091    }