]> source.dussan.org Git - archiva.git/blob
99cb83b5dbd3b0d5143a2237e6cb97f6c6d40990
[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 readArtifactContentText()
241         throws Exception
242     {
243         BrowseService browseService = getBrowseService( authorizationHeader, true );
244
245         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
246
247         String text =
248             browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
249                                                   "org/apache/commons/logging/LogSource.java",
250                                                   TEST_REPO_ID ).getContent();
251
252         log.debug( "text: {}", text );
253
254         assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
255     }
256
257
258     @Test
259     public void readArtifactContentTextPom()
260         throws Exception
261     {
262         BrowseService browseService = getBrowseService( authorizationHeader, true );
263
264         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
265
266         String text =
267             browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
268                                                   TEST_REPO_ID ).getContent();
269
270         log.info( "text: {}", text );
271
272         assertThat( text ).contains(
273             "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
274             "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
275     }
276
277
278     @Test
279     public void artifactsNumber()
280         throws Exception
281     {
282         BrowseService browseService = getBrowseService( authorizationHeader, true );
283
284         //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
285
286         int number = browseService.getArtifacts( TEST_REPO_ID ).size();
287
288         log.info( "getArtifactsNumber: {}", number );
289
290         assertTrue( number > 1 );
291     }
292
293     @Test
294     public void metadatainbatchmode()
295         throws Exception
296     {
297         scanRepo( TEST_REPO_ID );
298
299         BrowseService browseService = getBrowseService( authorizationHeader, false );
300
301         Map<String, String> inputMetadata = new HashMap<>( 3 );
302         inputMetadata.put( "buildNumber", "1" );
303         inputMetadata.put( "author", "alecharp" );
304         inputMetadata.put( "jenkins_version", "1.486" );
305
306         MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
307         metadataAddRequest.setGroupId( "commons-cli" );
308         metadataAddRequest.setArtifactId( "commons-cli" );
309         metadataAddRequest.setVersion( "1.0" );
310         metadataAddRequest.setMetadatas( inputMetadata );
311         browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
312
313         Map<String, String> metadatas =
314             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
315
316         assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
317             MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
318     }
319
320     @Before
321     public void initialiseTestRepo()
322         throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
323     {
324         // force guest user creation if not exists
325         if ( getUserService( authorizationHeader ).getGuestUser() == null )
326         {
327             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
328         }
329
330         createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
331                             false );
332
333         waitForScanToComplete( TEST_REPO_ID );
334     }
335
336     @After
337     public void deleteTestRepo()
338         throws Exception
339     {
340         scanRepo( TEST_REPO_ID );
341         waitForScanToComplete( TEST_REPO_ID );
342         deleteTestRepo( TEST_REPO_ID );
343     }
344 }