From b949895ed79eab6032acf494fcb0ae758846ec24 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 1 Feb 2013 15:30:59 +0000 Subject: [PATCH] more unit tests git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1441488 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/api/model/LdapGroupMapping.java | 28 +++++++++ .../api/services/LdapGroupMappingService.java | 4 +- .../services/LdapGroupMappingServiceTest.java | 62 +++++++++++++++++-- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/LdapGroupMapping.java b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/LdapGroupMapping.java index a9a66ebf..2dd9000a 100644 --- a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/LdapGroupMapping.java +++ b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/LdapGroupMapping.java @@ -76,4 +76,32 @@ public class LdapGroupMapping sb.append( '}' ); return sb.toString(); } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + LdapGroupMapping that = (LdapGroupMapping) o; + + if ( !group.equals( that.group ) ) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + return group.hashCode(); + } } diff --git a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java index e129dd4a..2d312773 100644 --- a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java +++ b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java @@ -29,6 +29,7 @@ import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @@ -64,10 +65,11 @@ public interface LdapGroupMappingService throws RedbackServiceException; @DELETE + @Path( "{group}" ) @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @RedbackAuthorization( permissions = RedbackRoleConstants.CONFIGURATION_EDIT_OPERATION ) - Boolean removeLdapGroupMapping( String group ) + Boolean removeLdapGroupMapping( @PathParam( "group" ) String group ) throws RedbackServiceException; @POST diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LdapGroupMappingServiceTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LdapGroupMappingServiceTest.java index 932620da..4a85b41d 100644 --- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LdapGroupMappingServiceTest.java +++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LdapGroupMappingServiceTest.java @@ -22,7 +22,9 @@ import org.apache.archiva.redback.components.apacheds.ApacheDs; import org.apache.archiva.redback.rest.api.model.LdapGroupMapping; import org.apache.archiva.redback.rest.api.services.LdapGroupMappingService; import org.apache.archiva.redback.rest.api.services.RedbackServiceException; +import org.apache.commons.lang.StringUtils; import org.fest.assertions.Assertions; +import org.fest.assertions.Condition; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.annotation.DirtiesContext; @@ -102,12 +104,9 @@ public class LdapGroupMappingServiceTest throws Exception { - // cleanup ldap entries InitialDirContext context = apacheDs.getAdminContext(); - - for ( String group : this.groups ) { context.unbind( createGroupDn( group ) ); @@ -178,7 +177,8 @@ public class LdapGroupMappingServiceTest } @Test - public void getLdapGroupMappings() throws Exception + public void getLdapGroupMappings() + throws Exception { try { @@ -194,4 +194,58 @@ public class LdapGroupMappingServiceTest throw e; } } + + @Test + public void addThenRemove() + throws Exception + { + try + { + LdapGroupMappingService service = getLdapGroupMappingService( authorizationHeader ); + + List mappings = service.getLdapGroupMappings(); + + Assertions.assertThat( mappings ).isNotNull().isNotEmpty().hasSize( 3 ); + + LdapGroupMapping ldapGroupMapping = new LdapGroupMapping( "ldap group", Arrays.asList( "redback role" ) ); + + service.addLdapGroupMapping( ldapGroupMapping ); + + mappings = service.getLdapGroupMappings(); + + Assertions.assertThat( mappings ).isNotNull().isNotEmpty().hasSize( 4 ).satisfies( new Condition>() + { + @Override + public boolean matches( List objects ) + { + boolean res = false; + + List mappingList = (List) objects; + + for ( LdapGroupMapping mapping : mappingList ) + { + if ( StringUtils.equals( "ldap group", mapping.getGroup() ) ) + { + Assertions.assertThat( mapping.getRoleNames() ).isNotNull().isNotEmpty().containsOnly( + "redback role" ); + return true; + } + } + + return res; + } + } ); + + service.removeLdapGroupMapping( "ldap group" ); + + mappings = service.getLdapGroupMappings(); + + Assertions.assertThat( mappings ).isNotNull().isNotEmpty().hasSize( 3 ); + } + catch ( Exception e ) + { + log.error( e.getMessage(), e ); + throw e; + } + } }