Skip to content

Commit

Permalink
Fixed broken build report action test.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/continuum/trunk@1677512 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
batkinson committed May 4, 2015
1 parent cf86e79 commit 38c59a5
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 42 deletions.
Expand Up @@ -135,9 +135,9 @@ public static boolean knownState( int state )

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>();

Expand All @@ -148,7 +148,7 @@ public void setServletResponse( HttpServletResponse response )
this.rawResponse = response;
}

private boolean isAuthorized( String projectGroupName )
protected boolean isAuthorized( String projectGroupName )
{
try
{
Expand All @@ -166,19 +166,48 @@ public void prepare()
{
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 )
{
Expand All @@ -187,11 +216,11 @@ public void prepare()
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()
Expand All @@ -206,7 +235,7 @@ public String init()
return REQUIRES_AUTHORIZATION;
}

if ( permittedGroups.isEmpty() )
if ( permittedGroupMap.isEmpty() )
{
addActionError( getText( "projectBuilds.report.noGroupsAuthorized" ) );
return REQUIRES_AUTHORIZATION;
Expand All @@ -228,7 +257,7 @@ public String execute()
return REQUIRES_AUTHORIZATION;
}

if ( permittedGroups.isEmpty() )
if ( permittedGroupMap.isEmpty() )
{
addActionError( getText( "projectBuilds.report.noGroupsAuthorized" ) );
return REQUIRES_AUTHORIZATION;
Expand Down Expand Up @@ -262,7 +291,7 @@ public String execute()
}
else
{
groupIds.addAll( permittedGroups.values() );
groupIds.addAll( permittedGroupMap.values() );
}

// Users can preview a limited number of records (use export for more)
Expand All @@ -279,7 +308,7 @@ public String execute()

for ( BuildResult result : results )
{
if ( permittedGroups.containsKey( result.getProject().getProjectGroup().getName() ) )
if ( permittedGroupMap.containsKey( result.getProject().getProjectGroup().getName() ) )
{
filteredResults.add( result );
}
Expand Down Expand Up @@ -329,7 +358,7 @@ public String downloadBuildsReport()
return REQUIRES_AUTHORIZATION;
}

if ( permittedGroups.isEmpty() )
if ( permittedGroupMap.isEmpty() )
{
addActionError( getText( "projectBuilds.report.noGroupsAuthorized" ) );
return REQUIRES_AUTHORIZATION;
Expand Down Expand Up @@ -377,7 +406,7 @@ public String downloadBuildsReport()
}
else
{
groupIds.addAll( permittedGroups.values() );
groupIds.addAll( permittedGroupMap.values() );
}

// Build the output file by walking through the results in batches
Expand All @@ -395,7 +424,7 @@ public String downloadBuildsReport()
for ( BuildResult result : results )
{

if ( !permittedGroups.containsKey( result.getProject().getProjectGroup().getName() ) )
if ( !permittedGroupMap.containsKey( result.getProject().getProjectGroup().getName() ) )
{
continue;
}
Expand Down Expand Up @@ -555,6 +584,6 @@ public List<BuildResult> getFilteredResults()

public Map<Integer, String> getProjectGroups()
{
return projectGroups;
return groupSelections;
}
}
Expand Up @@ -79,6 +79,16 @@ public void prepare()
}
}

/**
* 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;
Expand Down
Expand Up @@ -25,13 +25,17 @@
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;
Expand All @@ -48,24 +52,24 @@ public class ViewBuildsReportActionTest

private List<BuildResult> buildResults = new ArrayList<BuildResult>();

private ProjectGroup allowedGroup;

@Before
public void setUp()
throws Exception
{
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
Expand All @@ -86,7 +90,7 @@ public void testMalformedStartDate()
action.setStartDate( "not a date" );
String result = action.execute();

assertEquals( Action.ERROR, result );
assertEquals( Action.INPUT, result );
assertTrue( action.hasActionErrors() );
assertFalse( action.hasFieldErrors() );
}
Expand All @@ -97,7 +101,7 @@ public void testMalformedEndDate()
action.setEndDate( "not a date" );
String result = action.execute();

assertEquals( Action.ERROR, result );
assertEquals( Action.INPUT, result );
assertTrue( action.hasActionErrors() );
assertFalse( action.hasFieldErrors() );
}
Expand Down Expand Up @@ -152,7 +156,7 @@ public void testExportToCsv()
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 )
Expand All @@ -168,12 +172,9 @@ private List<BuildResult> createBuildResult( long timeInMillis )

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 );
Expand All @@ -185,11 +186,22 @@ private List<BuildResult> createBuildResult( long timeInMillis )
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 );
}
}
Expand Up @@ -20,10 +20,34 @@
*/

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
Expand Down

0 comments on commit 38c59a5

Please sign in to comment.