001    package org.hackystat.dailyprojectdata.resource.coupling;
002    
003    import static org.junit.Assert.assertEquals;
004    import javax.xml.datatype.XMLGregorianCalendar;
005    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
006    import org.hackystat.dailyprojectdata.resource.coupling.jaxb.CouplingDailyProjectData;
007    import org.hackystat.dailyprojectdata.test.DailyProjectDataTestHelper;
008    import org.hackystat.sensorbase.client.SensorBaseClient;
009    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
010    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
011    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
012    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
013    import org.hackystat.utilities.tstamp.Tstamp;
014    import org.junit.Test;
015    
016    /**
017     * Tests the Coupling DPD.
018     *  
019     * @author Philip Johnson
020     */
021    public class TestCouplingRestApi extends DailyProjectDataTestHelper {
022    
023      /** The user for this test case. */
024      private String user = "TestCoupling@hackystat.org";
025      
026      /** Used to guarantee tstamp uniqueness. */
027      private int counter = 0;
028    
029      /**
030       * Test that GET {host}/coupling/{user}/{project}/{starttime}/{type}?Tool={tool} works properly.
031       * First, it creates a test user and sends some sample Coupling 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 getCoupling() throws Exception {
039        // First, create a batch of Coupling sensor data.
040        SensorDatas batchData = new SensorDatas();
041        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-04-30T02:00:00");
042        String afferent1 = "1";
043        String efferent1 = "2";
044        String afferent2 = "1";
045        String efferent2 = "2";
046        batchData.getSensorData().add(makeCoupling(tstamp, afferent1, efferent1));
047        batchData.getSensorData().add(makeCoupling(tstamp, afferent2, efferent2));
048        
049        // Connect to the sensorbase and register the test user.
050        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
051        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
052        client.authenticate();
053        // Send the sensor data to the SensorBase.
054        client.putSensorDataBatch(batchData);
055    
056        // Now connect to the DPD server.
057        DailyProjectDataClient dpdClient = new DailyProjectDataClient(getDailyProjectDataHostName(),
058            user, user);
059        dpdClient.authenticate();
060        CouplingDailyProjectData coupling = 
061          dpdClient.getCoupling(user, "Default", tstamp, "class", "DependencyFinder");
062        assertEquals("Checking two entries returned", 2, coupling.getCouplingData().size());
063        assertEquals("Checking 1", 1, coupling.getCouplingData().get(0).getAfferent().intValue());
064        assertEquals("Checking 2", 2, coupling.getCouplingData().get(0).getEfferent().intValue());
065        assertEquals("Checking 2.1", 1, coupling.getCouplingData().get(1).getAfferent().intValue());
066        assertEquals("Checking 2.2", 2, coupling.getCouplingData().get(1).getEfferent().intValue());
067      }
068    
069      /**
070       * Creates a sample SensorData FileMetric instance given a timestamp and a list of coupling
071       * values.
072       *
073       * @param tstamp The timestamp, used as the Runtime and auto-incremented for the tstamp.
074       * @param afferent The afferent coupling value.
075       * @param efferent The efferent coupling value.
076       * @return The new SensorData Coupling instance.
077       * @throws Exception If problems occur.
078       */
079      private SensorData makeCoupling(XMLGregorianCalendar tstamp, String afferent, String efferent) 
080      throws Exception {
081        String sdt = "Coupling";
082        SensorData data = new SensorData();
083        String tool = "DependencyFinder";
084        data.setTool(tool);
085        data.setOwner(user);
086        data.setSensorDataType(sdt);
087        data.setTimestamp(Tstamp.incrementMinutes(tstamp, counter++));
088        data.setRuntime(tstamp);
089        data.setResource("/users/johnson/Foo-" + counter + ".java");
090        addProperty(data, "Afferent", afferent);
091        addProperty(data, "Efferent", efferent);
092        addProperty(data, "Type", "class");
093        return data;
094      }
095      
096      /**
097       * Updates the sensor data instance with the new property.
098       * @param data The sensor data instance. 
099       * @param key The new key.
100       * @param value The new value. 
101       */
102      private void addProperty(SensorData data, String key, String value) {
103        if (data.getProperties() == null) {
104          data.setProperties(new Properties());
105        }
106        Properties properties = data.getProperties();
107        Property property = new Property();
108        property.setKey(key);
109        property.setValue(value);
110        properties.getProperty().add(property);
111      }
112    
113    }