continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From batkin...@apache.org
Subject svn commit: r1677512 - in /continuum/trunk/continuum-webapp/src: main/java/org/apache/continuum/web/action/ main/java/org/apache/maven/continuum/web/action/ test/java/org/apache/continuum/web/action/ test/java/org/apache/continuum/web/action/stub/
Date Mon, 04 May 2015 01:43:09 GMT
Author: batkinson
Date: Mon May  4 01:43:08 2015
New Revision: 1677512

URL: http://svn.apache.org/r1677512
Log:
Fixed broken build report action test.

Modified:
    continuum/trunk/continuum-webapp/src/main/java/org/apache/continuum/web/action/ViewBuildsReportAction.java
    continuum/trunk/continuum-webapp/src/main/java/org/apache/maven/continuum/web/action/ContinuumActionSupport.java
    continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/ViewBuildsReportActionTest.java
    continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/stub/ViewBuildsReportActionStub.java

Modified: continuum/trunk/continuum-webapp/src/main/java/org/apache/continuum/web/action/ViewBuildsReportAction.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-webapp/src/main/java/org/apache/continuum/web/action/ViewBuildsReportAction.java?rev=1677512&r1=1677511&r2=1677512&view=diff
==============================================================================
--- continuum/trunk/continuum-webapp/src/main/java/org/apache/continuum/web/action/ViewBuildsReportAction.java
(original)
+++ continuum/trunk/continuum-webapp/src/main/java/org/apache/continuum/web/action/ViewBuildsReportAction.java
Mon May  4 01:43:08 2015
@@ -135,9 +135,9 @@ public class ViewBuildsReportAction
 
     private Map<Integer, String> buildStatuses;
 
-    private Map<Integer, String> projectGroups;
+    private Map<Integer, String> groupSelections;
 
-    private Map<String, Integer> permittedGroups;
+    private Map<String, Integer> permittedGroupMap;
 
     private List<BuildResult> filteredResults = new ArrayList<BuildResult>();
 
@@ -148,7 +148,7 @@ public class ViewBuildsReportAction
         this.rawResponse = response;
     }
 
-    private boolean isAuthorized( String projectGroupName )
+    protected boolean isAuthorized( String projectGroupName )
     {
         try
         {
@@ -166,19 +166,48 @@ public class ViewBuildsReportAction
     {
         super.prepare();
 
-        // Populate the state drop downs
-        buildStatuses = new LinkedHashMap<Integer, String>();
-        buildStatuses.put( 0, "ALL" );
-        for ( ResultState state : ResultState.values() )
+        Collection<ProjectGroup> permittedGroups = getAuthorizedGroups();
+
+        groupSelections = createGroupSelections( permittedGroups );
+        permittedGroupMap = createPermittedGroupMap( permittedGroups );
+        buildStatuses = createStatusSelections();
+    }
+
+    protected Map<String, Integer> createPermittedGroupMap( Collection<ProjectGroup>
allowedGroups )
+    {
+        Map<String, Integer> result = new HashMap<String, Integer>();
+        for ( ProjectGroup group : allowedGroups )
         {
-            buildStatuses.put( state.getDataId(), getText( state.getTextKey() ) );
+            result.put( group.getName(), group.getId() );
         }
+        return result;
+    }
 
-        permittedGroups = new HashMap<String, Integer>();
-        projectGroups = new LinkedHashMap<Integer, String>();
-        projectGroups.put( 0, "ALL" );
+    protected Map<Integer, String> createGroupSelections( Collection<ProjectGroup>
permittedGroups )
+    {
+        Map<Integer, String> result = new LinkedHashMap<Integer, String>();
+        result.put( 0, "ALL" );
+        for ( ProjectGroup group : permittedGroups )
+        {
+            result.put( group.getId(), group.getName() );
+        }
+        return result;
+    }
+
+    protected Map<Integer, String> createStatusSelections()
+    {
+        Map<Integer, String> result = new LinkedHashMap<Integer, String>();
+        result.put( 0, "ALL" );
+        for ( ResultState state : ResultState.values() )
+        {
+            result.put( state.getDataId(), getText( state.getTextKey() ) );
+        }
+        return result;
+    }
 
-        // TODO: Use these to limit results at the data layer
+    protected Collection<ProjectGroup> getAuthorizedGroups()
+    {
+        Collection<ProjectGroup> permitted = new ArrayList<ProjectGroup>();
         List<ProjectGroup> groups = getContinuum().getAllProjectGroups();
         if ( groups != null )
         {
@@ -187,11 +216,11 @@ public class ViewBuildsReportAction
                 String groupName = group.getName();
                 if ( isAuthorized( groupName ) )
                 {
-                    projectGroups.put( group.getId(), groupName );
-                    permittedGroups.put( groupName, group.getId() );
+                    permitted.add( group );
                 }
             }
         }
+        return permitted;
     }
 
     public String init()
@@ -206,7 +235,7 @@ public class ViewBuildsReportAction
             return REQUIRES_AUTHORIZATION;
         }
 
