From fba8ce10c6171138b526357c645137e59c060371 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Sat, 24 Mar 2012 22:01:00 +0000 Subject: [PATCH] add unit tests for browseService git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1304938 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/api/model/BrowseResultEntry.java | 45 ++++++++ .../services/AbstractArchivaRestTest.java | 27 ++++- .../rest/services/BrowseServiceTest.java | 109 +++++++++++++++++- 3 files changed, 173 insertions(+), 8 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/BrowseResultEntry.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/BrowseResultEntry.java index 718d37d6e..9ee7a1f1d 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/BrowseResultEntry.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/BrowseResultEntry.java @@ -69,4 +69,49 @@ public class BrowseResultEntry { return this.name.compareTo( browseGroupResultEntry.name ); } + + @Override + public String toString() + { + final StringBuilder sb = new StringBuilder(); + sb.append( "BrowseResultEntry" ); + sb.append( "{name='" ).append( name ).append( '\'' ); + sb.append( ", project=" ).append( project ); + sb.append( '}' ); + return sb.toString(); + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( !( o instanceof BrowseResultEntry ) ) + { + return false; + } + + BrowseResultEntry that = (BrowseResultEntry) o; + + if ( project != that.project ) + { + return false; + } + if ( !name.equals( that.name ) ) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = name.hashCode(); + result = 31 * result + ( project ? 1 : 0 ); + return result; + } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java index 5afb4a980..d8b3df876 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java @@ -201,7 +201,7 @@ public abstract class AbstractArchivaRestTest return service; } - protected BrowseService getBrowseService( String authzHeader ) + protected BrowseService getBrowseService( String authzHeader, boolean useXml ) { BrowseService service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", @@ -214,9 +214,16 @@ public abstract class AbstractArchivaRestTest } WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 ); - - WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE ); - WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE ); + if ( useXml ) + { + WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE ); + WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE ); + } + else + { + WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE ); + WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE ); + } return service; } @@ -348,7 +355,7 @@ public abstract class AbstractArchivaRestTest } - protected void createAndIndexRepo( String testRepoId, String repoPath ) + protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan ) throws Exception { if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null ) @@ -378,9 +385,17 @@ public abstract class AbstractArchivaRestTest getRoleManagementService( authorizationHeader ).assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" ); + if ( scan ) + { + getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true ); + } - getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true ); + } + protected void createAndIndexRepo( String testRepoId, String repoPath ) + throws Exception + { + createAndIndexRepo( testRepoId, repoPath, true ); } protected void deleteTestRepo( String id ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java index 1284ab92b..a573ba6ce 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java @@ -18,6 +18,10 @@ package org.apache.archiva.rest.services; * under the License. */ +import org.apache.archiva.metadata.model.ProjectVersionMetadata; +import org.apache.archiva.rest.api.model.BrowseResult; +import org.apache.archiva.rest.api.model.BrowseResultEntry; +import org.apache.archiva.rest.api.model.VersionsList; import org.apache.archiva.rest.api.services.BrowseService; import org.fest.assertions.MapAssert; import org.junit.Test; @@ -46,7 +50,7 @@ public class BrowseServiceTest createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" ); - BrowseService browseService = getBrowseService( authorizationHeader ); + BrowseService browseService = getBrowseService( authorizationHeader, false ); Map metadatas = browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ); @@ -77,7 +81,7 @@ public class BrowseServiceTest createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" ); - BrowseService browseService = getBrowseService( authorizationHeader ); + BrowseService browseService = getBrowseService( authorizationHeader, false ); Map metadatas = browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ); @@ -98,4 +102,105 @@ public class BrowseServiceTest deleteTestRepo( testRepoId ); } + + @Test + public void browserootGroups() + throws Exception + { + + String testRepoId = "test-repo"; + // force guest user creation if not exists + if ( getUserService( authorizationHeader ).getGuestUser() == null ) + { + assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); + } + + createAndIndexRepo( testRepoId, "src/test/repo-with-osgi", false ); + + BrowseService browseService = getBrowseService( authorizationHeader, false ); + + BrowseResult browseResult = browseService.getRootGroups( testRepoId ); + assertThat( browseResult ).isNotNull(); + assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 3 ).contains( + new BrowseResultEntry( "commons-cli", false ), new BrowseResultEntry( "commons-logging", false ), + new BrowseResultEntry( "org.apache", false ) ); + + deleteTestRepo( testRepoId ); + + } + + @Test + public void browsegroupId() + throws Exception + { + + String testRepoId = "test-repo"; + // force guest user creation if not exists + if ( getUserService( authorizationHeader ).getGuestUser() == null ) + { + assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); + } + + createAndIndexRepo( testRepoId, "src/test/repo-with-osgi", false ); + + BrowseService browseService = getBrowseService( authorizationHeader, false ); + + BrowseResult browseResult = browseService.browseGroupId( "org.apache", testRepoId ); + assertThat( browseResult ).isNotNull(); + assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 2 ).contains( + new BrowseResultEntry( "org.apache.felix", false ), + new BrowseResultEntry( "org.apache.karaf.features", false ) ); + + deleteTestRepo( testRepoId ); + + } + + @Test + public void versionsList() + throws Exception + { + + String testRepoId = "test-repo"; + // force guest user creation if not exists + if ( getUserService( authorizationHeader ).getGuestUser() == null ) + { + assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); + } + + createAndIndexRepo( testRepoId, "src/test/repo-with-osgi", false ); + + BrowseService browseService = getBrowseService( authorizationHeader, false ); + + VersionsList versions = + browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core", testRepoId ); + assertThat( versions ).isNotNull(); + assertThat( versions.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "2.2.1", "2.2.2" ); + + deleteTestRepo( testRepoId ); + + } + + @Test + public void getProjectVersionMetadata() + throws Exception + { + String testRepoId = "test-repo"; + // force guest user creation if not exists + if ( getUserService( authorizationHeader ).getGuestUser() == null ) + { + assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); + } + + createAndIndexRepo( testRepoId, "src/test/repo-with-osgi", false ); + + BrowseService browseService = getBrowseService( authorizationHeader, true ); + + ProjectVersionMetadata metadata = + browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core", + testRepoId ); + + assertThat( metadata ).isNotNull(); + + deleteTestRepo( testRepoId ); + } } -- 2.39.5