]> source.dussan.org Git - archiva.git/blob
21ecc85402229ad31b16626aa58e62f9eb03c001
[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.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;
30
31 import java.io.File;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import static org.fest.assertions.Assertions.assertThat;
37
38 /**
39  * @author Olivier Lamy
40  */
41 public class BrowseServiceTest
42     extends AbstractArchivaRestTest
43 {
44
45     Map<String, String> toMap( List<Entry> entries )
46     {
47         Map<String, String> map = new HashMap<String, String>( entries.size() );
48
49         for ( Entry entry : entries )
50         {
51             map.put( entry.getKey(), entry.getValue() );
52         }
53
54         return map;
55     }
56
57     @Test
58     public void metadatagetthenadd()
59         throws Exception
60     {
61
62         String testRepoId = "test-repo";
63         // force guest user creation if not exists
64         if ( getUserService( authorizationHeader ).getGuestUser() == null )
65         {
66             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
67         }
68
69         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
70
71         BrowseService browseService = getBrowseService( authorizationHeader, false );
72
73         Map<String, String> metadatas =
74             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
75
76         assertThat( metadatas ).isNotNull().isEmpty();
77
78         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
79
80         metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
81
82         assertThat( metadatas ).isNotNull().isNotEmpty().includes( MapAssert.entry( "wine", "bordeaux" ) );
83
84         deleteTestRepo( testRepoId );
85
86     }
87
88
89     @Test
90     public void metadatagetthenaddthendelete()
91         throws Exception
92     {
93
94         String testRepoId = "test-repo";
95         // force guest user creation if not exists
96         if ( getUserService( authorizationHeader ).getGuestUser() == null )
97         {
98             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
99         }
100
101         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
102
103         BrowseService browseService = getBrowseService( authorizationHeader, false );
104
105         Map<String, String> metadatas =
106             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
107
108         assertThat( metadatas ).isNotNull().isEmpty();
109
110         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
111
112         metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
113
114         assertThat( metadatas ).isNotNull().isNotEmpty().includes( MapAssert.entry( "wine", "bordeaux" ) );
115
116         browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", testRepoId );
117
118         metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
119
120         assertThat( metadatas ).isNotNull().isEmpty();
121
122         deleteTestRepo( testRepoId );
123
124     }
125
126     @Test
127     public void browserootGroups()
128         throws Exception
129     {
130
131         String testRepoId = "test-repo";
132         // force guest user creation if not exists
133         if ( getUserService( authorizationHeader ).getGuestUser() == null )
134         {
135             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
136         }
137
138         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
139
140         BrowseService browseService = getBrowseService( authorizationHeader, false );
141
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 ) );
147
148         deleteTestRepo( testRepoId );
149
150     }
151
152     @Test
153     public void browsegroupId()
154         throws Exception
155     {
156
157         String testRepoId = "test-repo";
158         // force guest user creation if not exists
159         if ( getUserService( authorizationHeader ).getGuestUser() == null )
160         {
161             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
162         }
163
164         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
165
166         BrowseService browseService = getBrowseService( authorizationHeader, false );
167
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 ) );
173
174         deleteTestRepo( testRepoId );
175
176     }
177
178     @Test
179     public void versionsList()
180         throws Exception
181     {
182
183         String testRepoId = "test-repo";
184         // force guest user creation if not exists
185         if ( getUserService( authorizationHeader ).getGuestUser() == null )
186         {
187             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
188         }
189
190         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
191
192         BrowseService browseService = getBrowseService( authorizationHeader, false );
193
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" );
198
199         deleteTestRepo( testRepoId );
200
201     }
202
203     @Test
204     public void getProjectVersionMetadata()
205         throws Exception
206     {
207         String testRepoId = "test-repo";
208         // force guest user creation if not exists
209         if ( getUserService( authorizationHeader ).getGuestUser() == null )
210         {
211             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
212         }
213
214         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
215
216         BrowseService browseService = getBrowseService( authorizationHeader, true );
217
218         ProjectVersionMetadata metadata =
219             browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
220                                                      testRepoId );
221
222         assertThat( metadata ).isNotNull();
223
224         deleteTestRepo( testRepoId );
225     }
226
227     @Test
228     public void readArtifactContentEntries()
229         throws Exception
230     {
231         String testRepoId = "test-repo";
232         // force guest user creation if not exists
233         if ( getUserService( authorizationHeader ).getGuestUser() == null )
234         {
235             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
236         }
237
238         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
239
240         BrowseService browseService = getBrowseService( authorizationHeader, true );
241
242         List<ArtifactContentEntry> artifactContentEntries =
243             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
244                                                      testRepoId );
245
246         log.info( "artifactContentEntries: {}", artifactContentEntries );
247
248         assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
249             new ArtifactContentEntry( "org", false, 0 ), new ArtifactContentEntry( "META-INF", false, 0 ) );
250         deleteTestRepo( testRepoId );
251     }
252
253     @Test
254     public void readArtifactContentEntriesRootPath()
255         throws Exception
256     {
257         String testRepoId = "test-repo";
258         // force guest user creation if not exists
259         if ( getUserService( authorizationHeader ).getGuestUser() == null )
260         {
261             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
262         }
263
264         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
265
266         BrowseService browseService = getBrowseService( authorizationHeader, true );
267
268         List<ArtifactContentEntry> artifactContentEntries =
269             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
270                                                      testRepoId );
271
272         log.info( "artifactContentEntries: {}", artifactContentEntries );
273
274         assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains(
275             new ArtifactContentEntry( "org/apache", false, 1 ) );
276         deleteTestRepo( testRepoId );
277     }
278
279     @Test
280     public void readArtifactContentEntriesFilesAndDirectories()
281         throws Exception
282     {
283         String testRepoId = "test-repo";
284         // force guest user creation if not exists
285         if ( getUserService( authorizationHeader ).getGuestUser() == null )
286         {
287             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
288         }
289
290         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
291
292         BrowseService browseService = getBrowseService( authorizationHeader, true );
293
294         List<ArtifactContentEntry> artifactContentEntries =
295             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
296                                                      "org/apache/commons/logging/", testRepoId );
297
298         log.info( "artifactContentEntries: {}", artifactContentEntries );
299
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 );
304     }
305
306
307 }