001 package org.hackystat.sensorbase.resource.sensordata; 002 003 import static org.junit.Assert.assertEquals; 004 import static org.junit.Assert.assertNotNull; 005 import static org.junit.Assert.assertTrue; 006 007 import java.util.Date; 008 import java.util.HashMap; 009 import java.util.Map; 010 011 import javax.xml.datatype.XMLGregorianCalendar; 012 013 import org.hackystat.sensorbase.client.SensorBaseClient; 014 import org.hackystat.sensorbase.client.SensorBaseClientException; 015 import org.hackystat.sensorbase.resource.sensordata.jaxb.Property; 016 import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties; 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.sensorbase.test.SensorBaseRestApiHelper; 022 import org.hackystat.utilities.tstamp.Tstamp; 023 import org.junit.Ignore; 024 import org.junit.Test; 025 026 027 /** 028 * Tests the SensorBase REST API for Sensor Data resources. 029 * @author Philip M. Johnson 030 */ 031 public class TestSensorDataRestApi extends SensorBaseRestApiHelper { 032 033 /** The test user. */ 034 private String user = "TestUser@hackystat.org"; 035 036 /** 037 * Test that GET host/sensorbase/sensordata returns an index containing all Sensor Data. 038 * We now @Ignore this method since it slows down testing so much. 039 * @throws Exception If problems occur. 040 */ 041 @Test @Ignore public void getSensorDataIndex() throws Exception { 042 // Create an admin client and check authentication. 043 SensorBaseClient client = new SensorBaseClient(getHostName(), adminEmail, adminPassword); 044 client.authenticate(); 045 // Get the index of all sensordata. 046 SensorDataIndex index = client.getSensorDataIndex(); 047 // Make sure that we can iterate through the data and dereference all hrefs. 048 for (SensorDataRef ref : index.getSensorDataRef()) { 049 client.getUri(ref.getHref()); 050 } 051 assertNotNull("Checking getLastMod()", index.getLastMod()); 052 assertTrue("Checking for sensor data 1", index.getSensorDataRef().size() > 1); 053 } 054 055 /** 056 * Test that DELETE host/sensorbase/sensordata/user deletes all data. 057 * @throws Exception If problems occur. 058 */ 059 @Test public void deleteAllData() throws Exception { 060 String testDeleteUser = "TestDeleteUser@hackystat.org"; 061 SensorBaseClient.registerUser(getHostName(), testDeleteUser); 062 SensorBaseClient client = new SensorBaseClient(getHostName(), testDeleteUser, testDeleteUser); 063 client.authenticate(); 064 // put some sensor data. 065 // Send a couple of sensor data instances. 066 for (int i = 0; i < 10; i++) { 067 client.putSensorData(makeSensorData(Tstamp.makeTimestamp(), testDeleteUser)); 068 } 069 // Make sure we have the 10 instances. 070 SensorDataIndex index = client.getSensorDataIndex(testDeleteUser); 071 assertTrue("Checking delete data size", index.getSensorDataRef().size() >= 10); 072 // Now delete it. 073 client.deleteSensorData(testDeleteUser); 074 // Now check that it's been deleted. 075 index = client.getSensorDataIndex(testDeleteUser); 076 assertEquals("Checking deleted data gone", 0, index.getSensorDataRef().size()); 077 } 078 079 /** 080 * Tests that caching works. 081 * @throws Exception If problems occur. 082 */ 083 @Test public void testCaching() throws Exception { 084 String testCacheUser = "TestCacheUser@hackystat.org"; 085 SensorBaseClient.registerUser(getHostName(), testCacheUser); 086 SensorBaseClient client = new SensorBaseClient(getHostName(), testCacheUser, testCacheUser); 087 client.authenticate(); 088 // First, let's generate 100 instances on the server. 089 for (int i = 0; i < 50; i++) { 090 client.putSensorData(makeSensorData(Tstamp.makeTimestamp(), testCacheUser)); 091 } 092 // Now get the references to them. 093 SensorDataIndex index = client.getSensorDataIndex(testCacheUser); 094 // Record how long it takes us to retrieve all 100 instances without caching. 095 long start = new Date().getTime(); 096 for (SensorDataRef ref : index.getSensorDataRef()) { 097 client.getSensorData(ref); 098 } 099 long elapsedTimeNoCache = new Date().getTime() - start; 100 // Now enable caching, and retrieve all 100 again to fill the cache. 101 client.enableCaching("TestCaching", "TestCaching", 1D, 25L); 102 for (SensorDataRef ref : index.getSensorDataRef()) { 103 client.getSensorData(ref); 104 } 105 // Now time how long it takes to get them again using the cache. 106 start = new Date().getTime(); 107 for (SensorDataRef ref : index.getSensorDataRef()) { 108 client.getSensorData(ref); 109 } 110 long elapsedTimeCache = new Date().getTime() - start; 111 System.out.println("No cache: " + elapsedTimeNoCache + ", with cache: " + elapsedTimeCache); 112 // Get rid of all of the entries so that the test works next time. 113 client.clearCache(); 114 client.deleteSensorData(testCacheUser); 115 assertTrue("Making sure that caching is faster", elapsedTimeNoCache > elapsedTimeCache); 116 } 117 118 /** 119 * Test that GET host/sensorbase/sensordata/TestUser@hackystat.org returns some sensor data. 120 * @throws Exception If problems occur. 121 */ 122 @Test public void getUserSensorDataIndex() throws Exception { 123 // Create the TestUser client and check authentication. 124 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 125 client.authenticate(); 126 // Retrieve the TestUser User resource and test a couple of fields. 127 SensorDataIndex index = client.getSensorDataIndex(user); 128 assertNotNull("Checking user getLastMod()", index.getLastMod()); 129 // Make sure that we can iterate through the data and dereference all hrefs. 130 for (SensorDataRef ref : index.getSensorDataRef()) { 131 client.getSensorData(ref); 132 // also make sure the Tool value isn't null. 133 assertNotNull("Checking tool value", ref.getTool()); 134 } 135 assertTrue("Checking for sensor data 2", index.getSensorDataRef().size() > 1); 136 } 137 138 139 140 /** 141 * Test that GET host/sensorbase/sensordata/TestUser@hackystat.org?sdt=TestSdt returns data. 142 * @throws Exception If problems occur. 143 */ 144 @Test public void getUserSdtSensorDataIndex() throws Exception { 145 // Create the TestUser client and check authentication. 146 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 147 client.authenticate(); 148 //client.enableHttpTracing(true); 149 //client.setTimeout(50000); 150 // Retrieve the TestUser User resource and test a couple of fields. 151 SensorDataIndex index = client.getSensorDataIndex(user, "TestSdt"); 152 // Make sure that we can iterate through the data and dereference all hrefs. 153 for (SensorDataRef ref : index.getSensorDataRef()) { 154 client.getUri(ref.getHref()); 155 } 156 assertTrue("Checking for sensor data 3", index.getSensorDataRef().size() > 1); 157 } 158 159 /** 160 * Test that GET host/sensorbase/sensordata/TestUser@hackystat.org? 161 * lastModStartTime={tstamp}&lastModEndTime={tstamp} returns data. 162 * @throws Exception If problems occur. 163 */ 164 @Test public void getSensorDataIndexLastMod() throws Exception { 165 // First, find the current time and make a sensordata instance with it. 166 XMLGregorianCalendar startTime = Tstamp.makeTimestamp(); 167 168 // Create the TestUser client and check authentication. 169 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 170 client.authenticate(); 171 // Send a couple of sensor data instances. 172 client.putSensorData(makeSensorData(startTime, user)); 173 XMLGregorianCalendar tstamp2 = Tstamp.makeTimestamp(); 174 client.putSensorData(makeSensorData(tstamp2, user)); 175 XMLGregorianCalendar endTime = Tstamp.makeTimestamp(); 176 177 // Now see that we can retrieve this sensor data using the 'lastMod' parameters. 178 SensorDataIndex index = client.getSensorDataIndexLastMod(user, startTime, endTime); 179 assertEquals("Checking for two items since method started", 2, index.getSensorDataRef().size()); 180 181 // Get rid of these. 182 client.deleteSensorData(user, startTime); 183 client.deleteSensorData(user, tstamp2); 184 } 185 186 187 /** 188 * Test GET host/sensorbase/sensordata/TestUser@hackystat.org/2007-04-30T09:00:00.000 189 * and see that it returns a SensorData instance.. 190 * @throws Exception If problems occur. 191 */ 192 @Test public void getUserSensorData() throws Exception { 193 // Create the TestUser client and check authentication. 194 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 195 client.authenticate(); 196 // Retrieve the TestUser User resource and test a couple of fields. 197 XMLGregorianCalendar timestamp = Tstamp.makeTimestamp("2007-04-30T09:00:00.000"); 198 SensorData data = client.getSensorData(user, timestamp); 199 assertTrue("Checking timestamp 1", Tstamp.equal(timestamp, data.getTimestamp())); 200 201 // Check that the admin can retrieve other people's data. 202 client = new SensorBaseClient(getHostName(), adminEmail, adminPassword); 203 client.authenticate(); 204 data = client.getSensorData(user, timestamp); 205 assertTrue("Checking timestamp 2", Tstamp.equal(timestamp, data.getTimestamp())); 206 } 207 208 209 /** 210 * Test GET host/sensorbase/sensordata/TestUser@hackystat.org/9999-04-30T09:00:00.000 211 * throws a SensorBaseClientException, since the data does not exist. 212 * @throws Exception If problems occur. 213 */ 214 @Test(expected = SensorBaseClientException.class) 215 public void getNonExistingUserSensorData() throws Exception { 216 // Create the TestUser client and check authentication. 217 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 218 client.authenticate(); 219 // Request a non-existing sensordata instance, which should throw SensorBaseClientException. 220 XMLGregorianCalendar timestamp = Tstamp.makeTimestamp("9999-04-30T09:00:00.000"); 221 client.getSensorData(user, timestamp); 222 } 223 224 225 /** 226 * Test that PUT and DELETE of 227 * host/sensorbase/sensordata/TestUser@hackystat.org/2007-04-30T02:00:00.000 works. 228 * @throws Exception If problems occur. 229 */ 230 @Test public void putSensorData() throws Exception { 231 // First, create a sample sensor data instance. 232 XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-04-30T02:00:00.000"); 233 SensorData data = makeSensorData(tstamp, user); 234 235 // Create the TestUser client and check authentication. 236 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 237 client.authenticate(); 238 // Send the sensor data. 239 client.putSensorData(data); 240 241 // Now see that we can retrieve it and check a field for equality. 242 SensorData data2 = client.getSensorData(user, tstamp); 243 assertEquals("Checking data timestamp field", tstamp, data2.getTimestamp()); 244 245 // Test that DELETE gets rid of this sensor data. 246 client.deleteSensorData(user, tstamp); 247 248 // Test that a second DELETE succeeds, even though da buggah is no longer in there. 249 client.deleteSensorData(user, tstamp); 250 } 251 252 /** 253 * Test that a batch PUT of sensor data works. 254 * @throws Exception If problems occur. 255 */ 256 @Test public void putBatchSensorData() throws Exception { 257 // First, create a sample sensor data instance. 258 XMLGregorianCalendar tstamp1 = Tstamp.makeTimestamp("2007-04-30T02:00:00.123"); 259 XMLGregorianCalendar tstamp2 = Tstamp.makeTimestamp("2007-04-30T02:00:00.124"); 260 SensorData data1 = makeSensorData(tstamp1, user); 261 SensorData data2 = makeSensorData(tstamp2, user); 262 263 SensorDatas batchData = new SensorDatas(); 264 batchData.getSensorData().add(data1); 265 batchData.getSensorData().add(data2); 266 267 // Create the TestUser client and check authentication. 268 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 269 client.authenticate(); 270 // Send the sensor data. 271 client.putSensorDataBatch(batchData); 272 273 // Now see that we can retrieve it and check a field for equality. 274 SensorData data3 = client.getSensorData(user, tstamp1); 275 assertEquals("Checking data timestamp field", tstamp1, data3.getTimestamp()); 276 277 // Delete this sensor data. 278 client.deleteSensorData(user, tstamp1); 279 client.deleteSensorData(user, tstamp2); 280 } 281 282 /** 283 * Creates a sample SensorData instance given a timestamp and a user. 284 * @param tstamp The timestamp. 285 * @param user The user. 286 * @return The new SensorData instance. 287 */ 288 private SensorData makeSensorData(XMLGregorianCalendar tstamp, String user) { 289 String sdt = "TestSdt"; 290 SensorData data = new SensorData(); 291 String tool = "Subversion"; 292 data.setTool(tool); 293 data.setOwner(user); 294 data.setSensorDataType(sdt); 295 data.setTimestamp(tstamp); 296 data.setResource("file://foo/bar/baz.txt"); 297 data.setRuntime(tstamp); 298 Property property = new Property(); 299 property.setKey("SampleField"); 300 property.setValue("The test value for Sample Field"); 301 Properties properties = new Properties(); 302 properties.getProperty().add(property); 303 data.setProperties(properties); 304 return data; 305 } 306 307 /** 308 * Tests the makeSensorData method. 309 * @throws Exception If problems occur. 310 */ 311 @Test 312 public void testMakeSensorData() throws Exception { 313 Map<String, String> keyValMap = new HashMap<String, String>(); 314 // Create the TestUser client. 315 SensorBaseClient client = new SensorBaseClient(getHostName(), user, user); 316 // See that we can create a SensorData instance with all defaults. 317 client.makeSensorData(keyValMap); 318 // Add a couple of fields and make a new one. 319 String tool = "Eclipse"; 320 keyValMap.put("Tool", tool); 321 keyValMap.put("MyProperty", "foobar"); 322 SensorData data = client.makeSensorData(keyValMap); 323 assertEquals("Checking sensor data val", tool, data.getTool()); 324 } 325 }