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 );
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 ) );
159 public void browsegroupIdWithReleaseStartNumber()
162 BrowseService browseService = getBrowseService( authorizationHeader, false );
163 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
164 log.info( "browseResult: {}", browseResult );
168 public void versionsList()
171 BrowseService browseService = getBrowseService( authorizationHeader, false );
173 VersionsList versions =
174 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
176 assertThat( versions ).isNotNull();
177 assertThat( versions.getVersions() ) //
181 .contains( "2.2.1", "2.2.2" );
185 public void getProjectVersionMetadata()
188 BrowseService browseService = getBrowseService( authorizationHeader, true );
190 ProjectVersionMetadata metadata =
191 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
194 assertThat( metadata ).isNotNull();
198 public void readArtifactContentEntries()
201 BrowseService browseService = getBrowseService( authorizationHeader, true );
203 List<ArtifactContentEntry> artifactContentEntries =
204 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
207 log.info( "artifactContentEntries: {}", artifactContentEntries );
209 assertThat( artifactContentEntries ).isNotNull() //
212 .contains( new ArtifactContentEntry( "org", false, 0, TEST_REPO_ID ), //
213 new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
217 public void readArtifactContentEntriesRootPath()
220 BrowseService browseService = getBrowseService( authorizationHeader, true );
222 List<ArtifactContentEntry> artifactContentEntries =
223 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
226 log.info( "artifactContentEntries: {}", artifactContentEntries );
228 assertThat( artifactContentEntries ).isNotNull() //
231 .contains( new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
235 public void readArtifactContentEntriesFilesAndDirectories()
238 BrowseService browseService = getBrowseService( authorizationHeader, true );
240 List<ArtifactContentEntry> artifactContentEntries =
241 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
242 "org/apache/commons/logging/", TEST_REPO_ID );
244 log.info( "artifactContentEntries: {}", artifactContentEntries );
246 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
247 new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
248 new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
252 public void getArtifactDownloadInfos()
255 BrowseService browseService = getBrowseService( authorizationHeader, true );
257 List<Artifact> artifactDownloadInfos =
258 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
260 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
261 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
266 public void getArtifactsByMetadata()
269 // START SNIPPET: get-artifacts-by-metadata
270 BrowseService browseService = getBrowseService( authorizationHeader, true );
272 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
274 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
275 // END SNIPPET: get-artifacts-by-metadata
280 public void getArtifactsByProjectVersionMetadata()
283 // START SNIPPET: get-artifacts-by-project-version-metadata
284 BrowseService browseService = getBrowseService( authorizationHeader, true );
286 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
288 List<Artifact> artifactDownloadInfos =
289 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
291 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
292 // END SNIPPET: get-artifacts-by-project-version-metadata
297 public void getArtifactsByProjectVersionMetadataWithNoRepository()
300 BrowseService browseService = getBrowseService( authorizationHeader, true );
302 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
304 List<Artifact> artifactDownloadInfos =
305 browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
307 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
312 public void getArtifactsByProperty()
315 // START SNIPPET: get-artifacts-by-property
316 BrowseService browseService = getBrowseService( authorizationHeader, true );
318 List<Artifact> artifactDownloadInfos =
319 browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
321 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
322 // END SNIPPET: get-artifacts-by-property
327 public void searchArtifacts()
330 // START SNIPPET: search-artifacts
331 BrowseService browseService = getBrowseService( authorizationHeader, true );
333 List<Artifact> artifactDownloadInfos =
334 browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
336 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
337 // END SNIPPET: search-artifacts
342 public void searchArtifactsByField()
345 // START SNIPPET: search-artifacts-by-field
346 BrowseService browseService = getBrowseService( authorizationHeader, true );
348 List<Artifact> artifactDownloadInfos =
349 browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
351 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
352 // END SNIPPET: search-artifacts-by-field
357 public void readArtifactContentText()
360 BrowseService browseService = getBrowseService( authorizationHeader, true );
362 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
365 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
366 "org/apache/commons/logging/LogSource.java",
367 TEST_REPO_ID ).getContent();
369 log.debug( "text: {}", text );
371 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
376 public void readArtifactContentTextPom()
379 BrowseService browseService = getBrowseService( authorizationHeader, true );
381 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
384 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
385 TEST_REPO_ID ).getContent();
387 log.info( "text: {}", text );
389 assertThat( text ).contains(
390 "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
391 "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
396 public void artifactsNumber()
399 BrowseService browseService = getBrowseService( authorizationHeader, true );
401 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
403 int number = browseService.getArtifacts( TEST_REPO_ID ).size();
405 log.info( "getArtifactsNumber: {}", number );
407 assertTrue( number > 1 );
411 public void metadatainbatchmode()
414 scanRepo( TEST_REPO_ID );
415 waitForScanToComplete( TEST_REPO_ID );
417 BrowseService browseService = getBrowseService( authorizationHeader, false );
419 Map<String, String> inputMetadata = new HashMap<>( 3 );
420 inputMetadata.put( "buildNumber", "1" );
421 inputMetadata.put( "author", "alecharp" );
422 inputMetadata.put( "jenkins_version", "1.486" );
424 MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
425 metadataAddRequest.setGroupId( "commons-cli" );
426 metadataAddRequest.setArtifactId( "commons-cli" );
427 metadataAddRequest.setVersion( "1.0" );
428 metadataAddRequest.setMetadatas( inputMetadata );
429 browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
431 Map<String, String> metadatas =
432 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
434 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
435 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
439 public void initialiseTestRepo()
440 throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
442 // force guest user creation if not exists
443 if ( getUserService( authorizationHeader ).getGuestUser() == null )
445 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
448 createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
451 waitForScanToComplete( TEST_REPO_ID );
455 public void deleteTestRepo()
458 scanRepo( TEST_REPO_ID );
459 waitForScanToComplete( TEST_REPO_ID );
460 deleteTestRepo( TEST_REPO_ID );