1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
22 import org.apache.archiva.rest.api.model.ArtifactContentEntry;
23 import org.apache.archiva.rest.api.model.BrowseResult;
24 import org.apache.archiva.rest.api.model.BrowseResultEntry;
25 import org.apache.archiva.rest.api.model.Entry;
26 import org.apache.archiva.rest.api.model.VersionsList;
27 import org.apache.archiva.rest.api.services.BrowseService;
28 import org.fest.assertions.MapAssert;
29 import org.junit.Test;
32 import java.util.HashMap;
33 import java.util.List;
36 import static org.fest.assertions.Assertions.assertThat;
39 * @author Olivier Lamy
41 public class BrowseServiceTest
42 extends AbstractArchivaRestTest
45 Map<String, String> toMap( List<Entry> entries )
47 Map<String, String> map = new HashMap<String, String>( entries.size() );
49 for ( Entry entry : entries )
51 map.put( entry.getKey(), entry.getValue() );
58 public void metadatagetthenadd()
62 String testRepoId = "test-repo";
63 // force guest user creation if not exists
64 if ( getUserService( authorizationHeader ).getGuestUser() == null )
66 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
69 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
71 BrowseService browseService = getBrowseService( authorizationHeader, false );
73 Map<String, String> metadatas =
74 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
76 assertThat( metadatas ).isNotNull().isEmpty();
78 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
80 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
82 assertThat( metadatas ).isNotNull().isNotEmpty().includes( MapAssert.entry( "wine", "bordeaux" ) );
84 deleteTestRepo( testRepoId );
90 public void metadatagetthenaddthendelete()
94 String testRepoId = "test-repo";
95 // force guest user creation if not exists
96 if ( getUserService( authorizationHeader ).getGuestUser() == null )
98 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
101 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
103 BrowseService browseService = getBrowseService( authorizationHeader, false );
105 Map<String, String> metadatas =
106 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
108 assertThat( metadatas ).isNotNull().isEmpty();
110 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
112 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
114 assertThat( metadatas ).isNotNull().isNotEmpty().includes( MapAssert.entry( "wine", "bordeaux" ) );
116 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", testRepoId );
118 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
120 assertThat( metadatas ).isNotNull().isEmpty();
122 deleteTestRepo( testRepoId );
127 public void browserootGroups()
131 String testRepoId = "test-repo";
132 // force guest user creation if not exists
133 if ( getUserService( authorizationHeader ).getGuestUser() == null )
135 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
138 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
140 BrowseService browseService = getBrowseService( authorizationHeader, false );
142 BrowseResult browseResult = browseService.getRootGroups( testRepoId );
143 assertThat( browseResult ).isNotNull();
144 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 3 ).contains(
145 new BrowseResultEntry( "commons-cli", false ), new BrowseResultEntry( "commons-logging", false ),
146 new BrowseResultEntry( "org.apache", false ) );
148 deleteTestRepo( testRepoId );
153 public void browsegroupId()
157 String testRepoId = "test-repo";
158 // force guest user creation if not exists
159 if ( getUserService( authorizationHeader ).getGuestUser() == null )
161 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
164 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
166 BrowseService browseService = getBrowseService( authorizationHeader, false );
168 BrowseResult browseResult = browseService.browseGroupId( "org.apache", testRepoId );
169 assertThat( browseResult ).isNotNull();
170 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
171 new BrowseResultEntry( "org.apache.felix", false ),
172 new BrowseResultEntry( "org.apache.karaf.features", false ) );
174 deleteTestRepo( testRepoId );
179 public void versionsList()
183 String testRepoId = "test-repo";
184 // force guest user creation if not exists
185 if ( getUserService( authorizationHeader ).getGuestUser() == null )
187 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
190 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
192 BrowseService browseService = getBrowseService( authorizationHeader, false );
194 VersionsList versions =
195 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core", testRepoId );
196 assertThat( versions ).isNotNull();
197 assertThat( versions.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "2.2.1", "2.2.2" );
199 deleteTestRepo( testRepoId );
204 public void getProjectVersionMetadata()
207 String testRepoId = "test-repo";
208 // force guest user creation if not exists
209 if ( getUserService( authorizationHeader ).getGuestUser() == null )
211 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
214 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
216 BrowseService browseService = getBrowseService( authorizationHeader, true );
218 ProjectVersionMetadata metadata =
219 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
222 assertThat( metadata ).isNotNull();
224 deleteTestRepo( testRepoId );
228 public void readArtifactContentEntries()
231 String testRepoId = "test-repo";
232 // force guest user creation if not exists
233 if ( getUserService( authorizationHeader ).getGuestUser() == null )
235 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
238 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
240 BrowseService browseService = getBrowseService( authorizationHeader, true );
242 List<ArtifactContentEntry> artifactContentEntries =
243 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
246 log.info( "artifactContentEntries: {}", artifactContentEntries );
248 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
249 new ArtifactContentEntry( "org", false, 0 ), new ArtifactContentEntry( "META-INF", false, 0 ) );
250 deleteTestRepo( testRepoId );
254 public void readArtifactContentEntriesRootPath()
257 String testRepoId = "test-repo";
258 // force guest user creation if not exists
259 if ( getUserService( authorizationHeader ).getGuestUser() == null )
261 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
264 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
266 BrowseService browseService = getBrowseService( authorizationHeader, true );
268 List<ArtifactContentEntry> artifactContentEntries =
269 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
272 log.info( "artifactContentEntries: {}", artifactContentEntries );
274 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains(
275 new ArtifactContentEntry( "org/apache", false, 1 ) );
276 deleteTestRepo( testRepoId );
280 public void readArtifactContentEntriesFilesAndDirectories()
283 String testRepoId = "test-repo";
284 // force guest user creation if not exists
285 if ( getUserService( authorizationHeader ).getGuestUser() == null )
287 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
290 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
292 BrowseService browseService = getBrowseService( authorizationHeader, true );
294 List<ArtifactContentEntry> artifactContentEntries =
295 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
296 "org/apache/commons/logging/", testRepoId );
298 log.info( "artifactContentEntries: {}", artifactContentEntries );
300 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
301 new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4 ),
302 new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4 ) );
303 deleteTestRepo( testRepoId );