001    package org.hackystat.dailyprojectdata.resource.complexity;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertTrue;
005    
006    import java.util.HashSet;
007    import java.util.Set;
008    
009    import javax.xml.datatype.XMLGregorianCalendar;
010    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
011    import org.hackystat.dailyprojectdata.resource.complexity.jaxb.ComplexityDailyProjectData;
012    import org.hackystat.dailyprojectdata.resource.complexity.jaxb.FileData;
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.SensorDatas;
019    import org.hackystat.utilities.tstamp.Tstamp;
020    import org.junit.Test;
021    
022    /**
023     * Tests the Complexity DPD.
024     *  
025     * @author Philip Johnson
026     */
027    public class TestComplexityRestApi extends DailyProjectDataTestHelper {
028    
029      /** The user for this test case. */
030      private String user = "TestComplexity@hackystat.org";
031      
032      /** Used to guarantee tstamp uniqueness. */
033      private int counter = 0;
034    
035      /**
036       * Test that GET {host}/complexity/{user}/{project}/{starttime}/{type}?Tool={tool} works properly.
037       * First, it creates a test user and sends some sample FileMetric data to the
038       * SensorBase. Then, it invokes the GET request and checks to see that it
039       * obtains the right answer. Finally, it deletes the data and the user.
040       *
041       * @throws Exception If problems occur.
042       */
043      @Test
044      public void getComplexity() throws Exception {
045        // First, create a batch of DevEvent sensor data.
046        SensorDatas batchData = new SensorDatas();
047        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-04-30T02:00:00");
048        String complexityList1 = "1, 2";
049        String complexityList2 = "3, 4";
050        String totalLines1 = "300";
051        String totalLines2 = "200";
052        batchData.getSensorData().add(makeFileMetric(tstamp, complexityList1, totalLines1));
053        batchData.getSensorData().add(makeFileMetric(tstamp, complexityList2, totalLines2));
054        
055        // Connect to the sensorbase and register the test user.
056        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
057        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
058        client.authenticate();
059        // Send the FileMetric sensor data to the SensorBase.
060        client.putSensorDataBatch(batchData);
061    
062        // Now connect to the DPD server.
063        DailyProjectDataClient dpdClient = new DailyProjectDataClient(getDailyProjectDataHostName(),
064            user, user);
065        dpdClient.authenticate();
066        ComplexityDailyProjectData complexity = 
067          dpdClient.getComplexity(user, "Default", tstamp, "Cyclomatic", "JavaNCSS");
068        assertEquals("Checking two entries returned", 2, complexity.getFileData().size());
069        Set<String> complexitySet = new HashSet<String>();
070        Set<String> totalLinesSet = new HashSet<String>();
071        for (FileData data : complexity.getFileData()) {
072          complexitySet.add(data.getComplexityValues());
073          totalLinesSet.add(data.getTotalLines());
074        }
075        assertTrue("Checking complexity 1", complexitySet.contains(complexityList1));
076        assertTrue("Checking complexity 2", complexitySet.contains(complexityList2));
077        assertTrue("Checking totalLines 1", totalLinesSet.contains(totalLines1));
078        assertTrue("Checking totalLines 2", totalLinesSet.contains(totalLines2));
079      }
080    
081      /**
082       * Creates a sample SensorData FileMetric instance given a timestamp and a list of complexity
083       * values.
084       *
085       * @param tstamp The timestamp, used as the Runtime and auto-incremented for the tstamp.
086       * @param values The list of complexity data values.
087       * @param totalLines The total LOC for this file used to compute the complexity data. 
088       * @return The new SensorData FileMetric instance.
089       * @throws Exception If problems occur.
090       */
091      private SensorData makeFileMetric(XMLGregorianCalendar tstamp, String values, String totalLines) 
092      throws Exception {
093        String sdt = "FileMetric";
094        SensorData data = new SensorData();
095        String tool = "JavaNCSS";
096        data.setTool(tool);
097        data.setOwner(user);
098        data.setSensorDataType(sdt);
099        data.setTimestamp(Tstamp.incrementMinutes(tstamp, counter++));
100        data.setRuntime(tstamp);
101        data.setResource("/users/johnson/Foo-" + counter + ".java");
102        Property property = new Property();
103        property.setKey("CyclomaticComplexityList");
104        property.setValue(values);
105        Property property2 = new Property();
106        property2.setKey("TotalLines");
107        property2.setValue(totalLines);
108        Properties properties = new Properties();
109        properties.getProperty().add(property);
110        properties.getProperty().add(property2);
111        data.setProperties(properties);
112        return data;
113      }
114    
115    }