001 package org.hackystat.telemetry.service.resource.chart; 002 003 import static org.junit.Assert.assertEquals; 004 import static org.junit.Assert.assertNull; 005 006 import java.util.List; 007 import javax.xml.datatype.XMLGregorianCalendar; 008 import org.hackystat.sensorbase.client.SensorBaseClient; 009 import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties; 010 import org.hackystat.sensorbase.resource.sensordata.jaxb.Property; 011 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData; 012 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas; 013 import org.hackystat.telemetry.service.client.TelemetryClient; 014 import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryChartData; 015 import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryPoint; 016 import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryStream; 017 import org.hackystat.telemetry.service.test.TelemetryTestHelper; 018 import org.hackystat.utilities.tstamp.Tstamp; 019 import org.junit.Test; 020 import org.junit.Before; 021 022 /** 023 * Tests the FileMetric Chart processing. 024 * @author Philip Johnson 025 */ 026 public class TestFileMetricChartRestApi extends TelemetryTestHelper { 027 028 /** The user for this test case. */ 029 private String user = "TestChart@hackystat.org"; 030 031 /** The telemetry client. */ 032 private TelemetryClient telemetryClient; 033 034 /** A counter so that each resource (file name) is unique in the sample data. */ 035 int counter = 0; 036 037 /** 038 * Creates FileMetrics on server for use in Telemetry processing. 039 * @throws Exception If problems occur. 040 */ 041 @Before 042 public void generateData() throws Exception { 043 // [1] First, create a batch of sensor data for Day 1. 044 SensorDatas batchData = new SensorDatas(); 045 String tstampString = "2007-08-01T02:00:00"; 046 XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(tstampString); 047 // Create four instances with unique tstamps and the same runtime. 048 batchData.getSensorData().add(makeData(tstamp, 100)); 049 batchData.getSensorData().add(makeData(tstamp, 200)); 050 051 // Connect to the sensorbase and register the user. 052 SensorBaseClient.registerUser(getSensorBaseHostName(), user); 053 SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user); 054 client.authenticate(); 055 // Send the sensor data to the SensorBase. 056 client.putSensorDataBatch(batchData); 057 058 // Now connect to the Telemetry server. 059 this.telemetryClient = new TelemetryClient(getTelemetryHostName(), user, user); 060 telemetryClient.authenticate(); 061 } 062 063 064 /** 065 * Tests the FileMetric chart. 066 * @throws Exception If problems occur. 067 */ 068 @Test public void testFileMetricChart() throws Exception { 069 String chartName = "FileMetric"; 070 String params = "TotalLines,*"; 071 TelemetryChartData chart = telemetryClient.getChart(chartName, user, "Default", "Day", 072 Tstamp.makeTimestamp("2007-08-01"), Tstamp.makeTimestamp("2007-08-04"), params); 073 // See if this chart contains 1 stream. 074 List<TelemetryStream> streams = chart.getTelemetryStream(); 075 assertEquals("Checking only 1 stream returned", 1, streams.size()); 076 // Get the data points in the single returned stream. 077 List<TelemetryPoint> points = streams.get(0).getTelemetryPoint(); 078 assertEquals("Checking for 4 points", 4, points.size()); 079 // Check that these four points are 0 and null (only first day has value). 080 assertEquals("Checking point 1 is 300.0", "300.0", points.get(0).getValue()); 081 assertNull("Checking point 2 is null", points.get(1).getValue()); 082 } 083 084 085 086 /** 087 * Creates a sample SensorData FileMetric instance given a timestamp and a size. 088 * Only the "TotalLines" size metric is saved. 089 * @param tstamp The timestamp, incremented each time to guarantee uniqueness. 090 * @param size The size to create. 091 * @return The new SensorData instance. 092 * @throws Exception If problems occur. 093 */ 094 private SensorData makeData(XMLGregorianCalendar tstamp, int size) throws Exception { 095 String sdt = "FileMetric"; 096 SensorData data = new SensorData(); 097 String tool = "SCLC"; 098 data.setTool(tool); 099 data.setOwner(user); 100 data.setSensorDataType(sdt); 101 data.setTimestamp(Tstamp.incrementMinutes(tstamp, counter++)); 102 data.setRuntime(tstamp); 103 data.setResource("/users/johnson/Foo-" + counter + ".java"); 104 Properties properties = new Properties(); 105 properties.getProperty().add(makeProperty("TotalLines", String.valueOf(size))); 106 data.setProperties(properties); 107 return data; 108 } 109 110 /** 111 * Creates and returns a Property initialized with key and value. 112 * @param key The key. 113 * @param value The value. 114 * @return The Property instance. 115 */ 116 private Property makeProperty(String key, String value) { 117 Property property = new Property(); 118 property.setKey(key); 119 property.setValue(value); 120 return property; 121 } 122 }