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;
39 import java.io.IOException;
40 import java.util.HashMap;
41 import java.util.List;
43 import javax.ws.rs.core.MediaType;
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() );
69 public void metadatagetthenadd()
72 scanRepo( 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()
93 scanRepo( TEST_REPO_ID );
95 BrowseService browseService = getBrowseService( authorizationHeader, false );
97 Map<String, String> metadatas =
98 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
100 assertThat( metadatas ).isNotNull().isEmpty();
102 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
104 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
106 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
108 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
110 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
112 assertThat( metadatas ).isNotNull().isEmpty();
116 public void browserootGroups()
119 BrowseService browseService = getBrowseService( authorizationHeader, false );
121 BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
122 assertThat( browseResult ).isNotNull();
123 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 3 ).contains(
124 new BrowseResultEntry( "commons-cli", false ), new BrowseResultEntry( "commons-logging", false ),
125 new BrowseResultEntry( "org.apache", false ) );
129 public void browsegroupId()
132 BrowseService browseService = getBrowseService( authorizationHeader, false );
134 BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
135 assertThat( browseResult ).isNotNull();
136 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
137 new BrowseResultEntry( "org.apache.felix", false ),
138 new BrowseResultEntry( "org.apache.karaf.features", false ) );
143 public void browsegroupIdWithReleaseStartNumber()
146 BrowseService browseService = getBrowseService( authorizationHeader, false );
147 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
148 log.info( "browseResult: {}", browseResult );
152 public void versionsList()
155 BrowseService browseService = getBrowseService( authorizationHeader, false );
157 VersionsList versions =
158 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core", TEST_REPO_ID );
159 assertThat( versions ).isNotNull();
160 assertThat( versions.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "2.2.1", "2.2.2" );
164 public void getProjectVersionMetadata()
167 BrowseService browseService = getBrowseService( authorizationHeader, true );
169 ProjectVersionMetadata metadata = browseService.getProjectVersionMetadata( "org.apache.karaf.features",
170 "org.apache.karaf.features.core",
173 assertThat( metadata ).isNotNull();
177 public void readArtifactContentEntries()
180 BrowseService browseService = getBrowseService( authorizationHeader, true );
182 List<ArtifactContentEntry> artifactContentEntries =
183 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
186 log.info( "artifactContentEntries: {}", artifactContentEntries );
188 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains( new ArtifactContentEntry(
189 "org", false, 0, TEST_REPO_ID ), new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
193 public void readArtifactContentEntriesRootPath()
196 BrowseService browseService = getBrowseService( authorizationHeader, true );
198 List<ArtifactContentEntry> artifactContentEntries =
199 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
202 log.info( "artifactContentEntries: {}", artifactContentEntries );
204 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains(
205 new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
209 public void readArtifactContentEntriesFilesAndDirectories()
212 BrowseService browseService = getBrowseService( authorizationHeader, true );
214 List<ArtifactContentEntry> artifactContentEntries =
215 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
216 "org/apache/commons/logging/", TEST_REPO_ID );
218 log.info( "artifactContentEntries: {}", artifactContentEntries );
220 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
221 new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
222 new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
226 public void getArtifactDownloadInfos()
229 BrowseService browseService = getBrowseService( authorizationHeader, true );
231 List<Artifact> artifactDownloadInfos =
232 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
234 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
235 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
240 public void getArtifactsByMetadata()
243 // START SNIPPET: get-artifacts-by-metadata
244 BrowseService browseService = getBrowseService( authorizationHeader, true );
246 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
248 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
249 // END SNIPPET: get-artifacts-by-metadata
254 public void getArtifactsByProjectVersionMetadata()
257 // START SNIPPET: get-artifacts-by-project-version-metadata
258 BrowseService browseService = getBrowseService( authorizationHeader, true );
260 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
262 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
264 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
265 // END SNIPPET: get-artifacts-by-project-version-metadata
270 public void getArtifactsByProjectVersionMetadataWithNoRepository()
273 BrowseService browseService = getBrowseService( authorizationHeader, true );
275 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
277 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
279 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
284 public void getArtifactsByProperty()
287 // START SNIPPET: get-artifacts-by-property
288 BrowseService browseService = getBrowseService( authorizationHeader, true );
290 List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
292 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
293 // END SNIPPET: get-artifacts-by-property
298 public void searchArtifacts()
301 // START SNIPPET: search-artifacts
302 BrowseService browseService = getBrowseService( authorizationHeader, true );
304 List<Artifact> artifactDownloadInfos = browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
306 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
307 // END SNIPPET: search-artifacts
312 public void searchArtifactsByField()
315 // START SNIPPET: search-artifacts-by-field
316 BrowseService browseService = getBrowseService( authorizationHeader, true );
318 List<Artifact> artifactDownloadInfos = browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
320 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
321 // END SNIPPET: search-artifacts-by-field
326 public void readArtifactContentText()
329 BrowseService browseService = getBrowseService( authorizationHeader, true );
331 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
334 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
335 "org/apache/commons/logging/LogSource.java",
336 TEST_REPO_ID ).getContent();
338 log.debug( "text: {}", text );
340 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
345 public void readArtifactContentTextPom()
348 BrowseService browseService = getBrowseService( authorizationHeader, true );
350 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
353 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
354 TEST_REPO_ID ).getContent();
356 log.info( "text: {}", text );
358 assertThat( text ).contains(
359 "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
360 "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
365 public void artifactsNumber()
368 BrowseService browseService = getBrowseService( authorizationHeader, true );
370 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
372 int number = browseService.getArtifacts( TEST_REPO_ID ).size();
374 log.info( "getArtifactsNumber: {}", number );
376 assertTrue( number > 1 );
380 public void metadatainbatchmode()
383 scanRepo( TEST_REPO_ID );
385 BrowseService browseService = getBrowseService( authorizationHeader, false );
387 Map<String, String> inputMetadata = new HashMap<>( 3 );
388 inputMetadata.put( "buildNumber", "1" );
389 inputMetadata.put( "author", "alecharp" );
390 inputMetadata.put( "jenkins_version", "1.486" );
392 MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
393 metadataAddRequest.setGroupId( "commons-cli" );
394 metadataAddRequest.setArtifactId( "commons-cli" );
395 metadataAddRequest.setVersion( "1.0" );
396 metadataAddRequest.setMetadatas( inputMetadata );
397 browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
399 Map<String, String> metadatas =
400 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
402 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
403 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
407 public void initialiseTestRepo()
408 throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
410 // force guest user creation if not exists
411 if ( getUserService( authorizationHeader ).getGuestUser() == null )
413 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
416 createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
419 waitForScanToComplete( TEST_REPO_ID );
423 public void deleteTestRepo()
426 scanRepo( TEST_REPO_ID );
427 waitForScanToComplete( TEST_REPO_ID );
428 deleteTestRepo( TEST_REPO_ID );