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.apache.commons.io.FileUtils;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
36 import javax.servlet.http.HttpServletResponse;
38 import java.io.InputStream;
39 import java.nio.charset.Charset;
40 import java.util.ArrayList;
41 import java.util.List;
45 * RepositoryServletRepositoryGroupTest
47 * Test Case 1. Accessing a valid repository group root url (e.g. http://machine.com/repository/repository-group/) returns a Bad Request (HTTP 400)
48 * Test Case 2. Accessing an invalid repository group root url is forwarded to managed repository checking (this is not covered here)
49 * Test Case 3. Accessing an artifact in a valid repository group will iterate over the managed repositories in the repository group
50 * Test Case 3.a. If an invalid managed repository is encountered (managed repository doesn't exist),
51 * a Not Found (HTTP 404) is returned and the iteration is broken
52 * Test Case 3.b. If an artifact is not found in a valid managed repository (after proxying, etc.),
53 * a Not Found (HTTP 404) is set but not returned yet, the iteration continues to the next managed repository.
54 * The Not Found (HTTP 404) is returned after exhausting all valid managed repositories
55 * Test Case 3.c. If an artifact is found in a valid managed repository,
56 * the artifact is returned, the iteration is broken and any Not Found (HTTP 404) is disregarded
57 * Test Case 4. Accessing a valid repository group with any http write method returns a Bad Request (HTTP 400)
59 public class RepositoryServletRepositoryGroupTest
60 extends AbstractRepositoryServletTestCase
62 protected File repoRootFirst;
64 protected File repoRootLast;
66 protected File repoRootInvalid;
68 protected static final String MANAGED_REPO_FIRST = "first";
70 protected static final String MANAGED_REPO_LAST = "last";
72 protected static final String MANAGED_REPO_INVALID = "invalid";
74 protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
76 protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
86 String appserverBase = System.getProperty( "appserver.base" );
88 Configuration configuration = archivaConfiguration.getConfiguration();
90 repoRootFirst = new File( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
91 repoRootLast = new File( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
93 configuration.addManagedRepository(
94 createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst, true ) );
95 configuration.addManagedRepository(
96 createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast, true ) );
98 List<String> managedRepoIds = new ArrayList<>();
99 managedRepoIds.add( MANAGED_REPO_FIRST );
100 managedRepoIds.add( MANAGED_REPO_LAST );
102 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
104 // Create the repository group with an invalid managed repository
105 repoRootInvalid = new File( appserverBase, "data/repositories/" + MANAGED_REPO_INVALID );
106 ManagedRepositoryConfiguration managedRepositoryConfiguration =
107 createManagedRepository( MANAGED_REPO_INVALID, "Invalid Test Repo", repoRootInvalid, true );
109 configuration.addManagedRepository(
110 createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst, true ) );
111 configuration.addManagedRepository( managedRepositoryConfiguration );
112 configuration.addManagedRepository(
113 createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast, true ) );
115 List<String> invalidManagedRepoIds = new ArrayList<>();
116 invalidManagedRepoIds.add( MANAGED_REPO_FIRST );
117 invalidManagedRepoIds.add( MANAGED_REPO_INVALID );
118 invalidManagedRepoIds.add( MANAGED_REPO_LAST );
120 configuration.addRepositoryGroup(
121 createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
123 configuration.removeManagedRepository( managedRepositoryConfiguration );
124 FileUtils.deleteDirectory( repoRootInvalid );
126 saveConfiguration( archivaConfiguration );
133 public void tearDown()
136 setupCleanRepo( repoRootFirst );
137 setupCleanRepo( repoRootLast );
146 public void testGetFromFirstManagedRepositoryReturnOk()
149 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
151 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
152 dummyInternalResourceFile.getParentFile().mkdirs();
153 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", Charset.defaultCharset() );
155 WebRequest request = new GetMethodWebRequest(
156 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
157 WebResponse response = getServletUnitClient().getResponse( request );
159 assertResponseOK( response );
160 assertThat( response.getContentAsString() ).isEqualTo( "first" );
167 public void testGetFromLastManagedRepositoryReturnOk()
170 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
172 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
173 dummyReleasesResourceFile.getParentFile().mkdirs();
174 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", Charset.defaultCharset() );
176 WebRequest request = new GetMethodWebRequest(
177 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
178 WebResponse response = getServletUnitClient().getResponse( request );
180 assertResponseOK( response );
182 assertThat( response.getContentAsString() ).isEqualTo( "last" );
189 public void testGetFromValidRepositoryGroupReturnNotFound()
192 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
194 WebRequest request = new GetMethodWebRequest(
195 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
196 WebResponse response = getServletUnitClient().getResponse( request );
198 assertResponseNotFound( response );
205 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
208 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
210 WebRequest request = new GetMethodWebRequest(
211 "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
212 WebResponse response = getServletUnitClient().getResponse( request );
214 assertResponseInternalServerError( response );
221 public void testPutValidRepositoryGroupReturnBadRequest()
224 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
225 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
226 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
228 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
229 WebResponse response = getServletUnitClient().getResponse( request );
231 assertResponseMethodNotAllowed( response );
236 public void testGetMergedMetadata()
239 // first metadata file
240 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
242 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
243 dummyInternalResourceFile.getParentFile().mkdirs();
244 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
245 + "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n"
246 + "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n"
247 + "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>",
248 Charset.defaultCharset() );
250 //second metadata file
251 resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
252 dummyInternalResourceFile = new File( repoRootLast, resourceName );
253 dummyInternalResourceFile.getParentFile().mkdirs();
254 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
255 + "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>"
256 + "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>"
257 + "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>"
258 + "</versioning></metadata>", Charset.defaultCharset() );
260 WebRequest request = new GetMethodWebRequest(
261 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
262 + "dummy-merged-metadata-resource/maven-metadata.xml" );
263 WebResponse response = getServletUnitClient().getResource( request );
265 File returnedMetadata = new File( "target/test-classes/retrievedMetadataFile.xml" );
266 FileUtils.writeStringToFile( returnedMetadata, response.getContentAsString() );
267 ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( returnedMetadata );
269 assertResponseOK( response );
271 assertThat( metadata.getAvailableVersions() ).isNotNull()
272 .hasSize( 4 ).contains( "1.0", "1.5", "2.0", "2.5" );
275 //check if the checksum files were generated
276 File checksumFileSha1 = new File( repoRootFirst, resourceName + ".sha1" );
277 checksumFileSha1.getParentFile().mkdirs();
278 FileUtils.writeStringToFile( checksumFileSha1, "3290853214d3687134", Charset.defaultCharset() );
280 File checksumFileMd5 = new File( repoRootFirst, resourceName + ".md5" );
281 checksumFileMd5.getParentFile().mkdirs();
282 FileUtils.writeStringToFile( checksumFileMd5, "98745897234eda12836423", Charset.defaultCharset() );
284 // request the sha1 checksum of the metadata
285 request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
286 + "dummy-merged-metadata-resource/maven-metadata.xml.sha1" );
287 response = getServletUnitClient().getResource( request );
289 assertResponseOK( response );
291 assertThat( response.getContentAsString() )
292 .isEqualTo( "add113b0d7f8c6adb92a5015a7a3701081edf998 maven-metadata-group-with-valid-repos.xml" );
294 // request the md5 checksum of the metadata
295 request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
296 + "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
297 response = getServletUnitClient().getResource( request );
299 assertResponseOK( response );
301 assertThat( response.getContentAsString() )
302 .isEqualTo( "5b85ea4aa5f52bb76760041a52f98de8 maven-metadata-group-with-valid-repos.xml" );
307 public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos()
310 String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt";
312 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
313 dummyInternalResourceFile.getParentFile().mkdirs();
314 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", Charset.defaultCharset() );
316 resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt";
318 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
319 dummyReleasesResourceFile.getParentFile().mkdirs();
320 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", Charset.defaultCharset() );
322 WebRequest request = new GetMethodWebRequest(
323 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/" );
324 WebResponse response = getServletUnitClient().getResource( request );
326 assertResponseOK( response );
328 assertThat( response.getContentAsString() ).contains( "Collection" )
329 .contains( "dummy/dummy-artifact" )
335 protected void assertResponseMethodNotAllowed( WebResponse response )
338 assertThat( response ).isNotNull();
340 assertThat( response.getStatusCode() ).isEqualTo( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
343 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
345 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
346 repoGroupConfiguration.setId( id );
347 repoGroupConfiguration.setRepositories( repositories );
348 return repoGroupConfiguration;