1 package org.apache.archiva.rest.services;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.archiva.admin.model.beans.ManagedRepository;
22 import org.apache.archiva.maven2.model.Artifact;
23 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
24 import org.apache.archiva.redback.rest.api.model.Role;
25 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
26 import org.apache.archiva.rest.api.model.ArtifactContentEntry;
27 import org.apache.archiva.rest.api.model.BrowseResult;
28 import org.apache.archiva.rest.api.model.BrowseResultEntry;
29 import org.apache.archiva.rest.api.model.Entry;
30 import org.apache.archiva.rest.api.model.MetadataAddRequest;
31 import org.apache.archiva.rest.api.model.VersionsList;
32 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
33 import org.apache.archiva.rest.api.services.BrowseService;
34 import org.apache.cxf.jaxrs.client.WebClient;
35 import org.assertj.core.data.MapEntry;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39
40 import javax.ws.rs.core.MediaType;
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.*;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46
47
48
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 @Test
68 public void metadatagetthenadd()
69 throws Exception
70 {
71 scanRepo( TEST_REPO_ID );
72 waitForScanToComplete( 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 try
94 {
95 scanRepo( TEST_REPO_ID );
96 waitForScanToComplete( TEST_REPO_ID );
97
98 BrowseService browseService = getBrowseService( authorizationHeader, false );
99
100 Map<String, String> metadatas =
101 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
102
103 assertThat( metadatas ).isNotNull().isEmpty();
104
105 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
106
107 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
108
109 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
110
111 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
112
113 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
114
115 assertThat( metadatas ).isNotNull().isEmpty();
116 }
117 catch ( ArchivaRestServiceException e )
118 {
119 log.error( e.getMessage(), e );
120 throw e;
121 }
122 }
123
124 @Test
125 public void browserootGroups()
126 throws Exception
127 {
128 BrowseService browseService = getBrowseService( authorizationHeader, false );
129
130 BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
131 assertThat( browseResult ).isNotNull();
132 assertThat( browseResult.getBrowseResultEntries() )
133 .isNotNull()
134 .isNotEmpty()
135 .hasSize( 3 )
136 .contains( new BrowseResultEntry( "commons-cli", false ),
137 new BrowseResultEntry( "commons-logging", false ),
138 new BrowseResultEntry( "org.apache", false ) );
139 }
140
141 @Test
142 public void browsegroupId()
143 throws Exception
144 {
145 BrowseService browseService = getBrowseService( authorizationHeader, false );
146
147 BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
148 assertThat( browseResult ).isNotNull();
149 assertThat( browseResult.getBrowseResultEntries() )
150 .isNotNull()
151 .isNotEmpty()
152 .hasSize( 2 )
153 .contains( new BrowseResultEntry( "org.apache.felix", false ),
154 new BrowseResultEntry( "org.apache.karaf.features", false ) );
155 }
156
157 @Test
158 public void listUserRepositories()
159 throws Exception
160 {
161 initSourceTargetRepo();
162 BrowseService browseService = getBrowseService( authorizationHeader, false );
163
164 List<ManagedRepository> browseResult = browseService.getUserRepositories();
165 assertThat( browseResult )
166 .isNotNull()
167 .isNotEmpty()
168 .hasSize(5);
169 List<String> repIds = new ArrayList<>();
170 for(ManagedRepository rep : browseResult) {
171 repIds.add(rep.getId());
172 }
173 assertThat(repIds).contains("internal","snapshots","test-repo","test-copy-target","test-origin-repo");
174
175 }
176
177
178 @Test
179 public void listUserManagableRepositories()
180 throws Exception
181 {
182 initSourceTargetRepo();
183
184 String name = "Repository Manager - internal";
185 Role role = getRoleManagementService( authorizationHeader ).getRole( name );
186 role.setUsers( Arrays.asList( getUserService( authorizationHeader ).getUser( "guest" ) ) );
187 getRoleManagementService( authorizationHeader ).updateRoleUsers( role );
188
189
190 BrowseService browseService = getBrowseService( "", false );
191
192 List<ManagedRepository> browseResult = browseService.getUserManagableRepositories();
193 assertThat( browseResult )
194 .isNotNull()
195 .isNotEmpty().hasSize(1);
196 List<String> repIds = new ArrayList<>();
197 for(ManagedRepository rep : browseResult) {
198 repIds.add(rep.getId());
199 }
200 assertThat(repIds).contains("internal");
201
202 }
203
204 @Test
205 public void browsegroupIdWithReleaseStartNumber()
206 throws Exception
207 {
208 BrowseService browseService = getBrowseService( authorizationHeader, false );
209 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
210 log.info( "browseResult: {}", browseResult );
211 }
212
213 @Test
214 public void versionsList()
215 throws Exception
216 {
217 BrowseService browseService = getBrowseService( authorizationHeader, false );
218
219 VersionsList versions =
220 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
221 TEST_REPO_ID );
222 assertThat( versions ).isNotNull();
223 assertThat( versions.getVersions() )
224 .isNotNull()
225 .isNotEmpty()
226 .hasSize( 2 )
227 .contains( "2.2.1", "2.2.2" );
228 }
229
230 @Test
231 public void getProjectVersionMetadata()
232 throws Exception
233 {
234 BrowseService browseService = getBrowseService( authorizationHeader, true );
235
236 ProjectVersionMetadata metadata =
237 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
238 TEST_REPO_ID );
239
240 assertThat( metadata ).isNotNull();
241 }
242
243 @Test
244 public void readArtifactContentEntries()
245 throws Exception
246 {
247 BrowseService browseService = getBrowseService( authorizationHeader, true );
248
249 List<ArtifactContentEntry> artifactContentEntries =
250 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
251 TEST_REPO_ID );
252
253 log.info( "artifactContentEntries: {}", artifactContentEntries );
254
255 assertThat( artifactContentEntries ).isNotNull()
256 .isNotEmpty()
257 .hasSize( 2 )
258 .contains( new ArtifactContentEntry( "org", false, 0, TEST_REPO_ID ),
259 new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
260 }
261
262 @Test
263 public void readArtifactContentEntriesRootPath()
264 throws Exception
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 TEST_REPO_ID );
271
272 log.info( "artifactContentEntries: {}", artifactContentEntries );
273
274 assertThat( artifactContentEntries ).isNotNull()
275 .isNotEmpty()
276 .hasSize( 1 )
277 .contains( new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
278 }
279
280 @Test
281 public void readArtifactContentEntriesFilesAndDirectories()
282 throws Exception
283 {
284 BrowseService browseService = getBrowseService( authorizationHeader, true );
285
286 List<ArtifactContentEntry> artifactContentEntries =
287 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
288 "org/apache/commons/logging/", TEST_REPO_ID );
289
290 log.info( "artifactContentEntries: {}", artifactContentEntries );
291
292 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
293 new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
294 new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
295 }
296
297 @Test
298 public void getArtifactDownloadInfos()
299 throws Exception
300 {
301 BrowseService browseService = getBrowseService( authorizationHeader, true );
302
303 List<Artifact> artifactDownloadInfos =
304 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
305
306 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
307 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
308 }
309
310
311 @Test
312 public void getArtifactsByMetadata()
313 throws Exception
314 {
315
316 BrowseService browseService = getBrowseService( authorizationHeader, true );
317
318 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
319
320 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
321
322 }
323
324
325 @Test
326 public void getArtifactsByProjectVersionMetadata()
327 throws Exception
328 {
329
330 BrowseService browseService = getBrowseService( authorizationHeader, true );
331
332 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
333
334 List<Artifact> artifactDownloadInfos =
335 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
336
337 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
338
339 }
340
341
342 @Test
343 public void getArtifactsByProjectVersionMetadataWithNoRepository()
344 throws Exception
345 {
346 BrowseService browseService = getBrowseService( authorizationHeader, true );
347
348 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
349
350 List<Artifact> artifactDownloadInfos =
351 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
352
353 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
354 }
355
356
357 @Test
358 public void getArtifactsByProperty()
359 throws Exception
360 {
361
362 BrowseService browseService = getBrowseService( authorizationHeader, true );
363
364 List<Artifact> artifactDownloadInfos =
365 browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
366
367 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
368
369 }
370
371
372 @Test
373 public void searchArtifacts()
374 throws Exception
375 {
376
377 BrowseService browseService = getBrowseService( authorizationHeader, true );
378
379 List<Artifact> artifactDownloadInfos =
380 browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
381
382 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
383
384 }
385
386
387 @Test
388 public void searchArtifactsByField()
389 throws Exception
390 {
391
392 BrowseService browseService = getBrowseService( authorizationHeader, true );
393
394 List<Artifact> artifactDownloadInfos =
395 browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
396
397 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
398
399 }
400
401
402 @Test
403 public void readArtifactContentText()
404 throws Exception
405 {
406 BrowseService browseService = getBrowseService( authorizationHeader, true );
407
408 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
409
410 String text =
411 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
412 "org/apache/commons/logging/LogSource.java",
413 TEST_REPO_ID ).getContent();
414
415 log.debug( "text: {}", text );
416
417 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
418 }
419
420
421 @Test
422 public void readArtifactContentTextPom()
423 throws Exception
424 {
425 BrowseService browseService = getBrowseService( authorizationHeader, true );
426
427 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
428
429 String text =
430 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
431 TEST_REPO_ID ).getContent();
432
433 log.info( "text: {}", text );
434
435 assertThat( text ).contains(
436 "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
437 "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
438 }
439
440
441 @Test
442 public void artifactsNumber()
443 throws Exception
444 {
445 BrowseService browseService = getBrowseService( authorizationHeader, true );
446
447
448
449 int number = browseService.getArtifacts( TEST_REPO_ID ).size();
450
451 log.info( "getArtifactsNumber: {}", number );
452
453 assertTrue( number > 1 );
454 }
455
456 @Test
457 public void metadatainbatchmode()
458 throws Exception
459 {
460 scanRepo( TEST_REPO_ID );
461 waitForScanToComplete( TEST_REPO_ID );
462
463 BrowseService browseService = getBrowseService( authorizationHeader, false );
464
465 Map<String, String> inputMetadata = new HashMap<>( 3 );
466 inputMetadata.put( "buildNumber", "1" );
467 inputMetadata.put( "author", "alecharp" );
468 inputMetadata.put( "jenkins_version", "1.486" );
469
470 MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
471 metadataAddRequest.setGroupId( "commons-cli" );
472 metadataAddRequest.setArtifactId( "commons-cli" );
473 metadataAddRequest.setVersion( "1.0" );
474 metadataAddRequest.setMetadatas( inputMetadata );
475 browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
476
477 Map<String, String> metadatas =
478 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
479
480 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
481 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
482 }
483
484 @Before
485 public void initialiseTestRepo()
486 throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
487 {
488
489 if ( getUserService( authorizationHeader ).getGuestUser() == null )
490 {
491 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
492 }
493
494 createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
495 false );
496
497 waitForScanToComplete( TEST_REPO_ID );
498 }
499
500 @After
501 public void deleteTestRepo()
502 throws Exception
503 {
504 scanRepo( TEST_REPO_ID );
505 waitForScanToComplete( TEST_REPO_ID );
506 deleteTestRepo( TEST_REPO_ID );
507 }
508 }