]> source.dussan.org Git - archiva.git/blob
ad492a6c7c19f2d9dbae237cb3419a076460b636
[archiva.git] /
1 package org.apache.archiva.rest.services;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
34
35 import java.io.File;
36 import java.util.List;
37
38 /**
39  * @author Olivier Lamy
40  */
41 public class RepositoriesServiceTest
42     extends AbstractArchivaRestTest
43 {
44
45     @Test( expected = ServerWebApplicationException.class )
46     public void scanRepoKarmaFailed()
47         throws Exception
48     {
49         RepositoriesService service = getRepositoriesService();
50         try
51         {
52             service.scanRepository( "id", true );
53         }
54         catch ( ServerWebApplicationException e )
55         {
56             assertEquals( 403, e.getStatus() );
57             throw e;
58         }
59     }
60
61     @Test
62     public void scanRepo()
63         throws Exception
64     {
65         RepositoriesService service = getRepositoriesService( authorizationHeader );
66
67         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
68
69         String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
70
71         int timeout = 20000;
72         while ( timeout > 0 && service.alreadyScanning( repoId ) )
73         {
74             Thread.sleep( 500 );
75             timeout -= 500;
76         }
77
78         assertTrue( service.scanRepository( repoId, true ) );
79     }
80
81     @Test( expected = ServerWebApplicationException.class )
82     public void deleteArtifactKarmaFailed()
83         throws Exception
84     {
85         try
86         {
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 );
93
94             RepositoriesService repositoriesService = getRepositoriesService( null );
95
96             repositoriesService.deleteArtifact( artifact );
97         }
98         catch ( ServerWebApplicationException e )
99         {
100             assertEquals( 403, e.getStatus() );
101             throw e;
102
103         }
104     }
105
106     @Test( expected = ServerWebApplicationException.class )
107     public void deleteWithRepoNull()
108         throws Exception
109     {
110         try
111         {
112
113             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
114
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" );
120
121             repositoriesService.deleteArtifact( artifact );
122         }
123         catch ( ServerWebApplicationException e )
124         {
125             assertEquals( "not http 400 status", 400, e.getStatus() );
126             throw e;
127         }
128     }
129
130
131     @Test
132     public void deleteArtifact()
133         throws Exception
134     {
135         initSourceTargetRepo();
136
137         BrowseService browseService = getBrowseService( authorizationHeader, false );
138
139         List<Artifact> artifacts =
140             browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
141                                                     "2.2.2", SOURCE_REPO_ID );
142
143         log.info( "artifacts: {}", artifacts );
144
145         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
146
147         VersionsList versionsList =
148             browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
149                                            SOURCE_REPO_ID );
150         Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
151
152         log.info( "artifacts.size: {}", artifacts.size() );
153
154         try
155         {
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" );
158
159             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
160
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 );
167
168             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
169
170             repositoriesService.deleteArtifact( artifact );
171
172             assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
173
174             artifacts =
175                 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
176                                                         "2.2.2", SOURCE_REPO_ID );
177
178             Assertions.assertThat( artifacts ).isNotNull().isEmpty();
179
180             versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
181                                                           SOURCE_REPO_ID );
182
183             Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
184
185         }
186         finally
187         {
188             cleanRepos();
189         }
190     }
191
192     @Test
193     public void deleteArtifactWithClassifier()
194         throws Exception
195     {
196         initSourceTargetRepo();
197
198         BrowseService browseService = getBrowseService( authorizationHeader, false );
199
200         List<Artifact> artifacts =
201             browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
202
203         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
204
205         VersionsList versionsList =
206             browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
207         Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
208
209         log.info( "artifacts.size: {}", artifacts.size() );
210
211         try
212         {
213             File artifactFile = new File(
214                 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
215
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" );
218
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" );
221
222             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
223
224             assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
225             assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
226
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 );
234
235             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
236
237             repositoriesService.deleteArtifact( artifact );
238
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() );
242
243             artifacts =
244                 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
245
246             log.info( "artifact: {}", artifacts );
247
248             Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
249
250             versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
251
252             log.info( "versionsList: {}", versionsList );
253
254             Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
255
256         }
257         finally
258         {
259             cleanRepos();
260         }
261     }
262
263
264     @Test
265     public void deleteGroupId()
266         throws Exception
267     {
268         initSourceTargetRepo();
269         try
270         {
271             BrowseService browseService = getBrowseService( authorizationHeader, false );
272
273             BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
274
275             assertNotNull( browseResult );
276
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 ) );
280
281             log.info( "browseResult: {}", browseResult );
282         }
283         finally
284         {
285             cleanRepos();
286         }
287     }
288
289     @Test
290     public void authorizedToDeleteArtifacts()
291         throws Exception
292     {
293         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
294         try
295         {
296             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
297             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
298             assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
299         }
300         finally
301         {
302             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
303                                                                                           true );
304         }
305     }
306
307     @Test
308     public void notAuthorizedToDeleteArtifacts()
309         throws Exception
310     {
311         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
312         try
313         {
314             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
315             RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
316             assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
317         }
318         finally
319         {
320             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
321                                                                                           true );
322         }
323     }
324
325     protected ManagedRepository getTestManagedRepository( String id, String path )
326     {
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,
329                                       true, false );
330     }
331
332     protected ManagedRepository getTestManagedRepository()
333     {
334         return getTestManagedRepository( "TEST", "test-repo" );
335     }
336
337 }