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.configuration.Configuration;
26 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
28 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
29 import org.apache.archiva.model.ArchivaRepositoryMetadata;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
34 import javax.servlet.http.HttpServletResponse;
35 import java.io.InputStream;
36 import java.nio.charset.Charset;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.ArrayList;
41 import java.util.List;
43 import static org.assertj.core.api.Assertions.assertThat;
47 * RepositoryServletRepositoryGroupTest
49 * Test Case 1. Accessing a valid repository group root url (e.g. http://machine.com/repository/repository-group/) returns a Bad Request (HTTP 400)
50 * Test Case 2. Accessing an invalid repository group root url is forwarded to managed repository checking (this is not covered here)
51 * Test Case 3. Accessing an artifact in a valid repository group will iterate over the managed repositories in the repository group
52 * Test Case 3.a. If an invalid managed repository is encountered (managed repository doesn't exist),
53 * a Not Found (HTTP 404) is returned and the iteration is broken
54 * Test Case 3.b. If an artifact is not found in a valid managed repository (after proxying, etc.),
55 * a Not Found (HTTP 404) is set but not returned yet, the iteration continues to the next managed repository.
56 * The Not Found (HTTP 404) is returned after exhausting all valid managed repositories
57 * Test Case 3.c. If an artifact is found in a valid managed repository,
58 * the artifact is returned, the iteration is broken and any Not Found (HTTP 404) is disregarded
59 * Test Case 4. Accessing a valid repository group with any http write method returns a Bad Request (HTTP 400)
61 public class RepositoryServletRepositoryGroupTest
62 extends AbstractRepositoryServletTestCase
64 protected Path repoRootFirst;
66 protected Path repoRootLast;
68 protected Path repoRootInvalid;
70 protected static final String MANAGED_REPO_FIRST = "first";
72 protected static final String MANAGED_REPO_LAST = "last";
74 protected static final String MANAGED_REPO_INVALID = "invalid";
76 protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
78 protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
88 String appserverBase = System.getProperty( "appserver.base" );
90 Configuration configuration = archivaConfiguration.getConfiguration();
92 repoRootFirst = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
93 repoRootLast = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
95 configuration.addManagedRepository(
96 createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst, true ) );
97 configuration.addManagedRepository(
98 createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast, true ) );
100 List<String> managedRepoIds = new ArrayList<>();
101 managedRepoIds.add( MANAGED_REPO_FIRST );
102 managedRepoIds.add( MANAGED_REPO_LAST );
104 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
106 // Create the repository group with an invalid managed repository
107 repoRootInvalid = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_INVALID );
108 ManagedRepositoryConfiguration managedRepositoryConfiguration =
109 createManagedRepository( MANAGED_REPO_INVALID, "Invalid Test Repo", repoRootInvalid, true );
111 configuration.addManagedRepository(
112 createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst, true ) );
113 configuration.addManagedRepository( managedRepositoryConfiguration );
114 configuration.addManagedRepository(
115 createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast, true ) );
117 List<String> invalidManagedRepoIds = new ArrayList<>();
118 invalidManagedRepoIds.add( MANAGED_REPO_FIRST );
119 invalidManagedRepoIds.add( MANAGED_REPO_INVALID );
120 invalidManagedRepoIds.add( MANAGED_REPO_LAST );
122 configuration.addRepositoryGroup(
123 createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
125 configuration.removeManagedRepository( managedRepositoryConfiguration );
126 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRootInvalid );
128 saveConfiguration( archivaConfiguration );
135 public void tearDown()
138 setupCleanRepo( repoRootFirst );
139 setupCleanRepo( repoRootLast );
148 public void testGetFromFirstManagedRepositoryReturnOk()
151 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
153 Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
154 Files.createDirectories( dummyInternalResourceFile.getParent() );
155 org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile, Charset.defaultCharset(), "first" );
157 WebRequest request = new GetMethodWebRequest(
158 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
159 WebResponse response = getServletUnitClient().getResponse( request );
161 assertResponseOK( response );
162 assertThat( response.getContentAsString() ).isEqualTo( "first" );
169 public void testGetFromLastManagedRepositoryReturnOk()
172 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
174 Path dummyReleasesResourceFile = repoRootLast.resolve( resourceName );
175 Files.createDirectories(dummyReleasesResourceFile.getParent());
176 org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
178 WebRequest request = new GetMethodWebRequest(
179 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
180 WebResponse response = getServletUnitClient().getResponse( request );
182 assertResponseOK( response );
184 assertThat( response.getContentAsString() ).isEqualTo( "last" );
191 public void testGetFromValidRepositoryGroupReturnNotFound()
194 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
196 WebRequest request = new GetMethodWebRequest(
197 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
198 WebResponse response = getServletUnitClient().getResponse( request );
200 assertResponseNotFound( response );
207 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
210 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
212 WebRequest request = new GetMethodWebRequest(
213 "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
214 WebResponse response = getServletUnitClient().getResponse( request );
216 assertResponseInternalServerError( response );
223 public void testPutValidRepositoryGroupReturnBadRequest()
226 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
227 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
228 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
230 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
231 WebResponse response = getServletUnitClient().getResponse( request );
233 assertResponseMethodNotAllowed( response );
238 public void testGetMergedMetadata()
241 // first metadata file
242 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
244 Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
245 Files.createDirectories(dummyInternalResourceFile.getParent());
246 org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile,
247 Charset.defaultCharset(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
248 + "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n"
249 + "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n"
250 + "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>" );
252 //second metadata file
253 resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
254 dummyInternalResourceFile = repoRootLast.resolve( resourceName );
255 Files.createDirectories(dummyInternalResourceFile.getParent());
256 org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile, Charset.defaultCharset(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
257 + "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>"
258 + "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>"
259 + "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>"
260 + "</versioning></metadata>" );
262 WebRequest request = new GetMethodWebRequest(
263 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
264 + "dummy-merged-metadata-resource/maven-metadata.xml" );
265 WebResponse response = getServletUnitClient().getResource( request );
267 Path returnedMetadata = Paths.get( "target/test-classes/retrievedMetadataFile.xml" );
268 org.apache.archiva.common.utils.FileUtils.writeStringToFile( returnedMetadata, Charset.defaultCharset(), response.getContentAsString() );
269 ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( returnedMetadata );
271 assertResponseOK( response );
273 assertThat( metadata.getAvailableVersions() ).isNotNull()
274 .hasSize( 4 ).contains( "1.0", "1.5", "2.0", "2.5" );
277 //check if the checksum files were generated
278 Path checksumFileSha1 = repoRootFirst.resolve( resourceName + ".sha1" );
279 Files.createDirectories(checksumFileSha1.getParent());
280 org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileSha1, Charset.defaultCharset(), "3290853214d3687134");
282 Path checksumFileMd5 = repoRootFirst.resolve( resourceName + ".md5" );
283 Files.createDirectories(checksumFileMd5.getParent());
284 org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileMd5, Charset.defaultCharset(), "98745897234eda12836423");
286 // request the sha1 checksum of the metadata
287 request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
288 + "dummy-merged-metadata-resource/maven-metadata.xml.sha1" );
289 response = getServletUnitClient().getResource( request );
291 assertResponseOK( response );
293 assertThat( response.getContentAsString() )
294 .isEqualTo( "add113b0d7f8c6adb92a5015a7a3701081edf998 maven-metadata-group-with-valid-repos.xml" );
296 // request the md5 checksum of the metadata
297 request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
298 + "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
299 response = getServletUnitClient().getResource( request );
301 assertResponseOK( response );
303 assertThat( response.getContentAsString() )
304 .isEqualTo( "5b85ea4aa5f52bb76760041a52f98de8 maven-metadata-group-with-valid-repos.xml" );
309 public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos()
312 String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt";
314 Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
315 Files.createDirectories(dummyInternalResourceFile.getParent());
316 org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyInternalResourceFile, Charset.defaultCharset(), "first");
318 resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt";
320 Path dummyReleasesResourceFile = repoRootLast.resolve( resourceName );
321 Files.createDirectories(dummyReleasesResourceFile.getParent());
322 org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
324 WebRequest request = new GetMethodWebRequest(
325 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/" );
326 WebResponse response = getServletUnitClient().getResource( request );
328 assertResponseOK( response );
330 assertThat( response.getContentAsString() ).contains( "Collection" )
331 .contains( "dummy/dummy-artifact" )
337 protected void assertResponseMethodNotAllowed( WebResponse response )
340 assertThat( response ).isNotNull();
342 assertThat( response.getStatusCode() ).isEqualTo( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
345 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
347 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
348 repoGroupConfiguration.setId( id );
349 repoGroupConfiguration.setRepositories( repositories );
350 return repoGroupConfiguration;