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
22 import com.meterware.httpunit.GetMethodWebRequest;
23 import com.meterware.httpunit.PutMethodWebRequest;
24 import com.meterware.httpunit.WebRequest;
25 import com.meterware.httpunit.WebResponse;
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.maven2.metadata.MavenMetadataReader;
30 import org.apache.archiva.model.ArchivaRepositoryMetadata;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.fest.assertions.api.Assertions;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
38 import javax.servlet.http.HttpServletResponse;
40 import java.io.InputStream;
41 import java.nio.charset.Charset;
42 import java.util.ArrayList;
43 import java.util.List;
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 File repoRootFirst;
66 protected File repoRootLast;
68 protected File 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 = new File( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
93 repoRootLast = new File( 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<String>();
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 = new File( 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<String>();
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 FileUtils.deleteDirectory( repoRootInvalid );
128 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 assertEquals( "Expected file contents", "first", response.getText() );
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 );
181 assertEquals( "Expected file contents", "last", response.getText() );
188 public void testGetFromValidRepositoryGroupReturnNotFound()
191 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
193 WebRequest request = new GetMethodWebRequest(
194 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
195 WebResponse response = getServletUnitClient().getResponse( request );
197 assertResponseNotFound( response );
204 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
207 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
209 WebRequest request = new GetMethodWebRequest(
210 "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
211 WebResponse response = getServletUnitClient().getResponse( request );
213 assertResponseInternalServerError( response );
220 public void testPutValidRepositoryGroupReturnBadRequest()
223 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
224 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
225 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
227 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
228 WebResponse response = getServletUnitClient().getResponse( request );
230 assertResponseMethodNotAllowed( response );
235 public void testGetMergedMetadata()
238 // first metadata file
239 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
241 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
242 dummyInternalResourceFile.getParentFile().mkdirs();
243 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
244 + "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n"
245 + "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n"
246 + "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>",
247 Charset.defaultCharset() );
249 //second metadata file
250 resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
251 dummyInternalResourceFile = new File( repoRootLast, resourceName );
252 dummyInternalResourceFile.getParentFile().mkdirs();
253 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
254 + "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>"
255 + "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>"
256 + "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>"
257 + "</versioning></metadata>", Charset.defaultCharset() );
259 WebRequest request = new GetMethodWebRequest(
260 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
261 + "dummy-merged-metadata-resource/maven-metadata.xml" );
262 WebResponse response = getServletUnitClient().getResource( request );
264 File returnedMetadata = new File( "target/test-classes/retrievedMetadataFile.xml" );
265 FileUtils.writeStringToFile( returnedMetadata, response.getText() );
266 ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( returnedMetadata );
268 assertResponseOK( response );
269 assertEquals( "Versions list size", 4, metadata.getAvailableVersions().size() );
270 assertTrue( "Versions list contains version 1.0", metadata.getAvailableVersions().contains( "1.0" ) );
271 assertTrue( "Versions list contains version 1.5", metadata.getAvailableVersions().contains( "1.5" ) );
272 assertTrue( "Versions list contains version 2.0", metadata.getAvailableVersions().contains( "2.0" ) );
273 assertTrue( "Versions list contains version 2.5", metadata.getAvailableVersions().contains( "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 );
290 assertEquals( "add113b0d7f8c6adb92a5015a7a3701081edf998 maven-metadata-group-with-valid-repos.xml",
291 response.getText() );
293 // request the md5 checksum of the metadata
294 request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
295 + "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
296 response = getServletUnitClient().getResource( request );
298 assertResponseOK( response );
299 assertEquals( "5b85ea4aa5f52bb76760041a52f98de8 maven-metadata-group-with-valid-repos.xml",
300 response.getText().trim() );
305 public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos()
308 String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt";
310 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
311 dummyInternalResourceFile.getParentFile().mkdirs();
312 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", Charset.defaultCharset() );
314 resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt";
316 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
317 dummyReleasesResourceFile.getParentFile().mkdirs();
318 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", Charset.defaultCharset() );
320 WebRequest request = new GetMethodWebRequest(
321 "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/" );
322 WebResponse response = getServletUnitClient().getResource( request );
324 assertResponseOK( response );
325 assertTrue( StringUtils.contains( response.getText(), "Collection" ) );
326 assertTrue( StringUtils.contains( response.getText(), "dummy/dummy-artifact" ) );
327 assertTrue( StringUtils.contains( response.getText(), "1.0" ) );
328 Assertions.assertThat( response.getText() ).contains( "2.0" );
329 //assertTrue( StringUtils.contains( response.getText(), "2.0" ) );
332 protected void assertResponseMethodNotAllowed( WebResponse response )
334 assertNotNull( "Should have recieved a response", response );
335 assertEquals( "Should have been an 405/Method Not Allowed response code.",
336 HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getResponseCode() );
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;