001    package org.hackystat.dailyprojectdata.resource.devtime;
002    
003    import static org.junit.Assert.assertEquals;
004    
005    import javax.xml.datatype.XMLGregorianCalendar;
006    
007    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
008    import org.hackystat.dailyprojectdata.resource.devtime.jaxb.DevTimeDailyProjectData;
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 DevTime part of the DailyProjectData REST API. 
020     * @author Philip Johnson
021     */
022    public class TestDevTimeRestApi extends DailyProjectDataTestHelper {
023      
024      /** The user for this test case. */
025      private String user = "TestDevTime@hackystat.org";
026      
027      /**
028       * Test that GET {host}/devtime/{user}/default/{starttime} works properly.
029       * First, it creates a test user and sends some sample DevEvent data to the SensorBase. 
030       * Then, it invokes the GET request and checks to see that it obtains the right answer. 
031       * Finally, it deletes the data and the user. 
032       * @throws Exception If problems occur.
033       */
034      @Test public void getDefaultDevTime() throws Exception {
035        // First, create a batch of DevEvent sensor data.
036        SensorDatas batchData = new SensorDatas();
037        batchData.getSensorData().add(makeDevEvent("2007-04-30T02:00:00", user));
038        batchData.getSensorData().add(makeDevEvent("2007-04-30T02:10:00", user));
039        batchData.getSensorData().add(makeDevEvent("2007-04-29T23:55:00", user));
040        batchData.getSensorData().add(makeDevEvent("2007-05-01T00:01:00", user));
041        
042        // Connect to the sensorbase and register the DailyProjectDataDevEvent user. 
043        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
044        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
045        client.authenticate();
046        // Send the sensor data to the SensorBase. 
047        client.putSensorDataBatch(batchData);
048        
049        // Now connect to the DPD server. 
050        DailyProjectDataClient dpdClient = new DailyProjectDataClient(getDailyProjectDataHostName(), 
051            user, user);
052        dpdClient.authenticate(); 
053        DevTimeDailyProjectData devTime = dpdClient.getDevTime(user, "Default", 
054            Tstamp.makeTimestamp("2007-04-30"));
055        assertEquals("Checking default devTime", 10, devTime.getTotalDevTime().intValue());
056        assertEquals("Checking MemberData size", 1, devTime.getMemberData().size());
057      }
058      
059      /**
060       * Creates a sample SensorData DevEvent instance given a timestamp and a user. 
061       * @param tstampString The timestamp as a string
062       * @param user The user.
063       * @return The new SensorData DevEvent instance.
064       * @throws Exception If problems occur. 
065       */
066      private SensorData makeDevEvent(String tstampString, String user) throws Exception {
067        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
068        String sdt = "DevEvent";
069        SensorData data = new SensorData();
070        String tool = "Emacs";
071        data.setTool(tool);
072        data.setOwner(user);
073        data.setSensorDataType(sdt);
074        data.setTimestamp(tstamp);
075        data.setResource("file://foo/bar/baz.txt");
076        data.setRuntime(tstamp);
077        Property property = new Property();
078        property.setKey("Type");
079        property.setValue("StateChange");
080        Properties properties = new Properties();
081        properties.getProperty().add(property);
082        data.setProperties(properties);
083        return data;
084      }
085    
086    }