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.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;
38 import javax.ws.rs.core.MediaType;
40 import java.io.IOException;
41 import java.util.HashMap;
42 import java.util.List;
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 );
73 BrowseService browseService = getBrowseService( authorizationHeader, false );
75 Map<String, String> metadatas =
76 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
78 assertThat( metadatas ).isNotNull().isEmpty();
80 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
82 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
84 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
89 public void metadatagetthenaddthendelete()
94 scanRepo( TEST_REPO_ID );
96 BrowseService browseService = getBrowseService( authorizationHeader, false );
98 Map<String, String> metadatas =
99 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
101 assertThat( metadatas ).isNotNull().isEmpty();
103 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
105 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
107 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
109 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
111 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
113 assertThat( metadatas ).isNotNull().isEmpty();
115 catch ( ArchivaRestServiceException e )
117 log.error( e.getMessage(), e );
123 public void browserootGroups()
126 BrowseService browseService = getBrowseService( authorizationHeader, false );
128 BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
129 assertThat( browseResult ).isNotNull();
130 assertThat( browseResult.getBrowseResultEntries() ) //
134 .contains( new BrowseResultEntry( "commons-cli", false ), //
135 new BrowseResultEntry( "commons-logging", false ), //
136 new BrowseResultEntry( "org.apache", false ) );
140 public void browsegroupId()
143 BrowseService browseService = getBrowseService( authorizationHeader, false );
145 BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
146 assertThat( browseResult ).isNotNull();
147 assertThat( browseResult.getBrowseResultEntries() ) //
151 .contains( new BrowseResultEntry( "org.apache.felix", false ), //
152 new BrowseResultEntry( "org.apache.karaf.features", false ) );
157 public void browsegroupIdWithReleaseStartNumber()
160 BrowseService browseService = getBrowseService( authorizationHeader, false );
161 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
162 log.info( "browseResult: {}", browseResult );
166 public void versionsList()
169 BrowseService browseService = getBrowseService( authorizationHeader, false );
171 VersionsList versions =
172 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
174 assertThat( versions ).isNotNull();
175 assertThat( versions.getVersions() ) //
179 .contains( "2.2.1", "2.2.2" );
183 public void getProjectVersionMetadata()
186 BrowseService browseService = getBrowseService( authorizationHeader, true );
188 ProjectVersionMetadata metadata =
189 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
192 assertThat( metadata ).isNotNull();
196 public void readArtifactContentEntries()
199 BrowseService browseService = getBrowseService( authorizationHeader, true );
201 List<ArtifactContentEntry> artifactContentEntries =
202 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
205 log.info( "artifactContentEntries: {}", artifactContentEntries );
207 assertThat( artifactContentEntries ).isNotNull() //
210 .contains( new ArtifactContentEntry( "org", false, 0, TEST_REPO_ID ), //
211 new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
215 public void readArtifactContentEntriesRootPath()
218 BrowseService browseService = getBrowseService( authorizationHeader, true );
220 List<ArtifactContentEntry> artifactContentEntries =
221 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
224 log.info( "artifactContentEntries: {}", artifactContentEntries );
226 assertThat( artifactContentEntries ).isNotNull() //
229 .contains( new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
233 public void readArtifactContentEntriesFilesAndDirectories()
236 BrowseService browseService = getBrowseService( authorizationHeader, true );
238 List<ArtifactContentEntry> artifactContentEntries =
239 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
240 "org/apache/commons/logging/", TEST_REPO_ID );
242 log.info( "artifactContentEntries: {}", artifactContentEntries );
244 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
245 new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
246 new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
250 public void getArtifactDownloadInfos()
253 BrowseService browseService = getBrowseService( authorizationHeader, true );
255 List<Artifact> artifactDownloadInfos =
256 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
258 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
259 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
264 public void getArtifactsByMetadata()
267 // START SNIPPET: get-artifacts-by-metadata
268 BrowseService browseService = getBrowseService( authorizationHeader, true );
270 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
272 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
273 // END SNIPPET: get-artifacts-by-metadata
278 public void getArtifactsByProjectVersionMetadata()
281 // START SNIPPET: get-artifacts-by-project-version-metadata
282 BrowseService browseService = getBrowseService( authorizationHeader, true );
284 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
286 List<Artifact> artifactDownloadInfos =
287 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
289 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
290 // END SNIPPET: get-artifacts-by-project-version-metadata
295 public void getArtifactsByProjectVersionMetadataWithNoRepository()
298 BrowseService browseService = getBrowseService( authorizationHeader, true );
300 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
302 List<Artifact> artifactDownloadInfos =
303 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
305 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
310 public void getArtifactsByProperty()
313 // START SNIPPET: get-artifacts-by-property
314 BrowseService browseService = getBrowseService( authorizationHeader, true );
316 List<Artifact> artifactDownloadInfos =
317 browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
319 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
320 // END SNIPPET: get-artifacts-by-property
325 public void searchArtifacts()
328 // START SNIPPET: search-artifacts
329 BrowseService browseService = getBrowseService( authorizationHeader, true );
331 List<Artifact> artifactDownloadInfos =
332 browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
334 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
335 // END SNIPPET: search-artifacts
340 public void searchArtifactsByField()
343 // START SNIPPET: search-artifacts-by-field
344 BrowseService browseService = getBrowseService( authorizationHeader, true );
346 List<Artifact> artifactDownloadInfos =
347 browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
349 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
350 // END SNIPPET: search-artifacts-by-field
355 public void readArtifactContentText()
358 BrowseService browseService = getBrowseService( authorizationHeader, true );
360 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
363 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
364 "org/apache/commons/logging/LogSource.java",
365 TEST_REPO_ID ).getContent();
367 log.debug( "text: {}", text );
369 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
374 public void readArtifactContentTextPom()
377 BrowseService browseService = getBrowseService( authorizationHeader, true );
379 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
382 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
383 TEST_REPO_ID ).getContent();
385 log.info( "text: {}", text );
387 assertThat( text ).contains(
388 "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
389 "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
394 public void artifactsNumber()
397 BrowseService browseService = getBrowseService( authorizationHeader, true );
399 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
401 int number = browseService.getArtifacts( TEST_REPO_ID ).size();
403 log.info( "getArtifactsNumber: {}", number );
405 assertTrue( number > 1 );
409 public void metadatainbatchmode()
412 scanRepo( TEST_REPO_ID );
414 BrowseService browseService = getBrowseService( authorizationHeader, false );
416 Map<String, String> inputMetadata = new HashMap<>( 3 );
417 inputMetadata.put( "buildNumber", "1" );
418 inputMetadata.put( "author", "alecharp" );
419 inputMetadata.put( "jenkins_version", "1.486" );
421 MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
422 metadataAddRequest.setGroupId( "commons-cli" );
423 metadataAddRequest.setArtifactId( "commons-cli" );
424 metadataAddRequest.setVersion( "1.0" );
425 metadataAddRequest.setMetadatas( inputMetadata );
426 browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
428 Map<String, String> metadatas =
429 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
431 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
432 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
436 public void initialiseTestRepo()
437 throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
439 // force guest user creation if not exists
440 if ( getUserService( authorizationHeader ).getGuestUser() == null )
442 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
445 createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
448 waitForScanToComplete( TEST_REPO_ID );
452 public void deleteTestRepo()
455 scanRepo( TEST_REPO_ID );
456 waitForScanToComplete( TEST_REPO_ID );
457 deleteTestRepo( TEST_REPO_ID );