1 package org.apache.archiva.rest.services;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.archiva.rest.api.model.Artifact;
25 import org.apache.archiva.rest.api.model.BrowseResult;
26 import org.apache.archiva.rest.api.model.BrowseResultEntry;
27 import org.apache.archiva.rest.api.model.VersionsList;
28 import org.apache.archiva.rest.api.services.BrowseService;
29 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
30 import org.apache.archiva.rest.api.services.RepositoriesService;
31 import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
32 import org.fest.assertions.Assertions;
33 import org.junit.Test;
36 import java.util.List;
39 * @author Olivier Lamy
41 public class RepositoriesServiceTest
42 extends AbstractArchivaRestTest
45 @Test( expected = ServerWebApplicationException.class )
46 public void scanRepoKarmaFailed()
49 RepositoriesService service = getRepositoriesService();
52 service.scanRepository( "id", true );
54 catch ( ServerWebApplicationException e )
56 assertEquals( 403, e.getStatus() );
62 public void scanRepo()
65 RepositoriesService service = getRepositoriesService( authorizationHeader );
67 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
69 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
72 while ( timeout > 0 && service.alreadyScanning( repoId ) )
78 assertTrue( service.scanRepository( repoId, true ) );
81 @Test( expected = ServerWebApplicationException.class )
82 public void deleteArtifactKarmaFailed()
87 Artifact artifact = new Artifact();
88 artifact.setGroupId( "commons-logging" );
89 artifact.setArtifactId( "commons-logging" );
90 artifact.setVersion( "1.0.1" );
91 artifact.setPackaging( "jar" );
92 artifact.setContext( SOURCE_REPO_ID );
94 RepositoriesService repositoriesService = getRepositoriesService( null );
96 repositoriesService.deleteArtifact( artifact );
98 catch ( ServerWebApplicationException e )
100 assertEquals( 403, e.getStatus() );
106 @Test( expected = ServerWebApplicationException.class )
107 public void deleteWithRepoNull()
113 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
115 Artifact artifact = new Artifact();
116 artifact.setGroupId( "commons-logging" );
117 artifact.setArtifactId( "commons-logging" );
118 artifact.setVersion( "1.0.1" );
119 artifact.setPackaging( "jar" );
121 repositoriesService.deleteArtifact( artifact );
123 catch ( ServerWebApplicationException e )
125 assertEquals( "not http 400 status", 400, e.getStatus() );
132 public void deleteArtifact()
135 initSourceTargetRepo();
137 BrowseService browseService = getBrowseService( authorizationHeader, false );
139 List<Artifact> artifacts =
140 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
141 "2.2.2", SOURCE_REPO_ID );
143 log.info( "artifacts: {}", artifacts );
145 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
147 VersionsList versionsList =
148 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
150 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
152 log.info( "artifacts.size: {}", artifacts.size() );
156 File artifactFile = new File(
157 "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
159 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
161 Artifact artifact = new Artifact();
162 artifact.setGroupId( "org.apache.karaf.features" );
163 artifact.setArtifactId( "org.apache.karaf.features.core" );
164 artifact.setVersion( "2.2.2" );
165 artifact.setPackaging( "jar" );
166 artifact.setContext( SOURCE_REPO_ID );
168 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
170 repositoriesService.deleteArtifact( artifact );
172 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
175 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
176 "2.2.2", SOURCE_REPO_ID );
178 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
180 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
183 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
193 public void deleteArtifactWithClassifier()
196 initSourceTargetRepo();
198 BrowseService browseService = getBrowseService( authorizationHeader, false );
200 List<Artifact> artifacts =
201 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
203 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
205 VersionsList versionsList =
206 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
207 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
209 log.info( "artifacts.size: {}", artifacts.size() );
213 File artifactFile = new File(
214 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
216 File artifactFilemd5 = new File(
217 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
219 File artifactFilesha1 = new File(
220 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
222 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
224 assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
225 assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
227 Artifact artifact = new Artifact();
228 artifact.setGroupId( "commons-logging" );
229 artifact.setArtifactId( "commons-logging" );
230 artifact.setVersion( "1.0.1" );
231 artifact.setClassifier( "javadoc" );
232 artifact.setPackaging( "jar" );
233 artifact.setContext( SOURCE_REPO_ID );
235 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
237 repositoriesService.deleteArtifact( artifact );
239 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
240 assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
241 assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
244 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
246 log.info( "artifact: {}", artifacts );
248 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
250 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
252 log.info( "versionsList: {}", versionsList );
254 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
265 public void deleteGroupId()
268 initSourceTargetRepo();
271 BrowseService browseService = getBrowseService( authorizationHeader, false );
273 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
275 assertNotNull( browseResult );
277 Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
278 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
279 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
281 log.info( "browseResult: {}", browseResult );
290 public void authorizedToDeleteArtifacts()
293 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
296 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
297 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
298 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
302 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
308 public void notAuthorizedToDeleteArtifacts()
311 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
314 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
315 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
316 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
320 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
325 protected ManagedRepository getTestManagedRepository( String id, String path )
327 String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
328 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
332 protected ManagedRepository getTestManagedRepository()
334 return getTestManagedRepository( "TEST", "test-repo" );