1 package org.apache.archiva.webdav;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import com.gargoylesoftware.htmlunit.WebRequest;
24 import com.gargoylesoftware.htmlunit.WebResponse;
25 import org.apache.archiva.common.utils.FileUtils;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
29 import org.apache.archiva.maven.metadata.MavenMetadataReader;
30 import org.apache.archiva.model.ArchivaRepositoryMetadata;
31 import org.apache.commons.lang3.StringUtils;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.InputStream;
38 import java.nio.charset.Charset;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.ArrayList;
43 import java.util.List;
45 import static org.assertj.core.api.Assertions.assertThat;
49 * RepositoryServletRepositoryGroupTest
51 * Test Case 1. Accessing a valid repository group root url (e.g. http://machine.com/repository/repository-group/) returns a Bad Request (HTTP 400)
52 * Test Case 2. Accessing an invalid repository group root url is forwarded to managed repository checking (this is not covered here)
53 * Test Case 3. Accessing an artifact in a valid repository group will iterate over the managed repositories in the repository group
54 * Test Case 3.a. If an invalid managed repository is encountered (managed repository doesn't exist),
55 * a Not Found (HTTP 404) is returned and the iteration is broken
56 * Test Case 3.b. If an artifact is not found in a valid managed repository (after proxying, etc.),
57 * a Not Found (HTTP 404) is set but not returned yet, the iteration continues to the next managed repository.
58 * The Not Found (HTTP 404) is returned after exhausting all valid managed repositories
59 * Test Case 3.c. If an artifact is found in a valid managed repository,
60 * the artifact is returned, the iteration is broken and any Not Found (HTTP 404) is disregarded
61 * Test Case 4. Accessing a valid repository group with any http write method returns a Bad Request (HTTP 400)
63 public class RepositoryServletRepositoryGroupTest
64 extends AbstractRepositoryServletTestCase
66 protected Path repoRootFirst;
68 protected Path repoRootLast;
70 protected Path repoRootInvalid;
72 protected static final String MANAGED_REPO_FIRST = "first";
74 protected static final String MANAGED_REPO_LAST = "last";
76 protected static final String MANAGED_REPO_INVALID = "invalid";
78 protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
80 protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
90 String appserverBase = getAppserverBase().toAbsolutePath().toString();
91 log.debug( "Appserver Base {}", appserverBase );
93 Configuration configuration = archivaConfiguration.getConfiguration();
95 repoRootFirst = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
96 repoRootLast = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
98 configuration.addManagedRepository(
99 createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst, true ) );
100 configuration.addManagedRepository(
101 createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast, true ) );
103 List<String> managedRepoIds = new ArrayList<>();
104 managedRepoIds.add( MANAGED_REPO_FIRST );
105 managedRepoIds.add( MANAGED_REPO_LAST );
107 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
109 // Create the repository group with an invalid managed repository
110 repoRootInvalid = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_INVALID );
111 ManagedRepositoryConfiguration managedRepositoryConfiguration =
112 createManagedRepository( MANAGED_REPO_INVALID, "Invalid Test Repo", repoRootInvalid, true );
114 configuration.addManagedRepository( managedRepositoryConfiguration );
116 List<String> invalidManagedRepoIds = new ArrayList<>();
117 invalidManagedRepoIds.add( MANAGED_REPO_FIRST );
118 invalidManagedRepoIds.add( MANAGED_REPO_INVALID );
119 invalidManagedRepoIds.add( MANAGED_REPO_LAST );
121 configuration.addRepositoryGroup(
122 createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
124 configuration.removeManagedRepository( managedRepositoryConfiguration );
125 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRootInvalid );
127 repositoryRegistry.reload();
128 saveConfiguration( archivaConfiguration );
136 public void tearDown()
139 setupCleanRepo( repoRootFirst );
140 setupCleanRepo( repoRootLast );
142 repositoryRegistry.destroy();
146 String appserverBase = System.getProperty( "appserver.base" );
147 if ( StringUtils.isNotEmpty( appserverBase ) )
149 FileUtils.deleteDirectory( Paths.get( appserverBase ) );
157 public void testGetFromFirstManagedRepositoryReturnOk()
160 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
162 Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
163 Files.createDirectories( dummyInternalResourceFile.getParent() );
164 org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile, Charset.defaultCharset(), "first" );
166 WebRequest request = new GetMethodWebRequest(
167 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
168 WebResponse response = getServletUnitClient().getResponse( request );
170 assertResponseOK( response );
171 assertThat( response.getContentAsString() ).isEqualTo( "first" );
178 public void testGetFromLastManagedRepositoryReturnOk()
181 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
183 Path dummyReleasesResourceFile = repoRootLast.resolve( resourceName );
184 Files.createDirectories(dummyReleasesResourceFile.getParent());
185 org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
187 WebRequest request = new GetMethodWebRequest(
188 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
189 WebResponse response = getServletUnitClient().getResponse( request );
191 assertResponseOK( response );
193 assertThat( response.getContentAsString() ).isEqualTo( "last" );
200 public void testGetFromValidRepositoryGroupReturnNotFound()
203 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
205 WebRequest request = new GetMethodWebRequest(
206 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
207 WebResponse response = getServletUnitClient().getResponse( request );
209 assertResponseNotFound( response );
216 public void testPutValidRepositoryGroupReturnBadRequest()
219 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
220 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
221 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
223 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
224 WebResponse response = getServletUnitClient().getResponse( request );
226 assertResponseMethodNotAllowed( response );
231 public void testGetMergedMetadata()
234 // first metadata file
235 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
237 Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
238 Files.createDirectories(dummyInternalResourceFile.getParent());
239 org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile,
240 Charset.defaultCharset(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
241 + "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n"
242 + "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n"
243 + "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>" );
245 //second metadata file
246 resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
247 dummyInternalResourceFile = repoRootLast.resolve( resourceName );
248 Files.createDirectories(dummyInternalResourceFile.getParent());
249 org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile, Charset.defaultCharset(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
250 + "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>"
251 + "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>"
252 + "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>"
253 + "</versioning></metadata>" );
255 WebRequest request = new GetMethodWebRequest(
256 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
257 + "dummy-merged-metadata-resource/maven-metadata.xml" );
258 WebResponse response = getServletUnitClient().getResource( request );
260 Path returnedMetadata = getProjectBase().resolve( "target/test-classes/retrievedMetadataFile.xml" );
261 org.apache.archiva.common.utils.FileUtils.writeStringToFile( returnedMetadata, Charset.defaultCharset(), response.getContentAsString() );
262 MavenMetadataReader metadataReader = new MavenMetadataReader( );
263 ArchivaRepositoryMetadata metadata = metadataReader.read( returnedMetadata );
265 assertResponseOK( response );
267 assertThat( metadata.getAvailableVersions() ).isNotNull()
268 .hasSize( 4 ).contains( "1.0", "1.5", "2.0", "2.5" );
271 //check if the checksum files were generated
272 Path checksumFileSha1 = repoRootFirst.resolve( resourceName + ".sha1" );
273 Files.createDirectories(checksumFileSha1.getParent());
274 org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileSha1, Charset.defaultCharset(), "3290853214d3687134");
276 Path checksumFileMd5 = repoRootFirst.resolve( resourceName + ".md5" );
277 Files.createDirectories(checksumFileMd5.getParent());
278 org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileMd5, Charset.defaultCharset(), "98745897234eda12836423");
280 // request the sha1 checksum of the metadata
281 request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
282 + "dummy-merged-metadata-resource/maven-metadata.xml.sha1" );
283 response = getServletUnitClient().getResource( request );
285 assertResponseOK( response );
287 assertThat( response.getContentAsString() )
288 .startsWith( "f8a7a858a46887368adf0b30874de1f807d91453" );
290 // request the md5 checksum of the metadata
291 request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
292 + "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
293 response = getServletUnitClient().getResource( request );
295 assertResponseOK( response );
297 assertThat( response.getContentAsString() )
298 .startsWith( "cec864b66849153dd45fddb7cdde12b2" );
303 public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos()
306 String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt";
308 Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
309 Files.createDirectories(dummyInternalResourceFile.getParent());
310 org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyInternalResourceFile, Charset.defaultCharset(), "first");
312 resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt";
314 Path dummyReleasesResourceFile = repoRootLast.resolve( resourceName );
315 Files.createDirectories(dummyReleasesResourceFile.getParent());
316 org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
318 WebRequest request = new GetMethodWebRequest(
319 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/" );
320 WebResponse response = getServletUnitClient().getResource( request );
322 assertResponseOK( response );
324 assertThat( response.getContentAsString() ).contains( "Collection" )
325 .contains( "dummy/dummy-artifact" )
331 protected void assertResponseMethodNotAllowed( WebResponse response )
334 assertThat( response ).isNotNull();
336 assertThat( response.getStatusCode() ).isEqualTo( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
339 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
341 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
342 repoGroupConfiguration.setId( id );
343 repoGroupConfiguration.setRepositories( repositories );
344 return repoGroupConfiguration;