-        if ( permittedGroups.isEmpty() )
+        if ( permittedGroupMap.isEmpty() )
         {
             addActionError( getText( "projectBuilds.report.noGroupsAuthorized" ) );
             return REQUIRES_AUTHORIZATION;
@@ -228,7 +257,7 @@ public class ViewBuildsReportAction
             return REQUIRES_AUTHORIZATION;
         }
 
-        if ( permittedGroups.isEmpty() )
+        if ( permittedGroupMap.isEmpty() )
         {
             addActionError( getText( "projectBuilds.report.noGroupsAuthorized" ) );
             return REQUIRES_AUTHORIZATION;
@@ -262,7 +291,7 @@ public class ViewBuildsReportAction
         }
         else
         {
-            groupIds.addAll( permittedGroups.values() );
+            groupIds.addAll( permittedGroupMap.values() );
         }
 
         // Users can preview a limited number of records (use export for more)
@@ -279,7 +308,7 @@ public class ViewBuildsReportAction
 
             for ( BuildResult result : results )
             {
-                if ( permittedGroups.containsKey( result.getProject().getProjectGroup().getName()
) )
+                if ( permittedGroupMap.containsKey( result.getProject().getProjectGroup().getName()
) )
                 {
                     filteredResults.add( result );
                 }
@@ -329,7 +358,7 @@ public class ViewBuildsReportAction
             return REQUIRES_AUTHORIZATION;
         }
 
-        if ( permittedGroups.isEmpty() )
+        if ( permittedGroupMap.isEmpty() )
         {
             addActionError( getText( "projectBuilds.report.noGroupsAuthorized" ) );
             return REQUIRES_AUTHORIZATION;
@@ -377,7 +406,7 @@ public class ViewBuildsReportAction
                 }
                 else
                 {
-                    groupIds.addAll( permittedGroups.values() );
+                    groupIds.addAll( permittedGroupMap.values() );
                 }
 
                 // Build the output file by walking through the results in batches
@@ -395,7 +424,7 @@ public class ViewBuildsReportAction
                     for ( BuildResult result : results )
                     {
 
-                        if ( !permittedGroups.containsKey( result.getProject().getProjectGroup().getName()
) )
+                        if ( !permittedGroupMap.containsKey( result.getProject().getProjectGroup().getName()
) )
                         {
                             continue;
                         }
@@ -555,6 +584,6 @@ public class ViewBuildsReportAction
 
     public Map<Integer, String> getProjectGroups()
     {
-        return projectGroups;
+        return groupSelections;
     }
 }
\ No newline at end of file

Modified: continuum/trunk/continuum-webapp/src/main/java/org/apache/maven/continuum/web/action/ContinuumActionSupport.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-webapp/src/main/java/org/apache/maven/continuum/web/action/ContinuumActionSupport.java?rev=1677512&r1=1677511&r2=1677512&view=diff
==============================================================================
--- continuum/trunk/continuum-webapp/src/main/java/org/apache/maven/continuum/web/action/ContinuumActionSupport.java
(original)
+++ continuum/trunk/continuum-webapp/src/main/java/org/apache/maven/continuum/web/action/ContinuumActionSupport.java
Mon May  4 01:43:08 2015
@@ -79,6 +79,16 @@ public class ContinuumActionSupport
         }
     }
 
+    /**
+     * Here for unit testing support, it allows configuring a mock security session.
+     *
+     * @param securitySession
+     */
+    protected void setSecuritySession( SecuritySession securitySession )
+    {
+        this.securitySession = securitySession;
+    }
+
     public Continuum getContinuum()
     {
         return continuum;

Modified: continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/ViewBuildsReportActionTest.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/ViewBuildsReportActionTest.java?rev=1677512&r1=1677511&r2=1677512&view=diff
==============================================================================
--- continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/ViewBuildsReportActionTest.java
(original)
+++ continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/ViewBuildsReportActionTest.java
Mon May  4 01:43:08 2015
@@ -25,13 +25,17 @@ import org.apache.maven.continuum.Contin
 import org.apache.maven.continuum.model.project.BuildResult;
 import org.apache.maven.continuum.model.project.Project;
 import org.apache.maven.continuum.model.project.ProjectGroup;
+import org.codehaus.plexus.redback.system.SecuritySession;
 import org.junit.Before;
 import org.junit.Test;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.ByteArrayOutputStream;
 import java.io.PrintWriter;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
@@ -48,6 +52,8 @@ public class ViewBuildsReportActionTest
 
     private List<BuildResult> buildResults = new ArrayList<BuildResult>();
 
+    private ProjectGroup allowedGroup;
+
     @Before
     public void setUp()
         throws Exception
@@ -55,17 +61,15 @@ public class ViewBuildsReportActionTest
         continuum = mock( Continuum.class );
 
         action = new ViewBuildsReportActionStub();
+        action.setSecuritySession( mock( SecuritySession.class ) );
         action.setContinuum( continuum );
-    }
 
