001    package org.hackystat.projectbrowser.page.telemetry;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertFalse;
005    import static org.junit.Assert.assertTrue;
006    import static org.junit.Assert.fail;
007    import java.util.ArrayList;
008    import java.util.List;
009    import java.util.Properties;
010    import javax.xml.datatype.XMLGregorianCalendar;
011    import org.apache.wicket.Component;
012    import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
013    import org.apache.wicket.markup.html.form.DropDownChoice;
014    import org.apache.wicket.markup.html.form.ListMultipleChoice;
015    import org.apache.wicket.markup.html.list.ListView;
016    import org.apache.wicket.util.tester.FormTester;
017    import org.apache.wicket.util.tester.WicketTester;
018    import org.hackystat.projectbrowser.ProjectBrowserApplication;
019    import org.hackystat.projectbrowser.ProjectBrowserProperties;
020    import org.hackystat.projectbrowser.authentication.SigninPage;
021    import org.hackystat.projectbrowser.page.loadingprocesspanel.LoadingProcessPanel;
022    import org.hackystat.projectbrowser.page.telemetry.datapanel.TelemetryDataPanel;
023    import org.hackystat.projectbrowser.page.telemetry.inputpanel.TelemetryDescriptionPanel;
024    import org.hackystat.projectbrowser.page.telemetry.inputpanel.TelemetryInputPanel;
025    import org.hackystat.projectbrowser.test.ProjectBrowserTestHelper;
026    import org.hackystat.sensorbase.client.SensorBaseClient;
027    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
028    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryChartDefinition;
029    import org.hackystat.utilities.tstamp.Tstamp;
030    import org.junit.After;
031    import org.junit.Before;
032    import org.junit.Test;
033    
034    /**
035     * Tests for Telemetry page.
036     * @author Shaoxuan Zhang
037     */
038    public class TestTelemetryPage extends ProjectBrowserTestHelper {
039      /** the test user. */
040      private String testUser = "TestTelemetryUser";
041      /** email of the test user. */
042      private String testUserEmail = "TestTelemetryUser@hackystat.org";
043      /** the test project. */
044      private String testProject = "TestTelemetryProject";
045      /** String of telemetry, for properties. */
046      private String telemetry = ".telemetry";
047      /** String of true. */
048      private String trueString = "true";
049      /** String of false. */
050      private String falseString = "false";
051      /** String of signinForm. */
052      private String signinFormKey = "signinForm";
053      /** String of user. */
054      private String userKey = "user";
055      /** String of password. */
056      private String passwordKey = "password";
057      /** String of Signin. */
058      private String signinButtonKey = "Signin";
059      /**
060       * Initialize data for testing.
061       * @throws Exception if problems occur. 
062       */
063      @Before
064      public void setUp() throws Exception {
065        SensorBaseClient.registerUser(super.getSensorBaseHostName(), testUserEmail);
066        this.generateSimData(testUser, testProject, Tstamp.makeTimestamp(), 0);
067      }
068      
069      /**
070       * Test the daily project data page.
071       * From opening the page to showing the telemetry chart.
072       * Most information during navigation is checked.
073       */
074      @SuppressWarnings("unchecked")
075      @Test 
076      public void testTelemetryPageNormalNavigation() {
077        this.generateSimData(testUser, testProject, Tstamp.makeTimestamp(), 3);
078    
079        Properties testProperties = getTestProperties();
080        testProperties.put(ProjectBrowserProperties.AVAILABLEPAGE_KEY + telemetry, trueString);
081        testProperties.put(ProjectBrowserProperties.BACKGROUND_PROCESS_KEY + telemetry, falseString);
082        WicketTester tester = new WicketTester(new ProjectBrowserApplication(testProperties));
083        tester.setupRequestAndResponse();
084        
085        tester.startPage(SigninPage.class); 
086        // Let's sign in.
087        FormTester signinForm = tester.newFormTester(signinFormKey);
088        signinForm.setValue(userKey, testUserEmail);
089        signinForm.setValue(passwordKey, testUserEmail);
090        signinForm.submit(signinButtonKey);
091        //first, go to daily project data page.
092        tester.clickLink("TelemetryPageLink");
093        tester.assertRenderedPage(TelemetryPage.class);
094        
095        tester.assertComponent("inputPanel", TelemetryInputPanel.class);
096        
097        FormTester inputForm = tester.newFormTester("inputPanel:inputForm");
098        //checkt the date field.
099        inputForm.setValue("endDateTextField", getDateBeforeAsString(2));
100        inputForm.setValue("startDateTextField", getDateBeforeAsString(5));
101    
102        //check telemetry choices.
103        Component telemetryComponent = inputForm.getForm().get("telemetryMenu");
104        assertTrue("Check telemetry select field", telemetryComponent instanceof DropDownChoice);
105        DropDownChoice telemetryChoice = (DropDownChoice) telemetryComponent;
106        assertFalse("Telemetry should not be null", telemetryChoice.getChoices().isEmpty());
107        String telemetryName = telemetryChoice.getModelObjectAsString();
108        //inputForm.select("telemetryMenu", 1);
109    
110        //check the project list content.
111        Component projectComponent = inputForm.getForm().get("projectMenu");
112        assertTrue("Check project select field", projectComponent instanceof ListMultipleChoice);
113        ListMultipleChoice projectChoice = (ListMultipleChoice) projectComponent;
114        int defaultIndex = -1;
115        int testProjectIndex = -1;
116        for (int i = 0; i < projectChoice.getChoices().size(); i++) {
117          Project project = (Project)projectChoice.getChoices().get(i);
118          if ("Default".equals(project.getName())) {
119            defaultIndex = i;
120          }
121          else if (testProject.equals(project.getName())) {
122            testProjectIndex = i;
123          }
124        }
125        if (defaultIndex < 0) {
126          fail("Default project not found in project list.");
127        }
128        if (testProjectIndex < 0) {
129          fail(testProject + " not found in project list.");
130        }
131        //select those choices.
132        inputForm.selectMultiple("projectMenu", new int[]{defaultIndex, testProjectIndex});
133        
134        inputForm.submit("submit");
135        //check the result.
136        tester.assertRenderedPage(TelemetryPage.class);
137        tester.assertComponent("dataPanel", TelemetryDataPanel.class);
138        tester.assertLabel("dataPanel:telemetryName", telemetryName);
139        //chart image should be empty initially.
140        tester.assertInvisible("dataPanel:selectedChart");
141        
142        FormTester streamForm = tester.newFormTester("dataPanel:streamForm");
143        
144        Component c1 = streamForm.getForm().get("dateList");
145        assertTrue("dateList should be ListView", c1 instanceof ListView);
146        ListView dateList = (ListView) c1;
147        assertEquals("There should be 4 dates in the table.", 4, dateList.getList().size());
148    
149        Component c2 = streamForm.getForm().get("projectTable");
150        assertTrue("dateList should be ListView", c2 instanceof ListView);
151        ListView projectTable = (ListView) c2;
152        assertEquals("There should be 2 projects in the table.", 2, projectTable.getList().size());
153        List projects = projectTable.getList();
154        List<String> projectNames = new ArrayList<String>();
155        projectNames.add(((Project)projects.get(0)).getName());
156        projectNames.add(((Project)projects.get(1)).getName());
157        assertTrue("Default project missed", projectNames.contains("Default"));
158        assertTrue("testProject project missed", projectNames.contains(testProject));
159    
160        //test selected chart display.
161        streamForm.setValue("projectTable:0:projectStream:0:streamCheckBox", String.valueOf(true));
162        streamForm.setValue("projectTable:1:projectStream:0:streamCheckBox", String.valueOf(true));
163        streamForm.submit();
164        tester.assertVisible("dataPanel:selectedChart");
165        /*
166        String selectedChartUrl = tester.getTagByWicketId("selectedChart").getAttribute("src");
167        assertTrue("chart image should be displayed now.", 
168            selectedChartUrl.contains("http://chart.apis.google.com/chart?"));
169            */
170      }
171    
172      /**
173       * Test telemetry page with background process.
174       * The loading panel will show probably.
175       * The cancel button is working correctly.
176       */
177      @Test
178      public void testTelemetryPageBackgroundProcess() {
179        Properties testProperties = getTestProperties();
180        testProperties.put(ProjectBrowserProperties.AVAILABLEPAGE_KEY + telemetry, trueString);
181        testProperties.put(ProjectBrowserProperties.BACKGROUND_PROCESS_KEY + telemetry, trueString);
182        WicketTester tester = new WicketTester(new ProjectBrowserApplication(testProperties));
183        tester.setupRequestAndResponse();
184        
185        tester.startPage(SigninPage.class); 
186        // Let's sign in.
187        FormTester signinForm = tester.newFormTester(signinFormKey);
188        signinForm.setValue(userKey, testUserEmail);
189        signinForm.setValue(passwordKey, testUserEmail);
190        signinForm.submit(signinButtonKey);
191        //first, go to daily project data page.
192        tester.clickLink("TelemetryPageLink");
193        tester.assertRenderedPage(TelemetryPage.class);
194        tester.assertComponent("inputPanel", TelemetryInputPanel.class);
195        
196        FormTester inputForm = tester.newFormTester("inputPanel:inputForm");
197        //check telemetry choices.
198        Component telemetryComponent = inputForm.getForm().get("telemetryMenu");
199        assertTrue("Check telemetry select field", telemetryComponent instanceof DropDownChoice);
200        DropDownChoice telemetryChoice = (DropDownChoice) telemetryComponent;
201        assertFalse("Telemetry should not be null", telemetryChoice.getChoices().isEmpty());
202        String telemetryName = telemetryChoice.getModelObjectAsString();
203        inputForm.submit("submit");
204        
205        //check the result.
206        tester.assertRenderedPage(TelemetryPage.class);
207        //loadingProcessPanel should be visible.
208        tester.assertComponent("loadingProcessPanel", LoadingProcessPanel.class);
209        //dataPanel should be invisible.
210        tester.assertInvisible("dataPanel");
211        //Check message in loadingProcessPanel
212        String msg = tester.getComponentFromLastRenderedPage("loadingProcessPanel:processingMessage").
213        getModelObjectAsString();
214        assertTrue("message error in loading panel", 
215            msg.contains("Retrieving telemetry") && msg.contains(telemetryName));
216        //check the cancel button.
217        FormTester cancelForm = tester.newFormTester("inputPanel:cancelForm");
218        cancelForm.submit("cancel");
219        assertTrue("Check process cancelled message."
220            , tester.getComponentFromLastRenderedPage("loadingProcessPanel:processingMessage").
221            getModelObjectAsString().contains("Process Cancelled."));
222      }
223      
224      /**
225       * Test pop up windows in telemetry page.
226       * All choices in telemetry menu should be in the description pop up window.
227       */
228      @SuppressWarnings("unchecked")
229      @Test
230      public void testTelemetryPopUpPanel() {
231        Properties testProperties = getTestProperties();
232        testProperties.put(ProjectBrowserProperties.AVAILABLEPAGE_KEY + telemetry, trueString);
233        testProperties.put(ProjectBrowserProperties.BACKGROUND_PROCESS_KEY + telemetry, falseString);
234        WicketTester tester = new WicketTester(new ProjectBrowserApplication(testProperties));
235        tester.setupRequestAndResponse();
236        
237        tester.startPage(SigninPage.class); 
238        // Let's sign in.
239        FormTester signinForm = tester.newFormTester(signinFormKey);
240        signinForm.setValue(userKey, testUserEmail);
241        signinForm.setValue(passwordKey, testUserEmail);
242        signinForm.submit(signinButtonKey);
243        //first, go to daily project data page.
244        tester.clickLink("TelemetryPageLink");
245        tester.assertRenderedPage(TelemetryPage.class);
246        tester.assertComponent("inputPanel", TelemetryInputPanel.class);
247        
248        FormTester inputForm = tester.newFormTester("inputPanel:inputForm");
249        //get telemetry choices.
250        Component telemetryComponent = inputForm.getForm().get("telemetryMenu");
251        assertTrue("Check telemetry select field", telemetryComponent instanceof DropDownChoice);
252        DropDownChoice telemetryChoice = (DropDownChoice) telemetryComponent;
253        assertFalse("Telemetry should not be null", telemetryChoice.getChoices().isEmpty());
254        List choices = telemetryChoice.getChoices();
255        //click out the pop up window
256        tester.clickLink("inputPanel:inputForm:chartDefPopup:showModalWindow");
257        tester.assertComponentOnAjaxResponse("inputPanel:inputForm:chartDefPopup:modalWindow");
258        ModalWindow modalWindow = (ModalWindow) 
259          tester.getComponentFromLastRenderedPage("inputPanel:inputForm:chartDefPopup:modalWindow");
260        //get the description list
261        TelemetryDescriptionPanel p = (TelemetryDescriptionPanel)
262                                            modalWindow.get(modalWindow.getContentId());
263        ListView listView = (ListView)p.get("descriptions");
264        List<String> telemetryInDescriptions = new ArrayList<String>();
265        for (Object o : listView.getList()) {
266          telemetryInDescriptions.add(((TelemetryChartDefinition)o).getName());
267        }
268        
269        assertTrue("All choices in telemetry menu should be in the description list.", 
270            telemetryInDescriptions.containsAll(choices));
271        //assertTrue(choices.containsAll(telemetryInDescriptions));
272      }
273    
274      /**
275       * Test telemetry page with page parameters.
276       */
277      /*
278      @Test
279      public void testTelemetryWithPageParamters() {  //NOPMD WicketTester has its own assert classes.
280        Properties testProperties = getTestProperties();
281        testProperties.put(ProjectBrowserProperties.AVAILABLEPAGE_KEY + telemetry, trueString);
282        testProperties.put(ProjectBrowserProperties.BACKGROUND_PROCESS_KEY + telemetry, falseString);
283        WicketTester tester = new WicketTester(new ProjectBrowserApplication(testProperties));
284        tester.setupRequestAndResponse();
285        
286        
287        //but still need to sign in first.
288        tester.startPage(SigninPage.class);
289        
290        FormTester signinForm = tester.newFormTester(signinFormKey);
291        signinForm.setValue(userKey, testUserEmail);
292        signinForm.setValue(passwordKey, testUserEmail);
293        signinForm.submit(signinButtonKey);
294        
295        //TODO add test.
296        PageParameters param = new PageParameters("Build/Day/2008-05-24/2008-06-02/" + 
297            this.testProject + "-" + this.testUser + "/*,*,*,*,false");
298        tester.startPage(TelemetryPage.class, param); 
299        tester.assertRenderedPage(TelemetryPage.class);
300        
301      }
302      */
303      
304      /**
305       * Clear testing data.
306       */
307      @After
308      public void clear() {
309        this.clearData(testUserEmail);
310      }
311      
312      /**
313       * return a String that represent a date before today.
314       * @param i the number of days before today.
315       * @return a String represent today.
316       */
317      public String getDateBeforeAsString(int i) {
318        XMLGregorianCalendar time = Tstamp.incrementDays(Tstamp.makeTimestamp(), -i);
319        String timeString = time.getYear() + "-";
320        timeString += (time.getMonth() >= 10) ? time.getMonth() : "0" + time.getMonth();
321        timeString += "-";
322        timeString += (time.getDay() >= 10) ? time.getDay() : "0" + time.getDay();
323        return timeString;
324      }
325    }