001    package org.hackystat.dailyprojectdata.resource.coverage;
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.coverage.jaxb.CoverageDailyProjectData;
009    import org.hackystat.dailyprojectdata.test.DailyProjectDataTestHelper;
010    import org.hackystat.sensorbase.client.SensorBaseClient;
011    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
012    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
014    import org.hackystat.utilities.tstamp.Tstamp;
015    import org.junit.Test;
016    
017    /**
018     * Tests the Coverage portion of the DailyProjectData REST API.
019     * 
020     * @author jsakuda
021     * @author austen
022     */
023    public class TestCoverageRestApi extends DailyProjectDataTestHelper {
024      /** Constant for Project. */
025      private static final String PROJECT = "Default";
026      /** The user for this test case. */
027      private String user = "TestCoverageDpd@hackystat.org";
028    
029      /**
030       * Test that GET {host}/coverage/{user}/Default/{starttime} works properly.
031       * First, it creates a test user and sends some sample Coverage data to the
032       * SensorBase. Then, it invokes the GET request and checks to see that it
033       * obtains the right answer. Finally, it deletes the data and the user.
034       * 
035       * @throws Exception If problems occur.
036       */
037      @Test
038      public void testGetCoverage() throws Exception {
039        // First, create a batch of Coverage sensor data.
040        String runtime = "2007-10-30T02:00:00";
041        SensorDatas batchData = new SensorDatas();
042        batchData.getSensorData().add(
043            TestCoverageData.createData("2007-10-30T02:00:00", runtime, user, "file://Foo.java"));
044        batchData.getSensorData().add(
045            TestCoverageData.createData("2007-10-30T02:10:00", runtime, user, "file://Foo2.java"));
046        batchData.getSensorData().add(
047            TestCoverageData.createData("2007-10-30T02:15:00", runtime, user, "file://Foo3.java"));
048        batchData.getSensorData().add(
049            TestCoverageData.createData("2007-10-30T02:20:00", runtime, user, "file://Foo4.java"));
050    
051        // Connect to the sensorbase and register the DailyProjectDataCodeIssue
052        // user.
053        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
054        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
055        client.authenticate();
056        // Send the sensor data to the SensorBase.
057        client.putSensorDataBatch(batchData);
058    
059        // Now connect to the DPD server.
060        DailyProjectDataClient dpdClient = new DailyProjectDataClient(
061            getDailyProjectDataHostName(), user, user);
062        dpdClient.authenticate();
063    
064        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-10-30");
065        CoverageDailyProjectData lineCoverage = dpdClient.getCoverage(user, PROJECT, tstamp, "line");
066        assertEquals("Checking 'line' granularity.", 4, lineCoverage.getConstructData().size());
067    
068        CoverageDailyProjectData classCoverage = dpdClient.getCoverage(user, PROJECT, tstamp, "class");
069        assertEquals("Checking 'class' granularity.", 4, classCoverage.getConstructData().size());
070        
071        // Now see that getting data for a day in which no data exists works OK.
072        XMLGregorianCalendar emptyDay = Tstamp.incrementDays(tstamp, -1);
073        CoverageDailyProjectData noCoverage = dpdClient.getCoverage(user, PROJECT, emptyDay, "class");
074        assertEquals("Checking empty day.", 0, noCoverage.getConstructData().size());
075    
076    
077        // Then, delete all sensor data sent by this user.
078        SensorDataIndex index = client.getSensorDataIndex(user);
079        for (SensorDataRef ref : index.getSensorDataRef()) {
080          client.deleteSensorData(user, ref.getTimestamp());
081        }
082        // Now delete the user too.
083        client.deleteUser(user);
084      }
085    }