001 package org.hackystat.dailyprojectdata.resource.snapshot; 002 003 import static org.junit.Assert.assertEquals; 004 005 import java.util.Iterator; 006 007 import javax.xml.datatype.XMLGregorianCalendar; 008 009 import org.hackystat.sensorbase.client.SensorBaseClient; 010 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData; 011 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas; 012 import org.hackystat.sensorbase.server.Server; 013 import org.hackystat.sensorbase.server.ServerProperties; 014 import org.hackystat.utilities.time.period.Day; 015 import org.hackystat.utilities.tstamp.Tstamp; 016 import org.junit.AfterClass; 017 import org.junit.BeforeClass; 018 import org.junit.Test; 019 020 /** 021 * Tests the functionality of the <code>SensorDataSnapshot</code>. 022 * 023 * @author jsakuda 024 */ 025 public class TestSensorDataSnapshot { 026 027 private static final String CODE_ISSUE = "CodeIssue"; 028 private static final String DEFAULT = "Default"; 029 private static final String CHECKSTYLE = "Checkstyle"; 030 private static final String PMD = "PMD"; 031 032 /** The test user. */ 033 private static String user = "TestSnapshot@hackystat.org"; 034 private static String host; 035 private static Server server; 036 037 /** 038 * Starts the server, creates the user, and sends the data. 039 * 040 * @throws Exception If problems occur setting up the server. 041 */ 042 @BeforeClass 043 public static void setupServer() throws Exception { 044 ServerProperties properties = new ServerProperties(); 045 properties.setTestProperties(); 046 TestSensorDataSnapshot.server = Server.newInstance(properties); 047 TestSensorDataSnapshot.host = TestSensorDataSnapshot.server.getHostName(); 048 SensorBaseClient.registerUser(host, user); 049 addSensorData(); 050 } 051 052 /** 053 * Deletes the sensordata and the user. 054 * 055 * @throws Exception If problems occur setting up the server. 056 */ 057 @AfterClass 058 public static void teardownServer() throws Exception { 059 SensorBaseClient client = new SensorBaseClient(host, user, user); 060 client.deleteSensorData(user); 061 client.deleteUser(user); 062 } 063 064 /** 065 * Tests the getting of the last snapshot of the day. 066 * 067 * @throws Exception Thrown if there is a failure to communicate with the sensorbase. 068 */ 069 @Test 070 public void testSnapshot() throws Exception { 071 SensorBaseClient client = new SensorBaseClient(host, user, user); 072 Day day = Day.getInstance("30-Oct-2007"); 073 SensorDataSnapshot snapshot = new SensorDataSnapshot(client, user, DEFAULT, CODE_ISSUE, 074 day, 30); 075 Iterator<SensorData> iterator = snapshot.iterator(); 076 int dataCount = 0; 077 while (iterator.hasNext()) { 078 SensorData sensorData = iterator.next(); 079 assertEquals("Checking tool.", CHECKSTYLE, sensorData.getTool()); 080 dataCount++; 081 } 082 assertEquals("Should have 3 data entries.", 3, dataCount); 083 } 084 085 /** 086 * Test the getting of the last snapshot of the day for a particular tool. 087 * 088 * @throws Exception Thrown if there is a failure communicating with the sensorbase. 089 */ 090 @Test 091 public void testSnapshotWithTool() throws Exception { 092 093 SensorBaseClient client = new SensorBaseClient(host, user, user); 094 Day day = Day.getInstance("30-Oct-2007"); 095 096 SensorDataSnapshot snapshot = new SensorDataSnapshot(client, user, DEFAULT, CODE_ISSUE, 097 day, 30, CHECKSTYLE); 098 099 int dataCount = 0; 100 for (SensorData sensorData : snapshot) { 101 assertEquals("Checking tool.", CHECKSTYLE, sensorData.getTool()); 102 dataCount++; 103 } 104 assertEquals("Should have 3 data entries.", 3, dataCount); 105 106 snapshot = new SensorDataSnapshot(client, user, DEFAULT, CODE_ISSUE, day, 30, PMD); 107 dataCount = 0; 108 for (SensorData sensorData : snapshot) { 109 assertEquals("Checking tool.", PMD, sensorData.getTool()); 110 dataCount++; 111 } 112 assertEquals("Should have 2 data entries.", 2, dataCount); 113 } 114 115 /** 116 * Tests that the number of buckets retrieved during snapshot creation is correct. 117 * 118 * @throws Exception Thrown if there is an error creating a snapshot. 119 */ 120 @Test 121 public void testSnapshotBucketSize() throws Exception { 122 SensorBaseClient client = new SensorBaseClient(host, user, user); 123 client.authenticate(); 124 125 // add additional test data 126 SensorDatas batchData = new SensorDatas(); 127 String runtime = "2007-10-30T22:00:00"; 128 String runtime2 = "2007-10-30T10:00:00"; 129 batchData.getSensorData().add( 130 makeSensorData("2007-10-30T22:00:00", user, runtime2, CHECKSTYLE)); 131 batchData.getSensorData().add( 132 makeSensorData("2007-10-30T22:10:00", user, runtime, CHECKSTYLE)); 133 batchData.getSensorData().add( 134 makeSensorData("2007-10-30T23:00:00", user, runtime, CHECKSTYLE)); 135 batchData.getSensorData().add( 136 makeSensorData("2007-10-30T23:15:00", user, runtime, CHECKSTYLE)); 137 batchData.getSensorData().add( 138 makeSensorData("2007-10-30T23:30:00", user, runtime, CHECKSTYLE)); 139 batchData.getSensorData().add( 140 makeSensorData("2007-10-30T23:45:00", user, runtime, CHECKSTYLE)); 141 142 client.putSensorDataBatch(batchData); 143 144 Day day = Day.getInstance("30-Oct-2007"); 145 146 // test 30 minute bucket 147 SensorDataSnapshot snapshot = new SensorDataSnapshot(client, user, DEFAULT, CODE_ISSUE, 148 day, 30); 149 assertEquals("Bucket count should be 4.", 4, snapshot.getNumberOfBucketsRetrieved()); 150 151 // test 45 minute bucket 152 snapshot = new SensorDataSnapshot(client, user, DEFAULT, CODE_ISSUE, day, 45); 153 assertEquals("Bucket count should be 3.", 3, snapshot.getNumberOfBucketsRetrieved()); 154 155 // test 2 hour bucket 156 snapshot = new SensorDataSnapshot(client, user, DEFAULT, CODE_ISSUE, day, 120); 157 assertEquals("Bucket count should be 1.", 1, snapshot.getNumberOfBucketsRetrieved()); 158 159 //client.deleteSensorData(user); 160 } 161 162 /** 163 * Adds test sensor data to the sensor base for testing snapshots. 164 * 165 * @throws Exception Thrown if there are errors with the sensorbase. 166 */ 167 private static void addSensorData() throws Exception { 168 SensorBaseClient client = new SensorBaseClient(host, user, user); 169 client.authenticate(); 170 171 String runtime = "2007-10-30T02:00:00"; 172 SensorDatas batchData = new SensorDatas(); 173 batchData.getSensorData().add(makeSensorData("2007-10-30T02:00:00", user, runtime, PMD)); 174 batchData.getSensorData().add(makeSensorData("2007-10-30T02:05:00", user, runtime, PMD)); 175 batchData.getSensorData().add(makeSensorData("2007-10-30T02:10:00", user, runtime, PMD)); 176 batchData.getSensorData().add(makeSensorData("2007-10-30T02:15:00", user, runtime, PMD)); 177 178 client.putSensorDataBatch(batchData); 179 180 String runtime2 = "2007-10-30T04:00:00"; 181 batchData = new SensorDatas(); 182 batchData.getSensorData().add( 183 makeSensorData("2007-10-30T04:00:00", user, runtime2, CHECKSTYLE)); 184 batchData.getSensorData().add( 185 makeSensorData("2007-10-30T04:15:00", user, runtime2, CHECKSTYLE)); 186 batchData.getSensorData().add( 187 makeSensorData("2007-10-30T04:35:00", user, runtime2, CHECKSTYLE)); 188 batchData.getSensorData().add( 189 makeSensorData("2007-10-30T04:45:00", user, runtime2, CHECKSTYLE)); 190 batchData.getSensorData().add( 191 makeSensorData("2007-10-30T04:50:00", user, runtime2, CHECKSTYLE)); 192 193 client.putSensorDataBatch(batchData); 194 195 String runtime3 = "2007-10-30T06:00:00"; 196 batchData = new SensorDatas(); 197 batchData.getSensorData().add(makeSensorData("2007-10-30T06:00:00", user, runtime3, PMD)); 198 batchData.getSensorData().add(makeSensorData("2007-10-30T06:05:00", user, runtime3, PMD)); 199 200 String runtime4 = "2007-10-30T08:00:00"; 201 batchData.getSensorData().add( 202 makeSensorData("2007-10-30T08:00:00", user, runtime4, CHECKSTYLE)); 203 batchData.getSensorData().add( 204 makeSensorData("2007-10-30T08:10:00", user, runtime4, CHECKSTYLE)); 205 batchData.getSensorData().add( 206 makeSensorData("2007-10-30T08:20:00", user, runtime4, CHECKSTYLE)); 207 208 client.putSensorDataBatch(batchData); 209 } 210 211 /** 212 * Makes a fake sensor data to be used for testing. 213 * 214 * @param tstampString The timestamp of the data. 215 * @param user The user for the data. 216 * @param runtimeString The batch runtime. 217 * @param tool The tool for the data. 218 * @return Returns the new sensor data instance. 219 * @throws Exception Thrown if errors occur. 220 */ 221 private static SensorData makeSensorData(String tstampString, String user, 222 String runtimeString, String tool) throws Exception { 223 XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString); 224 XMLGregorianCalendar runtime = Tstamp.makeTimestamp(runtimeString); 225 SensorData data = new SensorData(); 226 data.setSensorDataType(CODE_ISSUE); 227 data.setOwner(user); 228 data.setTimestamp(tstamp); 229 data.setTool(tool); 230 data.setResource("file://foo/bar/baz.txt"); 231 data.setRuntime(runtime); 232 233 return data; 234 } 235 }