-    @Test
-    public void testInvalidRowCount()
-    {
-        String result = action.execute();
+        allowedGroup = new ProjectGroup();
+        allowedGroup.setId( 1 );
+        allowedGroup.setName( "Allowed Group" );
+        action.setAuthorizedGroups( Arrays.asList( allowedGroup ) );
 
-        assertEquals( Action.INPUT, result );
-        assertTrue( action.hasFieldErrors() );
-        assertFalse( action.hasActionErrors() );
+        action.prepare();
     }
 
     @Test
@@ -86,7 +90,7 @@ public class ViewBuildsReportActionTest
         action.setStartDate( "not a date" );
         String result = action.execute();
 
-        assertEquals( Action.ERROR, result );
+        assertEquals( Action.INPUT, result );
         assertTrue( action.hasActionErrors() );
         assertFalse( action.hasFieldErrors() );
     }
@@ -97,7 +101,7 @@ public class ViewBuildsReportActionTest
         action.setEndDate( "not a date" );
         String result = action.execute();
 
-        assertEquals( Action.ERROR, result );
+        assertEquals( Action.INPUT, result );
         assertTrue( action.hasActionErrors() );
         assertFalse( action.hasFieldErrors() );
     }
@@ -152,7 +156,7 @@ public class ViewBuildsReportActionTest
         String result = action.downloadBuildsReport();
 
         assertNull( "result should be null", result );
-        assertFileContentsEqual( out.toString(), cal.getTime().toString() );
+        assertExportContents( results, out.toString() );
     }
 
     private void assertSuccessResult( String result )
@@ -168,12 +172,9 @@ public class ViewBuildsReportActionTest
 
         BuildResult result = new BuildResult();
 
-        ProjectGroup group = new ProjectGroup();
-        group.setName( "Test Group" );
-
         Project project = new Project();
         project.setName( "Test Project" );
-        project.setProjectGroup( group );
+        project.setProjectGroup( allowedGroup );
 
         result.setProject( project );
         result.setState( 2 );
@@ -185,11 +186,22 @@ public class ViewBuildsReportActionTest
         return results;
     }
 
-    private void assertFileContentsEqual( String report, String buildDate )
+    private void assertExportContents( List<BuildResult> results, String actualContents
)
     {
-        String result = "Project Group,Project Name,Build Date,Triggered By,Build Status\n"
+
-            "Test Group,Test Project," + buildDate + ",test-admin,Ok\n";
-
-        assertEquals( report, result );
+        DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
+        String expectedContents = "Group,Project,ID,Build#,Started,Duration,Triggered By,Status\n";
+        for ( BuildResult result : results )
+        {
+            Project p = result.getProject();
+            ProjectGroup pg = p.getProjectGroup();
+            expectedContents += String.format( "%s,%s,%s,%s,%s,%s,%s,%s\n",
+                                               pg.getName(), p.getName(), result.getId(),
result.getBuildNumber(),
+                                               dateFormat.format( new Date( result.getStartTime()
) ),
+                                               ( result.getEndTime() - result.getStartTime()
) / 1000,
+                                               result.getUsername(),
+                                               ViewBuildsReportAction.ResultState.fromId(
+                                                   result.getState() ).getTextKey() );
+        }
+        assertEquals( expectedContents, actualContents );
     }
 }

Modified: continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/stub/ViewBuildsReportActionStub.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/stub/ViewBuildsReportActionStub.java?rev=1677512&r1=1677511&r2=1677512&view=diff
==============================================================================
--- continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/stub/ViewBuildsReportActionStub.java
(original)
+++ continuum/trunk/continuum-webapp/src/test/java/org/apache/continuum/web/action/stub/ViewBuildsReportActionStub.java
Mon May  4 01:43:08 2015
@@ -20,10 +20,34 @@ package org.apache.continuum.web.action.
  */
 
 import org.apache.continuum.web.action.ViewBuildsReportAction;
+import org.apache.maven.continuum.model.project.ProjectGroup;
+import org.codehaus.plexus.redback.system.SecuritySession;
+
+import java.util.Collection;
+import java.util.Collections;
 
 public class ViewBuildsReportActionStub
     extends ViewBuildsReportAction
 {
+
+    Collection<ProjectGroup> authorizedGroups = Collections.EMPTY_LIST;
+
+    public void setSecuritySession( SecuritySession securitySession )
+    {
+        super.setSecuritySession( securitySession );
+    }
+
+    @Override
+    protected Collection<ProjectGroup> getAuthorizedGroups()
+    {
+        return authorizedGroups;
+    }
+
+    public void setAuthorizedGroups( Collection<ProjectGroup> authorizedGroups )
+    {
+        this.authorizedGroups = authorizedGroups;
+    }
+
     protected void checkViewProjectGroupAuthorization( String resource )
     {
         // skip authorization check



Mime
View raw message