001    package org.hackystat.dailyprojectdata.resource.commit;
002    
003    import static org.junit.Assert.assertSame;
004    import static org.junit.Assert.assertEquals;
005    
006    import javax.xml.datatype.XMLGregorianCalendar;
007    
008    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
009    import org.hackystat.dailyprojectdata.resource.commit.jaxb.CommitDailyProjectData;
010    import org.hackystat.dailyprojectdata.resource.commit.jaxb.MemberData;
011    import org.hackystat.dailyprojectdata.test.DailyProjectDataTestHelper;
012    import org.hackystat.sensorbase.client.SensorBaseClient;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
014    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
015    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
016    import org.hackystat.utilities.tstamp.Tstamp;
017    import org.junit.Test;
018    
019    /**
020     * Tests the Commit portion of the DailyProjectData REST API.
021     * 
022     * @author austen
023     */
024    public class TestCommitRestApi extends DailyProjectDataTestHelper {
025      /** Constant for Project. */
026      private static final String PROJECT = "Default";
027      /** The user for this test case. */
028      private String user = "TestCommit@hackystat.org";
029    
030      /**
031       * Test that GET {host}/commit/{user}/default/{starttime} works properly.
032       * First, it creates a test user and sends some sample CodeIssue 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 testGetCommit() throws Exception {
040        // First, create a batch of data.
041        String runtime = "2007-10-30T02:00:00";
042        SensorDatas batchData = new SensorDatas();
043        batchData.getSensorData().add(
044            TestCommitData.createData("2007-10-30T02:10:00", runtime, user, "C:\\foo.java", "10",
045                "20"));
046        batchData.getSensorData().add(
047            TestCommitData.createData("2007-10-30T02:11:00", runtime, user, "C:\\foo2.java", "40",
048                "50"));
049    
050        // Then, create a batch of unrelated data.
051        batchData.getSensorData().add(
052            TestCommitData.createData("2007-11-30T02:15:00", "2007-11-30T02:15:00", user,
053                "C:\\foo3.java", "70", "80"));
054        batchData.getSensorData().add(
055            TestCommitData.createData("2007-11-30T02:16:00", "2007-11-30T02:15:00", user,
056                "C:\\foo4.java", "100", "110"));
057    
058        // Connect to the sensorbase and register the DailyProjectDataCodeIssue
059        // user.
060        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
061        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
062        client.authenticate();
063        // Send the sensor data to the SensorBase.
064        client.putSensorDataBatch(batchData);
065    
066        // Now connect to the DPD server.
067        DailyProjectDataClient dpdClient = new DailyProjectDataClient(
068            getDailyProjectDataHostName(), user, user);
069        dpdClient.authenticate();
070    
071        XMLGregorianCalendar requestTstamp = Tstamp.makeTimestamp("2007-10-30");
072        CommitDailyProjectData commitDpd = dpdClient.getCommit(user, PROJECT, requestTstamp);
073        assertSame("Checking for 1 commit entries", 1, commitDpd.getMemberData().size());
074        MemberData memberData = commitDpd.getMemberData().get(0);
075        assertEquals("The amount of commits is incorrect.", 2, memberData.getCommits());
076        assertEquals("The amount of lines added is incorrect.", 50, memberData.getLinesAdded());
077        assertEquals("The amount of lines deleted is incorrect.", 70, memberData.getLinesDeleted());
078    
079        // Then, delete all sensor data sent by this user.
080        SensorDataIndex index = client.getSensorDataIndex(user);
081        for (SensorDataRef ref : index.getSensorDataRef()) {
082          client.deleteSensorData(user, ref.getTimestamp());
083        }
084        // Now delete the user too.
085        client.deleteUser(user);
086      }
087    }