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 readArtifactContentText()
243 BrowseService browseService = getBrowseService( authorizationHeader, true );
245 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
248 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
249 "org/apache/commons/logging/LogSource.java",
250 TEST_REPO_ID ).getContent();
252 log.debug( "text: {}", text );
254 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
259 public void readArtifactContentTextPom()
262 BrowseService browseService = getBrowseService( authorizationHeader, true );
264 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
267 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
268 TEST_REPO_ID ).getContent();
270 log.info( "text: {}", text );
272 assertThat( text ).contains(
273 "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
274 "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
279 public void artifactsNumber()
282 BrowseService browseService = getBrowseService( authorizationHeader, true );
284 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
286 int number = browseService.getArtifacts( TEST_REPO_ID ).size();
288 log.info( "getArtifactsNumber: {}", number );
290 assertTrue( number > 1 );
294 public void metadatainbatchmode()
297 scanRepo( TEST_REPO_ID );
299 BrowseService browseService = getBrowseService( authorizationHeader, false );
301 Map<String, String> inputMetadata = new HashMap<>( 3 );
302 inputMetadata.put( "buildNumber", "1" );
303 inputMetadata.put( "author", "alecharp" );
304 inputMetadata.put( "jenkins_version", "1.486" );
306 MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
307 metadataAddRequest.setGroupId( "commons-cli" );
308 metadataAddRequest.setArtifactId( "commons-cli" );
309 metadataAddRequest.setVersion( "1.0" );
310 metadataAddRequest.setMetadatas( inputMetadata );
311 browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
313 Map<String, String> metadatas =
314 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
316 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
317 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
321 public void initialiseTestRepo()
322 throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
324 // force guest user creation if not exists
325 if ( getUserService( authorizationHeader ).getGuestUser() == null )
327 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
330 createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
333 waitForScanToComplete( TEST_REPO_ID );
337 public void deleteTestRepo()
340 deleteTestRepo( TEST_REPO_ID );