001    package org.hackystat.sensor.ant.issue;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertTrue;
005    import static org.junit.Assert.fail;
006    import java.util.Arrays;
007    import java.util.List;
008    import org.apache.tools.ant.BuildException;
009    import org.hackystat.sensor.ant.test.AntSensorTestHelper;
010    import org.hackystat.sensorbase.client.SensorBaseClient;
011    import org.hackystat.sensorbase.client.SensorBaseClientException;
012    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
014    import org.hackystat.sensorshell.usermap.SensorShellMapException;
015    import org.junit.Test;
016    
017    /**
018     * Test cases for Issue Sensor.
019     * @author Shaoxuan
020     *
021     */
022    public class TestIssueSensor extends AntSensorTestHelper {
023    
024      private static final String NEW = "New";
025      private static final String STARTED = "Started";
026      private static final String ACCEPTED = "Accepted";
027      private static final String FIXED = "Fixed";
028      private static final String DEFECT = "Defect";
029      private static final String TASK = "Task";
030      private static final String ENHANCEMENT = "Enhancement";
031      private static final String MEDIUM = "Medium";
032      private static final String HIGH = "High";
033      private static final String CRITICAL = "Critical";
034      
035      /**
036       * Test the issue sensor with public Google Project Hosting service.
037       * It will get data from project http://code.google.com/feeds/p/hackystat-sensor-ant.
038       * @throws SensorBaseClientException if test fail.
039       * @throws SensorShellMapException if error when loading sensorshell map
040       */
041      //@Ignore("Broken on 2/12/2009. Also renamed method to prevent Ant-based invocation")
042      @Test
043      public void testIssueSensorWithGoogleProjectHosting() 
044        throws SensorBaseClientException, SensorShellMapException {
045        IssueSensor sensor = new IssueSensor();
046        sensor.setProjectName("hackystat-sensor-ant");
047        // As there is no shell map for mapping account, all data will be sensor under the name
048        // of TestAntSensors@hackystat.org.
049        sensor.setDataOwnerHackystatAccount(user);
050        sensor.setDataOwnerHackystatPassword(user);
051        sensor.setDefaultHackystatSensorbase(host);
052        sensor.setVerbose(true);
053    
054        System.out.println("Testing " + user + " in " + host);
055        
056        try {
057          SensorBaseClient client = new SensorBaseClient(host, user, user);
058          client.deleteSensorData(user);
059          sensor.execute();
060          int issueSize = client.getSensorDataIndex(user, IssueSensor.ISSUE_SENSOR_DATA_TYPE)
061                                .getSensorDataRef().size();
062          assertTrue("Should be at least 40 issues there.", issueSize > 40);
063          assertEquals("All issue should be found new.", issueSize, sensor.updatedIssues.size());
064          
065          sensor.execute();
066          assertEquals("Number of issue extract should be the same.", issueSize, 
067              client.getSensorDataIndex(user, IssueSensor.ISSUE_SENSOR_DATA_TYPE)
068              .getSensorDataRef().size());
069          assertEquals("Now none issue should be found new.", 0, sensor.updatedIssues.size());
070          System.out.println("Testing Issue Sensor.");
071        }
072        catch (BuildException e) {
073          System.out.println(e.getMessage());
074          if (e.getMessage().contains("IO error")) {
075            System.out.println("Test not run because of IO exceptions.");
076          }
077          fail();
078          //e.printStackTrace();
079          return;
080        }
081      }
082      
083      /**
084       * Test inner logic with makeup test data, in form of String arrays.
085       * @throws SensorShellMapException if error when loading sensorshell map
086       */
087      @Test
088      public void testIssueSensorWithMakeupTestData() throws SensorShellMapException {
089        IssueSensor sensor = new IssueSensor();
090        sensor.setProjectName("hackystat-sensor-ant");
091        // As there is no shell map for mapping account, all data will be sensor under the name
092        // of TestAntSensors@hackystat.org.
093        sensor.setDataOwnerHackystatAccount(user);
094        sensor.setDataOwnerHackystatPassword(user);
095        sensor.setDefaultHackystatSensorbase(host);
096        sensor.setVerbose(true);
097    
098        SensorBaseClient client = new SensorBaseClient(host, user, user);
099        try {
100          client.deleteSensorData(user);
101        }
102        catch (SensorBaseClientException e) {
103          e.printStackTrace();
104        }
105        
106        System.out.println("Testing " + user + " in " + host);
107        
108    
109        String tester1 = "issueSensorTester1@hackystat.org";
110        String tester2 = "issueSensorTester2@hackystat.org";
111    
112        String ms1 = "8.3";
113        String ms2 = "8.4";
114        
115        //"MMM dd, yyyy kk:mm:ss"
116        List<String[]> testIssueTableContents1 = Arrays.asList(
117            new String[]{"21", DEFECT, ACCEPTED, MEDIUM, ms1, tester1, "Sep 07, 2008 11:00:00"}, 
118            new String[]{"23", ENHANCEMENT, ACCEPTED, MEDIUM, "", tester1, "Jul 20, 2009 00:24:06"}, 
119            new String[]{"14", ENHANCEMENT, NEW, HIGH, "", tester2, "Jun 20, 2009 14:35:32"});
120        List<String[]> testIssueTableContents2 = Arrays.asList(
121            new String[]{"21", ENHANCEMENT, FIXED, MEDIUM, ms2, tester1, "Sep 07, 2008 T11:00:00"}, 
122            new String[]{"23", ENHANCEMENT, ACCEPTED, HIGH, "", tester2, "Jul 20, 2009 00:24:06"}, 
123            new String[]{"14", ENHANCEMENT, NEW, HIGH, "", tester2, "Jun 20, 2009 14:35:32"}, 
124            new String[]{"25", DEFECT, STARTED, CRITICAL, ms2, tester1, "Jul 21, 2009 20:00:00"}, 
125            new String[]{"18", TASK, FIXED, HIGH, "", tester2, "Jul 21, 2009 18:00:00"});
126        
127        sensor.processGoogleIssueCsvData(testIssueTableContents1);
128        try {
129          List<SensorDataRef> refs = client.getSensorDataIndex(user, IssueSensor.ISSUE_SENSOR_DATA_TYPE)
130            .getSensorDataRef();
131          assertEquals("Should be 3 sensordata.", 3, refs.size());
132          for (SensorDataRef ref : refs) {
133            SensorData sensorData = client.getSensorData(ref);
134            int id = IssueEntry.getIssueId(sensorData);
135            switch (id) {
136              case 21: 
137                assertIssueSensorData(testIssueTableContents1.get(0), sensorData);
138                break;
139              case 23: 
140                assertIssueSensorData(testIssueTableContents1.get(1), sensorData);
141                break;
142              case 14: 
143                assertIssueSensorData(testIssueTableContents1.get(2), sensorData);
144                break;
145              default: fail("Unexpected issue found with id = " + id);
146            }
147            assertEquals("All 3 issues should be found new.", 3, sensor.updatedIssues.size());
148          }
149        }
150        catch (SensorBaseClientException e) {
151          fail("SensorBaseClientException when checking sensordata. " + e.getMessage());
152          e.printStackTrace();
153        }
154        
155        sensor.processGoogleIssueCsvData(testIssueTableContents2);
156        try {
157          List<SensorDataRef> refs = client.getSensorDataIndex(user, IssueSensor.ISSUE_SENSOR_DATA_TYPE)
158            .getSensorDataRef();
159          assertEquals("Should be 5 sensordata.", 5, refs.size());
160          for (SensorDataRef ref : refs) {
161            SensorData sensorData = client.getSensorData(ref);
162            int id = IssueEntry.getIssueId(sensorData);
163            switch (id) {
164              case 21: 
165                assertIssueSensorData(testIssueTableContents2.get(0), sensorData);
166                break;
167              case 23: 
168                assertIssueSensorData(testIssueTableContents2.get(1), sensorData);
169                break;
170              case 14: 
171                assertIssueSensorData(testIssueTableContents2.get(2), sensorData);
172                break;
173              case 25: 
174                assertIssueSensorData(testIssueTableContents2.get(3), sensorData);
175                break;
176              case 18: 
177                assertIssueSensorData(testIssueTableContents2.get(4), sensorData);
178                break;
179              default: fail("Unexpected issue found with id = " + id);
180            }
181            assertEquals("4 of 5 issues should be found new.", 4, sensor.updatedIssues.size());
182          }
183        }
184        catch (SensorBaseClientException e) {
185          fail("SensorBaseClientException when checking sensordata. " + e.getMessage());
186          e.printStackTrace();
187        }
188      }
189    
190      /**
191       * Check SensorData with the issue information in String array.
192       * @param strings String array of issue information
193       * @param sensorData the sensorData.
194       */
195      private void assertIssueSensorData(String[] strings, SensorData sensorData) {
196        try {
197          assertEquals("Checking type.", strings[1], 
198              IssueEntry.getLatestValueWithKey(sensorData, IssueEntry.TYPE_PROPERTY_KEY));
199          assertEquals("Checking status.", strings[2], 
200              IssueEntry.getLatestValueWithKey(sensorData, IssueEntry.STATUS_PROPERTY_KEY));
201          assertEquals("Checking priority.", strings[3], 
202              IssueEntry.getLatestValueWithKey(sensorData, IssueEntry.PRIORITY_PROPERTY_KEY));
203          assertEquals("Checking milestone.", strings[4], 
204              IssueEntry.getLatestValueWithKey(sensorData, IssueEntry.MILESTONE_PROPERTY_KEY));
205          assertEquals("Checking owner.", strings[5], 
206              IssueEntry.getLatestValueWithKey(sensorData, IssueEntry.OWNER_PROPERTY_KEY));
207        }
208        catch (Exception e) {
209          fail("Fail to get latest value. " + e.getMessage());
210          e.printStackTrace();
211        }
212      }
213      
214    }