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;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
48 import static org.assertj.core.api.Assertions.assertThat;
51 * @author Olivier Lamy
53 public class BrowseServiceTest
54 extends AbstractArchivaRestTest
56 private static final String TEST_REPO_ID = "test-repo";
58 Map<String, String> toMap( List<Entry> entries )
60 Map<String, String> map = new HashMap<>( entries.size() );
62 for ( Entry entry : entries )
64 map.put( entry.getKey(), entry.getValue() );
71 public void metadatagetthenadd()
74 scanRepo( TEST_REPO_ID );
75 waitForScanToComplete( TEST_REPO_ID );
77 BrowseService browseService = getBrowseService( authorizationHeader, false );
79 Map<String, String> metadatas =
80 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
82 assertThat( metadatas ).isNotNull().isEmpty();
84 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
86 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
88 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
93 public void metadatagetthenaddthendelete()
98 scanRepo( TEST_REPO_ID );
99 waitForScanToComplete( TEST_REPO_ID );
101 BrowseService browseService = getBrowseService( authorizationHeader, false );
103 Map<String, String> metadatas =
104 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
106 assertThat( metadatas ).isNotNull().isEmpty();
108 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
110 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
112 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
114 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
116 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
118 assertThat( metadatas ).isNotNull().isEmpty();
120 catch ( ArchivaRestServiceException e )
122 log.error( e.getMessage(), e );
128 public void browserootGroups()
131 BrowseService browseService = getBrowseService( authorizationHeader, false );
133 BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
134 assertThat( browseResult ).isNotNull();
135 assertThat( browseResult.getBrowseResultEntries() ) //
139 .contains( new BrowseResultEntry( "commons-cli", false ), //
140 new BrowseResultEntry( "commons-logging", false ), //
141 new BrowseResultEntry( "org.apache", false ) );
145 public void browsegroupId()
148 BrowseService browseService = getBrowseService( authorizationHeader, false );
150 BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
151 assertThat( browseResult ).isNotNull();
152 assertThat( browseResult.getBrowseResultEntries() ) //
156 .contains( new BrowseResultEntry( "org.apache.felix", false ), //
157 new BrowseResultEntry( "org.apache.karaf.features", false ) );
161 public void listUserRepositories()
164 initSourceTargetRepo();
165 BrowseService browseService = getBrowseService( authorizationHeader, false );
167 List<ManagedRepository> browseResult = browseService.getUserRepositories();
168 assertThat( browseResult )
172 List<String> repIds = new ArrayList<>();
173 for(ManagedRepository rep : browseResult) {
174 repIds.add(rep.getId());
176 assertThat(repIds).contains("internal","snapshots","test-repo","test-copy-target","test-origin-repo");
182 public void listUserManagableRepositories()
185 initSourceTargetRepo();
186 // Giving the guest user a manager role
187 String name = "Repository Manager - internal";
188 Role role = getRoleManagementService( authorizationHeader ).getRole( name );
189 role.setUsers( Arrays.asList( getUserService( authorizationHeader ).getUser( "guest" ) ) );
190 getRoleManagementService( authorizationHeader ).updateRoleUsers( role );
192 // browseService with guest user
193 BrowseService browseService = getBrowseService( "", false );
195 List<ManagedRepository> browseResult = browseService.getUserManagableRepositories();
196 assertThat( browseResult )
198 .isNotEmpty().hasSize(1);
199 List<String> repIds = new ArrayList<>();
200 for(ManagedRepository rep : browseResult) {
201 repIds.add(rep.getId());
203 assertThat(repIds).contains("internal");
208 public void browsegroupIdWithReleaseStartNumber()
211 BrowseService browseService = getBrowseService( authorizationHeader, false );
212 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
213 log.info( "browseResult: {}", browseResult );
217 public void versionsList()
220 BrowseService browseService = getBrowseService( authorizationHeader, false );
222 VersionsList versions =
223 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
225 assertThat( versions ).isNotNull();
226 assertThat( versions.getVersions() ) //
230 .contains( "2.2.1", "2.2.2" );
234 public void getProjectVersionMetadata()
237 BrowseService browseService = getBrowseService( authorizationHeader, true );
239 ProjectVersionMetadata metadata =
240 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
243 assertThat( metadata ).isNotNull();
247 public void readArtifactContentEntries()
250 BrowseService browseService = getBrowseService( authorizationHeader, true );
252 List<ArtifactContentEntry> artifactContentEntries =
253 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
256 log.info( "artifactContentEntries: {}", artifactContentEntries );
258 assertThat( artifactContentEntries ).isNotNull() //
261 .contains( new ArtifactContentEntry( "org", false, 0, TEST_REPO_ID ), //
262 new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
266 public void readArtifactContentEntriesRootPath()
269 BrowseService browseService = getBrowseService( authorizationHeader, true );
271 List<ArtifactContentEntry> artifactContentEntries =
272 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
275 log.info( "artifactContentEntries: {}", artifactContentEntries );
277 assertThat( artifactContentEntries ).isNotNull() //
280 .contains( new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
284 public void readArtifactContentEntriesFilesAndDirectories()
287 BrowseService browseService = getBrowseService( authorizationHeader, true );
289 List<ArtifactContentEntry> artifactContentEntries =
290 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
291 "org/apache/commons/logging/", TEST_REPO_ID );
293 log.info( "artifactContentEntries: {}", artifactContentEntries );
295 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
296 new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
297 new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
301 public void getArtifactDownloadInfos()
304 BrowseService browseService = getBrowseService( authorizationHeader, true );
306 List<Artifact> artifactDownloadInfos =
307 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
309 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
310 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
315 public void getArtifactsByMetadata()
318 // START SNIPPET: get-artifacts-by-metadata
319 BrowseService browseService = getBrowseService( authorizationHeader, true );
321 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
323 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
324 // END SNIPPET: get-artifacts-by-metadata
329 public void getArtifactsByProjectVersionMetadata()
332 // START SNIPPET: get-artifacts-by-project-version-metadata
333 BrowseService browseService = getBrowseService( authorizationHeader, true );
335 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
338 List<Artifact> artifactDownloadInfos =
339 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
341 assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 3 );
342 // END SNIPPET: get-artifacts-by-project-version-metadata
348 public void getArtifactsByProjectVersionMetadataWithNoRepository()
351 final BrowseService browseService = getBrowseService( authorizationHeader, true );
353 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
357 List<Artifact> artifactDownloadInfos =
361 artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
363 catch ( ArchivaRestServiceException e )
365 throw new AssertionError( "ArchivaRestServiceException", e );
367 assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 3 );
373 public void getArtifactsByProperty()
376 // START SNIPPET: get-artifacts-by-property
377 BrowseService browseService = getBrowseService( authorizationHeader, true );
379 List<Artifact> artifactDownloadInfos =
380 browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
382 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
383 // END SNIPPET: get-artifacts-by-property
388 public void searchArtifacts()
391 // START SNIPPET: search-artifacts
392 BrowseService browseService = getBrowseService( authorizationHeader, true );
395 List<Artifact> artifactDownloadInfos =
396 browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
398 assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 7 );
400 // END SNIPPET: search-artifacts
405 public void searchArtifactsByField()
408 // START SNIPPET: search-artifacts-by-field
409 BrowseService browseService = getBrowseService( authorizationHeader, true );
411 List<Artifact> artifactDownloadInfos =
412 browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
414 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
415 // END SNIPPET: search-artifacts-by-field
420 public void readArtifactContentText()
423 BrowseService browseService = getBrowseService( authorizationHeader, true );
425 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
428 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
429 "org/apache/commons/logging/LogSource.java",
430 TEST_REPO_ID ).getContent();
432 log.debug( "text: {}", text );
434 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
439 public void readArtifactContentTextPom()
442 BrowseService browseService = getBrowseService( authorizationHeader, true );
444 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
447 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
448 TEST_REPO_ID ).getContent();
450 log.info( "text: {}", text );
452 assertThat( text ).contains(
453 "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
454 "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
459 public void artifactsNumber()
462 BrowseService browseService = getBrowseService( authorizationHeader, true );
464 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
466 int number = browseService.getArtifacts( TEST_REPO_ID ).size();
468 log.info( "getArtifactsNumber: {}", number );
470 assertTrue( number > 1 );
474 public void metadatainbatchmode()
477 scanRepo( TEST_REPO_ID );
478 waitForScanToComplete( TEST_REPO_ID );
480 BrowseService browseService = getBrowseService( authorizationHeader, false );
482 Map<String, String> inputMetadata = new HashMap<>( 3 );
483 inputMetadata.put( "buildNumber", "1" );
484 inputMetadata.put( "author", "alecharp" );
485 inputMetadata.put( "jenkins_version", "1.486" );
487 MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
488 metadataAddRequest.setGroupId( "commons-cli" );
489 metadataAddRequest.setArtifactId( "commons-cli" );
490 metadataAddRequest.setVersion( "1.0" );
491 metadataAddRequest.setMetadatas( inputMetadata );
492 browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
494 Map<String, String> metadatas =
495 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
497 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
498 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
502 public void initialiseTestRepo()
503 throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
505 // force guest user creation if not exists
506 if ( getUserService( authorizationHeader ).getGuestUser() == null )
508 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
511 createAndIndexRepo( TEST_REPO_ID, getProjectDirectory().resolve( "src/test/repo-with-osgi" ),
514 waitForScanToComplete( TEST_REPO_ID );
518 public void deleteTestRepo()
521 scanRepo( TEST_REPO_ID );
522 waitForScanToComplete( TEST_REPO_ID );
523 deleteTestRepo( TEST_REPO_ID );