001    package org.hackystat.sensorshell;
002    
003    import static org.junit.Assert.assertEquals;
004    
005    import java.util.HashMap;
006    import java.util.Map;
007    
008    import javax.xml.datatype.XMLGregorianCalendar;
009    
010    import org.hackystat.sensorbase.client.SensorBaseClient;
011    import org.hackystat.utilities.tstamp.Tstamp;
012    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
014    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
015    import org.hackystat.sensorbase.server.Server;
016    import org.hackystat.sensorbase.server.ServerProperties;
017    import org.junit.AfterClass;
018    import org.junit.BeforeClass;
019    import org.junit.Test;
020    
021    /**
022     * Provides simple unit tests for the SensorShell.
023     * @author Philip Johnson
024     */
025    public class TestSingleSensorShell {
026      
027      /** The test user. */
028      private static String user = "TestShellUser@hackystat.org";
029      private static String host;
030      private static Server server;
031      
032      /**
033       * Starts the server going for these tests, and makes sure our test user is registered. 
034       * @throws Exception If problems occur setting up the server. 
035       */
036      @BeforeClass public static void setupServer() throws Exception {
037        ServerProperties properties = new ServerProperties();
038        properties.setTestProperties();
039        TestSingleSensorShell.server = Server.newInstance(properties);
040        TestSingleSensorShell.host = TestSingleSensorShell.server.getHostName();
041        SensorBaseClient.registerUser(host, user);
042      }
043      
044      /**
045       * Gets rid of the sent sensor data and the user. 
046       * @throws Exception If problems occur setting up the server. 
047       */
048      @AfterClass public static void teardownServer() throws Exception {
049        // Now delete all data sent by this user.
050        SensorBaseClient client = new SensorBaseClient(host, user, user);
051        // First, delete all sensor data sent by this user. 
052        SensorDataIndex index = client.getSensorDataIndex(user);
053        for (SensorDataRef ref : index.getSensorDataRef()) {
054          client.deleteSensorData(user, ref.getTimestamp());
055        }
056        // Now delete the user too.
057        client.deleteUser(user);
058      }
059      
060      /**
061       * Tests that the SensorShell can send data to the Server and this data can be retrieved.
062       * @throws Exception If problems occur. 
063       */
064      @Test public void testSensorShell() throws Exception {
065        // First, create a "test" SensorShellProperties with disabled caching and logging.
066        SensorShellProperties properties = SensorShellProperties.getTestInstance(host, user, user);
067        // Create a SensorShell that is non-interactive, logging as "Test".
068        SingleSensorShell shell = new SingleSensorShell(properties, false, "Test");
069        
070        // Now construct a key-val map representing a SensorData instance. 
071        Map<String, String> keyValMap = new HashMap<String, String>();
072        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp();
073        keyValMap.put("Timestamp", tstamp.toString());
074        String tool = "Eclipse";
075        keyValMap.put("Tool", tool);
076        keyValMap.put("SensorDataType", "DevEvent");
077        keyValMap.put("DevEvent-Type", "Compile");
078        
079        // Now add it to the SensorShell and send it to the server. 
080        shell.add(keyValMap);
081        int numSent = shell.send();
082        assertEquals("Checking numSent", 1, numSent);
083        shell.ping();
084        shell.quit();
085        
086        // Now retrieve it from the server using a SensorBaseClient.
087        SensorBaseClient client = new SensorBaseClient(host, user, user);
088        // Now, get the data, dude. 
089        SensorData data = client.getSensorData(user, tstamp);
090        assertEquals("Checking data", tool, data.getTool());
091      }
092      
093      /**
094       * Tests that statechange works correctly. 
095       * @throws Exception If problems occur.
096       */
097      @Test public void testStateChange () throws Exception {
098        // First, create a "test" SensorShellProperties.
099        SensorShellProperties properties = SensorShellProperties.getTestInstance(host, user, user);
100        // Create a SensorShell that is non-interactive, logging as "Test".
101        SingleSensorShell shell = new SingleSensorShell(properties, true, "Test");
102        
103        // Now construct a key-val map representing a SensorData instance. 
104        Map<String, String> keyValMap = new HashMap<String, String>();
105        XMLGregorianCalendar tstamp = Tstamp.makeTimestamp();
106        keyValMap.put("Timestamp", tstamp.toString());
107        String tool = "Eclipse";
108        keyValMap.put("Tool", tool);
109        keyValMap.put("SensorDataType", "DevEvent");
110        keyValMap.put("DevEvent-Type", "Compile");
111        keyValMap.put("Resource", "file://foo.java");
112        
113        // Now test to see that StateChange works correctly.
114        shell.statechange(100L, keyValMap);  // should create an add.
115        shell.statechange(100L, keyValMap);  // should not create an add.
116        shell.statechange(200L, keyValMap); // should create an add.
117        keyValMap.put("Resource", "file://bar.java");
118        shell.statechange(200L, keyValMap); // should create an add.
119        int numSensorData = shell.send();
120        assertEquals("Checking numSensorData", 3, numSensorData);
121      }
122    
123    }