]> source.dussan.org Git - archiva.git/blob
9490cf6a6d1b60c03fc44db5aab3be3847bc800d
[archiva.git] /
1 package org.apache.archiva.rest.services;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
21 import org.apache.archiva.maven2.model.Artifact;
22 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
23 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
24 import org.apache.archiva.rest.api.model.ArtifactContentEntry;
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.Entry;
28 import org.apache.archiva.rest.api.model.MetadataAddRequest;
29 import org.apache.archiva.rest.api.model.VersionsList;
30 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
31 import org.apache.archiva.rest.api.services.BrowseService;
32 import org.apache.cxf.jaxrs.client.WebClient;
33 import org.assertj.core.data.MapEntry;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import javax.ws.rs.core.MediaType;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46
47 /**
48  * @author Olivier Lamy
49  */
50 public class BrowseServiceTest
51     extends AbstractArchivaRestTest
52 {
53     private static final String TEST_REPO_ID = "test-repo";
54
55     Map<String, String> toMap( List<Entry> entries )
56     {
57         Map<String, String> map = new HashMap<>( entries.size() );
58
59         for ( Entry entry : entries )
60         {
61             map.put( entry.getKey(), entry.getValue() );
62         }
63
64         return map;
65     }
66
67
68     @Test
69     public void metadatagetthenadd()
70         throws Exception
71     {
72         scanRepo( TEST_REPO_ID );
73
74         BrowseService browseService = getBrowseService( authorizationHeader, false );
75
76         Map<String, String> metadatas =
77             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
78
79         assertThat( metadatas ).isNotNull().isEmpty();
80
81         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
82
83         metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
84
85         assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
86     }
87
88
89     @Test
90     public void metadatagetthenaddthendelete()
91         throws Exception
92     {
93         scanRepo( TEST_REPO_ID );
94
95         BrowseService browseService = getBrowseService( authorizationHeader, false );
96
97         Map<String, String> metadatas =
98             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
99
100         assertThat( metadatas ).isNotNull().isEmpty();
101
102         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
103
104         metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
105
106         assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
107
108         browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
109
110         metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
111
112         assertThat( metadatas ).isNotNull().isEmpty();
113     }
114
115     @Test
116     public void browserootGroups()
117         throws Exception
118     {
119         BrowseService browseService = getBrowseService( authorizationHeader, false );
120
121         BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
122         assertThat( browseResult ).isNotNull();
123         assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 3 ).contains(
124             new BrowseResultEntry( "commons-cli", false ), new BrowseResultEntry( "commons-logging", false ),
125             new BrowseResultEntry( "org.apache", false ) );
126     }
127
128     @Test
129     public void browsegroupId()
130         throws Exception
131     {
132         BrowseService browseService = getBrowseService( authorizationHeader, false );
133
134         BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
135         assertThat( browseResult ).isNotNull();
136         assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
137             new BrowseResultEntry( "org.apache.felix", false ),
138             new BrowseResultEntry( "org.apache.karaf.features", false ) );
139     }
140
141
142     @Test
143     public void browsegroupIdWithReleaseStartNumber()
144         throws Exception
145     {
146         BrowseService browseService = getBrowseService( authorizationHeader, false );
147         BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
148         log.info( "browseResult: {}", browseResult );
149     }
150
151     @Test
152     public void versionsList()
153         throws Exception
154     {
155         BrowseService browseService = getBrowseService( authorizationHeader, false );
156
157         VersionsList versions =
158             browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core", TEST_REPO_ID );
159         assertThat( versions ).isNotNull();
160         assertThat( versions.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "2.2.1", "2.2.2" );
161     }
162
163     @Test
164     public void getProjectVersionMetadata()
165         throws Exception
166     {
167         BrowseService browseService = getBrowseService( authorizationHeader, true );
168
169         ProjectVersionMetadata metadata = browseService.getProjectVersionMetadata( "org.apache.karaf.features",
170                                                                                    "org.apache.karaf.features.core",
171                                                                                    TEST_REPO_ID );
172
173         assertThat( metadata ).isNotNull();
174     }
175
176     @Test
177     public void readArtifactContentEntries()
178         throws Exception
179     {
180         BrowseService browseService = getBrowseService( authorizationHeader, true );
181
182         List<ArtifactContentEntry> artifactContentEntries =
183             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
184                                                      TEST_REPO_ID );
185
186         log.info( "artifactContentEntries: {}", artifactContentEntries );
187
188         assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains( new ArtifactContentEntry(
189             "org", false, 0, TEST_REPO_ID ), new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
190     }
191
192     @Test
193     public void readArtifactContentEntriesRootPath()
194         throws Exception
195     {
196         BrowseService browseService = getBrowseService( authorizationHeader, true );
197
198         List<ArtifactContentEntry> artifactContentEntries =
199             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
200                                                      TEST_REPO_ID );
201
202         log.info( "artifactContentEntries: {}", artifactContentEntries );
203
204         assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains(
205             new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
206     }
207
208     @Test
209     public void readArtifactContentEntriesFilesAndDirectories()
210         throws Exception
211     {
212         BrowseService browseService = getBrowseService( authorizationHeader, true );
213
214         List<ArtifactContentEntry> artifactContentEntries =
215             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
216                                                      "org/apache/commons/logging/", TEST_REPO_ID );
217
218         log.info( "artifactContentEntries: {}", artifactContentEntries );
219
220         assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
221             new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
222             new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
223     }
224
225     @Test
226     public void getArtifactDownloadInfos()
227         throws Exception
228     {
229         BrowseService browseService = getBrowseService( authorizationHeader, true );
230
231         List<Artifact> artifactDownloadInfos =
232             browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
233
234         log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
235         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
236     }
237
238
239     @Test
240     public void getArtifactsByMetadata()
241         throws Exception
242     {
243         // START SNIPPET: get-artifacts-by-metadata
244         BrowseService browseService = getBrowseService( authorizationHeader, true );
245
246         List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
247
248         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
249         // END SNIPPET: get-artifacts-by-metadata
250     }
251
252
253     @Test
254     public void getArtifactsByProjectVersionMetadata()
255         throws Exception
256     {
257         // START SNIPPET: get-artifacts-by-project-version-metadata
258         BrowseService browseService = getBrowseService( authorizationHeader, true );
259
260         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
261
262         List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
263
264         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
265         // END SNIPPET: get-artifacts-by-project-version-metadata
266     }
267
268
269     @Test
270     public void getArtifactsByProjectVersionMetadataWithNoRepository()
271         throws Exception
272     {
273         BrowseService browseService = getBrowseService( authorizationHeader, true );
274
275         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
276
277         List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
278
279         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
280     }
281
282
283     @Test
284     public void getArtifactsByProperty()
285         throws Exception
286     {
287         // START SNIPPET: get-artifacts-by-property
288         BrowseService browseService = getBrowseService( authorizationHeader, true );
289
290         List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
291
292         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
293         // END SNIPPET: get-artifacts-by-property
294     }
295
296
297     @Test
298     public void searchArtifacts()
299         throws Exception
300     {
301         // START SNIPPET: search-artifacts
302         BrowseService browseService = getBrowseService( authorizationHeader, true );
303
304         List<Artifact> artifactDownloadInfos = browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
305
306         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
307         // END SNIPPET: search-artifacts
308     }
309
310
311     @Test
312     public void searchArtifactsByField()
313         throws Exception
314     {
315         // START SNIPPET: search-artifacts-by-field
316         BrowseService browseService = getBrowseService( authorizationHeader, true );
317
318         List<Artifact> artifactDownloadInfos = browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
319
320         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
321         // END SNIPPET: search-artifacts-by-field
322     }
323
324
325     @Test
326     public void readArtifactContentText()
327         throws Exception
328     {
329         BrowseService browseService = getBrowseService( authorizationHeader, true );
330
331         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
332
333         String text =
334             browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
335                                                   "org/apache/commons/logging/LogSource.java",
336                                                   TEST_REPO_ID ).getContent();
337
338         log.debug( "text: {}", text );
339
340         assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
341     }
342
343
344     @Test
345     public void readArtifactContentTextPom()
346         throws Exception
347     {
348         BrowseService browseService = getBrowseService( authorizationHeader, true );
349
350         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
351
352         String text =
353             browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
354                                                   TEST_REPO_ID ).getContent();
355
356         log.info( "text: {}", text );
357
358         assertThat( text ).contains(
359             "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
360             "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
361     }
362
363
364     @Test
365     public void artifactsNumber()
366         throws Exception
367     {
368         BrowseService browseService = getBrowseService( authorizationHeader, true );
369
370         //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
371
372         int number = browseService.getArtifacts( TEST_REPO_ID ).size();
373
374         log.info( "getArtifactsNumber: {}", number );
375
376         assertTrue( number > 1 );
377     }
378
379     @Test
380     public void metadatainbatchmode()
381         throws Exception
382     {
383         scanRepo( TEST_REPO_ID );
384
385         BrowseService browseService = getBrowseService( authorizationHeader, false );
386
387         Map<String, String> inputMetadata = new HashMap<>( 3 );
388         inputMetadata.put( "buildNumber", "1" );
389         inputMetadata.put( "author", "alecharp" );
390         inputMetadata.put( "jenkins_version", "1.486" );
391
392         MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
393         metadataAddRequest.setGroupId( "commons-cli" );
394         metadataAddRequest.setArtifactId( "commons-cli" );
395         metadataAddRequest.setVersion( "1.0" );
396         metadataAddRequest.setMetadatas( inputMetadata );
397         browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
398
399         Map<String, String> metadatas =
400             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
401
402         assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
403             MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
404     }
405
406     @Before
407     public void initialiseTestRepo()
408         throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
409     {
410         // force guest user creation if not exists
411         if ( getUserService( authorizationHeader ).getGuestUser() == null )
412         {
413             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
414         }
415
416         createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
417                             false );
418
419         waitForScanToComplete( TEST_REPO_ID );
420     }
421
422     @After
423     public void deleteTestRepo()
424         throws Exception
425     {
426         scanRepo( TEST_REPO_ID );
427         waitForScanToComplete( TEST_REPO_ID );
428         deleteTestRepo( TEST_REPO_ID );
429     }
430 }