001    package org.hackystat.sensorshell;
002    
003    import static org.junit.Assert.assertEquals;
004    import java.util.HashMap;
005    import java.util.Map;
006    import javax.xml.datatype.XMLGregorianCalendar;
007    import org.hackystat.sensorbase.client.SensorBaseClient;
008    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
009    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
010    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
011    import org.hackystat.sensorbase.server.Server;
012    import org.hackystat.sensorbase.server.ServerProperties;
013    import org.hackystat.utilities.tstamp.Tstamp;
014    import org.junit.AfterClass;
015    import org.junit.BeforeClass;
016    import org.junit.Test;
017    
018    /**
019     * Provides testing classes and a main method for experimenting with MultiSensorShell performance.
020     *
021     * @author Philip Johnson
022     *
023     */
024    public class TestMultiSensorShell {
025      
026      /** The test user. */
027      private static String user = "MultiShell@hackystat.org";
028      private static String host;
029      private static Server server;
030      
031      /**
032       * Starts the server going for these tests, and makes sure our test user is registered. 
033       * @throws Exception If problems occur setting up the server. 
034       */
035      @BeforeClass public static void setupServer() throws Exception {
036        ServerProperties properties = new ServerProperties();
037        properties.setTestProperties();
038        TestMultiSensorShell.server = Server.newInstance(properties);
039        TestMultiSensorShell.host = TestMultiSensorShell.server.getHostName();
040        SensorBaseClient.registerUser(host, user);
041      }
042      
043      /**
044       * Gets rid of the sent sensor data and the user. 
045       * @throws Exception If problems occur setting up the server. 
046       */
047      @AfterClass public static void teardownServer() throws Exception {
048        // Now delete all data sent by this user.
049        SensorBaseClient client = new SensorBaseClient(host, user, user);
050        // First, delete all sensor data sent by this user. 
051        SensorDataIndex index = client.getSensorDataIndex(user);
052        for (SensorDataRef ref : index.getSensorDataRef()) {
053          client.deleteSensorData(user, ref.getTimestamp());
054        }
055        // Now delete the user too.
056        client.deleteUser(user);
057      }
058      
059      /**
060       * Tests that the MultiSensorShell can send data to the Server and this data can be retrieved.
061       * @throws Exception If problems occur. 
062       */
063      @Test public void testMultiSensorShell() throws Exception {
064        // First, start up a SensorShell.
065        SensorShellProperties properties = new SensorShellProperties(host, user, user);
066        // Create a MultiSensorShell with default performance properties. 
067        Shell shell = new MultiSensorShell(properties, "Test");
068        
069        // Now construct a key-val map representing a SensorData instance. 
070        Map<String, String> keyValMap = new HashMap<String, String>();
071        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp();
072        keyValMap.put("Timestamp", tstamp.toString());
073        String tool = "Eclipse";
074        keyValMap.put("Tool", tool);
075        keyValMap.put("SensorDataType", "DevEvent");
076        keyValMap.put("DevEvent-Type", "Compile");
077        
078        // Now add it to the MultiSensorShell and send it to the server. 
079        shell.add(keyValMap);
080        shell.send();
081        shell.ping();
082        shell.quit();
083        
084        // Now retrieve it from the server using a SensorBaseClient.
085        SensorBaseClient client = new SensorBaseClient(host, user, user);
086        // Now, get the data, dude. 
087        SensorData data = client.getSensorData(user, tstamp);
088        assertEquals("Checking data", tool, data.getTool());
089      }
090      
091    }