001    package org.hackystat.dailyprojectdata.resource.build;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertSame;
005    
006    import java.util.List;
007    
008    import javax.xml.datatype.XMLGregorianCalendar;
009    
010    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
011    import org.hackystat.dailyprojectdata.resource.build.jaxb.BuildDailyProjectData;
012    import org.hackystat.dailyprojectdata.resource.build.jaxb.MemberData;
013    import org.hackystat.dailyprojectdata.test.DailyProjectDataTestHelper;
014    import org.hackystat.sensorbase.client.SensorBaseClient;
015    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
016    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
017    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
018    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
019    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
020    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
021    import org.hackystat.utilities.tstamp.Tstamp;
022    import org.junit.Test;
023    
024    /**
025     * Tests the Build portion of the DailyProjectData REST API.
026     * 
027     * @author jsakuda
028     */
029    public class TestBuildRestApi extends DailyProjectDataTestHelper {
030      /** Success result. */
031      private static final String SUCCESS = "Success";
032      /** Failure result. */
033      private static final String FAILURE = "Failure";
034    
035      /** The user for this test case. */
036      private String user = "TestBuild@hackystat.org";
037    
038      /**
039       * Test that GET {host}/build/{user}/Default/{starttime} works properly. First, it creates a
040       * test user and sends some sample Build data to the SensorBase. Then, it invokes the GET
041       * request and checks to see that it obtains the right answer. Finally, it deletes the data
042       * and the user.
043       * 
044       * @throws Exception If problems occur.
045       */
046      @Test
047      public void testGetBuilds() throws Exception {
048        // Create a batch of test build data
049        String runtime = "2007-10-30T02:00:00";
050        SensorDatas batchData = new SensorDatas();
051        batchData.getSensorData().add(
052            makeBuild("2007-10-30T02:00:00", user, runtime, "cruisecontrol", SUCCESS));
053        batchData.getSensorData().add(
054            makeBuild("2007-10-30T02:10:00", user, runtime, "local", FAILURE));
055        batchData.getSensorData().add(
056            makeBuild("2007-10-30T02:15:00", user, runtime, null, SUCCESS));
057        batchData.getSensorData().add(
058            makeBuild("2007-10-30T02:20:00", user, runtime, null, SUCCESS));
059        batchData.getSensorData().add(
060            makeBuild("2007-10-30T02:25:00", user, runtime, "local", FAILURE));
061    
062        // Connect to the sensorbase and register the DailyProjectDataCodeIssue user.
063        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
064        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
065        client.authenticate();
066        // Send the sensor data to the SensorBase.
067        client.putSensorDataBatch(batchData);
068    
069        // Now connect to the DPD server.
070        DailyProjectDataClient dpdClient = new DailyProjectDataClient(
071            getDailyProjectDataHostName(), user, user);
072        dpdClient.authenticate();
073    
074        XMLGregorianCalendar requestTstamp = Tstamp.makeTimestamp("2007-10-30");
075        
076        BuildDailyProjectData build = dpdClient.getBuild(user, "Default", requestTstamp, null);
077        List<MemberData> memberData = build.getMemberData();
078        assertEquals("Should only have 1 member entry.", 1, memberData.size());
079    
080        MemberData data = memberData.get(0);
081        assertSame("Should have 2 failures.", 2, data.getFailure());
082        assertSame("Should have 3 successes.", 3, data.getSuccess());
083        
084        // test Type=*
085        build = dpdClient.getBuild(user, "Default", requestTstamp, "*");
086        memberData = build.getMemberData();
087        assertEquals("Should only have 1 member entry.", 1, memberData.size());
088    
089        data = memberData.get(0);
090        assertSame("Should have 2 failures.", 2, data.getFailure());
091        assertSame("Should have 3 successes.", 3, data.getSuccess());
092        
093        
094        // test Type = "local"
095        build = dpdClient.getBuild(user, "Default", requestTstamp, "local");
096        memberData = build.getMemberData();
097        assertSame("Should only have 1 member entry.", 1, memberData.size());
098    
099        data = memberData.get(0);
100        assertSame("Should have 2 failures.", 2, data.getFailure());
101        assertSame("Should have 0 successes.", 0, data.getSuccess());
102    
103        // First, delete all sensor data sent by this user.
104        SensorDataIndex index = client.getSensorDataIndex(user);
105        for (SensorDataRef ref : index.getSensorDataRef()) {
106          client.deleteSensorData(user, ref.getTimestamp());
107        }
108        // Now delete the user too.
109        client.deleteUser(user);
110      }
111      
112      /**
113       * Creates a sample SensorData Build instance.
114       * 
115       * @param tstampString The timestamp as a string
116       * @param user The user.
117       * @param runtimeString The runtime.
118       * @param type The optional type of the build.
119       * @param result "Success" or "Failure".
120       * @return The new SensorData Build instance.
121       * @throws Exception If problems occur.
122       */
123      public static SensorData makeBuild(String tstampString, String user, String runtimeString,
124          String type, String result) throws Exception {
125        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString);
126        XMLGregorianCalendar runtime = Tstamp.makeTimestamp(runtimeString);
127        SensorData data = new SensorData();
128        data.setSensorDataType("Build");
129        data.setOwner(user);
130        data.setTimestamp(tstamp);
131        data.setTool("Ant");
132        data.setResource("file://foo/bar/baz.txt");
133        data.setRuntime(runtime);
134    
135        data.setProperties(new Properties());
136        Properties properties = data.getProperties();
137    
138        Property resultProperty = new Property();
139        resultProperty.setKey("Result");
140        resultProperty.setValue(result);
141        properties.getProperty().add(resultProperty);
142    
143        if (type != null) {
144          Property property = new Property();
145          property.setKey("Type");
146          property.setValue(type);
147          properties.getProperty().add(property);
148        }
149    
150        return data;
151      }
152    }