1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
40 import javax.ws.rs.core.MediaType;
42 import java.io.IOException;
45 import static org.assertj.core.api.Assertions.assertThat;
48 * @author Olivier Lamy
50 public class BrowseServiceTest
51 extends AbstractArchivaRestTest
53 private static final String TEST_REPO_ID = "test-repo";
55 Map<String, String> toMap( List<Entry> entries )
57 Map<String, String> map = new HashMap<>( entries.size() );
59 for ( Entry entry : entries )
61 map.put( entry.getKey(), entry.getValue() );
68 public void metadatagetthenadd()
71 scanRepo( TEST_REPO_ID );
72 waitForScanToComplete( TEST_REPO_ID );
74 BrowseService browseService = getBrowseService( authorizationHeader, false );
76 Map<String, String> metadatas =
77 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
79 assertThat( metadatas ).isNotNull().isEmpty();
81 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
83 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
85 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
90 public void metadatagetthenaddthendelete()
95 scanRepo( TEST_REPO_ID );
96 waitForScanToComplete( TEST_REPO_ID );
98 BrowseService browseService = getBrowseService( authorizationHeader, false );
100 Map<String, String> metadatas =
101 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
103 assertThat( metadatas ).isNotNull().isEmpty();
105 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
107 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
109 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
111 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
113 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
115 assertThat( metadatas ).isNotNull().isEmpty();
117 catch ( ArchivaRestServiceException e )
119 log.error( e.getMessage(), e );
125 public void browserootGroups()
128 BrowseService browseService = getBrowseService( authorizationHeader, false );
130 BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
131 assertThat( browseResult ).isNotNull();
132 assertThat( browseResult.getBrowseResultEntries() ) //
136 .contains( new BrowseResultEntry( "commons-cli", false ), //
137 new BrowseResultEntry( "commons-logging", false ), //
138 new BrowseResultEntry( "org.apache", false ) );
142 public void browsegroupId()
145 BrowseService browseService = getBrowseService( authorizationHeader, false );
147 BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
148 assertThat( browseResult ).isNotNull();
149 assertThat( browseResult.getBrowseResultEntries() ) //
153 .contains( new BrowseResultEntry( "org.apache.felix", false ), //
154 new BrowseResultEntry( "org.apache.karaf.features", false ) );
158 public void listUserRepositories()
161 initSourceTargetRepo();
162 BrowseService browseService = getBrowseService( authorizationHeader, false );
164 List<ManagedRepository> browseResult = browseService.getUserRepositories();
165 assertThat( browseResult )
169 List<String> repIds = new ArrayList<>();
170 for(ManagedRepository rep : browseResult) {
171 repIds.add(rep.getId());
173 assertThat(repIds).contains("internal","snapshots","test-repo","test-copy-target","test-origin-repo");
179 public void listUserManagableRepositories()
182 initSourceTargetRepo();
183 // Giving the guest user a manager role
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 );
189 // browseService with guest user
190 BrowseService browseService = getBrowseService( "", false );
192 List<ManagedRepository> browseResult = browseService.getUserManagableRepositories();
193 assertThat( browseResult )
195 .isNotEmpty().hasSize(1);
196 List<String> repIds = new ArrayList<>();
197 for(ManagedRepository rep : browseResult) {
198 repIds.add(rep.getId());
200 assertThat(repIds).contains("internal");
205 public void browsegroupIdWithReleaseStartNumber()
208 BrowseService browseService = getBrowseService( authorizationHeader, false );
209 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
210 log.info( "browseResult: {}", browseResult );
214 public void versionsList()
217 BrowseService browseService = getBrowseService( authorizationHeader, false );
219 VersionsList versions =
220 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
222 assertThat( versions ).isNotNull();
223 assertThat( versions.getVersions() ) //
227 .contains( "2.2.1", "2.2.2" );
231 public void getProjectVersionMetadata()
234 BrowseService browseService = getBrowseService( authorizationHeader, true );
236 ProjectVersionMetadata metadata =
237 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
240 assertThat( metadata ).isNotNull();
244 public void readArtifactContentEntries()
247 BrowseService browseService = getBrowseService( authorizationHeader, true );
249 List<ArtifactContentEntry> artifactContentEntries =
250 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
253 log.info( "artifactContentEntries: {}", artifactContentEntries );
255 assertThat( artifactContentEntries ).isNotNull() //
258 .contains( new ArtifactContentEntry( "org", false, 0, TEST_REPO_ID ), //
259 new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
263 public void readArtifactContentEntriesRootPath()
266 BrowseService browseService = getBrowseService( authorizationHeader, true );
268 List<ArtifactContentEntry> artifactContentEntries =
269 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
272 log.info( "artifactContentEntries: {}", artifactContentEntries );
274 assertThat( artifactContentEntries ).isNotNull() //
277 .contains( new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
281 public void readArtifactContentEntriesFilesAndDirectories()
284 BrowseService browseService = getBrowseService( authorizationHeader, true );
286 List<ArtifactContentEntry> artifactContentEntries =
287 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
288 "org/apache/commons/logging/", TEST_REPO_ID );
290 log.info( "artifactContentEntries: {}", artifactContentEntries );
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 ) );
298 public void getArtifactDownloadInfos()
301 BrowseService browseService = getBrowseService( authorizationHeader, true );
303 List<Artifact> artifactDownloadInfos =
304 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
306 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
307 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
312 public void getArtifactsByMetadata()
315 // START SNIPPET: get-artifacts-by-metadata
316 BrowseService browseService = getBrowseService( authorizationHeader, true );
318 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
320 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
321 // END SNIPPET: get-artifacts-by-metadata
326 public void getArtifactsByProjectVersionMetadata()
329 // START SNIPPET: get-artifacts-by-project-version-metadata
330 BrowseService browseService = getBrowseService( authorizationHeader, true );
332 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
334 List<Artifact> artifactDownloadInfos =
335 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
337 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
338 // END SNIPPET: get-artifacts-by-project-version-metadata
343 public void getArtifactsByProjectVersionMetadataWithNoRepository()
346 BrowseService browseService = getBrowseService( authorizationHeader, true );
348 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
350 List<Artifact> artifactDownloadInfos =
351 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
353 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
358 public void getArtifactsByProperty()
361 // START SNIPPET: get-artifacts-by-property
362 BrowseService browseService = getBrowseService( authorizationHeader, true );
364 List<Artifact> artifactDownloadInfos =
365 browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
367 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
368 // END SNIPPET: get-artifacts-by-property
373 public void searchArtifacts()
376 // START SNIPPET: search-artifacts
377 BrowseService browseService = getBrowseService( authorizationHeader, true );
379 List<Artifact> artifactDownloadInfos =
380 browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
382 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
383 // END SNIPPET: search-artifacts
388 public void searchArtifactsByField()
391 // START SNIPPET: search-artifacts-by-field
392 BrowseService browseService = getBrowseService( authorizationHeader, true );
394 List<Artifact> artifactDownloadInfos =
395 browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
397 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
398 // END SNIPPET: search-artifacts-by-field
403 public void readArtifactContentText()
406 BrowseService browseService = getBrowseService( authorizationHeader, true );
408 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
411 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
412 "org/apache/commons/logging/LogSource.java",
413 TEST_REPO_ID ).getContent();
415 log.debug( "text: {}", text );
417 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
422 public void readArtifactContentTextPom()
425 BrowseService browseService = getBrowseService( authorizationHeader, true );
427 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
430 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
431 TEST_REPO_ID ).getContent();
433 log.info( "text: {}", text );
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>" );
442 public void artifactsNumber()
445 BrowseService browseService = getBrowseService( authorizationHeader, true );
447 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
449 int number = browseService.getArtifacts( TEST_REPO_ID ).size();
451 log.info( "getArtifactsNumber: {}", number );
453 assertTrue( number > 1 );
457 public void metadatainbatchmode()
460 scanRepo( TEST_REPO_ID );
461 waitForScanToComplete( TEST_REPO_ID );
463 BrowseService browseService = getBrowseService( authorizationHeader, false );
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" );
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 );
477 Map<String, String> metadatas =
478 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
480 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
481 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
485 public void initialiseTestRepo()
486 throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
488 // force guest user creation if not exists
489 if ( getUserService( authorizationHeader ).getGuestUser() == null )
491 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
494 createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
497 waitForScanToComplete( TEST_REPO_ID );
501 public void deleteTestRepo()
504 scanRepo( TEST_REPO_ID );
505 waitForScanToComplete( TEST_REPO_ID );
506 deleteTestRepo( TEST_REPO_ID );