001    package org.hackystat.projectbrowser.page.projectportfolio;
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.ArrayList;
007    import java.util.List;
008    import java.util.Properties;
009    import org.apache.wicket.Component;
010    import org.apache.wicket.markup.html.form.ListMultipleChoice;
011    import org.apache.wicket.markup.html.list.ListView;
012    import org.apache.wicket.util.tester.FormTester;
013    import org.apache.wicket.util.tester.WicketTester;
014    import org.hackystat.projectbrowser.ProjectBrowserApplication;
015    import org.hackystat.projectbrowser.ProjectBrowserProperties;
016    import org.hackystat.projectbrowser.authentication.SigninPage;
017    import org.hackystat.projectbrowser.page.projectportfolio.configurationpanel.
018    ProjectPortfolioConfigurationPanel;
019    import org.hackystat.projectbrowser.page.projectportfolio.detailspanel.ProjectPortfolioDetailsPanel;
020    import org.hackystat.projectbrowser.page.projectportfolio.detailspanel.chart.MiniBarChart;
021    import org.hackystat.projectbrowser.page.projectportfolio.detailspanel.chart.StreamTrendClassifier;
022    import org.hackystat.projectbrowser.page.projectportfolio.inputpanel.ProjectPortfolioInputPanel;
023    import org.hackystat.projectbrowser.test.ProjectBrowserTestHelper;
024    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
025    import org.hackystat.utilities.tstamp.Tstamp;
026    import org.junit.After;
027    import org.junit.Before;
028    import org.junit.Test;
029    
030    /**
031     * Tests for ProjectPortfolio page.
032     * 
033     * @author Shaoxuan Zhang
034     */
035    public class TestProjectPortfolioPage extends ProjectBrowserTestHelper {
036    
037      /** the test user. */
038      private String testUser = "TestProjectPortfolioUser";
039      /** email of the test user. */
040      private String testUserEmail = testUser + "@hackystat.org";
041      /** the test user. */
042      private String testUser2 = "TestProjectPortfolioUser2";
043      /** the test project. */
044      private String testProject = "TestProjectPortfolioProject";
045    
046      /** The word of "true". */
047      private static final String TRUE = "true";
048      /** The word of "false". */
049      private static final String FALSE = "false";
050    
051      /** The start date. */
052      private static final String testStartDate = "2007-01-06";
053      /** The end date. */
054      private static final String testEndDate = "2007-01-08";
055    
056      /** path of configuration panel. */
057      private static final String configurationPanelPath = "configurationPanel";
058      /** path of configuration form. */
059      private static final String configurationFormPath = configurationPanelPath + ":configurationForm";
060      /** path of input panel. */
061      private static final String inputPanelPath = "inputPanel";
062      /** path of input form. */
063      private static final String inputFormPath = inputPanelPath + ":inputForm";
064      /** path of configuration link. */
065      private static final String configurationButton = "configuration";
066    
067      /** The test properties. */
068      private Properties testProperties;
069    
070      /** default HigherThreshold of Coverage. */
071      String defaultCoverageHigherThreshold = "90";
072      /** default LowerThreshold of Coverage. */
073      String defaultCoverageLowerThreshold = "40";
074    
075      /** The wicket tester. */
076      private WicketTester tester;
077    
078      /**
079       * Initialize data for testing.
080       * 
081       * @throws Exception when error occur.
082       */
083      @Before
084      public void setUp() throws Exception {
085        this.generateSimData(testUser, testProject, Tstamp.makeTimestamp(testEndDate), 0);
086        // prepare test properties.
087        testProperties = getTestProperties();
088        testProperties.put(ProjectBrowserProperties.AVAILABLEPAGE_KEY + ".projectportfolio", TRUE);
089        testProperties
090            .put(ProjectBrowserProperties.BACKGROUND_PROCESS_KEY + ".projectportfolio", FALSE);
091        tester = new WicketTester(new ProjectBrowserApplication(testProperties));
092    
093        tester.startPage(SigninPage.class);
094        // Let's sign in.
095        FormTester signinForm = tester.newFormTester("signinForm");
096        signinForm.setValue("user", testUserEmail);
097        signinForm.setValue("password", testUserEmail);
098        signinForm.submit("Signin");
099        // first, go to project portfolio page.
100        tester.clickLink("ProjectPortfolioPageLink");
101        tester.assertRenderedPage(ProjectPortfolioPage.class);
102      }
103    
104      /**
105       * Test the daily project data page.
106       * 
107       * @throws Exception error occur
108       */
109      @Test
110      public void testProjectPortfolioPage() throws Exception {
111        
112        this.generateSimData(testUser, testProject, Tstamp.makeTimestamp(testEndDate), 3);
113        this.generateSimData(testUser2, testProject, Tstamp.makeTimestamp(testEndDate), 2);
114        this.addMember(testProject, testUser, testUser2);
115    
116        tester.assertInvisible(configurationPanelPath);
117        tester.newFormTester(inputFormPath).submit(configurationButton);
118        //tester.clickLink(configurationLink);
119        tester.assertComponent(configurationPanelPath, ProjectPortfolioConfigurationPanel.class);
120    
121        FormTester configurationForm = tester.newFormTester(configurationFormPath);
122        configurationForm.submit("reset");
123        // tester.clickLink(configurationLink);
124        
125        configurationForm = tester.newFormTester(configurationFormPath);
126        ListView measureList = (ListView) configurationForm.getForm().get("measureList");
127        assertTrue("There should be at least 10 measures.", measureList.getList().size() >= 10);
128        // set the first measure(Coverage)'s granularity parameter to line.
129        assertEquals("Check measure name", "Coverage", tester.getComponentFromLastRenderedPage(
130            "configurationPanel:configurationForm:measureList:0:measureNameLabel")
131            .getModelObjectAsString());
132        configurationForm.setValue("measureList:0:telemetryParameters:parameterList:1:field", "line");
133        // set all others to be disable.
134        for (int i = 4; i < measureList.getList().size(); i++) {
135          configurationForm.setValue("measureList:" + i + ":enableCheckBox", FALSE);
136        }
137        configurationForm.submit("submit");
138        tester.assertInvisible(configurationPanelPath);
139    
140        tester.assertComponent(inputPanelPath, ProjectPortfolioInputPanel.class);
141        FormTester inputForm = tester.newFormTester(inputFormPath);
142    
143        // check the project list content.
144        Component component = inputForm.getForm().get("projectMenu");
145        assertTrue("Check project select field", component instanceof ListMultipleChoice);
146        ListMultipleChoice projectChoice = (ListMultipleChoice) component;
147        boolean pass = false;
148        int index = 0;
149        for (int i = 0; i < projectChoice.getChoices().size(); i++) {
150          Project project = (Project) projectChoice.getChoices().get(i);
151          if (this.testProject.equals(project.getName())) {
152            index = i;
153            pass = true;
154          }
155        }
156        if (!pass) {
157          fail(testProject + " not found in projects list.");
158        }
159        // select the default project.
160        inputForm.select("projectMenu", index);
161        inputForm.select("granularity", 0);
162        inputForm.setValue("startDateTextField", testStartDate);
163        inputForm.setValue("endDateTextField", testEndDate);
164        inputForm.submit("submit");
165        // check the result.
166        tester.assertRenderedPage(ProjectPortfolioPage.class);
167    
168        tester.isInvisible("loadingProcessPanel");
169        tester.assertComponent("detailPanel", ProjectPortfolioDetailsPanel.class);
170    
171        ListView measureheads = (ListView) tester
172            .getComponentFromLastRenderedPage("detailPanel:measureHeads");
173        assertEquals("Should be only 4 measure heads.", 4, measureheads.size());
174        assertEquals("Check the first measure's display name", "Coverage", tester
175            .getComponentFromLastRenderedPage("detailPanel:measureHeads:0:sortLink:measureName")
176            .getModelObjectAsString());
177        assertEquals("Check the second measure's display name", "Complexity", tester
178            .getComponentFromLastRenderedPage("detailPanel:measureHeads:1:sortLink:measureName")
179            .getModelObjectAsString());
180        assertEquals("Check the third measure's display name", "Coupling", tester
181            .getComponentFromLastRenderedPage("detailPanel:measureHeads:2:sortLink:measureName")
182            .getModelObjectAsString());
183        assertEquals("Check the fourth measure's display name", "Churn", tester
184            .getComponentFromLastRenderedPage("detailPanel:measureHeads:3:sortLink:measureName")
185            .getModelObjectAsString());
186    
187        ListView measures = (ListView) tester
188            .getComponentFromLastRenderedPage("detailPanel:projectTable:0:measures");
189        assertEquals("Should be only 4 measures there.", 4, measures.size());
190        List<String> projectNames = new ArrayList<String>();
191        projectNames.add(tester.getComponentFromLastRenderedPage(
192            "detailPanel:projectTable:0:projectName").getModelObjectAsString());
193        projectNames.add(tester.getComponentFromLastRenderedPage(
194            "detailPanel:projectTable:1:projectName").getModelObjectAsString());
195        assertTrue("Default should in the detail table", projectNames.contains("Default"));
196        assertTrue(testProject + " should in the detail table", projectNames.contains(testProject));
197        String testProjectPath = "";
198        if (testProject.equals(projectNames.get(0))) {
199          testProjectPath = "detailPanel:projectTable:0:";
200        }
201        else {
202          testProjectPath = "detailPanel:projectTable:1:";
203        }
204        // check value
205        assertEquals("Check Coverage value", "50.0", tester.getComponentFromLastRenderedPage(
206            testProjectPath + "measures:0:value").getModelObjectAsString());
207        assertEquals("Check Complexity value", "3.0", tester.getComponentFromLastRenderedPage(
208            testProjectPath + "measures:1:value").getModelObjectAsString());
209        assertEquals("Check Coupling value", "N/A", tester.getComponentFromLastRenderedPage(
210            testProjectPath + "measures:2:value").getModelObjectAsString());
211        assertEquals("Check Churn value", "80.0", tester.getComponentFromLastRenderedPage(
212            testProjectPath + "measures:3:value").getModelObjectAsString());
213        
214        // check inner data.
215        MiniBarChart coverage = (MiniBarChart) measures.getList().get(0);
216        assertEquals("Check measure name.", "Coverage", coverage.getConfiguration().getName());
217        assertEquals("Coverage should be colorable", 
218            StreamTrendClassifier.name, coverage.getConfiguration().getClassiferName());
219    
220        MiniBarChart complexity = (MiniBarChart) measures.getList().get(1);
221        assertEquals("Check measure name.", "CyclomaticComplexity", complexity.getConfiguration()
222            .getName());
223    
224      }
225    
226    
227      /**
228       * Test the configuration panel.
229       */
230      @Test
231      public void testConfigurationPanel() {
232        tester.newFormTester(inputFormPath).submit(configurationButton);
233        tester.assertComponent(configurationPanelPath, ProjectPortfolioConfigurationPanel.class);
234    
235        String firstHigherThreshold = "measureList:0:measurePanel:higherThreshold";
236        String firstlowerThreshold = "measureList:0:measurePanel:lowerThreshold";
237    
238        // Test validator
239        FormTester configurationForm = tester.newFormTester(configurationFormPath);
240        configurationForm.setValue(firstHigherThreshold, "10");
241        configurationForm.setValue(firstlowerThreshold, "20");
242        configurationForm.submit();
243        /*
244        tester.assertErrorMessages(new String[] { "Value of higherThreshold in Coverage " + 
245          "is not bigger than that of lowerThreshold." });
246         */
247        // set new value
248        configurationForm = tester.newFormTester(configurationFormPath);
249        configurationForm.setValue(firstHigherThreshold, "40");
250        configurationForm.setValue(firstlowerThreshold, "20");
251        configurationForm.submit();
252    
253        // test persistance
254        tester.destroy();
255        tester = new WicketTester(new ProjectBrowserApplication(testProperties));
256    
257        tester.startPage(SigninPage.class);
258        // sign in.
259        FormTester signinForm = tester.newFormTester("signinForm");
260        signinForm.setValue("user", testUserEmail);
261        signinForm.setValue("password", testUserEmail);
262        signinForm.submit("Signin");
263        // go to project portfolio page again.
264        tester.clickLink("ProjectPortfolioPageLink");
265        tester.assertRenderedPage(ProjectPortfolioPage.class);
266        // open configuration panel
267        tester.newFormTester(inputFormPath).submit(configurationButton);
268        assertEquals("HigherThreshold should set to default value.", "40", configurationForm
269            .getTextComponentValue(firstHigherThreshold));
270        assertEquals("LowerThreshold should set to default value.", "20", configurationForm
271            .getTextComponentValue(firstlowerThreshold));
272    
273        // test reset
274        configurationForm = tester.newFormTester(configurationFormPath);
275        configurationForm.submit("reset");
276        configurationForm = tester.newFormTester(configurationFormPath);
277        assertEquals("HigherThreshold should set to default value.", defaultCoverageHigherThreshold,
278            configurationForm.getTextComponentValue(firstHigherThreshold));
279        assertEquals("LowerThreshold should set to default value.", defaultCoverageLowerThreshold,
280            configurationForm.getTextComponentValue(firstlowerThreshold));
281    
282        tester.clickLink("configurationPanel:configurationForm:instructionPopup:showModalWindow");
283        tester
284            .assertComponentOnAjaxResponse(
285                "configurationPanel:configurationForm:instructionPopup:modalWindow");
286      }
287    
288      /**
289       * Clear testing data.
290       */
291      @After
292      public void clear() {
293        this.clearData(testUserEmail);
294      }
295    
296    }